This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
165 lines (122 loc) · 4.46 KB
/
app.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import * as dotenv from "dotenv";
dotenv.config();
import Deck from "./modules/Deck";
import Card from "./modules/Card";
import Suit from "./modules/Suit";
import CardMatch from "./modules/CardMatch";
export let VERBOSE = true; // process.env.VERBOSE;
export function println(...items: any) {
let out: string = "";
for (let item of items) out += (typeof(item) != "string" ? item.toString() : item) + " ";
console.log(out);
}
/*
async function getPost(req: Request){
let postdata: string = "";
req.on("data", (chunk) => {
postdata += chunk.toString();
if (postdata.length > 1e3) req.socket.destroy(); // Prevent requests over 1kB
});
let post: ParsedQs;
req.on("end", () => {
post = qs.parse(postdata);
if (VERBOSE) console.log("New post arrived:" + JSON.stringify(post));
});
return post;
}
*/
import express from "express";
import { Router, Request, Response, NextFunction } from "express";
import api from "./api";
//import middleware from "./middleware";
import websockets from './websockets';
import Poker from "./modules/Poker";
import PokerGame from "./modules/Poker";
import fs from "fs";
import qs, { ParsedQs } from "qs";
import Player from "./modules/Player";
const app = express();
app.disable('query parser');
app.use(express.query({}));
app.engine('.html', require('ejs').__express); // Rendering
app.set('view engine', 'html');
const port: number = Number(process.env.PORT) || 8080;
//middleware(app);
//api(app);
let games: PokerGame[] = new Array<PokerGame>();
app.get("/", (req: Request, res: Response, next: NextFunction) => {
res.send(`<a href="/progress">Progress</a>`);
});
app.get("/listgames", async (req: Request, res: Response, next: NextFunction) => {
res.status(200).set("Content-Type", "text/json").send(JSON.stringify(games.map((game: PokerGame) => {return {
gameId: game.id(),
currentPlayers: game.playerCount(),
maxPlayers: game.getMaxPlayers(),
}})));
next();
});
app.put("/progress", async (req: Request, res: Response, next: NextFunction) => {
let postdata: string = "";
req.on("data", (chunk) => {
postdata += chunk.toString();
if (postdata.length > 1e3) req.socket.destroy(); // Prevent requests over 1kB
});
let post: ParsedQs;
req.on("end", () => {
post = qs.parse(postdata);
let game = games.find((element) => element.id() == post.gameId);
if(game) game.progress(); else println("No game with this ID...");
res.status(200).send(JSON.stringify({gameId: game?.id(), gameState: game?.state()}));
next();
});
});
app.post("/newgame", async (req: Request, res: Response, next: NextFunction) => {
let postdata: string = "";
req.on("data", (chunk) => {
postdata += chunk.toString();
if (postdata.length > 1e3) req.socket.destroy(); // Prevent requests over 1kB
});
let post: ParsedQs;
req.on("end", () => {
post = qs.parse(postdata);
let id = PokerGame.makeid();
if (typeof post.gameId == "string") id = post.gameId;
while (games.find((element) => element.id() == id)) id = PokerGame.makeid();
games.push(new PokerGame(id).start());
res.status(200).send(JSON.stringify({ gameId: id }));
next();
});
});
app.get("/newgame", async (req: Request, res: Response, next: NextFunction) => {
let id = PokerGame.makeid();
while (games.find((element) => element.id() == id)) id = PokerGame.makeid();
games.push(new PokerGame(id).start());
res.render("newgame", {gameId: id});
next();
});
app.post("/join/:id", async(req: Request, res: Response, next: NextFunction) => {
let id = req.params["id"];
let game = games.find((element) => element.id() === id);
game?.addPlayer(new Player(game));
//println("Player joined game", id);
res.sendStatus(200);
});
const server = app.listen(port, () => {
if(VERBOSE) println(`Listening on ${port}`);
});
websockets(server, games);
for(let i = 0; i<5; i++){
games.push(new PokerGame("Test" + i).start());
}
/*
let c1: Card = new Card(3, new Suit('H'));
let c2: Card = new Card(3, new Suit('S'));
let c3: Card = new Card(3, new Suit('H'));
let c4: Card = new Card(4, new Suit('D'));
let c5: Card = new Card(5, new Suit('D'));
let c6: Card = new Card(6, new Suit('H'));
let c7: Card = new Card(6, new Suit('S'));
let myDeck: Deck = new Deck(c1, c2, c3, c4, c5, c6, c7);
myDeck.shuffle();
println("\n", CardMatch.match(myDeck));
*/