-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgames.ts
55 lines (52 loc) · 1.28 KB
/
games.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
import * as R from "ramda";
import * as db from "./db";
import logger from "./logger";
import { serializeGame, trimPlayer } from "./table/serialize";
import { Response } from "restify";
export const games = async (req, res, next) => {
try {
const games: any = await db.games(req.params.table);
res.send(200, games.map(serializeGame));
} catch (e) {
res.send(500, "Could not get games");
logger.error(e);
} finally {
next();
}
};
export const game = async (req, res, next) => {
try {
const game = await db.game(req.params.id);
res.send(200, [serializeGame(game)]);
} catch (e) {
res.send(500, "Could not get games");
logger.error(e);
} finally {
next();
}
};
export const chat = (source: "table" | "game") => async (
req,
res: Response,
next
) => {
try {
const chat: any = await (source === "table"
? db.chat(req.params.table)
: db.chatByGame(req.params.id));
if (req.query.text === "plain") {
res.sendRaw(
200,
chat.map(line => `${line.user?.name}: ${line.message}`).join("\n"),
{ "Content-Type": "text/plain; charset=utf-8" }
);
} else {
res.send(200, chat);
}
} catch (e) {
res.send(500, "Could not get chat");
logger.error(e);
} finally {
next();
}
};