-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketio.js
More file actions
102 lines (79 loc) · 4.38 KB
/
Copy pathsocketio.js
File metadata and controls
102 lines (79 loc) · 4.38 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
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 { Server } = require('socket.io');
const { SocketMappingCache } = require('./cache');
const { SocketIOResponse } = require('./utils/responses/socketioResponse');
const { votingSchema } = require('./schemas/votingSchema');
var SocketIO = (
function () {
var ioInstance = null;
function createSocketIO(server) {
var io = new Server(server, {
cors: {
origin: "*"
}
});
io.on("connection", (socket) => {
console.log("New connection");
socket.emit("hi", "hello");
socket.on("connecting", (credentials) => {
const judgeCode = credentials.judgeCode;
console.log("Connecting " + socket.id + " with judge code " + judgeCode);
SocketMappingCache.addSocketID(socket.id, judgeCode);
let record = votingSchema.judgeModel.records.findByPrimaryKey(judgeCode);
if (record != null) {
record.setValue("online", true);
record.acceptChanges();
}
});
socket.on("disconnect", () => {
console.log("Disconnect " + socket.id);
let judgeCode = SocketMappingCache.removeSocketID(socket.id);
let record = votingSchema.judgeModel.records.findByPrimaryKey(judgeCode);
if (record != null) {
record.setValue("online", false);
record.acceptChanges();
}
});
socket.on("nextCountry", (nextRunningCountry) => {
// Check if is admin
let runningOrder = nextRunningCountry.runningCountry;
votingSchema.runningCountry = runningOrder;
let countryRecord = votingSchema.countryModel.records.findByFieldName("runningOrder", runningOrder);
if (countryRecord != null) {
countryRecord.setValue("votingStatus", nextRunningCountry.votingStatus);
let data = {nextRunningCountry : countryRecord.serializeForDisplay()};
let response = SocketIOResponse.create(data, "Next country is %s", countryRecord.getValue("name"));
io.sockets.emit("nextCountry", response.toJSON());
}
});
socket.on("votingStatus", (votingStatus) => {
let countryRecord = votingSchema.countryModel.records.findByPrimaryKey(votingStatus.countryCode);
countryRecord.setValue("votingStatus", votingStatus.votingStatus);
countryRecord.acceptChanges();
let data = {votingStatus : votingStatus.votingStatus, country : countryRecord.serializeForDisplay()};
let response = SocketIOResponse.create(data, "Voting status for %s is now %s", countryRecord.getValue("name"), votingStatus.status);
socket.broadcast.emit("votingStatus", response.toJSON());
});
socket.on("general", (generalResponse) => {
let response = SocketIOResponse.create(generalResponse, generalResponse.message);
socket.broadcast.emit("general", response.toJSON());
});
});
return io;
}
return {
getSocketIO: function (server = null) {
if (ioInstance == null && server != null) {
ioInstance = createSocketIO(server);
}
return ioInstance;
},
sendVote: function(judgeRecord, countryRecord, points) {
let countryName = countryRecord.getValue("name");
let judgeName = judgeRecord.getValue("name");
let data = {voting : {judge: judgeRecord.serializeForDisplay(), country: countryRecord.serializeForDisplay(), points: points}};
let response = SocketIOResponse.create(data, "%s has voted %s points for %s", judgeName, points, countryName);
this.getSocketIO().sockets.emit("votes", response.toJSON());
},
};
})();
module.exports = {SocketIO};