-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (117 loc) · 3.75 KB
/
index.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const path = require('path');
const express = require('express')
const app = express();
// settings
app.set('port', process.env.PORT || 3000)
// Static Files
app.use(express.static( path.join(__dirname) ) )
// Start Server
const server = app.listen(app.get('port'), () => {
console.log("Server Start at port " + app.get('port'))
})
var SocketIO = require('socket.io')
var io = SocketIO(server)
var games = {}
var users = {}
var map_size = [9,9]
new_map(19, 19)
io.on('connection', (socket) => {
send_data_rooms()
socket.on('new_game', (username) => {
var key;
do{ key = new_game_key() } while (games[key])
games[key] = [[username],{}]
users[socket.id] = key
send_data_rooms()
socket.join(key)
io.sockets.in(key).emit('game:created',[key, username], socket.id)
})
socket.on('join_game', (data) => {
// si existe la sala
var key = data.key
if(games[key]){
// si no está llena
if(games[key][0].length != 2){
socket.join(key)
games[key][0].push(data.name)
// Crear sala
games[key][1] = {"turn": 0,
"map_size": [9,9],
"player_coors": [[9,17],[9,1]],
"data_map_walls": new_map(19,19),
"paddings": [10,10]
}
users[socket.id] = key
send_data_rooms()
io.sockets.in(key).emit('game:data',games[key], socket.id)
} else {
console.log("Full game");
}
} else {
console.log("This key does not exist");
}
})
socket.on('movement:player', (data) => {
var key = data["key"]
games[ key ][1]["player_coors"][data["player"]] = data["coords"]
games[key][1]["turn"] = (games[key][1]["turn"] == 0) ? 1 : 0
io.sockets.in(key).emit('get_data', games[ key ][1])
})
socket.on('movement:map', (data) => {
var key = data["key"]
games[ key ][1]["data_map_walls"] = data["map"]
games[key][1]["turn"] = (games[key][1]["turn"] == 0) ? 1 : 0
if(games[key][1]["paddings"][data["index"]] > 0) games[key][1]["paddings"][data["index"]] -= 1
io.sockets.in(key).emit('get_data', games[ key ][1])
})
socket.on('disconnect', () => {
var id = socket.id
var key_game = users[id]
io.sockets.in( key_game ).emit('game:deleted', "" , id)
for (var key in users) {
if(users[key] == key_game){
delete users[key]
}
}
delete games[ key_game ];
send_data_rooms()
});
})
function send_data_rooms(){
var rooms = []
for (var key in games) {
if(!games[key][0][1]){
rooms.push( [games[key][0][0], key] )
}
}
io.emit("load:rooms", rooms );
}
function new_game_key(){
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZQQQQQQQQQ0123456789"
var key = ""
for(var x = 0; x < 6; x++){
key += letters[Math.floor(Math.random() * 26)]
}
return key;
}
function new_map(xsize, ysize){
var arr = []
for(let x = 0; x < xsize; x++){
arr.push([])
if(x%2 == 0){
for(let y = 0; y < ysize; y++){
arr[x].push(0)
}
} else {
for(let y = 0; y < ysize; y++){
if(y%2 != 0){
arr[x].push(1)
} else {
arr[x].push(0)
}
}
}
}
return arr
}
//git push heroku master