Riskless/static/js/modules/interface/map.js
2023-03-13 14:52:14 +00:00

154 lines
3.7 KiB
JavaScript

import { game } from "./main.js";
let allPlaced = false;
// In standard Risk, this is 5
const _REINFORCEMENT_MULTIPLIER = 1;
const REGIONS = {};
class Continent {
constructor(name) {
this.name = name;
this.yield = 0;
}
}
class Strength {
constructor(cyphertext) {
this.cyphertext = cyphertext;
this.assumedStrength = null;
}
update(amount) {
if (this.cyphertext === null) {
this.cyphertext = amount;
} else {
this.cyphertext *= amount;
}
this.assumedStrength = null;
}
}
export class Region {
constructor(name, continent) {
this.name = name;
this.owner = null;
this.strength = new Strength(null);
this.neighbours = new Set();
this.continent = continent;
REGIONS[name] = this;
}
static setNeighbours(region1, region2) {
region1.neighbours.add(region2);
region2.neighbours.add(region1);
}
static allRegionsClaimed() {
return (
Object.values(REGIONS).find((region) => region.owner === null) === undefined
);
}
// todo fix
static reinforcementsRemaining() {
if (allPlaced) {
return 0;
} else {
let totalStrength = Object.values(REGIONS)
.filter((region) => region.owner === game.us)
.reduce(
(counter, region) => counter + region.strength.assumedStrength,
0
);
return _REINFORCEMENT_MULTIPLIER * (10 - game.playerCount()) - totalStrength;
}
}
static allReinforcementsPlaced() {
if (allPlaced) {
return true;
} else {
let totalStrength = Object.values(REGIONS).reduce(
(counter, region) => counter + region.strength.assumedStrength,
0
);
allPlaced =
totalStrength >=
game.playerCount() *
_REINFORCEMENT_MULTIPLIER *
(10 - game.playerCount());
return allPlaced;
}
}
static getRegion(name) {
return REGIONS[name];
}
static getAllRegions() {
return Object.values(REGIONS);
}
claim(player) {
this.owner = player;
this.strength.update(player.paillierPubKey.encrypt(1n));
this.strength.assumedStrength = 1;
}
reinforce(amount) {
this.strength.update(amount);
}
isClaimed() {
return this.strength.cyphertext !== null;
}
displayStrength() {
if (this.owner === null) {
return "";
} else if (this.owner === game.us) {
return window.paillier.privKey.decrypt(this.strength.cyphertext).toString();
} else if (this.strength.assumedStrength !== null) {
return `${this.strength.assumedStrength}`;
} else {
return "?";
}
}
}
const EAST = new Continent("East");
const WEST = new Continent("West");
const A = new Region("A", EAST);
const B = new Region("B", EAST);
const C = new Region("C", EAST);
const D = new Region("D", EAST);
const J = new Region("J", EAST);
const F = new Region("F", WEST);
const G = new Region("G", WEST);
const H = new Region("H", WEST);
const I = new Region("I", WEST);
const E = new Region("E", WEST);
Region.setNeighbours(A, B);
Region.setNeighbours(A, C);
Region.setNeighbours(B, C);
Region.setNeighbours(B, J);
Region.setNeighbours(C, D);
Region.setNeighbours(C, F);
Region.setNeighbours(E, J);
Region.setNeighbours(E, I);
Region.setNeighbours(E, H);
Region.setNeighbours(F, J);
Region.setNeighbours(F, G);
Region.setNeighbours(G, H);
Region.setNeighbours(G, I);
Region.setNeighbours(H, I);