-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
134 lines (103 loc) · 3.52 KB
/
server.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// import needed packages
const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http)
// const bodyParser = require("body-parser")
// allow cors?
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "YOUR-DOMAIN.TLD")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
// static files are stored in 'public' folder
app.use(express.static('static'))
// Data object (needs to be moved to database)
let gameData = {
"defaultGame": []
}
// routes for Home, join and host page
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
app.get('/index.html', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
app.get('/main.js', (req, res) => {
res.sendFile(__dirname + '/main.js')
})
app.get('/player.js', (req, res) => {
res.sendFile(__dirname + '/player.js')
})
app.get('/world.js', (req, res) => {
res.sendFile(__dirname + '/world.js')
})
app.get('/preloadSprites.js', (req, res) => {
res.sendFile(__dirname + '/preloadSprites.js')
})
app.get('/socket.js', (req, res) => {
res.sendFile(__dirname + '/socket.js')
})
app.get('/platform.js', (req, res) => {
res.sendFile(__dirname + '/platform.js')
})
app.get('/water.js', (req, res) => {
res.sendFile(__dirname + '/water.js')
})
app.get('/p5.js', (req, res) => {
res.sendFile(__dirname + '/p5.js')
})
app.get('/p5.sound.js', (req, res) => {
res.sendFile(__dirname + '/p5.sound.js')
})
app.get('/landing.html', (req, res) => {
res.sendFile(__dirname + '/landing.html')
})
app.get('/node_modules/socket.io/client-dist/socket.io.js', (req, res) => {
res.sendFile(__dirname + '/node_modules/socket.io/client-dist/socket.io.js')
})
app.get('/player2_gewonnen.html', (req, res) => {
res.sendFile(__dirname + '/player2_gewonnen.html')
})
app.get('/player1_gewonnen.html', (req, res) => {
res.sendFile(__dirname + '/player1_gewonnen.html')
})
// function for all socket connections
io.on('connection', (socket) => {
// add new connection to game room
socket.join(socket.handshake.query["gameCode"])
console.log("New user, room = ", Array.from(socket.rooms)[1])
socket.on('newPlayer', (name) => {
console.log("New player ", name)
// const gameCode = Array.from(socket.rooms)[1]
// gameData[gameCode].push({"name":name})
io.emit("newPlayer", name)
})
socket.on('playerUpdate', (data) => {
// const gameCode = Array.from(socket.rooms)[1]
// data = JSON.parse(data)
// if(gameData[gameCode].length === 0){
// gameData[gameCode].push(data)
// } else {
// for(let i = 0; i < gameData[gameCode].length; i++){
// if(gameData[gameCode][i]["name"] == data["name"]){
// gameData[gameCode].splice(i)
// }
// gameData[gameCode].push(data)
// console.log(gameData)
// }
// // gameData[gameCode].forEach(player => {
// // if(player["name"] == data["name"]){
// // gameData[gameCode][player["name"]]
// // console.log(gameData)
// // // console.log(JSON.parse(gameData[gameCode][player])["x"])
// // }
// // });
// }
// io.emit("playerUpdate", gameData[gameCode])
})
})
// start server
http.listen(3000, () => {
console.log('listening on *:3000')
})