Skip to content

Commit

Permalink
Add tests and doc blocks for conditions/game.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloreck committed Mar 26, 2024
1 parent a792450 commit 689fce0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 16 deletions.
84 changes: 77 additions & 7 deletions src/engine/scripts/declarations/conditions/game.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { beforeAll, describe, it } from "@jest/globals";
import { beforeAll, beforeEach, describe, expect, it, jest } from "@jest/globals";
import { game } from "xray16";

import { checkXrCondition } from "@/fixtures/engine";
import { IBaseSchemeState, IRegistryObjectState, registerObject, setPortableStoreValue } from "@/engine/core/database";
import { isBlackScreen } from "@/engine/core/utils/game";
import { ACTOR_ID } from "@/engine/lib/constants/ids";
import { EScheme, GameObject } from "@/engine/lib/types";
import {
callXrCondition,
checkXrCondition,
mockRegisteredActor,
mockSchemeState,
resetRegistry,
} from "@/fixtures/engine";
import { replaceFunctionMock } from "@/fixtures/jest";
import { MockGameObject } from "@/fixtures/xray";

jest.mock("@/engine/core/utils/game");

describe("game conditions declaration", () => {
beforeAll(() => {
Expand All @@ -21,13 +36,68 @@ describe("game conditions implementation", () => {
require("@/engine/scripts/declarations/conditions/game");
});

it.todo("signal should check if signal is active");
beforeEach(() => resetRegistry());

it("signal should check if signal is active", () => {
const object: GameObject = MockGameObject.mock();
const state: IRegistryObjectState = registerObject(object);
const schemeState: IBaseSchemeState = mockSchemeState(EScheme.WOUNDED, { signals: new LuaTable() });

state.activeScheme = EScheme.WOUNDED;
state[EScheme.WOUNDED] = schemeState;

it.todo("counter_greater should check counter value");
expect(callXrCondition("signal", MockGameObject.mockActor(), object, "some_signal")).toBe(false);

it.todo("counter_equal should check counter value");
schemeState.signals?.set("some_signal", true);
expect(callXrCondition("signal", MockGameObject.mockActor(), object, "some_signal")).toBe(true);

schemeState.signals?.set("some_signal", false);
expect(callXrCondition("signal", MockGameObject.mockActor(), object, "some_signal")).toBe(false);

schemeState.signals?.delete("some_signal");
expect(callXrCondition("signal", MockGameObject.mockActor(), object, "some_signal")).toBe(false);
});

it("counter_greater should check counter value", () => {
const { actorGameObject } = mockRegisteredActor();

setPortableStoreValue(ACTOR_ID, "test_greater", 10);

expect(callXrCondition("counter_greater", actorGameObject, MockGameObject.mock(), "test_greater", 9)).toBe(true);
expect(callXrCondition("counter_greater", actorGameObject, MockGameObject.mock(), "test_greater", 10)).toBe(false);
expect(callXrCondition("counter_greater", actorGameObject, MockGameObject.mock(), "test_greater", 11)).toBe(false);

expect(callXrCondition("counter_greater", actorGameObject, MockGameObject.mock(), "test_unknown", 11)).toBe(false);
});

it.todo("has_active_tutorial should check if any tutorial is active");
it("counter_equal should check counter value", () => {
const { actorGameObject } = mockRegisteredActor();

it.todo("black_screen should check if black screen is active");
setPortableStoreValue(ACTOR_ID, "test_one", 1);
setPortableStoreValue(ACTOR_ID, "test_two", 2);

expect(callXrCondition("counter_equal", actorGameObject, MockGameObject.mock(), "test_one", 1)).toBe(true);
expect(callXrCondition("counter_equal", actorGameObject, MockGameObject.mock(), "test_one", 2)).toBe(false);

expect(callXrCondition("counter_equal", actorGameObject, MockGameObject.mock(), "test_two", 1)).toBe(false);
expect(callXrCondition("counter_equal", actorGameObject, MockGameObject.mock(), "test_two", 2)).toBe(true);

expect(callXrCondition("counter_equal", actorGameObject, MockGameObject.mock(), "test_three", 10)).toBe(false);
});

it("has_active_tutorial should check if any tutorial is active", () => {
jest.spyOn(game, "has_active_tutorial").mockImplementationOnce(() => false);
expect(callXrCondition("has_active_tutorial", MockGameObject.mock(), MockGameObject.mock())).toBe(false);

jest.spyOn(game, "has_active_tutorial").mockImplementationOnce(() => true);
expect(callXrCondition("has_active_tutorial", MockGameObject.mock(), MockGameObject.mock())).toBe(true);
});

it("black_screen should check if black screen is active", () => {
replaceFunctionMock(isBlackScreen, () => false);
expect(callXrCondition("black_screen", MockGameObject.mock(), MockGameObject.mock())).toBe(false);

replaceFunctionMock(isBlackScreen, () => true);
expect(callXrCondition("black_screen", MockGameObject.mock(), MockGameObject.mock())).toBe(true);
});
});
26 changes: 17 additions & 9 deletions src/engine/scripts/declarations/conditions/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,40 @@ extern("xr_conditions.signal", (_: GameObject, object: GameObject, [name]: [TNam
});

/**
* Check if stored counter value is greater then.
* Checks data from object pstore.
* Check if stored counter value is greater than provided.
* Checks data from object portable store.
*
* Where:
* - key - portable store key value
* - count - number value to check against
*/
extern(
"xr_conditions.counter_greater",
(_: GameObject, __: GameObject, [name, count]: [Optional<TName>, Optional<TCount>]): boolean => {
if (!name || !count) {
(_: GameObject, __: GameObject, [key, count]: [Optional<TName>, Optional<TCount>]): boolean => {
if (!key || !count) {
abort("Invalid parameters supplied for condition 'counter_greater'.");
}

return getPortableStoreValue(ACTOR_ID, name, 0) > count;
return getPortableStoreValue(ACTOR_ID, key, 0) > count;
}
);

/**
* Check if stored counter value is equal.
* Checks data from object pstore.
* Checks data from object portable store.
*
* Where:
* - key - portable store key value
* - count - number value to check against
*/
extern(
"xr_conditions.counter_equal",
(_: GameObject, __: GameObject, [name, count]: [Optional<TName>, Optional<TCount>]): boolean => {
if (!name || !count) {
(_: GameObject, __: GameObject, [key, count]: [Optional<TName>, Optional<TCount>]): boolean => {
if (!key || !count) {
abort("Invalid parameters supplied for condition 'counter_equal'.");
}

return getPortableStoreValue(ACTOR_ID, name, 0) === count;
return getPortableStoreValue(ACTOR_ID, key, 0) === count;
}
);

Expand Down
1 change: 1 addition & 0 deletions src/fixtures/xray/mocks/interface/gameInterface.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export const mockGameInterface = {
get_game_time: jest.fn(() => MockCTime.now()),
start_tutorial: jest.fn(() => {}),
stop_tutorial: jest.fn(() => {}),
has_active_tutorial: jest.fn(() => false),
translate_string: jest.fn((key: string) => "translated_" + key),
};

0 comments on commit 689fce0

Please sign in to comment.