Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions DuckiePop/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,57 @@ import { createServer } from "http";
import { Server } from "socket.io";

const app = express();
app.use(express.json());

const server = createServer(app);
const io = new Server(server, { cors: { origin: "*" } });

const DEFAULT_DOMAIN = "influenza";

const domains = {
influenza: [
"influenza", "virus", "vaccine", "outbreak", "pandemic", "epidemic",
"symptoms", "fever", "cough", "transmission", "antiviral", "immunity",
"respiratory", "infection", "diagnosis", "treatment", "strain", "mutation",
"hemagglutinin", "neuraminidase", "h1n1", "h3n2", "swine", "avian",
"seasonal", "tamiflu", "oseltamivir", "zanamivir", "prophylaxis",
"surveillance", "quarantine", "isolation", "incubation"
],
javascript: [
"javascript", "react", "html", "css", "api", "framework", "frontend",
"backend", "typescript", "node", "express", "dom", "webpack", "babel",
"redux", "hooks", "component", "routing", "authentication", "database",
"rest", "graphql", "fetch", "axios", "promise", "async", "await",
"callback", "middleware", "session", "cookie", "deployment", "docker"
],
machinelearning: [
"algorithm", "dataset", "model", "tensor", "neuron", "gradient",
"backpropagation", "classification", "regression", "overfitting",
"validation", "hyperparameter", "feature", "vector", "bias", "activation",
"epoch", "parameter", "ensemble", "regularization", "kernel", "clustering",
"precision", "recall", "accuracy", "transformer", "embedding", "network",
"pipeline", "optimization", "inference", "dropout", "convolutional"
]
};

// GET /api/scans - returns words for the specified domain (default: influenza)
app.get("/api/scans", (req, res) => {
const domain = (req.query.domain || DEFAULT_DOMAIN).toLowerCase();
const words = domains[domain];
if (!words) {
return res.status(404).json({ error: `Domain '${domain}' not found.`, availableDomains: Object.keys(domains) });
}
res.json({ domain, words });
});

const games = {}; // Stores active games

io.on("connection", (socket) => {
console.log(`✅ User connected: ${socket.id}`);

socket.on("submit-number", ({ number, name }) => {
console.log(`🔹 Player ${name} submitted number: ${number}`);
socket.on("submit-number", ({ number, name, domain }) => {
const gameDomain = (domain || DEFAULT_DOMAIN).toLowerCase();
console.log(`🔹 Player ${name} submitted number: ${number} | domain: ${gameDomain}`);

let matchFound = null;
for (const [gameId, game] of Object.entries(games)) {
Expand All @@ -25,18 +66,20 @@ io.on("connection", (socket) => {
}
}

const domainWords = domains[gameDomain] || domains[DEFAULT_DOMAIN];

if (matchFound) {
const game = games[matchFound];
const [player1, player2] = game.players;

console.log(`✅ Match found: ${player1.name} vs ${player2.name} | gameId: ${matchFound}`);

io.to(player1.id).emit("start-game", { opponentName: player2.name, gameId: matchFound });
io.to(player2.id).emit("start-game", { opponentName: player1.name, gameId: matchFound });
io.to(player1.id).emit("start-game", { opponentName: player2.name, gameId: matchFound, domain: game.domain, domainWords: game.domainWords });
io.to(player2.id).emit("start-game", { opponentName: player1.name, gameId: matchFound, domain: game.domain, domainWords: game.domainWords });
} else {
const gameId = Math.floor(Math.random() * 1000000);
games[gameId] = { players: [{ id: socket.id, name }], number, words: {} };
console.log(`🕒 Player ${name} is waiting for a match on number ${number}`);
games[gameId] = { players: [{ id: socket.id, name }], number, words: {}, domain: gameDomain, domainWords };
console.log(`🕒 Player ${name} is waiting for a match on number ${number} | domain: ${gameDomain}`);
}
});

Expand Down