-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
98 lines (79 loc) · 2 KB
/
app.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
var express = require("express")
var app = express()
var server = require("http").Server(app)
var io = require("socket.io")(server)
app.use("/static", express.static(__dirname + "/static"))
app.use("/node_modules", express.static(__dirname + "/node_modules"))
app.get("/", function(req, res) {
res.sendFile(__dirname + "/views/splashpage.html")
})
app.get("/game", function(req, res) {
res.sendFile(__dirname + "/views/game.html")
})
var scoreboard = []
var playerCount = 0
var id = 0
var nicknames = []
// GAME LOGIC
io.on("connection", function(socket) {
playerCount++
id++
/* GAME LOGIC */
setTimeout(function startGame() {
socket.emit("connectedGame", {
playerId: id
})
io.emit("count", {
playerCount: playerCount
})
}, 1500)
socket.on("disconnect", function() {
playerCount--
io.emit("count", {
playerCount: playerCount
})
})
socket.on("updatePlayer", function(data) {
socket.broadcast.emit("updatedActor", data)
})
socket.on("pushScore", function(data) {
scoreboard.push({
playerID: data["playerID"],
score: data["score"]
})
if (scoreboard.length == 22) {
scoreboard.splice(1, 1)
}
refreshScoreboard()
})
function refreshScoreboard() {
io.emit("updatedScoreboard", {
scoreboard: scoreboard
})
}
setTimeout(function initializeScoreboard() {
io.emit("updatedScoreboard", {
scoreboard: scoreboard
})
}, 1500)
/* CHAT LOGIC */
socket.on("new user", function(data, callback) {
if (nicknames.indexOf(data) != -1) {
callback(false)
} else {
callback(true)
socket.nickname = data
nicknames.push(socket.nickname)
//io.sockets.emit("changePlayerID", socket.nickname) // <--- pick up from here
}
})
socket.on("send message", function(data) {
io.sockets.emit("new message", {
msg: data,
nick: socket.nickname,
playerID: id
})
})
})
server.listen(8080)
console.log("Listening on http://localhost:8080/")