fix activity
This commit is contained in:
parent
0231530bcd
commit
2dfa03e97d
|
|
@ -0,0 +1,5 @@
|
||||||
|
import { Room } from "./room";
|
||||||
|
|
||||||
|
export const activity = {
|
||||||
|
site: {} as Record<string, Room<{ code: { name: string } }>>,
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
import { ServerWebSocket } from "bun";
|
||||||
|
import { WSData } from "../../../../../pkgs/core/server/create";
|
||||||
|
import { conns } from "./conn";
|
||||||
|
|
||||||
|
export class Room<T extends Record<string, true | Record<string, any>>> {
|
||||||
|
name = "";
|
||||||
|
clients = new Map<ServerWebSocket<WSData>, Partial<T>>();
|
||||||
|
|
||||||
|
constructor() {}
|
||||||
|
|
||||||
|
findAll(where: Partial<T>) {
|
||||||
|
const clients = new Map<ServerWebSocket<WSData>, Partial<T>>();
|
||||||
|
for (const [ws, data] of this.clients) {
|
||||||
|
let match = true;
|
||||||
|
for (const key in where) {
|
||||||
|
if (where[key] === true) {
|
||||||
|
if (!data[key]) {
|
||||||
|
match = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (data[key] !== where[key]) {
|
||||||
|
match = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (match) clients.set(ws, data);
|
||||||
|
}
|
||||||
|
return clients;
|
||||||
|
}
|
||||||
|
|
||||||
|
private identify(client: { ws?: ServerWebSocket<WSData>; id?: string }) {
|
||||||
|
let ws = null as ServerWebSocket<WSData> | null;
|
||||||
|
if (client.ws) {
|
||||||
|
ws = client.ws;
|
||||||
|
} else if (client.id) {
|
||||||
|
const connws = conns.get(client.id)?.ws;
|
||||||
|
if (connws) ws = connws;
|
||||||
|
}
|
||||||
|
return ws;
|
||||||
|
}
|
||||||
|
|
||||||
|
join(client: { ws?: ServerWebSocket<WSData>; id?: string }) {
|
||||||
|
const ws = this.identify(client);
|
||||||
|
if (ws) {
|
||||||
|
this.clients.set(ws, {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
leave(client: { ws?: ServerWebSocket<WSData>; id?: string }) {
|
||||||
|
const ws = this.identify(client);
|
||||||
|
if (ws) {
|
||||||
|
this.clients.delete(ws);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -26,27 +26,32 @@ if (g.mode === "dev") {
|
||||||
await startDevWatcher();
|
await startDevWatcher();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** init lmdb */
|
/** init lmdb */
|
||||||
user.conf.init();
|
user.conf.init();
|
||||||
snapshot.init();
|
snapshot.init();
|
||||||
|
|
||||||
|
|
||||||
await preparePrisma();
|
await preparePrisma();
|
||||||
await ensureNotRunning();
|
await ensureNotRunning();
|
||||||
|
|
||||||
if (g.db) {
|
if (g.db) {
|
||||||
g.db.$connect().catch((e: any) => {
|
g.db
|
||||||
|
.$connect()
|
||||||
|
.catch((e: any) => {
|
||||||
g.log.error(`[DB ERROR]\n${e.message}`);
|
g.log.error(`[DB ERROR]\n${e.message}`);
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
g.log.info("Database connected");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
await initSrv();
|
await initSrv();
|
||||||
await syncActionDefinition();
|
await syncActionDefinition();
|
||||||
|
g.log.info("WS Action defined");
|
||||||
await generateAPIFrm();
|
await generateAPIFrm();
|
||||||
await prepareApiRoutes();
|
await prepareApiRoutes();
|
||||||
await prepareAPITypes();
|
await prepareAPITypes();
|
||||||
|
g.log.info("API Prepared");
|
||||||
|
|
||||||
await parcelBuild();
|
await parcelBuild();
|
||||||
await createServer();
|
await createServer();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue