-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
162 lines (133 loc) · 3.23 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
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
/*
* Node libs
*/
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
mongoose = require('mongoose');
/*
* Server libs
*/
require('./libs/misc');
var Settings = require('./libs/Settings') ,
DBManager = require('./libs/DBManager'),
User = require('./libs/User'),
Debug = require('./libs/Debug'),
World = require('./libs/World'),
Server = this;
/*
* This is a example of how you would include a js file from the client.
*
* So you do not need to write two snake classes.
*
* Snake = require(__dirname +'/public/js/Snake');
*/
/*
* Vars
*/
var num_users = 0;
var user_ids = 1000;
var users = new Array();
var EnvironmentObjects = new Array();
var MiniSnakes = new Array();
var gameScore = 0;
var currentGameTime = 0;
var RedTeam = [];
var BlueTeam = [];
var d = new Debug();
/*
* Express and SocketIO config
*/
server.listen(Settings.PORT);
app.configure(function () {
app.use(express.static(__dirname + '/public'));
});
app.get('/', function (req, res, next) {
res.render('public/index.html');
});
console.log('Server running at http://'+Settings.HOST+':'+Settings.PORT+'/');
dbm = new DBManager(Settings.MONGO);
io.set('log level', Settings.DEBUGLEVEl);
io.sockets.on('connection', function (socket) {
++num_users;
var user = new User(socket, Server.PlayerEvent, user_ids++);
d.log(1,'User '+user.userID+' at ('+socket.handshake.address.address+') connected!');
users.push(user);
});
/*
* Server Functions
*/
Server.CreateWorld = function()
{
world = new World();
};
Server.EndGame = function()
{
};
Server.StartGame = function()
{
};
Server.UpdateTimer = function()
{
};
Server.PlayerEvent = function(event)
{
//console.log("Player Event: ", event.type);
var user = event.user,
snake = user.getSnake();
switch(event.type) {
case 'team':
var blueTeamLength = BlueTeam.length,
redTeamLength = RedTeam.length,
team;
if (redTeamLength > blueTeamLength) {
team = 'Blue';
BlueTeam.push(user);
} else {
team = 'Red';
RedTeam.push(user);
}
snake.changeTeam(team);
break;
case 'intro':
user.sendIntroPacket(world.AddUser(user));
user.sendAddEnvironmentPacket(world.surroundingEnvironment(snake));
//Send other snakes to the user
var otherSnakes = world.surroundingSnakes(snake);
user.sendAddSnakePacket(otherSnakes);
// user.sendPlayerUpdate(otherSnakes);
//Send user to the other snakes
user.broadcastAddSnake();
// user.broadcastPlayerUpdate();
break;
case 'disconnect':
d.log(1,'User '+event.user+' has disconnected!');
// Remove the disconnected user
user.broadcastRemoveSnake();
event.user.remove();
switch (snake.team) {
case 0:
var team = RedTeam;
case 1:
var team = BlueTeam;
}
team.remove(user);
users.remove(user);
//--num_users;
default:
}
};
/*
* Init functions
*/
console.log("Creating World ...");
this.CreateWorld();
console.log("Starting Game ...");
this.StartGame();
function update() {
world.update.call(world, users);
//process.nextTick(update); // node 0.8
setImmediate(update); // node 0.10
}
update();