-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
273 lines (241 loc) · 6.89 KB
/
Copy pathgame.js
File metadata and controls
273 lines (241 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// 狼人杀游戏引擎
import { GAME_CONFIG, AI_PERSONALITIES } from './config.js';
export class WerewolfGame {
constructor() {
this.config = GAME_CONFIG;
this.players = [];
this.round = 0;
this.phase = null;
this.history = [];
this.nightActions = {};
this.voteResults = {};
this.winner = null;
this.logs = [];
this.speeches = [];
}
// 初始化游戏
init() {
this.players = this.createPlayers();
this.assignRoles();
this.assignPersonalities();
this.round = 1;
this.phase = this.config.phases.NIGHT;
this.addLog('gameStart', '🎮 游戏开始!12位玩家已就座,天黑请闭眼...');
return this;
}
// 创建玩家
createPlayers() {
const players = [];
const names = [
'张三', '李四', '王五', '赵六',
'孙七', '周八', '吴九', '郑十',
'陈一', '刘二', '杨三', '黄四'
];
for (let i = 1; i <= this.config.totalPlayers; i++) {
players.push({
id: i,
seat: i,
name: names[i - 1],
role: null,
alive: true,
personality: null,
// 特殊角色状态
hasPoison: true, // 女巫毒药
hasAntidote: true, // 女巫解药
lastGuarded: null, // 守卫上次守护的人
checkedPlayers: [] // 预言家查验记录
});
}
return players;
}
// 分配角色
assignRoles() {
const roles = [];
Object.entries(this.config.roles).forEach(([roleKey, roleConfig]) => {
for (let i = 0; i < roleConfig.count; i++) {
roles.push(roleKey);
}
});
// 洗牌算法
for (let i = roles.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[roles[i], roles[j]] = [roles[j], roles[i]];
}
// 分配给玩家
this.players.forEach((player, index) => {
player.role = roles[index];
});
}
// 分配性格
assignPersonalities() {
const shuffledPersonalities = [...AI_PERSONALITIES];
for (let i = shuffledPersonalities.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledPersonalities[i], shuffledPersonalities[j]] =
[shuffledPersonalities[j], shuffledPersonalities[i]];
}
this.players.forEach((player, index) => {
player.personality = shuffledPersonalities[index % shuffledPersonalities.length];
});
}
// 获取存活玩家
getAlivePlayers() {
return this.players.filter(p => p.alive);
}
// 获取特定角色的玩家
getPlayersByRole(roleKey) {
return this.players.filter(p => p.role === roleKey);
}
// 获取存活狼人
getAliveWolves() {
return this.players.filter(p => p.role === 'WEREWOLF' && p.alive);
}
// 获取存活好人
getAliveVillagers() {
return this.players.filter(p => p.role !== 'WEREWOLF' && p.alive);
}
// 检查游戏是否结束
checkGameOver() {
const aliveWolves = this.getAliveWolves().length;
const aliveVillagers = this.getAliveVillagers().length;
if (aliveWolves === 0) {
this.winner = 'village';
this.phase = this.config.phases.GAME_OVER;
this.addLog('gameEnd', '🎉 游戏结束!好人阵营获胜!');
return true;
}
if (aliveWolves >= aliveVillagers) {
this.winner = 'wolf';
this.phase = this.config.phases.GAME_OVER;
this.addLog('gameEnd', '🎉 游戏结束!狼人阵营获胜!');
return true;
}
return false;
}
// 添加日志
addLog(type, message, data = {}) {
const log = {
type,
message,
timestamp: new Date().toISOString(),
round: this.round,
phase: this.phase,
data
};
this.logs.push(log);
return log;
}
// 添加发言记录
addSpeech(playerId, content, round = this.round) {
const speech = {
playerId,
playerName: this.players.find(p => p.id === playerId)?.name,
seat: this.players.find(p => p.id === playerId)?.seat,
content,
round,
phase: this.phase,
timestamp: new Date().toISOString()
};
this.speeches.push(speech);
return speech;
}
// 记录夜晚行动
recordNightAction(playerId, action, target) {
if (!this.nightActions[this.round]) {
this.nightActions[this.round] = {};
}
this.nightActions[this.round][playerId] = {
action,
target,
timestamp: new Date().toISOString()
};
}
// 记录投票
recordVote(voterId, targetId, reason = '') {
if (!this.voteResults[this.round]) {
this.voteResults[this.round] = [];
}
this.voteResults[this.round].push({
voterId,
voterSeat: this.players.find(p => p.id === voterId)?.seat,
targetId,
targetSeat: this.players.find(p => p.id === targetId)?.seat,
reason,
timestamp: new Date().toISOString()
});
}
// 玩家死亡
killPlayer(playerId, cause = 'unknown') {
const player = this.players.find(p => p.id === playerId);
if (player && player.alive) {
player.alive = false;
player.deathRound = this.round;
player.deathCause = cause;
const roleConfig = this.config.roles[player.role];
this.addLog('playerDeath',
`💀 ${player.seat}号玩家【${player.name}】死亡,身份是【${roleConfig.name}】`,
{ playerId, cause }
);
return true;
}
return false;
}
// 进入下一轮
nextRound() {
this.round++;
this.addLog('roundStart', `📍 第${this.round}轮开始`);
return this;
}
// 获取游戏状态
getState() {
return {
round: this.round,
phase: this.phase,
players: this.players.map(p => ({
id: p.id,
seat: p.seat,
name: p.name,
alive: p.alive,
role: p.role,
roleInfo: this.config.roles[p.role],
personality: p.personality,
deathRound: p.deathRound,
deathCause: p.deathCause
})),
aliveCount: {
total: this.getAlivePlayers().length,
wolves: this.getAliveWolves().length,
villagers: this.getAliveVillagers().length
},
winner: this.winner,
recentLogs: this.logs.slice(-10),
recentSpeeches: this.speeches.slice(-20)
};
}
// 导出游戏记录
exportGame() {
return {
config: this.config,
players: this.players,
rounds: this.round,
winner: this.winner,
history: this.history,
logs: this.logs,
speeches: this.speeches,
nightActions: this.nightActions,
voteResults: this.voteResults
};
}
// 从存档恢复游戏
loadGame(savedGame) {
this.players = savedGame.players;
this.round = savedGame.rounds;
this.winner = savedGame.winner;
this.history = savedGame.history || [];
this.logs = savedGame.logs || [];
this.speeches = savedGame.speeches || [];
this.nightActions = savedGame.nightActions || {};
this.voteResults = savedGame.voteResults || {};
return this;
}
}