game.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // services/game.ts
  2. import { Result, createSuccess, createError } from '@/types/result'
  3. import { cloudApi } from '@/api'
  4. import { type Game, GameType } from '@/types/game'
  5. import { USE_MOCK } from '@/services'
  6. // Mock游戏数据
  7. const mockGames: Game[] = [
  8. {
  9. gameId: '1',
  10. title: '海龟汤',
  11. type: GameType.TURTLE_SOUP,
  12. image: 'https://images.unsplash.com/photo-1582845512747-e42001c95638?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
  13. minPlayers: 1,
  14. maxPlayers: 10,
  15. duration: '30-60',
  16. rating: 4.8,
  17. isNew: true,
  18. isHot: true,
  19. description: '海龟汤是一种猜谜游戏,游戏开始时,主持人会讲述一个故事的结果,参与者需要通过提问来猜测故事的真相。',
  20. rules: '1. 主持人讲述一个故事的结果\n2. 参与者通过提问来猜测故事的真相\n3. 主持人只能回答"是"、"否"或"不重要"',
  21. category: '推理',
  22. playCount: 1000,
  23. completionRate: 0.8,
  24. tips: [
  25. '提问时要注意逻辑思维',
  26. '尝试从多个角度思考问题',
  27. '注意主持人的回答,特别是"不重要"的部分'
  28. ],
  29. examples: [
  30. {
  31. question: '一个人走进酒吧,点了一杯水,喝完就离开了。为什么?',
  32. answer: '这个人有打嗝的困扰,想通过喝水来缓解。'
  33. }
  34. ]
  35. },
  36. {
  37. gameId: '2',
  38. title: '剧本杀',
  39. type: GameType.WORD_GAME,
  40. image: 'https://images.unsplash.com/photo-1529156069898-49953e39b3ac?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
  41. minPlayers: 4,
  42. maxPlayers: 8,
  43. duration: '120-180',
  44. rating: 4.5,
  45. isNew: false,
  46. isHot: true,
  47. description: '剧本杀是一种角色扮演游戏,每个玩家扮演一个角色,通过阅读剧本和相互交流来解决一个谜题。',
  48. rules: '1. 每个玩家扮演一个角色\n2. 阅读剧本并获取个人信息\n3. 通过交流和推理解决谜题',
  49. category: '角色扮演',
  50. playCount: 1500,
  51. completionRate: 0.7,
  52. tips: [
  53. '认真阅读你的角色背景',
  54. '注意收集和分析信息',
  55. '积极参与角色扮演和讨论'
  56. ],
  57. examples: [
  58. {
  59. question: '谁是凶手?',
  60. answer: '根据线索和证据,管家是凶手。'
  61. }
  62. ]
  63. },
  64. {
  65. gameId: '3',
  66. title: '狼人杀',
  67. type: GameType.WORD_GAME,
  68. image: 'https://images.unsplash.com/photo-1529156069898-49953e39b3ac?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
  69. minPlayers: 6,
  70. maxPlayers: 12,
  71. duration: '30-45',
  72. rating: 4.7,
  73. isNew: false,
  74. isHot: true,
  75. description: '狼人杀是一种桌游,玩家分为狼人和村民两个阵营,狼人试图消灭村民,村民则要找出并消灭狼人。',
  76. rules: '1. 玩家分为狼人和村民两个阵营\n2. 夜晚狼人选择一名玩家"杀死"\n3. 白天所有人讨论并投票处决一名玩家',
  77. category: '桌游',
  78. playCount: 2000,
  79. completionRate: 0.6,
  80. tips: [
  81. '仔细观察其他玩家的言行',
  82. '理性分析每晚的死亡情况',
  83. '善用自己的角色技能'
  84. ],
  85. examples: [
  86. {
  87. question: '如何判断谁是狼人?',
  88. answer: '通过分析发言逻辑、投票行为和表情变化,找出可疑的玩家。'
  89. }
  90. ]
  91. }
  92. ]
  93. // 游戏通用服务
  94. export const gameService = {
  95. // 获取游戏列表
  96. async getGames(options?: { category?: string }): Promise<Result<Game[]>> {
  97. // 如果在开发环境或非小程序环境,使用Mock数据
  98. if (USE_MOCK || process.env.TARO_ENV !== 'weapp') {
  99. return new Promise(resolve => {
  100. setTimeout(() => {
  101. let filteredGames = [...mockGames];
  102. // 如果指定了分类,进行过滤
  103. if (options?.category) {
  104. filteredGames = filteredGames.filter(
  105. game => game.category === options.category
  106. );
  107. }
  108. resolve(createSuccess(filteredGames));
  109. }, 500); // 模拟网络延迟
  110. });
  111. }
  112. // 使用cloudApi调用云函数
  113. return cloudApi.call<Game[]>('getGames', options)
  114. .then(result => {
  115. // 如果没有返回数据,使用mock数据
  116. if (result.success && (!result.data || result.data.length === 0)) {
  117. return createSuccess(mockGames);
  118. }
  119. return result;
  120. })
  121. .catch(error => {
  122. console.error('获取游戏列表失败:', error);
  123. return createError(error.message || '获取游戏列表失败');
  124. });
  125. },
  126. // 获取游戏详情
  127. async getGameDetail(id: string): Promise<Result<Game>> {
  128. // 如果在开发环境或非小程序环境,使用Mock数据
  129. if (USE_MOCK || process.env.TARO_ENV !== 'weapp') {
  130. return new Promise(resolve => {
  131. setTimeout(() => {
  132. const game = mockGames.find(g => g.gameId === id);
  133. if (game) {
  134. resolve(createSuccess(game));
  135. } else {
  136. resolve(createError('未找到指定游戏'));
  137. }
  138. }, 300); // 模拟网络延迟
  139. });
  140. }
  141. // 使用cloudApi调用云函数
  142. return cloudApi.call<Game>('getGameDetail', { id })
  143. .then(result => {
  144. // 如果没有返回数据,尝试从mock找
  145. if (result.success && !result.data) {
  146. const mockGame = mockGames.find(g => g.gameId === id);
  147. if (mockGame) {
  148. return createSuccess(mockGame);
  149. }
  150. return createError('未找到指定游戏');
  151. }
  152. return result;
  153. })
  154. .catch(error => {
  155. console.error('获取游戏详情失败:', error);
  156. return createError(error.message || '获取游戏详情失败');
  157. });
  158. }
  159. }