Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move challenge contexts to the session #36

Merged
merged 1 commit into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 14 additions & 23 deletions components/candle/challengeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
import {
handleEvent,
HandleEventOptions,
Timer,
} from "@peacockproject/statemachine-parser"
import { SavedChallengeGroup } from "../types/challenges"
import { fastClone } from "../utils"
Expand Down Expand Up @@ -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: {
Expand All @@ -177,7 +165,6 @@ export class ChallengeService extends ChallengeRegistry {

constructor(controller: Controller) {
super(controller)
this._challengeContexts = {}
this.hooks = {
onChallengeCompleted: new SyncHook(),
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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!")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When mastery progression exists, it may be wise to change to "Progression will be disabled" as the global challenges should be tracked by the same system as normal challenges.

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`)
Expand All @@ -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") {
Expand Down
1 change: 1 addition & 0 deletions components/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ export function newSession(
IsWinner: false,
timerEnd: null,
},
challengeContexts: {},
})
userIdToTempSession.set(userId, sessionId)

Expand Down
18 changes: 18 additions & 0 deletions components/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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[]
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@
"packaging/typedefs"
],
"packageManager": "yarn@3.2.4"
}
}