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

Refactor SaveServer and the things that interact with it to be async #1064

Merged
merged 3 commits into from
Jan 10, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions project/src/callbacks/GameCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ export class GameCallbacks implements OnLoad {
* Save profiles on game close
* @returns IGameLogoutResponseData
*/
public gameLogout(
public async gameLogout(
url: string,
info: IEmptyRequestData,
sessionID: string,
): IGetBodyResponseData<IGameLogoutResponseData> {
this.saveServer.save();
): Promise<IGetBodyResponseData<IGameLogoutResponseData>> {
await this.saveServer.saveProfile(sessionID);
return this.httpResponse.getBody({ status: "ok" });
}

Expand Down
8 changes: 4 additions & 4 deletions project/src/callbacks/LauncherCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class LauncherCallbacks {
return !output ? "FAILED" : output;
}

public register(url: string, info: IRegisterData, sessionID: string): "FAILED" | "OK" {
const output = this.launcherController.register(info);
public async register(url: string, info: IRegisterData, sessionID: string): Promise<"FAILED" | "OK"> {
const output = await this.launcherController.register(info);
return !output ? "FAILED" : "OK";
}

Expand Down Expand Up @@ -60,8 +60,8 @@ export class LauncherCallbacks {
return this.httpResponse.noBody("pong!");
}

public removeProfile(url: string, info: IRemoveProfileData, sessionID: string): string {
return this.httpResponse.noBody(this.saveServer.removeProfile(sessionID));
public async removeProfile(url: string, info: IRemoveProfileData, sessionID: string): Promise<string> {
return this.httpResponse.noBody(await this.saveServer.removeProfile(sessionID));
}

public getCompatibleTarkovVersion(): string {
Expand Down
7 changes: 3 additions & 4 deletions project/src/callbacks/ProfileCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ export class ProfileCallbacks {
/**
* Handle client/game/profile/create
*/
public createProfile(
public async createProfile(
url: string,
info: IProfileCreateRequestData,
sessionID: string,
): IGetBodyResponseData<ICreateProfileResponse> {
const id = this.profileController.createProfile(info, sessionID);
return this.httpResponse.getBody({ uid: id });
): Promise<IGetBodyResponseData<ICreateProfileResponse>> {
return this.httpResponse.getBody({ uid: await this.profileController.createProfile(info, sessionID) });
}

/**
Expand Down
6 changes: 3 additions & 3 deletions project/src/callbacks/SaveCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export class SaveCallbacks implements OnLoad, OnUpdate {
}

public async onLoad(): Promise<void> {
this.backupService.init();
this.saveServer.load();
await this.backupService.init();
await this.saveServer.load();
}

public getRoute(): string {
Expand All @@ -31,7 +31,7 @@ export class SaveCallbacks implements OnLoad, OnUpdate {
public async onUpdate(secondsSinceLastRun: number): Promise<boolean> {
// run every 15 seconds
if (secondsSinceLastRun > this.coreConfig.profileSaveIntervalSeconds) {
this.saveServer.save();
await this.saveServer.save();
return true;
}
return false;
Expand Down
10 changes: 5 additions & 5 deletions project/src/controllers/LauncherController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ export class LauncherController {
return "";
}

public register(info: IRegisterData): string {
public async register(info: IRegisterData): Promise<string> {
for (const sessionID in this.saveServer.getProfiles()) {
if (info.username === this.saveServer.getProfile(sessionID).info.username) {
return "";
}
}

return this.createAccount(info);
return await this.createAccount(info);
}

protected createAccount(info: IRegisterData): string {
protected async createAccount(info: IRegisterData): Promise<string> {
const profileId = this.generateProfileId();
const scavId = this.generateProfileId();
const newProfileDetails: Info = {
Expand All @@ -112,8 +112,8 @@ export class LauncherController {
};
this.saveServer.createProfile(newProfileDetails);

this.saveServer.loadProfile(profileId);
this.saveServer.saveProfile(profileId);
await this.saveServer.loadProfile(profileId);
await this.saveServer.saveProfile(profileId);

return profileId;
}
Expand Down
4 changes: 2 additions & 2 deletions project/src/controllers/ProfileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export class ProfileController {
* @param sessionID Player id
* @returns Profiles _id value
*/
public createProfile(info: IProfileCreateRequestData, sessionID: string): string {
return this.createProfileService.createProfile(sessionID, info);
public async createProfile(info: IProfileCreateRequestData, sessionID: string): Promise<string> {
return await this.createProfileService.createProfile(sessionID, info);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion project/src/di/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class ItemEventRouterDefinition extends Router {
}

export class SaveLoadRouter extends Router {
public handleLoad(profile: ISptProfile): ISptProfile {
public async handleLoad(profile: ISptProfile): Promise<ISptProfile> {
throw new Error("This method needs to be overrode by the router classes");
}
}
Expand Down
2 changes: 1 addition & 1 deletion project/src/routers/save_load/HealthSaveLoadRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class HealthSaveLoadRouter extends SaveLoadRouter {
return [new HandledRoute("spt-health", false)];
}

public override handleLoad(profile: ISptProfile): ISptProfile {
public override async handleLoad(profile: ISptProfile): Promise<ISptProfile> {
if (!profile.vitality) {
// Occurs on newly created profiles
profile.vitality = { health: undefined, effects: undefined };
Expand Down
2 changes: 1 addition & 1 deletion project/src/routers/save_load/InraidSaveLoadRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class InraidSaveLoadRouter extends SaveLoadRouter {
return [new HandledRoute("spt-inraid", false)];
}

public override handleLoad(profile: ISptProfile): ISptProfile {
public override async handleLoad(profile: ISptProfile): Promise<ISptProfile> {
if (profile.inraid === undefined) {
profile.inraid = { location: "none", character: "none" };
}
Expand Down
2 changes: 1 addition & 1 deletion project/src/routers/save_load/InsuranceSaveLoadRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class InsuranceSaveLoadRouter extends SaveLoadRouter {
return [new HandledRoute("spt-insurance", false)];
}

public override handleLoad(profile: ISptProfile): ISptProfile {
public override async handleLoad(profile: ISptProfile): Promise<ISptProfile> {
if (profile.insurance === undefined) {
profile.insurance = [];
}
Expand Down
2 changes: 1 addition & 1 deletion project/src/routers/save_load/ProfileSaveLoadRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class ProfileSaveLoadRouter extends SaveLoadRouter {
return [new HandledRoute("spt-profile", false)];
}

public override handleLoad(profile: ISptProfile): ISptProfile {
public override async handleLoad(profile: ISptProfile): Promise<ISptProfile> {
if (!profile.characters) {
profile.characters = { pmc: {} as IPmcData, scav: {} as IPmcData };
}
Expand Down
2 changes: 1 addition & 1 deletion project/src/routers/static/GameStaticRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class GameStaticRouter extends StaticRouter {
sessionID: string,
output: string,
): Promise<IGetBodyResponseData<IGameLogoutResponseData>> => {
return this.gameCallbacks.gameLogout(url, info, sessionID);
return await this.gameCallbacks.gameLogout(url, info, sessionID);
},
),
new RouteAction(
Expand Down
4 changes: 2 additions & 2 deletions project/src/routers/static/LauncherStaticRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class LauncherStaticRouter extends StaticRouter {
new RouteAction(
"/launcher/profile/register",
async (url: string, info: any, sessionID: string, output: string): Promise<string> => {
return this.launcherCallbacks.register(url, info, sessionID);
return await this.launcherCallbacks.register(url, info, sessionID);
},
),
new RouteAction(
Expand Down Expand Up @@ -57,7 +57,7 @@ export class LauncherStaticRouter extends StaticRouter {
new RouteAction(
"/launcher/profile/remove",
async (url: string, info: any, sessionID: string, output: string): Promise<string> => {
return this.launcherCallbacks.removeProfile(url, info, sessionID);
return await this.launcherCallbacks.removeProfile(url, info, sessionID);
},
),
new RouteAction(
Expand Down
Loading
Loading