1
mirror of https://github.com/thepeacockproject/Peacock synced 2024-11-22 22:12:45 +01:00

Move challenge contexts to the session

This commit is contained in:
Reece Dunham 2022-11-20 12:06:18 -05:00 committed by Lennard Fonteijn
parent 770187586b
commit 840cd0b57a
4 changed files with 34 additions and 24 deletions

View File

@ -42,7 +42,6 @@ import {
import {
handleEvent,
HandleEventOptions,
Timer,
} from "@peacockproject/statemachine-parser"
import { SavedChallengeGroup } from "../types/challenges"
import { fastClone } from "../utils"
@ -150,17 +149,6 @@ export abstract class ChallengeRegistry {
}
export class ChallengeService extends ChallengeRegistry {
// TODO: Move onto sessions - this is user specific, and I've been
// procrastinating this fix. -RD
private readonly _challengeContexts: {
[sessionId: string]: {
[challengeId: string]: {
context: unknown
state: string
timers: Timer[]
}
}
}
// we'll use this after writing details to the user's profile
private _justCompletedChallengeIds: string[] = []
public hooks: {
@ -177,7 +165,6 @@ export class ChallengeService extends ChallengeRegistry {
constructor(controller: Controller) {
super(controller)
this._challengeContexts = {}
this.hooks = {
onChallengeCompleted: new SyncHook(),
}
@ -316,8 +303,9 @@ export class ChallengeService extends ChallengeRegistry {
sessionId: string,
session: ContractSession,
): void {
this._challengeContexts[sessionId] = {}
const { gameVersion, contractId } = session
// we know we will have challenge contexts because this session is
// brand new.
const { gameVersion, contractId, challengeContexts } = session
const challengeGroups = this.getChallengesForContract(
contractId,
@ -358,7 +346,7 @@ export class ChallengeService extends ChallengeRegistry {
})
}
this._challengeContexts[sessionId][challenge.Id] = {
challengeContexts[challenge.Id] = {
context:
fastClone(challenge.Definition?.Context || {}) || {},
state: progression.Completed ? "Success" : "Start",
@ -377,11 +365,15 @@ export class ChallengeService extends ChallengeRegistry {
): void {
const writeQueue: PendingProgressionWrite[] = []
for (const challengeId of Object.keys(
this._challengeContexts[sessionId],
)) {
if (!session.challengeContexts) {
log(LogLevel.WARN, "Session does not have challenge contexts.")
log(LogLevel.WARN, "Challenges will be disabled!")
return
}
for (const challengeId of Object.keys(session.challengeContexts)) {
const challenge = this.getChallengeById(challengeId)
const data = this._challengeContexts[sessionId][challengeId]
const data = session.challengeContexts[challengeId]
if (!challenge) {
log(LogLevel.WARN, `Challenge ${challengeId} not found`)
@ -406,9 +398,8 @@ export class ChallengeService extends ChallengeRegistry {
options,
)
this._challengeContexts[sessionId][challengeId].state =
result.state
this._challengeContexts[sessionId][challengeId].context =
session.challengeContexts[challengeId].state = result.state
session.challengeContexts[challengeId].context =
result.context || challenge.Definition?.Context || {}
if (previousState !== "Success" && result.state === "Success") {

View File

@ -283,6 +283,7 @@ export function newSession(
IsWinner: false,
timerEnd: null,
},
challengeContexts: {},
})
userIdToTempSession.set(userId, sessionId)

View File

@ -23,6 +23,7 @@ import type { Request } from "express"
import { SavedChallenge } from "./challenges"
import { SessionGhostModeDetails } from "../multiplayer/multiplayerService"
import { IContextListener } from "../statemachines/contextListeners"
import { Timer } from "@peacockproject/statemachine-parser"
/**
* A duration or relative point in time expressed in seconds.
@ -229,7 +230,24 @@ export interface ContractSession {
objectiveDefinitions: Map<string, unknown>
objectiveStates: Map<string, string>
objectiveContexts: Map<string, unknown>
/**
* Session Ghost Mode details.
*
* @since v5.0.0
*/
ghost?: SessionGhostModeDetails
/**
* The current state of the challenges.
*
* @since v5.6.0-dev.1
*/
challengeContexts?: {
[challengeId: string]: {
context: unknown
state: string
timers: Timer[]
}
}
}
/**