-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (154 loc) · 3.78 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Use ES6
"use strict";
const express = require('express')
const bodyParser = require('body-parser');
const app = express()
const http = require('http').Server(app);
const path = require('path');
const io = require('socket.io')(http);
const _ = require('lodash');
const session = require('express-session')
app.use(
session({
secret: 'ez',
name: "uniqueSessionID",
saveUninitialized: false,
})
);
const port = 40030// Dejalo en este puerto por que asilo deje configurado, no quiero que
// Los bots esten todo el rato escaneando el puerto 3000
http.listen(port, function() {
console.log('listening on *:'+port);
});
const fs = require('fs')
const cors = require('cors')
var emotes = require('./routes/emotes');
var badges = require('./routes/badges');
var points = require('./routes/points');
var bonfires = require('./routes/bonfires');
var chatters = require('./routes/chatters');
var token = require('./routes/token');
var dashboard = require('./routes/dashboard');
var profile = require('./routes/profile');
// Parse URL-encoded bodies (as sent by HTML forms)
app.use(express.urlencoded());
// Parse JSON bodies (as sent by API clients)
app.use(express.json());
app.use(bodyParser.json());
app.use(cors())
app.use('/api/emotes', emotes);
app.use('/api/badges', badges);
app.use('/api/points', points);
app.use('/api/bonfires', bonfires);
app.use('/api/chatters', chatters);
app.use('/api/token', token);
app.use('/dashboard', dashboard);
app.use('/api/profile', profile);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.get("/login", (req, res) => {
res.render("pages/login");
});
app.post(
"/authenticate",
bodyParser.urlencoded(),
(req, res, next) => {
console.log(req.body.password)
console.log(process.env.PASSWORD)
if (req.body.password == process.env.PASSWORD) {
next();
} else res.sendStatus(401);
},
(req, res) => {
req.session.loggedIn = true;
res.redirect("/dashboard/emotes");
}
);
const Snake = require('./games/snake/snake');
const Apple = require('./games/snake/apple');
// ID's seed
let autoId = 0;
// Grid size
const GRID_SIZE = 60;
// Remote players 🐍
let players = [];
// Apples 🍎
let apples = [];
/*
* Serve client
*/
app.use(express.static(path.join(__dirname, 'client')))
app.get('/minigames/snake/:nickname', function(req, res) {
const nickname = req.params.nickname
res.render('pages/snake', {
nickname: nickname
});
});
app.get('/minigames/agario', function(req, res) {
res.sendFile(path.join(__dirname, 'views/agario.html'))
});
/*app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});*/
/*
* Listen for incoming clients
*/
io.on('connection', (client) => {
let player;
let id;
client.on('auth', (opts, cb) => {
// Create player
id = ++autoId;
player = new Snake(_.assign({
id,
dir: 'right',
gridSize: GRID_SIZE,
snakes: players,
apples,
io,
client
}, opts));
players.push(player);
// Callback with id
cb({ id: autoId });
});
// Receive keystrokes
client.on('key', (key) => {
// and change direction accordingly
if(player) {
player.changeDirection(key);
}
});
// Remove players on disconnect
client.on('disconnect', () => {
_.remove(players, player);
});
});
// Create apples
for(var i=0; i < 50; i++) {
apples.push(new Apple({
gridSize: GRID_SIZE,
snakes: players,
apples
}));
}
// Main loop
setInterval(() => {
players.forEach((p) => {
p.move();
});
io.emit('state', {
players: players.map((p) => ({
x: p.x,
y: p.y ,
id: p.id,
nickname: p.nickname,
points: p.points,
tail: p.tail
})),
apples: apples.map((a) => ({
x: a.x,
y: a.y
}))
});
}, 500);