Skip to content

Commit fb3a748

Browse files
committed
feat: Implement UnstorageDb for match storage and retrieval
- Added UnstorageDb class for managing game state, metadata, and logs using Unstorage. - Created unit tests for UnstorageDb to validate match creation, fetching, and filtering. - Introduced BroadcastChannelPubSub for pub/sub messaging using BroadcastChannel API. - Implemented InMemoryPubSub for local pub/sub functionality. - Integrated SocketIO transport layer for real-time communication in the game server. - Updated transport and socket handling to support new pub/sub mechanisms. - Removed deprecated pubsub implementation and added tests for new pub/sub classes. - Updated deno.json to include new dependencies for socket.io and rfc6902.
1 parent cb65dd9 commit fb3a748

14 files changed

Lines changed: 551 additions & 71 deletions

deno.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
"./dist"
2222
],
2323
"entrypoint": "dist/server.js"
24-
}
24+
},
25+
"unstable": ["broadcast-channel", "kv"]
2526
}

deno.lock

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { UnstorageDb } from "./db.ts";
1+
// deno-lint-ignore-file no-explicit-any
2+
import { UnstorageDb } from "./unstorage-db.ts";
23
import type { LogEntry, Server, State } from "boardgame.io";
34
import {
45
afterEach,
@@ -10,7 +11,7 @@ import {
1011
import { expect } from "jsr:@std/expect";
1112
import { createStorage } from "unstorage";
1213

13-
describe("FlatFile", () => {
14+
describe("UnstorageDb", () => {
1415
let db: UnstorageDb;
1516

1617
beforeAll(async () => {
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import type { LogEntry, Server, State, StorageAPI } from "boardgame.io";
2-
import { Async } from "boardgame.io/internal";
3-
import { prefixStorage, type Storage } from "unstorage";
2+
import { createStorage, prefixStorage, type Storage } from "unstorage";
43

5-
export class UnstorageDb extends Async {
4+
export class UnstorageDb implements StorageAPI.Async {
65
private store: Storage;
7-
constructor(store: Storage) {
8-
super();
6+
constructor(store?: Storage) {
7+
if (!store) store = createStorage();
98
this.store = prefixStorage(store, "bgio");
109
}
1110

12-
override async connect() {}
11+
type(): StorageAPI.Type {
12+
return 1;
13+
}
14+
15+
async connect() {}
1316

14-
override async createMatch(
17+
async createMatch(
1518
matchID: string,
1619
opts: StorageAPI.CreateMatchOpts,
1720
): Promise<void> {
@@ -22,29 +25,29 @@ export class UnstorageDb extends Async {
2225
await this.setMetadata(matchID, opts.metadata);
2326
}
2427

25-
override async setState(
28+
async setState(
2629
matchID: string,
2730
state: State,
2831
deltalog?: LogEntry[],
2932
) {
3033
if (deltalog && deltalog.length > 0) {
3134
const key = LogKey(matchID);
32-
const log: LogEntry[] = ((await this.store.getItem(key)) as LogEntry[]) ||
35+
const log: LogEntry[] = (await this.store.getItem<LogEntry[]>(key)) ||
3336
[];
3437
await this.store.setItem(key, [...log, ...deltalog]);
3538
}
3639
return await this.store.setItem(matchID, state);
3740
}
3841

39-
override async setMetadata(
42+
async setMetadata(
4043
matchID: string,
4144
metadata: Server.MatchData,
4245
) {
4346
const key = MetadataKey(matchID);
4447
return await this.store.setItem(key, metadata);
4548
}
4649

47-
override async fetch<O extends StorageAPI.FetchOpts>(
50+
async fetch<O extends StorageAPI.FetchOpts>(
4851
matchID: string,
4952
opts: O,
5053
): Promise<StorageAPI.FetchResult<O>> {
@@ -76,17 +79,17 @@ export class UnstorageDb extends Async {
7679
return result as StorageAPI.FetchResult<O>;
7780
}
7881

79-
override async wipe(matchID: string) {
82+
async wipe(matchID: string) {
8083
await this.store.removeItem(matchID);
8184
await this.store.removeItem(InitialStateKey(matchID));
8285
await this.store.removeItem(LogKey(matchID));
8386
await this.store.removeItem(MetadataKey(matchID));
8487
}
8588

86-
override async listMatches(
89+
async listMatches(
8790
opts?: StorageAPI.ListMatchesOpts,
8891
): Promise<string[]> {
89-
const keys = await this.store.keys();
92+
const keys = await this.store.getKeys();
9093
const suffix = ":metadata";
9194

9295
const arr = await Promise.all(

server/bgio/lobby.ts

Whitespace-only changes.

server/bgio/pubsub.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)