@Helixable/

Multiplayer Tag v1.0

Express

No description

fork
loading
Files
  • index.js
  • client
  • package-lock.json
  • package.json
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
var express = require('express');
var app = express();
var serv = require('http').Server(app);
var puser = "Default"
app.get('/', function(req, res) {
	res.sendFile(__dirname + '/client/index.html');
});
app.use('/client', express.static(__dirname + '/client'));
serv.listen(process.env.PORT);
console.log("Server started");
var SOCKET_LIST = {};
var PLAYER_LIST = {};
//^^These are the lists that keep track of the players and connected clients

function checkIfIt() {
  if (Object.keys(PLAYER_LIST).length === 1) {
    for (var i in PLAYER_LIST) {
      if(PLAYER_LIST[i].username !== "Default") {
        PLAYER_LIST[i].it = true;
      }
    }
    } else {
      var anyBody = false;
      for (var i in PLAYER_LIST) {
        if (PLAYER_LIST[i].it) {
          if(PLAYER_LIST[i].username !== "Default") {
            anyBody = true;
          }
        }
      }
      if (!anyBody) {
        var picked = false;
        for (var i in PLAYER_LIST) {
          if (!picked) {
            if(PLAYER_LIST[i].username !== "Default") {
              PLAYER_LIST[i].it = true;
              picked = true;
            }
          }
        }
      }
    }
}
var Player = function(id) {
	var self = {
		x: 1000,
		y: 5000,
		maxx: 0,
		maxy: 0,
    admin: false,
		username: puser,
    usrlength: 0,
		width: 10,
		height: 10,
		id: id,
    kicked: false,
    kicker: "",
    kmessage: "",
    message: "",
		number: id,
		pressingRight: false,
		pressingLeft: false,
		pressingUp: false,
		pressingDown: false,
    pressingKick: false,
		maxSpd: 4,
		it: false,
		canBeIt: true
	};
	self.updatePosition = function() {
		if (self.pressingRight) self.x += self.maxSpd;
		if (self.pressingLeft) self.x -= self.maxSpd;
		if (self.pressingUp) self.y -= self.maxSpd;
		if (self.pressingDown) self.y += self.maxSpd;
		//^^If any of the keys are pressed down it moves the player in a direction
		//Collisions
		if (self.x + self.width > self.maxx) {
			self.x = self.maxx - self.width;
		}
		if (self.x < 0) {
			self.x = 0;
		}
		if (self.y + self.height > self.maxy) {
			self.y = self.maxy - self.height;
		}
		if (self.y < 0) {
			self.y = 0;
		}
		//^^If the player is out of bounds set him to the bounds
		if (self.it) {
      //HERE
			for (var i in PLAYER_LIST) {
				if (PLAYER_LIST[i].it) {
          if (PLAYER_LIST[i].it !== self.it) {
            PLAYER_LIST[i].it = false
				  }
				}
			}
      if (self.admin) {
        self.maxSpd = 8;
      }else{
        self.maxSpd = 6;
      }
			self.canBeIt = false;
			self.right = self.x + self.width;
			self.bottom = self.y + self.height;
			//^^The top and left values are just self.x and self.y
			for (var i in PLAYER_LIST) {
				if (PLAYER_LIST[i].id !== self.id) {
					var other = {
						x: PLAYER_LIST[i].x,
						y: PLAYER_LIST[i].y,
						right: PLAYER_LIST[i].x + PLAYER_LIST[i].width,
						bottom: PLAYER_LIST[i].y + PLAYER_LIST[i].height
					};
					if (self.right > other.x && self.x < other.right && self.bottom > other.y && self.y < other.bottom) {
						if (PLAYER_LIST[i].canBeIt) {
							PLAYER_LIST[i].it = true;
							self.it = false;
              if (self.admin) {
                self.maxSpd = 25;
              }else{
                self.maxSpd = 6;
              }
							self.canBeIt = false;
							self.setCan(1000);
						}
					}
				}
			}
		}
	};
	self.setCan = (time) => {
		setTimeout(() => {
			self.canBeIt = true;
		}, time);
	};
	return self;
};
//^^Function for generating players
var io = require('socket.io')(serv, {});
io.sockets.on('connection', function(socket) {
	socket.id = Math.random();
	SOCKET_LIST[socket.id] = socket;
	var player = Player(socket.id);
	PLAYER_LIST[socket.id] = player;
	//On connection adds the player and client to the lists of players and clients
	if (Object.keys(PLAYER_LIST).length === 1) {
		PLAYER_LIST[socket.id].it = true;
	}
	socket.on('disconnect', function() {
    if(player.username !== "Default") {
      console.log(player.username + " has left");
    }
		delete SOCKET_LIST[socket.id];
		delete PLAYER_LIST[socket.id];
		//When the client disconnects they get removed from the lists
		if (Object.keys(PLAYER_LIST).length === 1) {
			for (var i in PLAYER_LIST) {
				PLAYER_LIST[i].it = true;
			}
		} else {
			var anyBody = false;
			for (var i in PLAYER_LIST) {
				if (PLAYER_LIST[i].it) {
					anyBody = true;
				}
			}
			if (!anyBody) {
				var picked = false;
				for (var i in PLAYER_LIST) {
					if (!picked) {
						PLAYER_LIST[i].it = true;
						picked = true;
					}
				}
			}
		}
	});

	socket.on('newMaxPos', function(data) {
		player.maxx = data.mw
		player.maxy = data.mh
	});
  function admin(){
    player.admin = true
    player.maxSpd = 8
    console.log("Admin Logged In")
  }
  function sendUsr(usr){
    player.username = usr;
    player.usrlength = usr.length*4.5
    player.health = 100
    player.x = Math.floor(1000 * Math.random())
	  player.y = Math.floor(1000 * Math.random())
    var date = new Date()
    console.log(player.username + " has joined at "+ (date.getHours()-4)+":"+date.getMinutes()+" PM");
  }
	socket.on('keyPress', function(data) {
    checkIfIt()
		if(data.inputId === 'left') player.pressingLeft = data.state;
		else if (data.inputId === 'right') player.pressingRight = data.state;
		else if (data.inputId === 'up') player.pressingUp = data.state;
		else if (data.inputId === 'down') player.pressingDown = data.state;
    if (data.inputId === 'kick') player.pressingKick = data.state;
	});
  socket.on('kickUsr', function(dat) {
    var b = dat.user;
    console.log(dat.sndr+" kicked ");
    /*
    PLAYER_LIST[dat.user].kicked = true;
    PLAYER_LIST[dat.user].kmessage = dat.msg;
    PLAYER_LIST[dat.user].kicker = dat.sndr;
    PLAYER_LIST[dat.sid].pressingKick = false;
    */
	});
  socket.on('checkAdmin', function(data){
    if(data.inp.substring(0,3) === "*A*") {
      admin()
      sendUsr("[Admin]"+data.inp.substring(3))
    }else{
      sendUsr(data.inp)
    }
  });
});

setInterval(function() {
	var pack = [];
	for (var i in PLAYER_LIST) {
		var player = PLAYER_LIST[i];
		player.updatePosition();
		pack.push({
			username: player.username,
			x: player.x,
			y: player.y,
      kicked: player.kicked,
      kicker: player.kicker,
      kmessage: player.kmessage,
      pressingKick: player.pressingKick,
			health: player.health,
			number: player.number,
			it: player.it,
			maxx: player.maxx,
      usrlength: player.usrlength,
			maxy: player.maxy,
      admin:player.admin,
		});
	}
	//^^Loops through all the players in the list and pushes their data to an array
	for (var n in SOCKET_LIST) {
		var socket = SOCKET_LIST[n];
		socket.emit('newPositions', pack);
	}
	//^^Loops through all the clients in the list and sends them the positions of all the players
}, 1000 / 40);