You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.7 KiB
104 lines
2.7 KiB
2 years ago
|
import {Room} from "../room"
|
||
|
import {ServerGame} from "./game_standard"
|
||
|
import {Client} from "../client"
|
||
|
|
||
|
export class Chainreact extends ServerGame {
|
||
|
|
||
|
readyForTurn: Client[];
|
||
|
currentTurnIndex: number;
|
||
|
currentGameData: any;
|
||
|
colorHues: { [id: string]: number };
|
||
|
|
||
|
constructor(room: Room, settings: Settings.Global) {
|
||
|
super(room, settings);
|
||
|
this.readyForTurn = []
|
||
|
}
|
||
|
|
||
|
setEvents(client: Client) {
|
||
|
let socket = client.socket;
|
||
|
socket.on('ready-for-turn', isDead => {
|
||
|
if (isDead) {
|
||
|
client.isPlayer = false;
|
||
|
client.isSpectator = true;
|
||
|
this.room.toAll('client-list', this.room.clients)
|
||
|
} else {
|
||
|
this.readyForTurn.push(client)
|
||
|
}
|
||
|
|
||
|
let allReady = true;
|
||
|
this.room.players.forEach(c => {
|
||
|
if (this.readyForTurn.find(r => r.id === c.id) == null) {
|
||
|
allReady = false
|
||
|
}
|
||
|
});
|
||
|
if (allReady) {
|
||
|
this.nextTurn();
|
||
|
this.readyForTurn = []
|
||
|
}
|
||
|
});
|
||
|
socket.on('set-slot', (fieldsIndex: number, slotsIndex: number) => {
|
||
|
this.room.toAll('set-slot', fieldsIndex, slotsIndex, socket.id)
|
||
|
});
|
||
|
socket.on('game-data', data => this.currentGameData = data)
|
||
|
}
|
||
|
|
||
|
addClient(client: Client): void {
|
||
|
super.addClient(client);
|
||
|
if (client.isSpectator) {
|
||
|
let room = this.room;
|
||
|
let data = this.currentGameData;
|
||
|
let hues = this.colorHues;
|
||
|
let turnId = '';
|
||
|
if (this.room.players[this.currentTurnIndex])
|
||
|
turnId = this.room.players[this.currentTurnIndex].id;
|
||
|
client.send('start-spectate', room, data, hues, turnId)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
removeClient(client: Client): void {
|
||
|
super.removeClient(client);
|
||
|
if (this.room.players.indexOf(client) === this.currentTurnIndex)
|
||
|
this.nextTurn(true);
|
||
|
|
||
|
let s = client.socket;
|
||
|
s.removeAllListeners('set-slot');
|
||
|
s.removeAllListeners('ready-for-turn');
|
||
|
s.removeAllListeners('game-data')
|
||
|
}
|
||
|
|
||
|
nextTurn(skip?: boolean) {
|
||
|
if (this.currentTurnIndex != null && !skip) {
|
||
|
this.currentTurnIndex++;
|
||
|
if (this.currentTurnIndex >= this.room.players.length) {
|
||
|
this.currentTurnIndex = 0
|
||
|
}
|
||
|
} else if (!skip) {
|
||
|
this.setTurnAndColors()
|
||
|
}
|
||
|
let index = this.currentTurnIndex;
|
||
|
if (skip) {
|
||
|
index = this.currentTurnIndex + 1;
|
||
|
if (index >= this.room.players.length) {
|
||
|
index = 0;
|
||
|
this.currentTurnIndex = 0
|
||
|
}
|
||
|
}
|
||
|
if (this.room.players.length) {
|
||
|
this.room.toAll('current-turn', this.room.players[index].id)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
setTurnAndColors() {
|
||
|
this.currentTurnIndex = Math.floor(Math.random() * this.room.players.length);
|
||
|
let colorHues = [0, 60, 120, 240];
|
||
|
this.colorHues = {};
|
||
|
for (let c of this.room.players) {
|
||
|
let index = Math.floor(Math.random() * colorHues.length);
|
||
|
let hue = colorHues[index];
|
||
|
colorHues.splice(index, 1);
|
||
|
this.colorHues[c.id] = hue
|
||
|
}
|
||
|
this.room.toAll('player-colors', this.colorHues)
|
||
|
}
|
||
|
|
||
|
}
|