123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- // services/game.ts
- import { Result, createSuccess, createError } from '@/types/result'
- import { cloudApi } from '@/api'
- import { type Game, GameType } from '@/types/game'
- import { USE_MOCK } from '@/services'
- // Mock游戏数据
- const mockGames: Game[] = [
- {
- gameId: '1',
- title: '海龟汤',
- type: GameType.TURTLE_SOUP,
- image: 'https://images.unsplash.com/photo-1582845512747-e42001c95638?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
- minPlayers: 1,
- maxPlayers: 10,
- duration: '30-60',
- rating: 4.8,
- isNew: true,
- isHot: true,
- description: '海龟汤是一种猜谜游戏,游戏开始时,主持人会讲述一个故事的结果,参与者需要通过提问来猜测故事的真相。',
- rules: '1. 主持人讲述一个故事的结果\n2. 参与者通过提问来猜测故事的真相\n3. 主持人只能回答"是"、"否"或"不重要"',
- category: '推理',
- playCount: 1000,
- completionRate: 0.8,
- tips: [
- '提问时要注意逻辑思维',
- '尝试从多个角度思考问题',
- '注意主持人的回答,特别是"不重要"的部分'
- ],
- examples: [
- {
- question: '一个人走进酒吧,点了一杯水,喝完就离开了。为什么?',
- answer: '这个人有打嗝的困扰,想通过喝水来缓解。'
- }
- ]
- },
- {
- gameId: '2',
- title: '剧本杀',
- type: GameType.WORD_GAME,
- image: 'https://images.unsplash.com/photo-1529156069898-49953e39b3ac?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
- minPlayers: 4,
- maxPlayers: 8,
- duration: '120-180',
- rating: 4.5,
- isNew: false,
- isHot: true,
- description: '剧本杀是一种角色扮演游戏,每个玩家扮演一个角色,通过阅读剧本和相互交流来解决一个谜题。',
- rules: '1. 每个玩家扮演一个角色\n2. 阅读剧本并获取个人信息\n3. 通过交流和推理解决谜题',
- category: '角色扮演',
- playCount: 1500,
- completionRate: 0.7,
- tips: [
- '认真阅读你的角色背景',
- '注意收集和分析信息',
- '积极参与角色扮演和讨论'
- ],
- examples: [
- {
- question: '谁是凶手?',
- answer: '根据线索和证据,管家是凶手。'
- }
- ]
- },
- {
- gameId: '3',
- title: '狼人杀',
- type: GameType.WORD_GAME,
- image: 'https://images.unsplash.com/photo-1529156069898-49953e39b3ac?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
- minPlayers: 6,
- maxPlayers: 12,
- duration: '30-45',
- rating: 4.7,
- isNew: false,
- isHot: true,
- description: '狼人杀是一种桌游,玩家分为狼人和村民两个阵营,狼人试图消灭村民,村民则要找出并消灭狼人。',
- rules: '1. 玩家分为狼人和村民两个阵营\n2. 夜晚狼人选择一名玩家"杀死"\n3. 白天所有人讨论并投票处决一名玩家',
- category: '桌游',
- playCount: 2000,
- completionRate: 0.6,
- tips: [
- '仔细观察其他玩家的言行',
- '理性分析每晚的死亡情况',
- '善用自己的角色技能'
- ],
- examples: [
- {
- question: '如何判断谁是狼人?',
- answer: '通过分析发言逻辑、投票行为和表情变化,找出可疑的玩家。'
- }
- ]
- }
- ]
- // 游戏通用服务
- export const gameService = {
- // 获取游戏列表
- async getGames(options?: { category?: string }): Promise<Result<Game[]>> {
- // 如果在开发环境或非小程序环境,使用Mock数据
- if (USE_MOCK || process.env.TARO_ENV !== 'weapp') {
- return new Promise(resolve => {
- setTimeout(() => {
- let filteredGames = [...mockGames];
-
- // 如果指定了分类,进行过滤
- if (options?.category) {
- filteredGames = filteredGames.filter(
- game => game.category === options.category
- );
- }
-
- resolve(createSuccess(filteredGames));
- }, 500); // 模拟网络延迟
- });
- }
-
- // 使用cloudApi调用云函数
- return cloudApi.call<Game[]>('getGames', options)
- .then(result => {
- // 如果没有返回数据,使用mock数据
- if (result.success && (!result.data || result.data.length === 0)) {
- return createSuccess(mockGames);
- }
- return result;
- })
- .catch(error => {
- console.error('获取游戏列表失败:', error);
- return createError(error.message || '获取游戏列表失败');
- });
- },
- // 获取游戏详情
- async getGameDetail(id: string): Promise<Result<Game>> {
- // 如果在开发环境或非小程序环境,使用Mock数据
- if (USE_MOCK || process.env.TARO_ENV !== 'weapp') {
- return new Promise(resolve => {
- setTimeout(() => {
- const game = mockGames.find(g => g.gameId === id);
-
- if (game) {
- resolve(createSuccess(game));
- } else {
- resolve(createError('未找到指定游戏'));
- }
- }, 300); // 模拟网络延迟
- });
- }
-
- // 使用cloudApi调用云函数
- return cloudApi.call<Game>('getGameDetail', { id })
- .then(result => {
- // 如果没有返回数据,尝试从mock找
- if (result.success && !result.data) {
- const mockGame = mockGames.find(g => g.gameId === id);
- if (mockGame) {
- return createSuccess(mockGame);
- }
- return createError('未找到指定游戏');
- }
- return result;
- })
- .catch(error => {
- console.error('获取游戏详情失败:', error);
- return createError(error.message || '获取游戏详情失败');
- });
- }
- }
|