Add some stuff

This commit is contained in:
jude
2023-02-10 15:47:21 +00:00
parent 6db33c112e
commit 235c0a0e42
13 changed files with 326 additions and 18 deletions

View File

@ -76,20 +76,22 @@ document.addEventListener("DOMContentLoaded", () => {
barrier.resolve(data);
break;
case "CLAIM":
case "ACT":
if (data.author !== currentPlayer().id) {
return;
}
// Claim a region in the pregame.
if (currentPlayer().claim(data)) {
// Increment to next player.
if (!allRegionsClaimed()) {
// Claim a region in the pregame.
if (currentPlayer().claim(data)) {
// Increment to next player.
currentPlayer().endTurn();
}
} else if (!allReinforcementsPlaced()) {
currentPlayer().reinforce(data);
currentPlayer().endTurn();
}
if (allRegionsClaimed()) {
console.log("switching to initial reinforcements");
}
updateDom();
break;
}

View File

@ -27,15 +27,6 @@ class Region {
return REGIONS[name];
}
/**
* Test if all regions are claimed or not.
*
* @returns {boolean}
*/
static allClaimed() {
return Object.values(REGIONS).find((r) => r.owner === null) === null;
}
claim(player) {
this.owner = player;
this.strength = 1;
@ -64,3 +55,20 @@ const E = new Region("E", WEST);
function allRegionsClaimed() {
return Object.values(REGIONS).find((region) => region.owner === null) === undefined;
}
let allPlaced = false;
function allReinforcementsPlaced() {
if (allPlaced) {
return true;
} else {
let totalStrength = Object.values(REGIONS).reduce(
(counter, region) => counter + region.strength,
0
);
let numPlayers = Object.values(players).length;
allPlaced = totalStrength >= numPlayers * 5 * (10 - numPlayers);
return allPlaced;
}
}

View File

@ -35,7 +35,14 @@ class Packet {
static createRegionClaim(region) {
return {
...this._createBase("CLAIM"),
...this._createBase("ACT"),
region: region,
};
}
static createReinforce(region) {
return {
...this._createBase("ACT"),
region: region,
};
}

View File

@ -46,6 +46,22 @@ class Player {
}
}
/**
* Reinforce a region of the map.
*
* @param data Data received via socket.
*/
reinforce(data) {
let region = Region.getRegion(data.region);
if (region.owner === this) {
region.reinforce(1);
return true;
} else {
return false;
}
}
/**
* Start a player's turn.
*/