Riskless/static/js/player.js
2023-02-06 12:30:24 +00:00

74 lines
1.5 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(() => {
if (players[this.id] !== undefined) {
delete players[this.id];
}
updatePlayerDom();
}, TIMEOUT);
}
/**
* Allow the user to claim regions whilst in the pre-game phase.
*/
claimRegions() {
unlockMapDom();
lockMapDom();
}
/**
* 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;
this.nextPlayer().startTurn();
}
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];
}
}