import fastify from "fastify"; import fastify_static from "fastify-static" import { join } from "path"; import { Server as SocketIOServer } from "socket.io"; import { users } from "./config"; const app = fastify(); const io = new SocketIOServer(app.server); app.register(fastify_static, { root: join(__dirname, "static") }) let global = { last_time: Date.now(), last_timeout_id: 0 }; let juanwang: [string, number][] = []; let juangroup = new Set(); let lastjuan: { [key: string]: number } = {}; app.get("/", async (req, res) => { res.redirect("index.html"); }); app.get("/juanwang", async (req, res) => { return juanwang; }); app.post("/start_new", async () => { juangroup.clear(); juanwang = []; }); app.post("/juan", async (req, res) => { let cur_time = Date.now(); let data = JSON.parse(req.body as string); let name: string = data.name; if (!name) { return "你是谁?"; } let group = users[data.name]; if (!group) { return "你是谁??"; } let last_time = lastjuan[name] ?? 0; lastjuan[name] = cur_time; if (cur_time - last_time < 3000) return "3秒内只能卷一次"; if (juangroup.has(group)) return "每组在一次抢答中只能卷一次"; juanwang.push([name, group]); juangroup.add(group); let notify_update = () => { global.last_time = Date.now(); io.emit("update"); }; if (Date.now() - global.last_time > 300) notify_update(); clearTimeout(global.last_timeout_id); global.last_timeout_id = setTimeout(notify_update, 250) as unknown as number; return "卷成功了"; }); io.on("connection", s => { s.emit("update"); }); app.listen(5000, "0.0.0.0");