Skip to content

Commit

Permalink
Fixup other tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Feb 26, 2025
1 parent 7ae6bd6 commit cf46408
Show file tree
Hide file tree
Showing 26 changed files with 233 additions and 217 deletions.
28 changes: 18 additions & 10 deletions spec/unit/logic/gamestate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect, describe } from "@jest/globals";
import { Team, TeamGroup, WormIdentity } from "../../../src/logic/teams";
import { TeamDefinition, TeamGroup, WormIdentity } from "../../../src/logic/teams";
import { GameRules, GameState } from "../../../src/logic/gamestate";
import { GameWorld } from "../../../src/world";
import { DefaultWeaponSchema } from "../../../src/weapons/schema";
Expand All @@ -10,14 +10,14 @@ const DEAD_WORM: WormIdentity = {
maxHealth: 100,
}

const RED_TEAM: Team = {
const RED_TEAM: TeamDefinition = {
name: "Lovely Reds",
group: TeamGroup.Red,
worms: [{
name: "Diabolical Steve",
health: 25,
maxHealth: 100,
},{
}, {
name: "Generous Greggory",
health: 25,
maxHealth: 100,
Expand All @@ -27,7 +27,7 @@ const RED_TEAM: Team = {
uuid: "red",
}

const RED_TEAM_2: Team = {
const RED_TEAM_2: TeamDefinition = {
name: "Passed over Reds",
group: TeamGroup.Red,
worms: [{
Expand All @@ -40,7 +40,7 @@ const RED_TEAM_2: Team = {
uuid: "red2",
}

const BLUE_TEAM: Team = {
const BLUE_TEAM: TeamDefinition = {
name: "Melodramatic Blues",
group: TeamGroup.Blue,
worms: [{
Expand Down Expand Up @@ -70,6 +70,8 @@ function completeRound(gameState: GameState) {
gameState.beginRound();
gameState.playerMoved();
gameState.markAsFinished();
// Don't return toast.
delete round.toast;
return round;
}

Expand All @@ -78,7 +80,7 @@ describe('GameState', () => {
expect(() => new GameState([], {} as GameWorld, DefaultRules)).toThrow();
});
test('should be able to get active teams', () => {
const gameState = new GameState([RED_TEAM, {...BLUE_TEAM, worms: [DEAD_WORM]}], {} as GameWorld, DefaultRules);
const gameState = new GameState([RED_TEAM, { ...BLUE_TEAM, worms: [DEAD_WORM] }], {} as GameWorld, DefaultRules);
const teams = gameState.getActiveTeams();
expect(teams).toHaveLength(1);
});
Expand Down Expand Up @@ -141,7 +143,7 @@ describe('GameState', () => {
expect(gameState.iteration).toEqual(1);
gameState.beginRound();
for (let index = 0; index < 5; index++) {
gameState.update({ deltaMS: 1000 });
gameState.update({ deltaMS: 1000 });
}
expect(gameState.remainingRoundTime).toEqual(DefaultRules.roundDurationMs);
expect(gameState.isPreRound).toEqual(false);
Expand All @@ -155,16 +157,22 @@ describe('GameState', () => {
expect(gameState.iteration).toEqual(1);
gameState.beginRound();
for (let index = 0; index < 5; index++) {
gameState.update({ deltaMS: 1000 });
gameState.update({ deltaMS: 1000 });
}
expect(gameState.remainingRoundTime).toEqual(DefaultRules.roundDurationMs);
expect(gameState.isPreRound).toEqual(false);
expect(gameState.paused).toEqual(false);
for (let index = 0; index < 45; index++) {
gameState.update({ deltaMS: 1000 });
gameState.update({ deltaMS: 1000 });
}
expect(gameState.remainingRoundTime).toEqual(0);
expect(gameState.advanceRound()).toEqual({ nextTeam: blueTeam, nextWorm: blueTeam.worms[0] });
const state = gameState.advanceRound();
if ('winningTeams' in state) {
throw Error('Unexpected win');
}
const { nextTeam, nextWorm } = state;
expect(nextTeam).toEqual(blueTeam);
expect(nextWorm).toEqual(blueTeam.worms[0]);
});

});
96 changes: 47 additions & 49 deletions spec/unit/net/netGameState.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { test, expect, describe } from "@jest/globals";
import { Team, TeamGroup, WormIdentity } from "../../../src/logic/teams";
import { test, describe } from "@jest/globals";
import { TeamDefinition, TeamGroup, WormIdentity } from "../../../src/logic/teams";
import { GameRules, GameState } from "../../../src/logic/gamestate";
import { GameWorld } from "../../../src/world";
import { DefaultWeaponSchema } from "../../../src/weapons/schema";
import { NetGameState } from "../../../src/net/netGameState";
import { StateRecorder, StateRecorderStore } from "../../../src/state/recorder";
import { StateRecordLine, StateRecordWormGameState } from "../../../src/state/model";
import { TypedEventEmitter } from "matrix-js-sdk";
import { EventEmitter } from "pixi.js";

const DEAD_WORM: WormIdentity = {
Expand All @@ -19,14 +17,14 @@ const playerHost = "@one:example.org";
const playerTwo = "@two:example.org";
const playerThree = "@three:example.org";

const RED_TEAM: Team = {
const RED_TEAM: TeamDefinition = {
name: "Lovely Reds",
group: TeamGroup.Red,
worms: [{
name: "Diabolical Steve",
health: 25,
maxHealth: 100,
},{
}, {
name: "Generous Greggory",
health: 25,
maxHealth: 100,
Expand All @@ -37,7 +35,7 @@ const RED_TEAM: Team = {
}


const BLUE_TEAM: Team = {
const BLUE_TEAM: TeamDefinition = {
name: "Melodramatic Blues",
group: TeamGroup.Blue,
worms: [{
Expand All @@ -50,17 +48,17 @@ const BLUE_TEAM: Team = {
uuid: 'blue',
}

const GREEN_TEAM: Team = {
name: "Grand Greens",
group: TeamGroup.Green,
worms: [{
name: "Unlucky dave",
health: 100,
maxHealth: 100,
}],
ammo: {},
playerUserId: playerThree,
uuid: 'green',
const GREEN_TEAM: TeamDefinition = {
name: "Grand Greens",
group: TeamGroup.Green,
worms: [{
name: "Unlucky dave",
health: 100,
maxHealth: 100,
}],
ammo: {},
playerUserId: playerThree,
uuid: 'green',
}

const DefaultRules: GameRules = {
Expand All @@ -84,44 +82,44 @@ function completeRound(gameState: GameState) {
}

class TestRecorderStore extends EventEmitter<{
data: (data: StateRecordLine) => void;
data: (data: StateRecordLine) => void;
}> implements StateRecorderStore {
async writeLine(data: StateRecordLine): Promise<void> {
this.emit('data', data);
}
async writeLine(data: StateRecordLine): Promise<void> {
this.emit('data', data);
}

}

function createEnvironment() {
const recorderStoreOne = new TestRecorderStore();
const recorderStoreTwo = new TestRecorderStore();
const recorderStoreThree = new TestRecorderStore();
const stateOne = new NetGameState([{...RED_TEAM}, {...BLUE_TEAM}, {...GREEN_TEAM}], { } as any, {...DefaultRules}, new StateRecorder(recorderStoreOne), playerHost);
const stateTwo = new NetGameState([{...RED_TEAM}, {...BLUE_TEAM}, {...GREEN_TEAM}], { } as any, {...DefaultRules}, new StateRecorder(recorderStoreTwo), playerTwo);
const stateThree = new NetGameState([{...RED_TEAM}, {...BLUE_TEAM}, {...GREEN_TEAM}], { } as any, {...DefaultRules}, new StateRecorder(recorderStoreThree), playerThree);
const recorderStoreOne = new TestRecorderStore();
const recorderStoreTwo = new TestRecorderStore();
const recorderStoreThree = new TestRecorderStore();
const stateOne = new NetGameState([{ ...RED_TEAM }, { ...BLUE_TEAM }, { ...GREEN_TEAM }], {} as any, { ...DefaultRules }, new StateRecorder(recorderStoreOne), playerHost);
const stateTwo = new NetGameState([{ ...RED_TEAM }, { ...BLUE_TEAM }, { ...GREEN_TEAM }], {} as any, { ...DefaultRules }, new StateRecorder(recorderStoreTwo), playerTwo);
const stateThree = new NetGameState([{ ...RED_TEAM }, { ...BLUE_TEAM }, { ...GREEN_TEAM }], {} as any, { ...DefaultRules }, new StateRecorder(recorderStoreThree), playerThree);

recorderStoreOne.on('data', (d) => {
// N.B: This needs filtering out in a different layer in runtime.
// stateOne.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
stateTwo.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
stateThree.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
});
recorderStoreTwo.on('data', (d) => {
stateOne.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
// stateTwo.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
stateThree.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
});
recorderStoreThree.on('data', (d) => {
stateOne.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
stateTwo.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
// stateThree.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
});
recorderStoreOne.on('data', (d) => {
// N.B: This needs filtering out in a different layer in runtime.
// stateOne.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
stateTwo.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
stateThree.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
});
recorderStoreTwo.on('data', (d) => {
stateOne.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
// stateTwo.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
stateThree.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
});
recorderStoreThree.on('data', (d) => {
stateOne.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
stateTwo.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
// stateThree.applyGameStateUpdate(d.data as StateRecordWormGameState["data"]);
});

return [
{gameState: stateOne, store: recorderStoreOne},
{gameState: stateTwo, store: recorderStoreTwo},
{gameState: stateThree, store: recorderStoreThree}
];
return [
{ gameState: stateOne, store: recorderStoreOne },
{ gameState: stateTwo, store: recorderStoreTwo },
{ gameState: stateThree, store: recorderStoreThree }
];
}

describe('NetGameState', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/phys/bazookaShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@dimforge/rapier2d-compat";
import { Coordinate, MetersValue } from "../../utils/coodinate";
import { AssetPack } from "../../assets";
import { WormInstance } from "../../logic/teams";
import { WormInstance } from "../../logic";
import { angleForVector } from "../../utils";
import { EntityType } from "../type";

Expand Down
2 changes: 1 addition & 1 deletion src/entities/phys/firework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { AssetPack } from "../../assets";
import { BitmapTerrain } from "../bitmapTerrain";
import { angleForVector } from "../../utils";
import { EntityType } from "../type";
import { WormInstance } from "../../logic/teams";
import { WormInstance } from "../../logic";

const COLOUR_SET = [0x08ff08, 0xffcf00, 0xfe1493, 0xff5555, 0x00fdff, 0xccff02];

Expand Down
2 changes: 1 addition & 1 deletion src/entities/phys/grenade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { magnitude } from "../../utils";
import { Coordinate, MetersValue } from "../../utils/coodinate";
import { AssetPack } from "../../assets";
import { DefaultTextStyle } from "../../mixins/styles";
import { WormInstance } from "../../logic/teams";
import { WormInstance } from "../../logic";
import { EntityType } from "../type";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/entities/phys/homingMissile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@dimforge/rapier2d-compat";
import { Coordinate, MetersValue } from "../../utils/coodinate";
import { AssetPack } from "../../assets";
import { WormInstance } from "../../logic/teams";
import { WormInstance } from "../../logic";
import { angleForVector, mult } from "../../utils";
import { EntityType } from "../type";
import Logger from "../../log";
Expand Down
2 changes: 1 addition & 1 deletion src/entities/phys/timedExplosive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { PhysicsEntity } from "./physicsEntity";
import { GameWorld, RapierPhysicsObject } from "../../world";
import { Vector2 } from "@dimforge/rapier2d-compat";
import { MetersValue } from "../../utils/coodinate";
import { WormInstance } from "../../logic/teams";
import { WormInstance } from "../../logic";
import { handleDamageInRadius } from "../../utils/damage";
import { RecordedEntityState } from "../../state/model";

Expand Down
2 changes: 1 addition & 1 deletion src/entities/playable/playable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { GameWorld, RapierPhysicsObject } from "../../world";
import { magnitude, MetersValue, mult, sub } from "../../utils";
import { Vector2 } from "@dimforge/rapier2d-compat";
import { IPhysicalEntity, OnDamageOpts } from "../entity";
import { teamGroupToColorSet, WormInstance } from "../../logic/teams";
import { teamGroupToColorSet, WormInstance } from "../../logic";
import { applyGenericBoxStyle, DefaultTextStyle } from "../../mixins/styles";
import { Viewport } from "pixi-viewport";
import { handleDamageInRadius } from "../../utils/damage";
Expand Down
2 changes: 1 addition & 1 deletion src/entities/playable/remoteWorm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Viewport } from "pixi-viewport";
import { WormInstance } from "../../logic/teams";
import { WormInstance } from "../../logic";
import { Toaster } from "../../overlays/toaster";
import { Coordinate } from "../../utils";
import { GameWorld } from "../../world";
Expand Down
2 changes: 1 addition & 1 deletion src/entities/playable/testDummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ColliderDesc,
RigidBodyDesc,
} from "@dimforge/rapier2d-compat";
import { WormInstance } from "../../logic/teams";
import { WormInstance } from "../../logic";
import { PlayableEntity } from "./playable";
import { Viewport } from "pixi-viewport";
import { EntityType } from "../type";
Expand Down
2 changes: 1 addition & 1 deletion src/entities/playable/worm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ export class Worm extends PlayableEntity<WormRecordedState> {
} else {
this.weaponSprite.position.set(
this.sprite.x -
(this.sprite.width + this.currentWeapon.sprite.offset.x),
(this.sprite.width + this.currentWeapon.sprite.offset.x),
this.sprite.y + this.currentWeapon.sprite.offset.y,
);
this.weaponSprite.rotation = this.fireAngle - Math.PI;
Expand Down
6 changes: 3 additions & 3 deletions src/interop/gamechannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import EventEmitter from "events";
import TypedEmitter from "typed-emitter";
import type { Team } from "../logic/teams";
import type { TeamDefinition } from "../logic/teams";
import type { IWeaponCode, IWeaponDefiniton } from "../weapons/weapon";

interface GoToMenuEvent {
winDetails?: WinDetails;
}

interface WinDetails {
winningTeams: Team[];
teams: Team[];
winningTeams: TeamDefinition[];
teams: TeamDefinition[];
}

export type AmmoCount = [IWeaponDefiniton, number][];
Expand Down
12 changes: 6 additions & 6 deletions src/levels/scenarioParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
TiledTileset,
} from "./types";
import { WormSpawnRecordedState } from "../entities/state/wormSpawn";
import { Team, TeamGroup, WormIdentity } from "../logic/teams";
import { TeamDefinition, TeamGroup, WormIdentity } from "../logic/teams";
import { IWeaponCode } from "../weapons/weapon";
import { DefaultWeaponSchema } from "../weapons/schema";
import { getSpawnPoints } from "../terrain/spawner";
Expand All @@ -32,7 +32,7 @@ export interface Scenario {
};
objects: RecordedEntityState[];
rules: GameRules;
teams: Team[];
teams: TeamDefinition[];
}

function parseObjectToRecordedState(
Expand All @@ -49,11 +49,11 @@ function parseObjectToRecordedState(
}
}

function determineTeams(teamProps: TiledTeamProperties[]): Team[] {
function determineTeams(teamProps: TiledTeamProperties[]): TeamDefinition[] {
return teamProps.map((tiledTeam) => {
const health = tiledTeam["wormgine.starting_health"] ?? 100;
// TODO: Make this cleaner
const ammo: Team["ammo"] = {};
const ammo: TeamDefinition["ammo"] = {};
for (const [wep, ammoCount] of Object.entries(
tiledTeam["wormgine.loadout"] ?? {},
)) {
Expand Down Expand Up @@ -156,7 +156,7 @@ export class ScenarioBuilder {
private readonly tileset: TiledTileset;
private readonly objectState: BaseRecordedState[];
private readonly providedGameRules: GameRules;
private readonly providedTeams: Team[];
private readonly providedTeams: TeamDefinition[];

get hasWormSpawns() {
return this.objectLayer.objects.some(
Expand Down Expand Up @@ -244,7 +244,7 @@ export class ScenarioBuilder {
return this;
}

addSpawns(teams: Team[]): ScenarioBuilder {
addSpawns(teams: TeamDefinition[]): ScenarioBuilder {
if (!this.bitmap) {
throw Error("Bitmap hasn't been loaded");
}
Expand Down
4 changes: 2 additions & 2 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export enum LogLevels {
}

export default class Logger {
public static LogLevel = LogLevels.Verbose;
constructor(private readonly moduleName: string) {}
public static LogLevel = LogLevels.Error;
constructor(private readonly moduleName: string) { }

public verbose(...info: unknown[]) {
if (Logger.LogLevel > LogLevels.Verbose) {
Expand Down
Loading

0 comments on commit cf46408

Please sign in to comment.