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

New saving/loading behaviors to fix bugs related to saving #73

Merged
merged 27 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
66f0b63
Fix a bug causing an infinite loop when certain challenges are completed
RDIL Dec 28, 2022
67d4f20
Add missing locale strings
RDIL Dec 28, 2022
ccced30
Bump version to v5.6.1
RDIL Dec 28, 2022
1e505e9
Update axios to v1.2.2, fixing an 'unexpected end of file' error
grappigegovert Dec 30, 2022
fd20845
Use the new axios across all workspaces
RDIL Dec 31, 2022
72d7e3a
Fix challenges being able to cause a recursion deadlock
RDIL Dec 31, 2022
714e9dc
Bump version to v5.6.2
RDIL Dec 31, 2022
5ea114f
Merge branch 'v6' of https://github.com/thepeacockproject/Peacock int…
moonysolari Jan 4, 2023
c11e4d3
Merge branch 'v6' of https://github.com/thepeacockproject/Peacock int…
moonysolari Jan 7, 2023
ff7decd
Merge branch 'v6' of github.com:moonysolari/Peacock into v6
moonysolari Jan 8, 2023
2f5ea95
Add debug info for saving and loading
moonysolari Jan 8, 2023
7cae759
Fix contract session saving bug
moonysolari Jan 9, 2023
799fe09
Added type for UpdateUserSaveFileTable request
moonysolari Jan 9, 2023
d007428
Move saveSession and loadSession to profileHandler
moonysolari Jan 9, 2023
a167c01
Add saves data to user profiles
moonysolari Jan 9, 2023
9e6aa8b
Now deletes old session saves and checks timestamp
moonysolari Jan 9, 2023
bdcbf88
Fix bug in deleting old save
moonysolari Jan 9, 2023
c0e44ba
Update a function comment
moonysolari Jan 9, 2023
85463db
Update components/databaseHandler.ts
moonysolari Jan 9, 2023
efc92ee
Update components/eventHandler.ts
moonysolari Jan 9, 2023
2e9fb89
Update components/eventHandler.ts
moonysolari Jan 9, 2023
953d5be
Update components/eventHandler.ts
moonysolari Jan 9, 2023
8f236fe
Update components/profileHandler.ts
moonysolari Jan 9, 2023
0a9498e
Update components/profileHandler.ts
moonysolari Jan 9, 2023
5385428
Update components/profileHandler.ts
moonysolari Jan 9, 2023
ab0048c
Resolve lint issue
moonysolari Jan 9, 2023
a540bfd
Added null checks for new property in profiles
moonysolari Jan 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions components/databaseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { serializeSession, deserializeSession } from "./sessionSerialization"
import { castUserProfile } from "./utils"
import { getConfig } from "./configSwizzleManager"
import { log, LogLevel } from "./loggingInterop"
import { unlink, readdir } from "fs/promises"

/**
* Container for functions that handle file read/writes,
Expand Down Expand Up @@ -216,33 +217,50 @@ export async function writeExternalUserData(
/**
* Reads a contract session from the contractSessions folder.
*
* @param sessionId The ID of the session to load.
* @param identifier The identifier for the saved session, in the format of token_sessionID.
* @returns The contract session.
*/
export async function getContractSession(
sessionId: string,
identifier: string,
): Promise<ContractSession> {
const files = await readdir("contractSessions")
const filtered = files.filter((fn) => fn.endsWith(`_${identifier}.json`))

if (filtered.length === 0) {
throw new Error(`No session saved with identifier ${identifier}`)
}

// The filtered files have the same identifier, they are just stored at different slots
// So we can read any of them and it will be the same.
return deserializeSession(
JSON.parse(
(
await readFile(join("contractSessions", `${sessionId}.json`))
).toString(),
(await readFile(join("contractSessions", filtered[0]))).toString(),
),
)
}

/**
* Writes a contract session to the contractsSessions folder.
*
* @param sessionId The session's ID.
* @param identifier The identifier for the saved session, in the format of slot_token_sessionID.
* @param session The contract session.
*/
export async function writeContractSession(
sessionId: string,
identifier: string,
session: ContractSession,
): Promise<void> {
return await writeFile(
join("contractSessions", `${sessionId}.json`),
join("contractSessions", `${identifier}.json`),
JSON.stringify(serializeSession(session)),
)
}

/**
* Deletes a saved contract session from the contractsSessions folder.
*
* @param fileName The identifier for the saved session, in the format of slot_token_sessionID.
* @throws ENOENT if the file is not found.
*/
export async function deleteContractSession(fileName: string): Promise<void> {
return await unlink(join("contractSessions", `${fileName}.json`))
}
42 changes: 8 additions & 34 deletions components/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,13 @@ import {
import { extractToken, ServerVer } from "./utils"
import { json as jsonMiddleware } from "body-parser"
import { log, LogLevel } from "./loggingInterop"
import {
getContractSession,
getUserData,
writeContractSession,
writeUserData,
} from "./databaseHandler"
import { getUserData, writeUserData } from "./databaseHandler"
import { controller } from "./controller"
import { swapToLocationStatus } from "./discordRp"
import { randomUUID } from "crypto"
import { liveSplitManager } from "./livesplit/liveSplitManager"
import { handleMultiplayerEvent } from "./multiplayer/multiplayerService"
import { handleEvent } from "@peacockproject/statemachine-parser"
import picocolors from "picocolors"
import { encodePushMessage } from "./multiplayer/multiplayerUtils"
import {
ActorTaggedC2SEvent,
Expand All @@ -65,6 +59,7 @@ import {
SpottedC2SEvent,
WitnessesC2SEvent,
} from "./types/events"
import picocolors from "picocolors"

const eventRouter = Router()

Expand Down Expand Up @@ -682,14 +677,20 @@ function saveEvents(
case "IntroCutEnd":
if (!session.timerStart) {
session.timerStart = event.Timestamp
log(
LogLevel.DEBUG,
`Mission started at: ${session.timerStart}`,
)
}
break
case "exit_gate":
session.timerEnd = event.Timestamp
log(LogLevel.DEBUG, `Mission ended at: ${session.timerEnd}`)
break
case "ContractEnd":
if (!session.timerEnd) {
session.timerEnd = event.Timestamp
log(LogLevel.DEBUG, `Mission ended at: ${session.timerEnd}`)
}
break
case "ObjectiveCompleted":
Expand Down Expand Up @@ -799,31 +800,4 @@ function saveEvents(
return response
}

export async function saveSession(
sessionId: string,
token: string,
): Promise<void> {
if (!contractSessions.has(sessionId)) {
log(LogLevel.WARN, `Refusing to save ${sessionId} as it doesn't exist`)
return
}

await writeContractSession(
token + "_" + sessionId,
contractSessions.get(sessionId)!,
)
}

export async function loadSession(
sessionId: string,
token: string,
sessionData?: ContractSession,
): Promise<void> {
if (!sessionData) {
sessionData = await getContractSession(token + "_" + sessionId)
}

contractSessions.set(sessionId, sessionData)
}

export { eventRouter }
Loading