|
@@ -0,0 +1,190 @@
|
|
|
+import { defineStore } from 'pinia'
|
|
|
+import { ref, computed } from 'vue'
|
|
|
+// 只导入需要的类型和枚举
|
|
|
+import { TurtleSoupGame, GameStatus } from '@/types/game'
|
|
|
+// 使用类型导入避免命名冲突
|
|
|
+import type { GameResult as GameResultType } from '@/types/game'
|
|
|
+import Taro from '@tarojs/taro'
|
|
|
+
|
|
|
+// 定义云函数返回结果的接口,重命名避免冲突
|
|
|
+interface CloudFunctionResult {
|
|
|
+ game?: TurtleSoupGame;
|
|
|
+ questionId?: string;
|
|
|
+ success?: boolean;
|
|
|
+ error?: string;
|
|
|
+ gameResult?: GameResultType;
|
|
|
+ [key: string]: any;
|
|
|
+}
|
|
|
+
|
|
|
+export const useGameStore = defineStore('game', () => {
|
|
|
+ // 当前游戏
|
|
|
+ const currentGame = ref<TurtleSoupGame | null>(null)
|
|
|
+ // 是否正在加载
|
|
|
+ const loading = ref(false)
|
|
|
+
|
|
|
+ // 当前游戏状态
|
|
|
+ const gameStatus = computed(() => currentGame.value?.status || null)
|
|
|
+ // 是否游戏中
|
|
|
+ const isPlaying = computed(() => gameStatus.value === GameStatus.ONGOING)
|
|
|
+ // 游戏是否已解决
|
|
|
+ const isSolved = computed(() => gameStatus.value === GameStatus.SOLVED)
|
|
|
+ // 已公开的提示
|
|
|
+ const revealedHints = computed(() => {
|
|
|
+ if (!currentGame.value) return []
|
|
|
+ return currentGame.value.revealedHints.map(index => currentGame.value!.hints[index])
|
|
|
+ })
|
|
|
+
|
|
|
+ // 获取游戏数据
|
|
|
+ async function getGameData(gameId: string, role: string) {
|
|
|
+ loading.value = true
|
|
|
+ try {
|
|
|
+ const res = await Taro.cloud.callFunction({
|
|
|
+ name: 'getGameData',
|
|
|
+ data: { gameId, role }
|
|
|
+ })
|
|
|
+
|
|
|
+ // 使用类型断言
|
|
|
+ const result = res.result as CloudFunctionResult
|
|
|
+
|
|
|
+ if (result && result.game) {
|
|
|
+ currentGame.value = result.game
|
|
|
+ return { success: true, game: currentGame.value }
|
|
|
+ }
|
|
|
+ return { success: false, error: result?.error || '获取游戏数据失败' }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取游戏数据失败', error)
|
|
|
+ return { success: false, error: '获取游戏数据失败' }
|
|
|
+ } finally {
|
|
|
+ loading.value = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提交问题
|
|
|
+ async function submitQuestion(content: string) {
|
|
|
+ if (!currentGame.value) {
|
|
|
+ return { success: false, error: '游戏未初始化' }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const res = await Taro.cloud.callFunction({
|
|
|
+ name: 'submitQuestion',
|
|
|
+ data: {
|
|
|
+ gameId: currentGame.value.id,
|
|
|
+ content
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ // 使用类型断言
|
|
|
+ const result = res.result as CloudFunctionResult
|
|
|
+
|
|
|
+ if (result && result.questionId) {
|
|
|
+ // 可以在这里直接更新本地游戏状态,也可以重新获取游戏数据
|
|
|
+ return { success: true, questionId: result.questionId }
|
|
|
+ }
|
|
|
+ return { success: false, error: result?.error || '提交问题失败' }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('提交问题失败', error)
|
|
|
+ return { success: false, error: '提交问题失败' }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 回答问题 (主持人用)
|
|
|
+ async function answerQuestion(questionId: string, answer: string) {
|
|
|
+ try {
|
|
|
+ const res = await Taro.cloud.callFunction({
|
|
|
+ name: 'answerQuestion',
|
|
|
+ data: { questionId, answer }
|
|
|
+ })
|
|
|
+
|
|
|
+ // 使用类型断言
|
|
|
+ const result = res.result as CloudFunctionResult
|
|
|
+
|
|
|
+ if (result && result.success) {
|
|
|
+ // 可以在这里直接更新本地游戏状态,也可以重新获取游戏数据
|
|
|
+ return { success: true }
|
|
|
+ }
|
|
|
+ return { success: false, error: result?.error || '回答问题失败' }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('回答问题失败', error)
|
|
|
+ return { success: false, error: '回答问题失败' }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 公开提示 (主持人用)
|
|
|
+ async function revealHint(hintIndex: number) {
|
|
|
+ if (!currentGame.value) {
|
|
|
+ return { success: false, error: '游戏未初始化' }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const res = await Taro.cloud.callFunction({
|
|
|
+ name: 'revealHint',
|
|
|
+ data: {
|
|
|
+ gameId: currentGame.value.id,
|
|
|
+ hintIndex
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ // 使用类型断言
|
|
|
+ const result = res.result as CloudFunctionResult
|
|
|
+
|
|
|
+ if (result && result.success) {
|
|
|
+ // 可以在这里直接更新本地游戏状态,也可以重新获取游戏数据
|
|
|
+ return { success: true }
|
|
|
+ }
|
|
|
+ return { success: false, error: result?.error || '公开提示失败' }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('公开提示失败', error)
|
|
|
+ return { success: false, error: '公开提示失败' }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 结束游戏
|
|
|
+ async function endGame(result: { solved: boolean, solvedBy?: string }) {
|
|
|
+ if (!currentGame.value) {
|
|
|
+ return { success: false, error: '游戏未初始化' }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const res = await Taro.cloud.callFunction({
|
|
|
+ name: 'endGame',
|
|
|
+ data: {
|
|
|
+ gameId: currentGame.value.id,
|
|
|
+ result
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ // 使用类型断言
|
|
|
+ const finalResult = res.result as CloudFunctionResult
|
|
|
+
|
|
|
+ if (finalResult && finalResult.success) {
|
|
|
+ // 可以在这里直接更新本地游戏状态,也可以重新获取游戏数据
|
|
|
+ return { success: true, gameResult: finalResult.gameResult }
|
|
|
+ }
|
|
|
+ return { success: false, error: finalResult?.error || '结束游戏失败' }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('结束游戏失败', error)
|
|
|
+ return { success: false, error: '结束游戏失败' }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清除游戏数据
|
|
|
+ function clearGame() {
|
|
|
+ currentGame.value = null
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ currentGame,
|
|
|
+ loading,
|
|
|
+ gameStatus,
|
|
|
+ isPlaying,
|
|
|
+ isSolved,
|
|
|
+ revealedHints,
|
|
|
+ getGameData,
|
|
|
+ submitQuestion,
|
|
|
+ answerQuestion,
|
|
|
+ revealHint,
|
|
|
+ endGame,
|
|
|
+ clearGame
|
|
|
+ }
|
|
|
+})
|