Bugfix: don't allow other players to move warships (#879)

## Description:
The MoveWarshipExecution now verifies that the player who requested to
move owns the warship. This prevents players from moving other players'
warships.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

## Please put your Discord username so you can be contacted if a bug or
regression is found:

evan
This commit is contained in:
evanpelle
2025-05-25 13:24:04 -07:00
committed by evanpelle
parent 27792428a0
commit 182a42e0db
3 changed files with 66 additions and 21 deletions
+49 -1
View File
@@ -175,7 +175,7 @@ describe("Warship", () => {
game.addExecution(new WarshipExecution(warship));
game.addExecution(
new MoveWarshipExecution(warship.id(), game.ref(coastX + 5, 15)),
new MoveWarshipExecution(player1, warship.id(), game.ref(coastX + 5, 15)),
);
executeTicks(game, 10);
@@ -211,4 +211,52 @@ describe("Warship", () => {
// Trade ship should not be captured
expect(tradeShip.owner().id()).toBe(player2.id());
});
test("MoveWarshipExecution fails if player is not the owner", async () => {
const originalPatrolTile = game.ref(coastX + 1, 10);
const warship = player1.buildUnit(
UnitType.Warship,
game.ref(coastX + 1, 5),
{
patrolTile: originalPatrolTile,
},
);
new MoveWarshipExecution(
player2,
warship.id(),
game.ref(coastX + 5, 15),
).init(game, 0);
expect(warship.patrolTile()).toBe(originalPatrolTile);
});
test("MoveWarshipExecution fails if warship is not active", async () => {
const originalPatrolTile = game.ref(coastX + 1, 10);
const warship = player1.buildUnit(
UnitType.Warship,
game.ref(coastX + 1, 5),
{
patrolTile: originalPatrolTile,
},
);
warship.delete();
new MoveWarshipExecution(
player1,
warship.id(),
game.ref(coastX + 5, 15),
).init(game, 0);
expect(warship.patrolTile()).toBe(originalPatrolTile);
});
test("MoveWarshipExecution fails gracefully if warship not found", async () => {
const exec = new MoveWarshipExecution(
player1,
123,
game.ref(coastX + 5, 15),
);
// Verify that no error is thrown.
exec.init(game, 0);
expect(exec.isActive()).toBe(false);
});
});