-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- function declareGreatestEmpire(player) on usePlayer() - test usePlayer.declareGreatestEmpire - function: empireSize(board): Record<Player, number> - test empire-size - refactor: move isConquering({ ... }) from useBoard() - check if player is creating greatest empire on build action - TODO: check if player is creating greatest empire on conquer!
- Loading branch information
1 parent
dbc6e59
commit 93ed0c1
Showing
7 changed files
with
184 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { empireSize } from "./empire-size"; | ||
|
||
const board = { | ||
"-2,3": { | ||
terrain: "field", | ||
building: { owner: "player", type: "town" }, | ||
piece: undefined, | ||
}, | ||
"-1,3": { | ||
terrain: "field", | ||
building: undefined, | ||
piece: undefined, | ||
}, | ||
"0,3": { | ||
terrain: "field", | ||
building: { owner: "enemy3", type: "village" }, | ||
piece: undefined, | ||
}, | ||
"1,3": { | ||
terrain: "field", | ||
building: { owner: "enemy3", type: "city" }, | ||
piece: undefined, | ||
}, | ||
"2,3": { | ||
terrain: "field", | ||
building: { owner: "enemy1", type: "city" }, | ||
piece: undefined, | ||
}, | ||
} as Board; | ||
|
||
describe("empireSize()", () => { | ||
it("returns number of buildings grouped by player", () => { | ||
expect(empireSize(board)).toEqual({ | ||
player: 1, | ||
enemy1: 1, | ||
enemy2: 0, | ||
enemy3: 2, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export function empireSize(board: Board) { | ||
return Object.entries(board).reduce( | ||
(acc, [_, tile]) => { | ||
if (tile.building) { | ||
const player = tile.building.owner; | ||
acc[player] = acc[player] + 1; | ||
} | ||
return acc; | ||
}, | ||
{ | ||
player: 0, | ||
enemy1: 0, | ||
enemy2: 0, | ||
enemy3: 0, | ||
} | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export function isConquering({ | ||
targetTile, | ||
player, | ||
}: { | ||
targetTile: Tile; | ||
player: Player; | ||
}) { | ||
if (targetTile.building && targetTile.building.owner !== player) { | ||
console.info(`Score: ${player} is conquering a settlement`); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
export function isCreatingGreatesEmpire({ | ||
building, | ||
empires, | ||
}: { | ||
building: Building; | ||
empires: Record<Player, number>; | ||
}) { | ||
const player = building.owner; | ||
const newEmpireSize = empires[player] + 1; | ||
if (newEmpireSize < 3) { | ||
return false; | ||
} | ||
const isGreatestEmpire = Object.values(empires).every( | ||
(size) => size < newEmpireSize | ||
); | ||
if (isGreatestEmpire) { | ||
console.info(`Score: ${player} is creating the greatest empire`); | ||
return true; | ||
} | ||
return false; | ||
} |