Skip to content

Commit

Permalink
Store entire objective in sessions instead of only the sm definition
Browse files Browse the repository at this point in the history
Now we can read the session's objectives during scoring instead of
reading from the contract file (which misses gamechanger objectives)
  • Loading branch information
grappigegovert committed Sep 2, 2024
1 parent 5e7ebd3 commit 26d3ebd
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion components/contracts/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const SESSION_SET_PROPS: (keyof ContractSession)[] = [
const SESSION_MAP_PROPS: (keyof ContractSession)[] = [
"objectiveStates",
"objectiveContexts",
"objectiveDefinitions",
"objectives",
]

/**
Expand Down
14 changes: 9 additions & 5 deletions components/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function registerObjectiveListener(
let context = objective.Definition.Context || {}
let state = "Start"

session.objectiveDefinitions.set(objective.Id, objective.Definition)
session.objectives.set(objective.Id, objective)

const immediate = handleEvent(
// @ts-expect-error Type issue, needs to be corrected in sm-p.
Expand Down Expand Up @@ -359,7 +359,7 @@ export function newSession(
difficulty,
objectiveContexts: new Map(),
objectiveStates: new Map(),
objectiveDefinitions: new Map(),
objectives: new Map(),
ghost: {
deaths: 0,
unnoticedKills: 0,
Expand Down Expand Up @@ -691,10 +691,14 @@ function saveEvents(
session,
)

for (const objectiveId of session.objectiveStates.keys()) {
for (const [objectiveId, objective] of session.objectives) {
try {
const objectiveDefinition =
session.objectiveDefinitions.get(objectiveId)
const objectiveDefinition = objective.Definition

if (!objectiveDefinition) {
continue
}

const objectiveState = session.objectiveStates.get(objectiveId)
const objectiveContext =
session.objectiveContexts.get(objectiveId)
Expand Down
8 changes: 5 additions & 3 deletions components/scoreHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
difficultyToString,
EVERGREEN_LEVEL_INFO,
evergreenLevelForXp,
isTrueForEveryElement,
handleAxiosError,
isObjectiveActive,
levelForXp,
Expand All @@ -31,7 +32,7 @@ import {
sniperLevelForXp,
xpRequiredForLevel,
} from "./utils"
import { contractSessions, enqueueEvent, getCurrentState } from "./eventHandler"
import { contractSessions, enqueueEvent } from "./eventHandler"
import { getConfig } from "./configSwizzleManager"
import { controller } from "./controller"
import type {
Expand Down Expand Up @@ -151,7 +152,8 @@ export function calculateScore(
gameVersion === "h1" ||
contractData.Metadata.Id ===
"2d1bada4-aa46-4954-8cf5-684989f1668a" ||
contractData.Data.Objectives?.every(
isTrueForEveryElement(
contractSession.objectives.values(),
(obj: MissionManifestObjective) =>
obj.ExcludeFromScoring ||
contractSession.completedObjectives.has(obj.Id) ||
Expand All @@ -161,7 +163,7 @@ export function calculateScore(
contractSession.completedObjectives,
)) ||
"Success" ===
getCurrentState(contractSession.Id, obj.Id),
contractSession.objectiveStates.get(obj.Id),
),
fractionNumerator: 2,
fractionDenominator: 3,
Expand Down
2 changes: 1 addition & 1 deletion components/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export interface ContractSession {
compat: boolean
currentDisguise: string
difficulty: number
objectiveDefinitions: Map<string, unknown>
objectives: Map<string, MissionManifestObjective>
objectiveStates: Map<string, string>
objectiveContexts: Map<string, unknown>
/**
Expand Down
13 changes: 13 additions & 0 deletions components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,16 @@ export function getSublocations(gameVersion: GameVersion): SublocationMap {

return sublocations
}

export function isTrueForEveryElement<Type>(
iter: Iterable<Type>,
test: (elem: Type) => boolean,
): boolean {
for (const element of iter) {
if (!test(element)) {
return false
}
}

return true
}
2 changes: 1 addition & 1 deletion tests/src/__snapshots__/databaseHandler.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ exports[`contract session storage > can read and write a basic contract session
"markedTargets": [],
"npcKills": [],
"objectiveContexts": [],
"objectiveDefinitions": [],
"objectiveStates": [],
"objectives": [],
"pacifications": [],
"recording": "NOT_SPOTTED",
"sessionStart": 1234,
Expand Down
2 changes: 1 addition & 1 deletion tests/src/databaseHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const basicFakeSession: ContractSession = {
difficulty: 2,
objectiveContexts: new Map(),
objectiveStates: new Map(),
objectiveDefinitions: new Map(),
objectives: new Map(),
ghost: {
deaths: 0,
unnoticedKills: 0,
Expand Down

0 comments on commit 26d3ebd

Please sign in to comment.