62 lines
1.2 KiB
JavaScript
62 lines
1.2 KiB
JavaScript
class Player {
|
|
constructor(id, name) {
|
|
this.name = name;
|
|
this.timeout = null;
|
|
this.id = id;
|
|
this.ready = false;
|
|
this.isPlaying = false;
|
|
}
|
|
|
|
resetTimeout() {
|
|
if (this.timeout !== null) {
|
|
window.clearTimeout(this.timeout);
|
|
}
|
|
|
|
this.timeout = window.setTimeout(() => {
|
|
delete players[this.id];
|
|
updatePlayerDom();
|
|
}, TIMEOUT);
|
|
}
|
|
|
|
/**
|
|
* Claim a region of the map.
|
|
*
|
|
* @param data Data received via socket.
|
|
*/
|
|
claim(data) {
|
|
let region = Region.getRegion(data.region);
|
|
|
|
if (region.owner === null) {
|
|
region.claim(this);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Start a player's turn.
|
|
*/
|
|
startTurn() {
|
|
this.isPlaying = true;
|
|
}
|
|
|
|
/**
|
|
* Perform some game action on your turn.
|
|
*
|
|
* @param data Data received via socket.
|
|
*/
|
|
perform(data) {}
|
|
|
|
/**
|
|
* End player's turn
|
|
*/
|
|
endTurn() {
|
|
this.isPlaying = false;
|
|
}
|
|
|
|
nextPlayer() {
|
|
let sorted = Object.values(players).sort((a, b) => (a.id < b.id ? -1 : 1));
|
|
let ourIndex = sorted.findIndex((player) => player.id === ID);
|
|
|
|
return sorted[(ourIndex + 1) % sorted.length];
|
|
}
|
|
}
|