-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-wss.js
More file actions
77 lines (66 loc) · 2.37 KB
/
Copy pathtest-wss.js
File metadata and controls
77 lines (66 loc) · 2.37 KB
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
const { io } = require("socket.io-client");
// Set up Manager Socket
const managerSocket = io("http://localhost:3000", {
path: "/ws",
auth: { clientId: "SYSTEM_TESTER_MANAGER" }
});
// Set up Player Socket
const playerSocket = io("http://localhost:3000", {
path: "/ws",
auth: { clientId: "SYSTEM_TESTER_PLAYER_1" }
});
let testGameId = null;
let testInviteCode = null;
managerSocket.on("connect", () => {
console.log("[Manager] connected!");
// Authenticate as manager
managerSocket.emit("manager:auth", "1234");
});
managerSocket.on("manager:quizzList", (quizzes) => {
console.log("[Manager] Got quizzes. Creating room...");
// Create first quiz in the list
if (quizzes.length > 0) {
managerSocket.emit("game:create", quizzes[0].id);
}
});
managerSocket.on("manager:gameCreated", ({ gameId, inviteCode }) => {
console.log(`[Manager] Game created. GameId: ${gameId}, InviteCode: ${inviteCode}`);
testGameId = gameId;
testInviteCode = inviteCode;
// Now player can join using the invite code
console.log(`[Player] Connecting to room with PIN: ${inviteCode}`);
playerSocket.emit("player:join", inviteCode);
});
playerSocket.on("game:successRoom", (gameId) => {
console.log("[Player] Successfully found room. Logging in...");
// Login with username "TestPlayer"
playerSocket.emit("player:login", { gameId, data: { username: "TestPlayer", teamName: "Testers" } });
});
playerSocket.on("game:successJoin", () => {
console.log("[Player] Successfully configured character context and joined lobby!");
// Start the game via Manager
console.log("[Manager] Starting the game...");
managerSocket.emit("manager:startGame", { gameId: testGameId });
});
playerSocket.on("game:status", (status) => {
if (status.name === "showQuestion") {
console.log("[Game] Status advanced to " + status.name);
}
if (status.name === "selectAnswer") {
console.log("[Player] Received question! Submitting answer...");
// Submit an answer (e.g. answer key 0)
playerSocket.emit("player:selectedAnswer", { gameId: testGameId, data: { answerKey: 0 } });
}
if (status.name === "showResult") {
console.log("[Player] Result computed: ", status.data);
setTimeout(() => {
console.log("[Test] Finishing test suite. Success.");
process.exit(0);
}, 1500);
}
});
// Fail safe
setTimeout(() => {
console.log("Test timed out!");
process.exit(1);
}, 20000);