Skip to content

Commit

Permalink
Added tests for getSquadRelationToActorById.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloreck committed Jun 22, 2023
1 parent d60ec7d commit 9a096ca
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
35 changes: 32 additions & 3 deletions src/engine/core/utils/relation/get.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { beforeEach, describe, expect, it } from "@jest/globals";

import { registerActor, registerStoryLink, registry } from "@/engine/core/database";
import { Squad } from "@/engine/core/objects";
import {
getNumberRelationBetweenCommunities,
getObjectsRelationSafe,
getSquadCommunityRelationToActor,
getSquadMembersRelationToActor,
getSquadMembersRelationToActorSafe,
getSquadRelationToActorById,
} from "@/engine/core/utils/relation/get";
import { ERelation } from "@/engine/core/utils/relation/types";
import { communities } from "@/engine/lib/constants/communities";
Expand Down Expand Up @@ -84,7 +86,17 @@ describe("'relation/get' utils", () => {
});

it("'getNumberRelationBetweenCommunities' should correctly check relation", () => {
// todo;
expect(getNumberRelationBetweenCommunities(null, null)).toBeNull();
expect(getNumberRelationBetweenCommunities(null, communities.stalker)).toBeNull();
expect(getNumberRelationBetweenCommunities(communities.stalker, null)).toBeNull();
expect(getNumberRelationBetweenCommunities(communities.none, communities.stalker)).toBeNull();
expect(getNumberRelationBetweenCommunities(communities.stalker, communities.none)).toBeNull();

expect(getNumberRelationBetweenCommunities(communities.actor, communities.actor)).toBe(1000);
expect(getNumberRelationBetweenCommunities(communities.actor, communities.stalker)).toBe(250);
expect(getNumberRelationBetweenCommunities(communities.stalker, communities.actor)).toBe(200);
expect(getNumberRelationBetweenCommunities(communities.monolith, communities.monster)).toBe(-1000);
expect(getNumberRelationBetweenCommunities(communities.monster, communities.bandit)).toBe(-5000);
});

it("'getSquadCommunityRelationToActor' should correctly get realation of squad community", () => {
Expand Down Expand Up @@ -121,6 +133,23 @@ describe("'relation/get' utils", () => {
});

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

mockServerAlifeCreatureActor();

expect(() => getSquadRelationToActorById(10002000)).toThrow("Squad with id '10002000' is not found.");
expect(getSquadRelationToActorById(emptyMonolithSquad.id)).toBe(ERelation.ENEMY);
expect(getSquadRelationToActorById(emptyArmySquad.id)).toBe(ERelation.FRIEND);
expect(getSquadRelationToActorById(friendlySquad.id)).toBe(ERelation.FRIEND);
expect(getSquadRelationToActorById(neutralSquad.id)).toBe(ERelation.NEUTRAL);
expect(getSquadRelationToActorById(enemySquad.id)).toBe(ERelation.ENEMY);

mixedSquad.relationship = ERelation.FRIEND;
expect(getSquadRelationToActorById(mixedSquad.id)).toBe(ERelation.FRIEND);
mixedSquad.relationship = ERelation.NEUTRAL;
expect(getSquadRelationToActorById(mixedSquad.id)).toBe(ERelation.NEUTRAL);
mixedSquad.relationship = ERelation.ENEMY;
expect(getSquadRelationToActorById(mixedSquad.id)).toBe(ERelation.ENEMY);
});
});
39 changes: 21 additions & 18 deletions src/engine/core/utils/relation/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,22 @@ export function getSquadMembersRelationToActor(squad: Squad): Optional<ERelation
}

/**
* todo;
* Get relation value between communities.
* Handle possible exceptional cases.
*
* @param from - community from
* @param to - community to
* @returns goodwill value from community to another one
*/
export function getNumberRelationBetweenCommunities(
from: Optional<TCommunity>,
to: Optional<TCommunity>
): Optional<TCount> {
if (from !== null && to !== null && from !== communities.none && to !== communities.none) {
return relation_registry.community_relation(from, to);
} else {
if (!from || !to || from === communities.none || to === communities.none) {
return null;
}

return relation_registry.community_relation(from, to);
}

/**
Expand Down Expand Up @@ -153,21 +158,19 @@ export function getSquadCommunityRelationToActor(squadStoryId: TStringId): ERela
export function getSquadRelationToActorById(squadId: TNumberId): ERelation {
const squad: Optional<Squad> = alife().object<Squad>(squadId);

assert(squad, "No such squad %s in board.", squadId);

if (squad.relationship === null) {
let goodwill: ERelation = ERelation.NEUTRAL;
assert(squad, "Squad with id '%s' is not found.", squadId);

if (relation_registry.community_relation(squad.getCommunity(), alife().actor().community()) >= EGoodwill.FRIENDS) {
goodwill = ERelation.FRIEND;
} else if (
relation_registry.community_relation(squad.getCommunity(), alife().actor().community()) <= EGoodwill.ENEMIES
) {
goodwill = ERelation.ENEMY;
}

return goodwill;
} else {
if (squad.relationship) {
return squad.relationship;
} else {
const goodwill: TCount = relation_registry.community_relation(squad.getCommunity(), alife().actor().community());

if (goodwill >= EGoodwill.FRIENDS) {
return ERelation.FRIEND;
} else if (goodwill <= EGoodwill.ENEMIES) {
return ERelation.ENEMY;
} else {
return ERelation.NEUTRAL;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { jest } from "@jest/globals";

import { communities, TCommunity } from "@/engine/lib/constants/communities";
import { ACTOR_ID } from "@/engine/lib/constants/ids";
import { ServerActorObject, TNumberId } from "@/engine/lib/types";
import {
Expand All @@ -11,6 +14,10 @@ import {
export class MockAlifeCreatureActor extends MockAlifeDynamicObjectVisual {
public override id: TNumberId = 0;

public community(): TCommunity {
return communities.actor;
}

public asMock(): ServerActorObject {
return this as unknown as ServerActorObject;
}
Expand All @@ -23,5 +30,9 @@ export function mockServerAlifeCreatureActor({
id = ACTOR_ID,
...base
}: Partial<ServerActorObject> = {}): ServerActorObject {
return mockServerAlifeDynamicObjectVisual({ id, ...base }) as ServerActorObject;
return mockServerAlifeDynamicObjectVisual({
id,
community: jest.fn(() => communities.actor),
...base,
}) as ServerActorObject;
}
4 changes: 2 additions & 2 deletions src/fixtures/xray/mocks/relations/communityRelations.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { TCount, TName, TNumberId } from "@/engine/lib/types";
export const communityGoodwill: Record<TName, Record<TName, TCount>> = {
[communities.actor]: {
[communities.actor]: 1000,
[communities.stalker]: 0,
[communities.stalker]: 250,
[communities.bandit]: 0,
[communities.army]: 1000,
[communities.monolith]: -1000,
[communities.monster]: -1000,
},
[communities.stalker]: {
[communities.stalker]: 1000,
[communities.actor]: 500,
[communities.actor]: 200,
[communities.bandit]: -1000,
[communities.army]: 0,
[communities.monolith]: -1000,
Expand Down

0 comments on commit 9a096ca

Please sign in to comment.