Skip to content

Commit

Permalink
Mocks updated. Reuse squad mock util. Add tests for squad relations c…
Browse files Browse the repository at this point in the history
…hecks.
  • Loading branch information
Neloreck committed Jun 22, 2023
1 parent e8ae576 commit bebd6ee
Show file tree
Hide file tree
Showing 36 changed files with 346 additions and 214 deletions.
6 changes: 3 additions & 3 deletions cli/test/jest_global.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mockEngineGlobals } from "@/fixtures/engine";
import { mockLuaLib } from "@/fixtures/lua";
import { mockXRay16 } from "@/fixtures/xray";
import { mockEngineGlobals } from "@/fixtures/engine/mockEngineGlobals";
import { mockLuaLib } from "@/fixtures/lua/mockLuaLib";
import { mockXRay16 } from "@/fixtures/xray/mockXRay16";

mockEngineGlobals();
mockLuaLib();
Expand Down
4 changes: 2 additions & 2 deletions src/engine/core/objects/binders/creature/StalkerBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import {
getObjectSquad,
updateObjectInvulnerability,
} from "@/engine/core/utils/object/object_general";
import { ERelation, setClientObjectsRelation, setObjectSympathy } from "@/engine/core/utils/relation";
import { ERelation, setClientObjectRelation, setObjectSympathy } from "@/engine/core/utils/relation";
import { emitSchemeEvent, trySwitchToAnotherSection } from "@/engine/core/utils/scheme";
import { createEmptyVector } from "@/engine/core/utils/vector";
import { communities, TCommunity } from "@/engine/lib/constants/communities";
Expand Down Expand Up @@ -161,7 +161,7 @@ export class StalkerBinder extends object_binder {
const relation: Optional<ERelation> = registry.goodwill.relations.get(objectId);

if (relation !== null && actor) {
setClientObjectsRelation(this.object, actor, relation);
setClientObjectRelation(this.object, actor, relation);
}

const sympathy: Optional<TCount> = registry.goodwill.sympathy.get(objectId);
Expand Down
12 changes: 6 additions & 6 deletions src/engine/core/objects/server/squad/Squad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ import {
areCommunitiesEnemies,
ERelation,
getSquadMembersRelationToActor,
getSquadRelationToActor,
setClientObjectsRelation,
getSquadMembersRelationToActorSafe,
setClientObjectRelation,
setObjectSympathy,
setServerObjectsRelation,
setServerObjectRelation,
} from "@/engine/core/utils/relation";
import { isEmpty } from "@/engine/core/utils/table";
import { gameConfig } from "@/engine/lib/configs/GameConfig";
Expand Down Expand Up @@ -844,9 +844,9 @@ export class Squad extends cse_alife_online_offline_group implements ISimulation
const object: Optional<ClientObject> = registry.objects.get(squadMember.id)?.object;

if (object !== null) {
setClientObjectsRelation(object, registry.actor, relation);
setClientObjectRelation(object, registry.actor, relation);
} else {
setServerObjectsRelation(simulator.object(squadMember.id), simulator.actor(), relation);
setServerObjectRelation(simulator.object(squadMember.id), simulator.actor(), relation);
}
}
}
Expand Down Expand Up @@ -964,7 +964,7 @@ export class Squad extends cse_alife_online_offline_group implements ISimulation
if (isSquadMonsterCommunity(this.faction)) {
spot = mapMarks.alife_presentation_squad_monster_debug;
} else {
const relation: ERelation = getSquadRelationToActor(this);
const relation: ERelation = getSquadMembersRelationToActorSafe(this);

switch (relation) {
case ERelation.FRIEND:
Expand Down
2 changes: 1 addition & 1 deletion src/engine/core/schemes/sr_teleport/SchemeTeleport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ISchemeTeleportState } from "@/engine/core/schemes/sr_teleport/ISchemeT
import { SchemeTeleport } from "@/engine/core/schemes/sr_teleport/SchemeTeleport";
import { loadSchemeImplementation } from "@/engine/core/utils/scheme/setup";
import { ClientObject, EScheme, ESchemeType, IniFile } from "@/engine/lib/types";
import { luaTableToArray } from "@/fixtures/lua/mocks/utils";
import { luaTableToArray } from "@/fixtures/lua/mocks/lua_utils";
import { mockClientGameObject, mockIniFile } from "@/fixtures/xray";

describe("SchemeTeleport functionality", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/core/schemes/sr_timer/SchemeTimer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ETimerType, ISchemeTimerState } from "@/engine/core/schemes/sr_timer/IS
import { SchemeTimer } from "@/engine/core/schemes/sr_timer/SchemeTimer";
import { loadSchemeImplementation } from "@/engine/core/utils/scheme/setup";
import { ClientObject, EScheme, ESchemeType, IniFile } from "@/engine/lib/types";
import { luaTableToObject } from "@/fixtures/lua/mocks/utils";
import { luaTableToObject } from "@/fixtures/lua/mocks/lua_utils";
import { mockClientGameObject, mockIniFile } from "@/fixtures/xray";

describe("SchemeTimer functionality", () => {
Expand Down
8 changes: 4 additions & 4 deletions src/engine/core/ui/debug/sections/DebugObjectSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { getObjectSquad } from "@/engine/core/utils/object/object_general";
import {
ERelation,
getObjectsRelationSafe,
getSquadRelationToActor,
setClientObjectsRelation,
getSquadMembersRelationToActorSafe,
setClientObjectRelation,
} from "@/engine/core/utils/relation";
import { resolveXmlFile } from "@/engine/core/utils/ui";
import { NIL } from "@/engine/lib/constants/words";
Expand Down Expand Up @@ -119,7 +119,7 @@ export class DebugObjectSection extends AbstractDebugSection {
"object relation: " + getObjectsRelationSafe(targetStalker, registry.actor)
);
this.uiTargetStalkerSquadRelationLabel.SetText(
"squad relation: " + (squad ? getSquadRelationToActor(squad) : NIL)
"squad relation: " + (squad ? getSquadMembersRelationToActorSafe(squad) : NIL)
);
} else {
this.uiNearestStalkerLabel.SetText("Nearest: " + NIL);
Expand Down Expand Up @@ -210,7 +210,7 @@ export class DebugObjectSection extends AbstractDebugSection {

if (targetObject) {
logger.info("Set actor relation for:", targetObject.name(), relation);
setClientObjectsRelation(targetObject, registry.actor, relation);
setClientObjectRelation(targetObject, registry.actor, relation);
this.initializeState();
} else {
logger.info("No object found for relation change");
Expand Down
2 changes: 1 addition & 1 deletion src/engine/core/utils/ini/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, it } from "@jest/globals";
import { IBaseSchemeLogic } from "@/engine/core/schemes";
import { addConditionToList } from "@/engine/core/utils/ini/config";
import { LuaArray, TIndex } from "@/engine/lib/types";
import { luaTableToObject } from "@/fixtures/lua/mocks/utils";
import { luaTableToObject } from "@/fixtures/lua/mocks/lua_utils";

describe("'config' utils for ini file", () => {
it("'addCondition' util should fill table and return new index", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/core/utils/ini/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { IConfigCondition } from "@/engine/core/utils/ini/types";
import { NIL } from "@/engine/lib/constants/words";
import { Flags32, IniFile, LuaArray } from "@/engine/lib/types";
import { luaTableToArray, luaTableToObject } from "@/fixtures/lua/mocks/utils";
import { luaTableToArray, luaTableToObject } from "@/fixtures/lua/mocks/lua_utils";
import { mockIniFile } from "@/fixtures/xray";
import { MockFlags32 } from "@/fixtures/xray/mocks/objects/Flags32.mock";

Expand Down
2 changes: 1 addition & 1 deletion src/engine/core/utils/ini/read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
readIniTwoNumbers,
} from "@/engine/core/utils/ini/read";
import { IniFile, Optional } from "@/engine/lib/types";
import { luaTableToObject } from "@/fixtures/lua/mocks/utils";
import { luaTableToObject } from "@/fixtures/lua/mocks/lua_utils";
import { mockIniFile } from "@/fixtures/xray/mocks/ini";

describe("'read' utils for ini file", () => {
Expand Down
102 changes: 17 additions & 85 deletions src/engine/core/utils/relation/check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ import { getSquadCommunityRelationToActor } from "@/engine/core/utils/relation/g
import { ERelation } from "@/engine/core/utils/relation/types";
import { communities } from "@/engine/lib/constants/communities";
import { ACTOR_ID } from "@/engine/lib/constants/ids";
import { ServerGroupObject, ServerHumanObject } from "@/engine/lib/types";
import {
MockAlifeOnlineOfflineGroup,
MockAlifeSimulator,
mockServerAlifeCreatureActor,
mockServerAlifeHumanStalker,
mockServerAlifeOnlineOfflineGroup,
} from "@/fixtures/xray";
import { mockCharactersGoodwill } from "@/fixtures/xray/mocks/relations";
import { ServerGroupObject } from "@/engine/lib/types";
import { mockRelationsSquads } from "@/fixtures/engine";
import { MockAlifeSimulator, mockServerAlifeCreatureActor, mockServerAlifeOnlineOfflineGroup } from "@/fixtures/xray";

describe("'relation/check' utils", () => {
beforeEach(() => {
Expand Down Expand Up @@ -98,84 +92,22 @@ describe("'relation/check' utils", () => {
});

it("'isAnySquadMemberEnemyToActor' should correctly check relation of squad members", () => {
const emptyGroup: MockAlifeOnlineOfflineGroup = new MockAlifeOnlineOfflineGroup("test");
const friendlyGroup: MockAlifeOnlineOfflineGroup = new MockAlifeOnlineOfflineGroup("test");
const neutralGroup: MockAlifeOnlineOfflineGroup = new MockAlifeOnlineOfflineGroup("test");
const mixedGroup: MockAlifeOnlineOfflineGroup = new MockAlifeOnlineOfflineGroup("test");
const enemyGroup: MockAlifeOnlineOfflineGroup = new MockAlifeOnlineOfflineGroup("test");

const friend: ServerHumanObject = mockServerAlifeHumanStalker();
const enemy: ServerHumanObject = mockServerAlifeHumanStalker();
const neutral: ServerHumanObject = mockServerAlifeHumanStalker();
const almostEnemy: ServerHumanObject = mockServerAlifeHumanStalker();
const almostFriend: ServerHumanObject = mockServerAlifeHumanStalker();

mockCharactersGoodwill(friend.id, ACTOR_ID, 1000);
mockCharactersGoodwill(enemy.id, ACTOR_ID, -1000);
mockCharactersGoodwill(neutral.id, ACTOR_ID, 0);
mockCharactersGoodwill(almostEnemy.id, ACTOR_ID, -999);
mockCharactersGoodwill(almostFriend.id, ACTOR_ID, 999);

friendlyGroup.addSquadMember(friend);
friendlyGroup.addSquadMember(friend);
friendlyGroup.addSquadMember(friend);

neutralGroup.addSquadMember(neutral);
neutralGroup.addSquadMember(almostEnemy);
neutralGroup.addSquadMember(almostFriend);

enemyGroup.addSquadMember(enemy);
enemyGroup.addSquadMember(enemy);

mixedGroup.addSquadMember(friend);
mixedGroup.addSquadMember(neutral);
mixedGroup.addSquadMember(enemy);

expect(isAnySquadMemberEnemyToActor(emptyGroup.asSquad())).toBeFalsy();
expect(isAnySquadMemberEnemyToActor(neutralGroup.asSquad())).toBeFalsy();
expect(isAnySquadMemberEnemyToActor(friendlyGroup.asSquad())).toBeFalsy();
expect(isAnySquadMemberEnemyToActor(enemyGroup.asSquad())).toBeTruthy();
expect(isAnySquadMemberEnemyToActor(mixedGroup.asSquad())).toBeTruthy();
const { emptyMonolithSquad, neutralSquad, enemySquad, friendlySquad, mixedSquad } = mockRelationsSquads();

expect(isAnySquadMemberEnemyToActor(emptyMonolithSquad)).toBeFalsy();
expect(isAnySquadMemberEnemyToActor(neutralSquad)).toBeFalsy();
expect(isAnySquadMemberEnemyToActor(friendlySquad)).toBeFalsy();
expect(isAnySquadMemberEnemyToActor(enemySquad)).toBeTruthy();
expect(isAnySquadMemberEnemyToActor(mixedSquad)).toBeTruthy();
});

it("'isAnySquadMemberFriendToActor' should correctly check relation of squad members", () => {
const emptyGroup: MockAlifeOnlineOfflineGroup = new MockAlifeOnlineOfflineGroup("test");
const friendlyGroup: MockAlifeOnlineOfflineGroup = new MockAlifeOnlineOfflineGroup("test");
const neutralGroup: MockAlifeOnlineOfflineGroup = new MockAlifeOnlineOfflineGroup("test");
const mixedGroup: MockAlifeOnlineOfflineGroup = new MockAlifeOnlineOfflineGroup("test");
const enemyGroup: MockAlifeOnlineOfflineGroup = new MockAlifeOnlineOfflineGroup("test");

const friend: ServerHumanObject = mockServerAlifeHumanStalker();
const enemy: ServerHumanObject = mockServerAlifeHumanStalker();
const neutral: ServerHumanObject = mockServerAlifeHumanStalker();
const almostEnemy: ServerHumanObject = mockServerAlifeHumanStalker();
const almostFriend: ServerHumanObject = mockServerAlifeHumanStalker();

mockCharactersGoodwill(friend.id, ACTOR_ID, 1000);
mockCharactersGoodwill(enemy.id, ACTOR_ID, -1000);
mockCharactersGoodwill(neutral.id, ACTOR_ID, 0);
mockCharactersGoodwill(almostEnemy.id, ACTOR_ID, -999);
mockCharactersGoodwill(almostFriend.id, ACTOR_ID, 999);

friendlyGroup.addSquadMember(friend);
friendlyGroup.addSquadMember(friend);
friendlyGroup.addSquadMember(friend);

neutralGroup.addSquadMember(neutral);
neutralGroup.addSquadMember(almostEnemy);
neutralGroup.addSquadMember(almostFriend);

enemyGroup.addSquadMember(enemy);
enemyGroup.addSquadMember(enemy);

mixedGroup.addSquadMember(friend);
mixedGroup.addSquadMember(neutral);
mixedGroup.addSquadMember(enemy);

expect(isAnySquadMemberFriendToActor(emptyGroup.asSquad())).toBeFalsy();
expect(isAnySquadMemberFriendToActor(neutralGroup.asSquad())).toBeFalsy();
expect(isAnySquadMemberFriendToActor(enemyGroup.asSquad())).toBeFalsy();
expect(isAnySquadMemberFriendToActor(friendlyGroup.asSquad())).toBeTruthy();
expect(isAnySquadMemberFriendToActor(mixedGroup.asSquad())).toBeTruthy();
const { emptyMonolithSquad, neutralSquad, enemySquad, friendlySquad, mixedSquad } = mockRelationsSquads();

expect(isAnySquadMemberFriendToActor(emptyMonolithSquad)).toBeFalsy();
expect(isAnySquadMemberFriendToActor(neutralSquad)).toBeFalsy();
expect(isAnySquadMemberFriendToActor(enemySquad)).toBeFalsy();
expect(isAnySquadMemberFriendToActor(friendlySquad)).toBeTruthy();
expect(isAnySquadMemberFriendToActor(mixedSquad)).toBeTruthy();
});
});
70 changes: 62 additions & 8 deletions src/engine/core/utils/relation/get.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";

import { registerStoryLink, registry } from "@/engine/core/database";
import { registerActor, registerStoryLink, registry } from "@/engine/core/database";
import { Squad } from "@/engine/core/objects";
import { getSquadCommunityRelationToActor } from "@/engine/core/utils/relation/get";
import {
getObjectsRelationSafe,
getSquadCommunityRelationToActor,
getSquadMembersRelationToActor,
getSquadMembersRelationToActorSafe,
} from "@/engine/core/utils/relation/get";
import { ERelation } from "@/engine/core/utils/relation/types";
import { communities } from "@/engine/lib/constants/communities";
import { ACTOR_ID } from "@/engine/lib/constants/ids";
import { ServerGroupObject } from "@/engine/lib/types";
import { MockAlifeSimulator, mockServerAlifeCreatureActor, mockServerAlifeOnlineOfflineGroup } from "@/fixtures/xray";
import { ClientObject, ServerGroupObject } from "@/engine/lib/types";
import { mockRelationsSquads } from "@/fixtures/engine";
import {
mockActorClientGameObject,
MockAlifeSimulator,
mockClientGameObject,
mockServerAlifeCreatureActor,
mockServerAlifeOnlineOfflineGroup,
} from "@/fixtures/xray";

describe("'relation/get' utils", () => {
beforeEach(() => {
Expand All @@ -18,15 +30,57 @@ describe("'relation/get' utils", () => {
});

it("'getObjectsRelationSafe' should correctly check relation", () => {
// todo;
expect(getObjectsRelationSafe(mockClientGameObject(), null)).toBeNull();
expect(getObjectsRelationSafe(null, mockClientGameObject())).toBeNull();
expect(getObjectsRelationSafe(null, null)).toBeNull();

const first: ClientObject = mockClientGameObject({ relation: () => 0 });
const second: ClientObject = mockClientGameObject({ relation: () => 1 });
const third: ClientObject = mockClientGameObject({ relation: () => 2 });
const fourth: ClientObject = mockClientGameObject({ relation: () => 3 });

expect(getObjectsRelationSafe(first, second)).toBe(0);
expect(getObjectsRelationSafe(second, first)).toBe(1);
expect(getObjectsRelationSafe(third, second)).toBe(2);
expect(getObjectsRelationSafe(fourth, second)).toBe(3);
});

it("'getSquadRelationToActor' should correctly check relation", () => {
// todo;
it("'getSquadMembersRelationToActorSafe' should correctly check relation", () => {
const { emptyMonolithSquad, emptyArmySquad, neutralSquad, friendlySquad, mixedSquad, enemySquad } =
mockRelationsSquads();

// No actor.
expect(registry.actor).toBeNull();
expect(getSquadMembersRelationToActorSafe(emptyMonolithSquad)).toBe(ERelation.NEUTRAL);
expect(getSquadMembersRelationToActorSafe(emptyArmySquad)).toBe(ERelation.NEUTRAL);

registerActor(mockActorClientGameObject());

expect(getSquadMembersRelationToActorSafe(emptyMonolithSquad)).toBe(ERelation.ENEMY);
expect(getSquadMembersRelationToActorSafe(emptyArmySquad)).toBe(ERelation.FRIEND);
expect(getSquadMembersRelationToActorSafe(friendlySquad)).toBe(ERelation.FRIEND);
expect(getSquadMembersRelationToActorSafe(mixedSquad)).toBe(ERelation.NEUTRAL);
expect(getSquadMembersRelationToActorSafe(neutralSquad)).toBe(ERelation.NEUTRAL);
expect(getSquadMembersRelationToActorSafe(enemySquad)).toBe(ERelation.ENEMY);
});

it("'getSquadMembersRelationToActor' should correctly check relation", () => {
// todo;
const { emptyMonolithSquad, emptyArmySquad, neutralSquad, friendlySquad, mixedSquad, enemySquad } =
mockRelationsSquads();

// No actor.
expect(registry.actor).toBeNull();
expect(getSquadMembersRelationToActor(emptyMonolithSquad)).toBe(ERelation.NEUTRAL);
expect(getSquadMembersRelationToActor(emptyArmySquad)).toBe(ERelation.NEUTRAL);

registerActor(mockActorClientGameObject());

expect(getSquadMembersRelationToActor(emptyMonolithSquad)).toBeNull();
expect(getSquadMembersRelationToActor(emptyArmySquad)).toBeNull();
expect(getSquadMembersRelationToActor(friendlySquad)).toBe(ERelation.FRIEND);
expect(getSquadMembersRelationToActor(mixedSquad)).toBe(ERelation.NEUTRAL);
expect(getSquadMembersRelationToActor(neutralSquad)).toBe(ERelation.NEUTRAL);
expect(getSquadMembersRelationToActor(enemySquad)).toBe(ERelation.ENEMY);
});

it("'getNumberRelationBetweenCommunities' should correctly check relation", () => {
Expand Down
12 changes: 11 additions & 1 deletion src/engine/core/utils/relation/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ export function getObjectsRelationSafe(
/**
* Returns relation to actor based on average of squad members.
* If no squad members present, check relation of faction.
*
* Offline squad may be empty.
*
* @param squad - target squad to check
* @returns relation of squad members to actor or squad community relation is squad is empty
*/
export function getSquadRelationToActor(squad: Squad): ERelation {
export function getSquadMembersRelationToActorSafe(squad: Squad): ERelation {
const actor: Optional<ClientObject> = registry.actor;

// Actor may be registered after other alife objects.
Expand Down Expand Up @@ -59,6 +64,11 @@ export function getSquadRelationToActor(squad: Squad): ERelation {
}

/**
* Returns relation to actor based on average of squad members.
* If no squad members present, return `null`.
*
* Offline squad may be empty.
*
* @returns average relation of squad members to actor, `null` if squad is empty
*/
export function getSquadMembersRelationToActor(squad: Squad): Optional<ERelation> {
Expand Down
4 changes: 2 additions & 2 deletions src/engine/core/utils/relation/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const logger: LuaLogger = new LuaLogger($filename);
/**
* todo;
*/
export function setClientObjectsRelation(
export function setClientObjectRelation(
from: Optional<ClientObject>,
to: Optional<ClientObject>,
newRelation: ERelation
Expand All @@ -43,7 +43,7 @@ export function setClientObjectsRelation(
* todo;
* todo: Unify?
*/
export function setServerObjectsRelation(
export function setServerObjectRelation(
from: Optional<ServerCreatureObject>,
to: Optional<ServerCreatureObject>,
nextRelation: ERelation
Expand Down
Loading

0 comments on commit bebd6ee

Please sign in to comment.