-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanager.js
73 lines (64 loc) · 1.43 KB
/
manager.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
/*
Trigon.io (manager)
� & � iO Ninja
*/
//Setup socket.io and listen on port 3000
var express = require("express");
var app = express();
var server = require("http").Server(app);
var cors = require("cors");
var settings = require("./settings.json");
server.listen(3000);
app.use(cors());
var ins = [];
var usedPorts = [];
var totalPlayers = 0;
var Ins = function(client,port) {
this.io = client;
this.port = port;
this.players = 0;
this.io.on("players",function(n) {
var lastPlayers = this.players;
this.players = n;
totalPlayers += this.players - lastPlayers;
console.log(totalPlayers + " players");
}.bind(this));
this.io.on("disconnect",function() {
ins.splice(ins.indexOf(this),1);
}.bind(this));
this.io.emit("start",this.port);
};
app.get("/ping",function(request,response) {
which = -1;
for (i in ins) {
if (ins[i].players < settings.insMax) {
which = i;
break;
};
};
if (which > -1) {
response.send(String(ins[which].port));
} else {
response.send("0");
};
});
app.get("/players", function(request,response) {
response.send(String(totalPlayers));
});
var io = require("socket.io").listen(2999);
io.on("connection",function(client) {
client.on("auth",function(k) {
if (k == settings.authKey) {
found = false;
pc = 3000;
while (!found) {
pc++;
if (!ins.find(function(o){return o.port == pc;})) {
found = true;
p = pc;
};
};
ins.push(new Ins(client,p));
};
});
});