-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
89 lines (74 loc) · 2.72 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import http from "http";
import { LobbyRoom, Server } from "colyseus";
import { WebSocketTransport } from "@colyseus/ws-transport";
// dirname
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// environment
import * as dotenv from "dotenv";
dotenv.config();
// database
import { Database } from "./app-server/database/database.js";
const db = new Database();
await db.connect();
console.log("connected to database");
process.on("exit", () => {
db.close().then(() => console.log("disconnected from database"));
});
// express app
const cookieKeys = [process.env.COOKIE_KEY_1, process.env.COOKIE_KEY_2];
const { app, session } = createApp(cookieKeys, __dirname, db);
const port = process.env.PORT || 3000;
const server = http.createServer(app);
// game server
const gameServer = new Server({
transport: new WebSocketTransport({
verifyClient: (info, next) => {
session(info.req as any, {} as any, () => next(true));
},
server: server,
}),
});
if (process.env.NODE_ENV !== "production") {
// simulate 80ms latency between server and client.
gameServer.simulateLatency(80);
}
// rooms
import { createApp } from "./app-server/app.js";
import { DiscWarRoom } from "./app-server/disc-war/room/discwar-room.js";
import { ChatRoom } from "./app-server/rooms/chat-room.js";
import SocialRoom from "./app-server/rooms/social-room.js";
import QueueRoom from "./app-server/rooms/queue-room.js";
import { DiscWarEngine } from "./app-shared/disc-war/index.js";
import { TagWarEngine } from "./app-shared/tag-war/tag-war.js";
import { TagWarRoom } from "./app-server/tag-war/room/tagwar-room.js";
gameServer.define("social", SocialRoom);
gameServer.define("chat-room", ChatRoom);
gameServer.define("lobby", LobbyRoom);
gameServer.define("disc-war", DiscWarRoom, {
dbCreateGame: db.discWar.createGame.bind(db.discWar),
dbGetProfile: db.discWar.getProfile.bind(db.discWar),
dbUpdateProfile: db.discWar.updateProfile.bind(db.discWar),
dbGetUserShop: db.getUserShop.bind(db),
dbAddCoins: db.addCoins.bind(db),
engine: DiscWarEngine,
});
gameServer.define("tag-war", TagWarRoom, {
dbCreateGame: db.tagWar.createGame.bind(db.tagWar),
dbGetProfile: db.tagWar.getProfile.bind(db.tagWar),
dbUpdateProfile: db.tagWar.updateProfile.bind(db.tagWar),
dbGetUserShop: db.getUserShop.bind(db),
dbAddCoins: db.addCoins.bind(db),
engine: TagWarEngine,
});
gameServer
.define("queue-disc-war", QueueRoom, { gameName: "disc-war" })
.enableRealtimeListing();
gameServer
.define("queue-tag-war", QueueRoom, { gameName: "tag-war" })
.enableRealtimeListing();
server.listen(port, () => {
console.log(`local: http://localhost:${port}`);
});