-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
104 lines (76 loc) · 3.29 KB
/
main.js
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
const { prototype } = require("events");
const express = require("express");
const PORT = 8000;
const path = require("path");
const { createServer } = require("http");
const { Server } = require("socket.io");
const app = express();
const fs = require("fs");
const { get } = require("express/lib/response");
app.set("view engine", "ejs");
// telling the express module that the public dir has all of our site assests
app.use(express.static(__dirname + "/public"));
// allowing for the frontend to pass information to the backend
// initalizing socket.io's connect to the application
const httpServer = createServer(app);
const io = new Server(httpServer, {});
var data = JSON.parse(fs.readFileSync("data.json"));
function check_correctness(choice) {
// error handling
if (!data[choice.left] && data[choice.right]) {
return {"correct": true, "age": data[choice.right].age};
} else if (!data[choice.right]) {
return {"correct": true, "age": "unknown"};
}
let age_two = data[choice.right].age;
let age_one = data[choice.left].age;
if (choice.more) {
return {"correct": age_two > age_one, "age": age_two};
} else {
return {"correct": age_two < age_one, "age": age_two};
}
}
function get_random_person(given) {
let keyArray = Object.keys(data);
let value;
do {
value = keyArray[Math.floor(Math.random() * keyArray.length)];
} while (value == given || data[value]["pic-link"] == "");
return value;
}
io.on("connection", (socket) => {
socket.on("choice_question", (choice) => {
io.emit("choice_response", check_correctness(choice));
});
socket.on("page_opening", () => {
let left_person = get_random_person();
let right_person = get_random_person(left_person);
io.emit("page_opening", {"left": {"name": left_person, "age": data[left_person]["age"]},
"right": right_person,
"cheating_answer": data[right_person]["age"],
"image": {"left": data[left_person]["pic-link"], "right": data[right_person]["pic-link"]}});
});
socket.on("update_page", (previous_value) => {
// error checking
let new_person = get_random_person(previous_value.right);
if (!data[previous_value.right]) {
io.emit("update_page", {"left": {"name": previous_value.right, "age": "unknown"},
"cheating_answer": data[new_person]["age"],
"right": new_person});
} else {
io.emit("update_page", {"left": {"name": previous_value.right, "age": data[previous_value.right]["age"]},
"right": new_person,
"cheating_answer": data[new_person]["age"],
"image": {"left": data[previous_value.right]["pic-link"], "right": data[new_person]["pic-link"]}});
}
});
});
// this creates an endpoint on the website for "/"
app.get("/", (req, res) => {``
res.sendFile(path.join(__dirname, "/index.html"));
});
app.get("/lost/:score", (req, res) => {
res.sendFile(path.join(__dirname, "/lost.html"));
})
// this starts hosting the webpage on the given port
httpServer.listen(PORT);