Stub for act method to perform generic actions during a turn

This commit is contained in:
jude 2023-02-13 15:32:54 +00:00
parent 8cf3e3c41a
commit d99b5b3373
4 changed files with 65 additions and 1 deletions

View File

@ -3,6 +3,7 @@
right: 0; right: 0;
top: 0; top: 0;
padding: 12px; padding: 12px;
z-index: 2;
} }
#ready { #ready {

View File

@ -1,5 +1,15 @@
function unlockMapDom() { function unlockMapDom() {
document.querySelectorAll(".action").forEach((e) => e.classList.remove("hidden")); Object.values(REGIONS).forEach((region) => {
if (!allRegionsClaimed() && region.owner === null) {
document
.querySelector(`.node[data-name=${region.name}] .action`)
.classList.remove("hidden");
} else if (region.owner === us) {
document
.querySelector(`.node[data-name=${region.name}] .action`)
.classList.remove("hidden");
}
});
} }
function lockMapDom() { function lockMapDom() {
@ -23,6 +33,10 @@ function updateMapDom() {
lockMapDom(); lockMapDom();
} }
if (allRegionsClaimed()) {
document.querySelectorAll(".action").forEach((e) => (e.textContent = "Reinf."));
}
for (let region of Object.values(REGIONS)) { for (let region of Object.values(REGIONS)) {
const element = document.querySelector(`.node[data-name=${region.name}]`); const element = document.querySelector(`.node[data-name=${region.name}]`);
element.querySelector(".strength").textContent = region.strength || ""; element.querySelector(".strength").textContent = region.strength || "";

View File

@ -91,6 +91,10 @@ document.addEventListener("DOMContentLoaded", () => {
if (currentPlayer().reinforce(data)) { if (currentPlayer().reinforce(data)) {
currentPlayer().endTurn(); currentPlayer().endTurn();
} }
} else {
if (currentPlayer().act(data)) {
currentPlayer().endTurn();
}
} }
updateDom(); updateDom();

View File

@ -1,10 +1,19 @@
PHASE_REINFORCE = 1;
PHASE_ATTACK = 1;
PHASE_FORTIFY = 1;
class Player { class Player {
constructor(id, name) { constructor(id, name) {
this.name = name; this.name = name;
this.timeout = null; this.timeout = null;
this.id = id; this.id = id;
this.ready = false; this.ready = false;
// Data which is reset every turn
this.isPlaying = false; this.isPlaying = false;
this.reinforcementsPlaced = 0;
this.reinforcementsAvailable = 0;
this.turnPhase = PHASE_REINFORCE;
this.resetColor(); this.resetColor();
} }
@ -66,11 +75,47 @@ class Player {
} }
} }
/**
* Process a generic action packet representing a player's move.
*
* @param data Data received via socket
* @returns {boolean} Whether this player's turn has ended or not.
*/
act(data) {
if (this.turnPhase === PHASE_REINFORCE) {
if (this.reinforce(data)) {
this.reinforcementsPlaced += 1;
}
if (this.reinforcementsPlaced === this.reinforcementsAvailable) {
this.turnPhase = PHASE_ATTACK;
}
return false;
}
}
/**
* Calculate the number of additional reinforcements to gain on this turn.
*/
additionalReinforcements() {
return Math.min(
3,
Math.floor(
Object.values(REGIONS).filter((region) => region.owner === this).length /
3
)
);
}
/** /**
* Start a player's turn. * Start a player's turn.
*/ */
startTurn() { startTurn() {
this.isPlaying = true; this.isPlaying = true;
this.reinforcementsPlaced = 0;
this.reinforcementsAvailable = this.additionalReinforcements();
this.turnPhase = PHASE_REINFORCE;
} }
/** /**