123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- // services/game.ts
- import { Result, createSuccess, createError } from '@/types/result'
- import { cloudApi } from '@/api'
- import { type Game } from '@/types/game'
- import { USE_MOCK } from '@/services'
- // Mock游戏数据
- const mockGames: Game[] = [
- {
- id: '1',
- title: '海龟汤',
- image: 'https://images.unsplash.com/photo-1582845512747-e42001c95638?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
- players: '3-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: '这个人有打嗝的困扰,想通过喝水来缓解。'
- }
- ]
- },
- {
- id: '2',
- title: '剧本杀',
- image: 'https://images.unsplash.com/photo-1529156069898-49953e39b3ac?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
- players: '4-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: '根据线索和证据,管家是凶手。'
- }
- ]
- },
- {
- id: '3',
- title: '狼人杀',
- image: 'https://images.unsplash.com/photo-1529156069898-49953e39b3ac?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
- players: '6-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.id === 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.id === id);
- if (mockGame) {
- return createSuccess(mockGame);
- }
- return createError('未找到指定游戏');
- }
- return result;
- })
- .catch(error => {
- console.error('获取游戏详情失败:', error);
- return createError(error.message || '获取游戏详情失败');
- });
- },
-
- // 获取热门游戏
- async getHotGames(limit: number = 5): Promise<Result<Game[]>> {
- const result = await this.getGames();
-
- if (!result.success) {
- return result;
- }
-
- const hotGames = result.data
- ?.filter(game => game.isHot)
- .sort((a, b) => b.rating - a.rating)
- .slice(0, limit);
-
- return createSuccess(hotGames || []);
- },
-
- // 获取新游戏
- async getNewGames(limit: number = 5): Promise<Result<Game[]>> {
- const result = await this.getGames();
-
- if (!result.success) {
- return result;
- }
-
- const newGames = result.data
- ?.filter(game => game.isNew)
- .slice(0, limit);
-
- return createSuccess(newGames || []);
- },
-
- // 搜索游戏
- async searchGames(keyword: string): Promise<Result<Game[]>> {
- // 如果在开发环境或非小程序环境,使用Mock数据
- if (USE_MOCK || process.env.TARO_ENV !== 'weapp') {
- return new Promise(resolve => {
- setTimeout(() => {
- if (!keyword.trim()) {
- resolve(createSuccess(mockGames));
- return;
- }
-
- const searchKey = keyword.toLowerCase();
- const results = mockGames.filter(game =>
- game.title.toLowerCase().includes(searchKey) ||
- game.description.toLowerCase().includes(searchKey) ||
- game.category.toLowerCase().includes(searchKey)
- );
-
- resolve(createSuccess(results));
- }, 300);
- });
- }
-
- // 使用cloudApi调用云函数
- return cloudApi.call<Game[]>('searchGames', { keyword })
- .then(result => {
- // 如果云函数搜索失败,降级到前端搜索mock数据
- if (!result.success || !result.data || result.data.length === 0) {
- const searchKey = keyword.toLowerCase();
- const results = mockGames.filter(game =>
- game.title.toLowerCase().includes(searchKey) ||
- game.description.toLowerCase().includes(searchKey) ||
- game.category.toLowerCase().includes(searchKey)
- );
-
- return createSuccess(results);
- }
- return result;
- })
- .catch(error => {
- console.error('搜索游戏失败:', error);
- return createError(error.message || '搜索游戏失败');
- });
- },
-
- // 获取游戏分类
- async getCategories(): Promise<Result<string[]>> {
- // 如果在开发环境或非小程序环境,使用Mock数据
- if (USE_MOCK || process.env.TARO_ENV !== 'weapp') {
- return new Promise(resolve => {
- setTimeout(() => {
- // 从mock数据中提取唯一分类
- const categories = [...new Set(mockGames.map(game => game.category))];
- resolve(createSuccess(categories));
- }, 200);
- });
- }
-
- // 使用cloudApi调用云函数
- return cloudApi.call<string[]>('getGameCategories')
- .then(result => {
- // 如果没有返回数据,从mock数据提取
- if (result.success && (!result.data || result.data.length === 0)) {
- const categories = [...new Set(mockGames.map(game => game.category))];
- return createSuccess(categories);
- }
- return result;
- })
- .catch(error => {
- console.error('获取游戏分类失败:', error);
- return createError(error.message || '获取游戏分类失败');
- });
- }
- }
|