1
mirror of https://github.com/thepeacockproject/Peacock synced 2025-02-23 03:35:25 +01:00

Fixed issue where restarting Peacock would require to first go offline again

This commit is contained in:
Lennard Fonteijn 2022-11-19 23:12:47 +01:00 committed by Lennard Fonteijn
parent 39b659d733
commit 2cc63c9ec3
3 changed files with 60 additions and 3 deletions

@ -107,6 +107,33 @@ export function getUserData(
return data
}
/**
* Only attempt to load a user's profile if it hasn't been loaded yet
*
* @param userId The user's ID.
* @param gameVersion The game's version.
*/
export async function cheapLoadUserData(
userId: string,
gameVersion: GameVersion,
): Promise<void> {
if (!userId || !gameVersion) {
return
}
const userProfile = asyncGuard.getProfile(`${userId}.${gameVersion}`)
if (userProfile) {
return
}
try {
await loadUserData(userId, gameVersion)
} catch (e) {
log(LogLevel.DEBUG, "Unable to load profile information.")
}
}
/**
* Loads a user's profile data.
*

@ -30,7 +30,7 @@ import {
Seconds,
ServerToClientEvent,
} from "./types/types"
import { extractToken, ServerVer } from "./utils"
import { extractToken, gameDifficulty, ServerVer } from "./utils"
import { json as jsonMiddleware } from "body-parser"
import { log, LogLevel } from "./loggingInterop"
import { getUserData, writeUserData } from "./databaseHandler"
@ -461,7 +461,25 @@ function saveEvents(
const processed: string[] = []
const userData = getUserData(req.jwt.unique_name, req.gameVersion)
events.forEach((event) => {
const session = contractSessions.get(event.ContractSessionId)
let session = contractSessions.get(event.ContractSessionId)
if (!session) {
log(
LogLevel.WARN,
"Creating a fake session to avoid problems... scoring will not work!",
)
newSession(
event.ContractSessionId,
event.ContractId,
req.jwt.unique_name,
gameDifficulty.normal,
req.gameVersion,
false,
)
session = contractSessions.get(event.ContractSessionId)
}
if (
!session ||

@ -80,6 +80,7 @@ import { multiplayerRouter } from "./multiplayer/multiplayerService"
import { multiplayerMenuDataRouter } from "./multiplayer/multiplayerMenuData"
import { pack, unpack } from "msgpackr"
import { liveSplitManager } from "./livesplit/liveSplitManager"
import { cheapLoadUserData } from "./databaseHandler"
// welcome to the bleeding edge
setFlagsFromString("--harmony")
@ -356,7 +357,18 @@ app.use(
next()
}),
)
).use(async (req: RequestWithJwt, _res, next): Promise<void> => {
if (!req.jwt) {
next()
return
}
// make sure the userdata is always loaded if a proper jwt token is available
await cheapLoadUserData(req.jwt.unique_name, req.gameVersion)
next()
})
function generateBlobConfig(req: RequestWithJwt) {
return {