Riskless/static/js/player.js

78 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-01-31 12:34:13 +00:00
class Player {
2023-01-29 16:47:37 +00:00
constructor(id, name) {
this.name = name;
this.timeout = null;
this.id = id;
this.ready = false;
this.isPlaying = false;
2023-02-08 17:55:45 +00:00
let randomColor = Math.random() * 360;
this.color = `hsl(${randomColor} 57% 50%)`;
2023-01-29 16:47:37 +00:00
}
resetTimeout() {
if (this.timeout !== null) {
2023-01-31 12:34:13 +00:00
window.clearTimeout(this.timeout);
2023-01-29 16:47:37 +00:00
}
this.timeout = window.setTimeout(() => {
2023-02-06 12:30:24 +00:00
if (players[this.id] !== undefined) {
delete players[this.id];
}
2023-02-06 13:03:25 +00:00
updateDom();
2023-01-29 16:47:37 +00:00
}, TIMEOUT);
}
2023-02-06 11:04:37 +00:00
2023-02-08 17:55:45 +00:00
/**
* Get our color as used on the board.
*/
getColor() {
return this.color;
}
2023-02-06 11:04:37 +00:00
/**
* 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);
2023-02-08 17:55:45 +00:00
return true;
} else {
return false;
2023-02-06 11:04:37 +00:00
}
}
/**
* 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;
2023-02-06 12:30:24 +00:00
this.nextPlayer().startTurn();
2023-02-06 11:04:37 +00:00
}
nextPlayer() {
let sorted = Object.values(players).sort((a, b) => (a.id < b.id ? -1 : 1));
2023-02-08 17:55:45 +00:00
let ourIndex = sorted.findIndex((player) => player.id === this.id);
2023-02-06 11:04:37 +00:00
return sorted[(ourIndex + 1) % sorted.length];
}
2023-01-29 16:47:37 +00:00
}