-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
176 lines (123 loc) · 4.08 KB
/
server.js
File metadata and controls
176 lines (123 loc) · 4.08 KB
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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require("socket.io")(http);
var path = require('path');
var sanitizeHtml = require('sanitize-html');
var args = process.argv.slice(2);
console.dir(args);
//For including other JS files server side
var fs = require("fs");
function read(f) {
return fs.readFileSync(f).toString();
}
function include(f) {
eval.apply(global, [read(f)]);
}
include('./js/misc.js');
include('./js/compressing.js');
include('./js/quadTree.js');
include('./js/spatialHash.js');
include('./js/gameObjects.js');
include('./js/gameLogic.js');
var TARGET_TICK_RATE = 1000;
var TICKRATE = 1000/TARGET_TICK_RATE;
app.use("/gameDat", express.static(__dirname + '/gameDat'));
app.use("/css", express.static(__dirname + '/css'));
app.get('/js/compressing.js', function(req, res){
res.sendFile(__dirname + '/js/compressing.js');
});
app.get('/', function(req, res){
res.sendFile(__dirname + '/index2.html');
});
/*
app.get('/gameDat/badGuy.png', function(req, res){
res.sendFile(__dirname + '/gameDat/badGuy.png');
});
*/
io.on("connection",function(socket){
socket.on('join',function firstConnect(name){
//Put the socket in a group
socket.join(socket.id);
//Then send a message only to that group
//Send init info which is there id, and the current map status
io.sockets.in(socket.id).emit('join', {id: socket.id,towers:g.towers,base:g.base,mapWidth : g.mapWidth,mapHeight : g.mapHeight});
g.newPlayer(new Player(name,socket));
var address = socket.handshake.address;
console.log("Player " + name + " joined and was given id " + socket.id + " ip " + address);
data = {};
data.msg = name + " has joined";
data.usr = "Server";
io.emit('chat', data);
});
socket.on('turret',function(data){
//Has 3 things, id , x and y pos
for(var i = 0;i < data.length;i++){
g.toggleTower(data[i].id,data[i].x,data[i].y);
}
sendMapData();
});
socket.on('chat',function(data){
data.msg = sanitizeHtml(data.msg);
data.use = sanitizeHtml(data.usr);
io.emit('chat', data);
});
socket.on('disconnect', function () {
console.log("PlayerID left : " + socket.id);
var player = g.getPlayerName(socket.id);
data = {};
data.usr = "Server";
if(player == null){
data.msg = "Unkown player has left";
}else{
data.msg = player + " has left";
}
io.emit('chat', data);
});
});
http.listen(args[0], function(){
console.log('listening on *:' + args[0]);
});
g = new Game();
function sendMapData(){
g.updateMap = true;
}
g.init();
var lastTickTime = new Date().getTime();
//How to make the interval to work because javascript :/
setInterval(function(){
var currentTime = new Date().getTime();
//Check if we need to update everything
if(currentTime - lastTickTime > TICKRATE){
//We ticked changed the time we are waiting for now
lastTickTime = currentTime;
g.update(io)
if(g.badGuys.length < 1000){
var x = Math.floor(Math.random() * (g.mapWidth - 2)) + 1;
var y = Math.floor(Math.random() * (g.mapWidth - 2)) + 1;
var speed = 3*(Math.floor(Math.random()*50) + 150);
//x=0;
//y=0;
//Loop tillwe find a valid spot
while(!g.checkValidSpot(x,y)){
x = Math.floor(Math.random() * (g.mapWidth - 2)) + 1;
y = Math.floor(Math.random() * (g.mapWidth - 2)) + 1;
}
g.addBadGuy(x,y,speed,Math.floor(Math.random()*1000));
}
}
}, 1);
function updateMap( )
{
if(g != undefined){
g.base.x += 1;
if(g.base.x >= g.mapWidth){
g.base.x = 0;
g.base.y += 1;
if(g.base.y >= g.mapHeight){
g.base.y = 0;
}
}
sendMapData();
}
}