Skip to content

Commit

Permalink
refact(skymp5-client): refactor clientside gamemode api implementation (
Browse files Browse the repository at this point in the history
  • Loading branch information
Pospelove authored Mar 31, 2024
1 parent de9fd82 commit e4a73a3
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 296 deletions.
97 changes: 0 additions & 97 deletions skymp5-client/src/gamemodeApi/gamemodeApiSupport.ts

This file was deleted.

60 changes: 0 additions & 60 deletions skymp5-client/src/gamemodeApi/updateOwner.ts

This file was deleted.

4 changes: 4 additions & 0 deletions skymp5-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import { NetInfoService } from "./services/services/netInfoService";
import { AnimDebugService } from "./services/services/animDebugService";
import { TimersService } from "./services/services/timersService";
import { PlayerBowShotService } from "./services/services/playerBowShotService";
import { GamemodeEventSourceService } from "./services/services/gamemodeEventSourceService";
import { GamemodeUpdateService } from "./services/services/gamemodeUpdateService";
import { FrontHotReloadService } from "./services/services/frontHotReloadService";

once("update", () => {
Expand Down Expand Up @@ -91,6 +93,8 @@ const main = () => {
new AnimDebugService(sp, controller),
new TimersService(sp, controller),
new PlayerBowShotService(sp, controller),
new GamemodeEventSourceService(sp, controller),
new GamemodeUpdateService(sp, controller),
new FrontHotReloadService(sp, controller)
];
SpApiInteractor.setup(listeners);
Expand Down
78 changes: 78 additions & 0 deletions skymp5-client/src/services/services/gamemodeEventSourceService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { localIdToRemoteId, remoteIdToLocalId } from "../../view/worldViewMisc";
import { ConnectionMessage } from "../events/connectionMessage";
import { CustomEventMessage } from "../messages/customEventMessage";
import { UpdateGamemodeDataMessage } from "../messages/updateGameModeDataMessage";
import { ClientListener, CombinedController, Sp } from "./clientListener";
import { MsgType } from "../../messages";

export class GamemodeEventSourceService extends ClientListener {
constructor(private sp: Sp, private controller: CombinedController) {
super();

if (Array.isArray(this.sp.storage['eventSourceContexts'])) {
this.sp.storage['eventSourceContexts'] = this.sp.storage['eventSourceContexts'].filter(
(ctx: Record<string, unknown>) => !ctx._expired,
);
(this.sp.storage['eventSourceContexts'] as any).forEach((ctx: any) => {
this.setupEventSource(ctx);
});
}

this.controller.emitter.on("updateGamemodeDataMessage", (e) => this.onUpdateGamemodeDataMessage(e));
}

private onUpdateGamemodeDataMessage(event: ConnectionMessage<UpdateGamemodeDataMessage>) {
if (!Array.isArray(this.sp.storage['eventSourceContexts'])) {
this.sp.storage['eventSourceContexts'] = [];
} else {
this.sp.storage['eventSourceContexts'].forEach((ctx: Record<string, unknown>) => {
ctx.sendEvent = () => { };
ctx._expired = true;
});
}
const eventNames = Object.keys(event.message.eventSources);
eventNames.forEach((eventName) => {
try {
const fn = new Function('ctx', event.message.eventSources[eventName]);
const ctx = {
sp: this.sp,
sendEvent: (...args: unknown[]) => {
const message: CustomEventMessage = {
t: MsgType.CustomEvent,
args,
eventName
};
this.controller.emitter.emit("sendMessage", {
message: message,
reliability: "reliable"
});
},
getFormIdInServerFormat: (clientsideFormId: number) => {
return localIdToRemoteId(clientsideFormId);
},
getFormIdInClientFormat: (serversideFormId: number) => {
return remoteIdToLocalId(serversideFormId);
},
_fn: fn,
_eventName: eventName,
state: {},
};
(this.sp.storage['eventSourceContexts'] as Record<string, any>).push(ctx);
this.setupEventSource(ctx);
} catch (e) {
this.sp.printConsole(`'eventSources.${eventName}' -`, e);
}
});
}

private setupEventSource = (ctx: any) => {
this.controller.once('update', () => {
try {
ctx._fn(ctx);
this.sp.printConsole(`'eventSources.${ctx._eventName}' - Added`);
} catch (e) {
this.sp.printConsole(`'eventSources.${ctx._eventName}' -`, e);
}
});
};
};
Loading

0 comments on commit e4a73a3

Please sign in to comment.