Skip to content

Commit

Permalink
Merge pull request #88 from bananu7/dont-allow-attack-self
Browse files Browse the repository at this point in the history
Dont allow attack self
  • Loading branch information
bananu7 authored Jun 23, 2024
2 parents d259f21 + 08dad9e commit c5482dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/server/src/game/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export const attackCommand = (ctx: CommandContext, cmd: CommandAttack) => {
throw new ComponentMissingError("Attacker");
}

if (ctx.unit.id === cmd.target)
throw new InvalidCommandError("Unit tried to attack itself");

// target not existing is a common situation if the target got destroyed
// after the command was made
const target = g.units.find(u => u.id === cmd.target);
Expand Down
20 changes: 20 additions & 0 deletions packages/server/test/behaviour.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ test('attack-move action', () => {
expect(game.units[0].state.action).toBe('Attacking');
});

describe('attack action', () => {
test("don't allow targetting self", () => {
const game = createBasicGame({});
spawnUnit(game, 1, "Trooper", {x: 4, y: 10});

command({
command: { typ: 'Attack', target: 1, },
unitIds: [1],
shift: false,
},
game,
1
);
tick(TICK_MS, game);

expect(game.units[0].state.state).toBe('idle');
expect(game.units[0].state.action).toBe('Idle');
});
})

describe('produce action', () => {
test('ensure resources', () => {
const game = createBasicGame({});
Expand Down

0 comments on commit c5482dc

Please sign in to comment.