99 lines
2.5 KiB
TypeScript
99 lines
2.5 KiB
TypeScript
|
|
// src/api/games.ts
|
||
|
|
import pb from './pocketbase'
|
||
|
|
import type { Game, GamePlatform } from '@/types'
|
||
|
|
|
||
|
|
// 获取游戏列表
|
||
|
|
export async function getGames(options?: {
|
||
|
|
page?: number
|
||
|
|
limit?: number
|
||
|
|
platform?: GamePlatform
|
||
|
|
search?: string
|
||
|
|
}): Promise<{ items: Game[], total: number }> {
|
||
|
|
const { page = 1, limit = 20, platform, search } = options || {}
|
||
|
|
|
||
|
|
let filter = ''
|
||
|
|
if (platform) {
|
||
|
|
filter = `platform="${platform}"`
|
||
|
|
}
|
||
|
|
if (search) {
|
||
|
|
const searchFilter = `name ~ "${search}"`
|
||
|
|
filter = filter ? `${filter} && ${searchFilter}` : searchFilter
|
||
|
|
}
|
||
|
|
|
||
|
|
const result = await pb.collection('games').getList(page, limit, {
|
||
|
|
filter,
|
||
|
|
sort: '-popularCount'
|
||
|
|
})
|
||
|
|
|
||
|
|
return {
|
||
|
|
items: result.items as unknown as Game[],
|
||
|
|
total: result.totalItems
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取热门游戏
|
||
|
|
export async function getPopularGames(limit = 10): Promise<Game[]> {
|
||
|
|
const result = await pb.collection('games').getList(1, limit, {
|
||
|
|
sort: '-popularCount'
|
||
|
|
})
|
||
|
|
|
||
|
|
return result.items as unknown as Game[]
|
||
|
|
}
|
||
|
|
|
||
|
|
// 搜索游戏
|
||
|
|
export async function searchGames(query: string, limit = 20): Promise<Game[]> {
|
||
|
|
if (!query.trim()) return []
|
||
|
|
|
||
|
|
const result = await pb.collection('games').getList(1, limit, {
|
||
|
|
filter: `name ~ "${query}"`,
|
||
|
|
sort: '-popularCount'
|
||
|
|
})
|
||
|
|
|
||
|
|
return result.items as unknown as Game[]
|
||
|
|
}
|
||
|
|
|
||
|
|
// 添加游戏(需要管理员权限)
|
||
|
|
export async function addGame(data: {
|
||
|
|
name: string
|
||
|
|
platform: GamePlatform
|
||
|
|
tags?: string[]
|
||
|
|
cover?: string
|
||
|
|
}) {
|
||
|
|
return pb.collection('games').create(data)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 更新游戏热度
|
||
|
|
export async function incrementGamePopularity(gameId: string) {
|
||
|
|
const game = await pb.collection('games').getOne(gameId)
|
||
|
|
return pb.collection('games').update(gameId, {
|
||
|
|
popularCount: (game.popularCount || 0) + 1
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取游戏详情
|
||
|
|
export async function getGame(gameId: string): Promise<Game> {
|
||
|
|
return pb.collection('games').getOne(gameId) as unknown as Game
|
||
|
|
}
|
||
|
|
|
||
|
|
// 按平台获取游戏
|
||
|
|
export async function getGamesByPlatform(platform: GamePlatform): Promise<Game[]> {
|
||
|
|
const result = await pb.collection('games').getList(1, 50, {
|
||
|
|
filter: `platform="${platform}"`,
|
||
|
|
sort: '-popularCount'
|
||
|
|
})
|
||
|
|
|
||
|
|
return result.items as unknown as Game[]
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取所有平台
|
||
|
|
export function getAllPlatforms(): GamePlatform[] {
|
||
|
|
return ['PC', 'PS5', 'Xbox', 'Switch', 'Mobile']
|
||
|
|
}
|
||
|
|
|
||
|
|
// 订阅游戏变更
|
||
|
|
export function subscribeGames(callback: (game: Game) => void) {
|
||
|
|
return pb.collection('games').subscribe('*', (payload) => {
|
||
|
|
callback(payload.record as unknown as Game)
|
||
|
|
})
|
||
|
|
}
|