Skip to content
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hathora",
"version": "0.12.2",
"version": "0.13.0",
"description": "Multiplayer game framework",
"bin": "lib/cli.js",
"files": [
Expand Down
1 change: 1 addition & 0 deletions templates/base/server/.hathora/methods.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export interface Methods<T> {
{{#if tick}}
onTick(state: T, ctx: Context, timeDelta: number): void;
{{/if}}
onShutdown(state: T, ctx: Context): void;
}
20 changes: 10 additions & 10 deletions templates/base/server/.hathora/store.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ setInterval(() => {
{{#if tick}}
const currTime = Date.now();
rooms.forEach(async ({ impl, chance, subscriptions }, stateId) => {
if (subscriptions.size > 0) {
const timeDelta = currTime - prevUpdateTime;
await impl.onTick(ctx(chance, currTime, stateId), timeDelta / 1000);
const changedAt = impl.changedAt();
if (changedAt !== undefined) {
changedStates.set(stateId, changedAt);
pendingMessages.delete(stateId);
log.append(stateId, currTime, new Writer().writeUInt8(0xff).writeUVarint(timeDelta).toBuffer());
}
const timeDelta = currTime - prevUpdateTime;
await impl.onTick(ctx(chance, currTime, stateId), timeDelta / 1000);
const changedAt = impl.changedAt();
if (changedAt !== undefined) {
changedStates.set(stateId, changedAt);
pendingMessages.delete(stateId);
log.append(stateId, currTime, new Writer().writeUInt8(0xff).writeUVarint(timeDelta).toBuffer());
}
});
prevUpdateTime = currTime;
Expand All @@ -65,8 +63,10 @@ setInterval(() => {
pendingMessages.clear();
}, {{#if tick}}{{tick}}{{else}}50{{/if}});
setInterval(() => {
rooms.forEach(({ subscriptions }, stateId) => {
rooms.forEach(async ({ impl, chance, subscriptions }, stateId) => {
if (subscriptions.size === 0) {
const currTime = Date.now();
await impl.onShutdown(ctxNoEvents(chance, currTime, stateId));
rooms.delete(stateId);
log.unload(stateId);
}
Expand Down
4 changes: 4 additions & 0 deletions templates/base/server/.hathora/wrapper.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export class ImplWrapper {
restoreSideEffectFunctions();
}
{{/if}}
public async onShutdown(ctx: Context) {
const impl = await getLatestImpl();
return impl.onShutdown(this.state, ctx);
}
public async getUserState(userId: UserId) {
const impl = await getLatestImpl();
return impl.getUserState(onChange.target(this.state), userId);
Expand Down
1 change: 1 addition & 0 deletions templates/bootstrap/server/impl.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Impl implements Methods<InternalState> {
{{#if tick}}
onTick(state: InternalState, ctx: Context, timeDelta: number): void {}
{{/if}}
onShutdown(state: InternalState, ctx: Context): void {}
}
{{#*inline "renderDefault"}}
{{#if (eq type "array")}}
Expand Down