This repository was archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscoreboard.js
More file actions
56 lines (46 loc) · 1.4 KB
/
scoreboard.js
File metadata and controls
56 lines (46 loc) · 1.4 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
import express from "express";
import io from "socket.io";
import controllerModel from "./controllerModel";
import RankingMaker from "./rankingMaker";
import eventPublisher from "./publisher";
let scoreboardInstance = null;
function Scoreboard(port) {
if (scoreboardInstance !== null) {
return scoreboardInstance;
}
scoreboardInstance = this;
this.app = express();
this.server = require("http").Server(this.app);
this.io = require("socket.io")(this.server);
this.io.origins(`localhost:${port}`);
this.app.use(express.static("scoreboard"));
this.server.listen(port, () => {
console.log(`score is listening on port ${port}`);
});
this.currentRanking = null;
this.rankingMaker = new RankingMaker();
this.sockets = [];
this.io.on("connection", socket => {
console.log("a scoreboard connected.");
this.sockets.push(socket);
if (this.currentRanking !== null) {
socket.emit("data", this.currentRanking);
}
});
eventPublisher.on("updatedHp", () => {
this.updateRanking();
});
eventPublisher.on("updateLink", () => {
this.updateRanking();
});
eventPublisher.on("color", () => {
this.updateRanking();
});
}
Scoreboard.prototype.updateRanking = function() {
this.currentRanking = this.rankingMaker.make(controllerModel.controllers);
this.sockets.forEach(socket => {
socket.emit("data", this.currentRanking);
});
};
module.exports = Scoreboard;