123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- import { defineStore } from 'pinia'
- import { werewolfAPI } from '@/services/api/games/werewolf'
- // 玩家角色类型
- export type WerewolfRole =
- | 'werewolf' // 狼人
- | 'villager' // 村民
- | 'seer' // 预言家
- | 'witch' // 女巫
- | 'hunter' // 猎人
- | 'guard' // 守卫
- | 'idiot' // 白痴
- | 'elder' // 长老
- | 'cupid' // 丘比特
- | 'thief' // 盗贼
- | 'moderator'; // 主持人/上帝
- // 游戏阶段
- export type GamePhase =
- | 'waiting' // 等待开始
- | 'night' // 夜晚
- | 'day' // 白天
- | 'vote' // 投票
- | 'ended'; // 游戏结束
- // 行动类型
- export type ActionType =
- | 'kill' // 狼人杀人
- | 'check' // 预言家查验
- | 'save' // 女巫救人
- | 'poison' // 女巫毒人
- | 'protect' // 守卫保护
- | 'shoot' // 猎人开枪
- | 'vote' // 投票
- | 'pair'; // 丘比特连线
- // 玩家状态
- export type PlayerStatus =
- | 'alive' // 存活
- | 'dead' // 死亡
- | 'protected' // 受保护
- | 'poisoned' // 中毒
- | 'coupled'; // 情侣
- // 游戏结果类型
- export type GameResult =
- | 'werewolf_win' // 狼人阵营胜利
- | 'villager_win' // 村民阵营胜利
- | 'couple_win' // 情侣胜利
- | 'draw' // 平局
- | null; // 游戏进行中
- // 玩家信息
- export interface WerewolfPlayer {
- id: string;
- name: string;
- avatar: string;
- role: WerewolfRole;
- status: PlayerStatus;
- isReady: boolean;
- isDead: boolean;
- actions: {
- [key in ActionType]?: boolean;
- };
- // 特殊状态
- isLovers?: boolean; // 是否为情侣
- lastWords?: string; // 遗言
- voteTarget?: string; // 投票目标
- deadRound?: number; // 死亡回合
- }
- // 游戏日志
- export interface GameLog {
- round: number;
- phase: GamePhase;
- content: string;
- timestamp: number;
- isPublic: boolean;
- targetPlayers?: string[];
- }
- // 游戏状态
- export interface WerewolfGame {
- id: string;
- title: string;
- hosterId: string;
- hosterName: string;
- createTime: number;
- startTime: number | null;
- endTime: number | null;
- players: WerewolfPlayer[];
- currentRound: number;
- phase: GamePhase;
- result: GameResult;
- currentAction: ActionType | null;
- timeLimit: number; // 每个阶段的时间限制(秒)
- logs: GameLog[];
- options: {
- roles: {
- [key in WerewolfRole]?: number; // 角色数量配置
- };
- includeSpecialRoles: boolean; // 是否包含特殊角色
- allowLastWords: boolean; // 是否允许遗言
- nightTimeLimit: number; // 夜晚时间限制
- dayTimeLimit: number; // 白天时间限制
- voteTimeLimit: number; // 投票时间限制
- };
- }
- // 定义 Store
- export const useWerewolfStore = defineStore('werewolf', {
- state: () => ({
- currentGame: null as WerewolfGame | null,
- loading: false,
- error: null as string | null,
- currentPlayer: null as WerewolfPlayer | null,
- timer: null as number | null,
- remainingTime: 0,
- myRole: null as WerewolfRole | null,
- myActions: [] as ActionType[],
- gameResult: null as GameResult,
- showRoleInfo: false,
- showActionInfo: false
- }),
-
- getters: {
- // 获取活着的玩家
- alivePlayers: (state) => {
- if (!state.currentGame) return [];
- return state.currentGame.players.filter(p => !p.isDead);
- },
-
- // 获取死亡的玩家
- deadPlayers: (state) => {
- if (!state.currentGame) return [];
- return state.currentGame.players.filter(p => p.isDead);
- },
-
- // 获取狼人玩家
- werewolfPlayers: (state) => {
- if (!state.currentGame) return [];
- return state.currentGame.players.filter(p => p.role === 'werewolf');
- },
-
- // 获取村民阵营玩家
- villagerTeamPlayers: (state) => {
- if (!state.currentGame) return [];
- return state.currentGame.players.filter(p =>
- p.role !== 'werewolf' && p.role !== 'moderator'
- );
- },
-
- // 当前玩家是否是狼人
- isWerewolf: (state) => {
- return state.myRole === 'werewolf';
- },
-
- // 是否是夜晚
- isNightPhase: (state) => {
- return state.currentGame?.phase === 'night';
- },
-
- // 是否是白天
- isDayPhase: (state) => {
- return state.currentGame?.phase === 'day';
- },
-
- // 是否是投票阶段
- isVotePhase: (state) => {
- return state.currentGame?.phase === 'vote';
- },
-
- // 游戏是否结束
- isGameEnded: (state) => {
- return state.currentGame?.phase === 'ended';
- },
-
- // 当前玩家是否可以行动
- canAct: (state) => {
- if (!state.currentGame || !state.myRole || !state.currentPlayer) return false;
- if (state.currentPlayer.isDead) return false;
-
- const { phase, currentAction } = state.currentGame;
-
- // 根据游戏阶段和角色确定是否可以行动
- if (phase === 'night') {
- switch (currentAction) {
- case 'kill': return state.myRole === 'werewolf';
- case 'check': return state.myRole === 'seer';
- case 'save':
- case 'poison': return state.myRole === 'witch';
- case 'protect': return state.myRole === 'guard';
- case 'pair': return state.myRole === 'cupid';
- default: return false;
- }
- } else if (phase === 'vote') {
- return !state.currentPlayer.voteTarget;
- }
-
- return false;
- },
-
- // 获取当前可选择的目标玩家
- availableTargets: (state) => {
- if (!state.currentGame || !state.myRole) return [];
-
- const { phase, currentAction, players } = state.currentGame;
-
- // 根据不同行动类型过滤可选目标
- if (phase === 'night') {
- switch (currentAction) {
- case 'kill': // 狼人只能杀死非狼人
- return players.filter(p => !p.isDead && p.role !== 'werewolf');
- case 'check': // 预言家查验活着的玩家
- return players.filter(p => !p.isDead);
- case 'save': // 女巫只能救死亡的玩家
- return players.filter(p => p.status === 'poisoned');
- case 'poison': // 女巫只能毒活着的玩家
- return players.filter(p => !p.isDead);
- case 'protect': // 守卫只能保护活着的玩家
- return players.filter(p => !p.isDead);
- default:
- return [];
- }
- } else if (phase === 'vote') {
- // 投票阶段可以选择所有活着的玩家(除了自己)
- return players.filter(p => !p.isDead && p.id !== state.currentPlayer?.id);
- }
-
- return [];
- }
- },
-
- actions: {
- // 加载游戏数据
- async loadGame(gameId: string, role: string) {
- this.loading = true;
- this.error = null;
-
- try {
- const result = await werewolfAPI.getGameData(gameId, role);
- if (result && result.game) {
- this.currentGame = result.game;
-
- // 找到当前玩家
- if (this.currentGame.players) {
- // 这里需要根据实际情况从某处获取当前用户ID
- const currentUserId = 'current_user_id'; // 应替换为实际的用户ID获取方式
- this.currentPlayer = this.currentGame.players.find(p => p.id === currentUserId) || null;
- this.myRole = this.currentPlayer?.role || null;
- }
-
- return true;
- }
-
- this.error = '加载游戏数据失败';
- return false;
- } catch (err) {
- console.error('加载狼人杀游戏失败:', err);
- this.error = err instanceof Error ? err.message : '未知错误';
- return false;
- } finally {
- this.loading = false;
- }
- },
-
- // 准备游戏
- async readyGame() {
- if (!this.currentGame || !this.currentPlayer) return false;
-
- try {
- await werewolfAPI.playerAction(
- this.currentGame.id,
- this.currentPlayer.id,
- 'ready'
- );
- return true;
- } catch (err) {
- console.error('准备游戏失败:', err);
- return false;
- }
- },
-
- // 玩家行动
- async performAction(action: ActionType, targetId?: string) {
- if (!this.currentGame || !this.currentPlayer) return false;
-
- try {
- await werewolfAPI.playerAction(
- this.currentGame.id,
- this.currentPlayer.id,
- action,
- targetId
- );
- return true;
- } catch (err) {
- console.error('执行行动失败:', err);
- return false;
- }
- },
-
- // 狼人杀人
- async killPlayer(targetId: string) {
- return this.performAction('kill', targetId);
- },
-
- // 预言家查验
- async checkPlayer(targetId: string) {
- return this.performAction('check', targetId);
- },
-
- // 女巫救人
- async savePlayer(targetId: string) {
- return this.performAction('save', targetId);
- },
-
- // 女巫毒人
- async poisonPlayer(targetId: string) {
- return this.performAction('poison', targetId);
- },
-
- // 守卫保护
- async protectPlayer(targetId: string) {
- return this.performAction('protect', targetId);
- },
-
- // 投票
- async votePlayer(targetId: string) {
- return this.performAction('vote', targetId);
- },
-
- // 猎人开枪
- async shootPlayer(targetId: string) {
- return this.performAction('shoot', targetId);
- },
-
- // 丘比特连线
- async couplePlayer(player1Id: string, player2Id: string) {
- try {
- await werewolfAPI.playerAction(
- this.currentGame!.id,
- this.currentPlayer!.id,
- 'pair',
- JSON.stringify([player1Id, player2Id])
- );
- return true;
- } catch (err) {
- console.error('丘比特连线失败:', err);
- return false;
- }
- },
-
- // 留遗言
- async leaveLastWords(message: string) {
- if (!this.currentGame || !this.currentPlayer) return false;
-
- try {
- await werewolfAPI.playerAction(
- this.currentGame.id,
- this.currentPlayer.id,
- 'lastWords',
- message
- );
- return true;
- } catch (err) {
- console.error('留遗言失败:', err);
- return false;
- }
- },
-
- // 开始倒计时
- startTimer(seconds: number) {
- this.remainingTime = seconds;
-
- // 清除现有的计时器
- if (this.timer !== null) {
- clearInterval(this.timer);
- }
-
- // 设置新的计时器
- this.timer = window.setInterval(() => {
- if (this.remainingTime > 0) {
- this.remainingTime--;
- } else {
- if (this.timer !== null) {
- clearInterval(this.timer);
- this.timer = null;
- }
- }
- }, 1000);
- },
-
- // 停止倒计时
- stopTimer() {
- if (this.timer !== null) {
- clearInterval(this.timer);
- this.timer = null;
- }
- },
-
- // 显示角色信息
- showRoleInformation() {
- this.showRoleInfo = true;
- },
-
- // 隐藏角色信息
- hideRoleInformation() {
- this.showRoleInfo = false;
- },
-
- // 清理游戏数据
- clearGame() {
- this.currentGame = null;
- this.currentPlayer = null;
- this.myRole = null;
- this.myActions = [];
- this.gameResult = null;
- this.stopTimer();
- }
- }
- });
|