-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proxy.js
126 lines (98 loc) · 3.96 KB
/
Proxy.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
//
var Assert = require("assert");
var NMP = require("minecraft-protocol");
var Client = require("./Client.js");
var IdleServerInit = require("./IdleServer.js");
module.exports = Proxy;
function Proxy(){
this.ServerNMP = undefined;
this.numIdleServers = 3;
this.serverInfos = {};
this.clients = [];
this.usernameToClient = {};
}
Proxy.prototype.tick = function(){
for(Index in this.clients){
var Client = this.clients[Index];
Client.tick();
var Commands = Client.getCommands();
for(var i in Commands){
var Argv = Commands[i];
Argv.shift();
if(Argv[0] == 'quit'){
this.removeClient(Client.username);
}else if(Argv[0] == 'connect'){
var ServerInfo = this.serverInfos[Argv[1]];
var UserInfo = {"username":Argv[2]};
// Make sure we don't try to connect to a non-existant server
if(ServerInfo == undefined || UserInfo.username == undefined){
Client.warn("Invalid connect");
}else{
Client.addOutgoing(ServerInfo, UserInfo);
}
}else if(Argv[0] == 'switch'){
if(Client.isValidOutgoing(Argv[1], Argv[2])){
Client.setOutgoing(Argv[1], Argv[2]);
}else{
Client.warn("Invalid switch");
}
}else if(Argv[0] == 'disconnect'){
if(Client.isValidOutgoing(Argv[1], Argv[2])){
Client.endOutgoing(Argv[1], Argv[2]);
}else{
Client.warn("Invalid disconnect");
}
}
}
}
}
Proxy.prototype.listen = function(ServerSettings){
var ServerNMP = NMP.createServer(ServerSettings);
for(var i = 1; i <= this.numIdleServers; i++){
IdleServerInit({
"port": ServerSettings.port + i,
"online-mode": false,
"version": ServerSettings.version
});
this.addServer({
"name": "idle" + i,
"ip": "localhost",
"port": ServerSettings.port + i,
});
}
var Self = this;
ServerNMP.on('login', function(Client){ Self.addClient.bind(Self)(Client); });
this.ServerNMP = ServerNMP;
}
Proxy.prototype.addClient = function(IncomingClient){
if(this.usernameToClient[IncomingClient.username] == undefined){
// Player is not already active.
var LocalClient = new Client(IncomingClient);
LocalClient.addOutgoing(this.serverInfos["idle1"], {"username":IncomingClient.username});
LocalClient.setOutgoing("idle1", IncomingClient.username);
this.clients.push(LocalClient);
this.usernameToClient[LocalClient.IncomingClient.getUsername()] = LocalClient;
}else{
// Player has a client instance active.
this.usernameToClient[IncomingClient.username].setIncomingClient(IncomingClient);
}
}
Proxy.prototype.removeClient = function(Username){
this.usernameToClient[Username].end();
var ClientIndex = this.clients.indexOf(this.usernameToClient[Username]);
this.clients = this.clients.splice(ClientIndex, 1);
delete this.usernameToClient[Username];
}
Proxy.prototype.addServer = function(ServerInfo){
Assert(ServerInfo != undefined, "Server info should not be undefined!");
Assert(ServerInfo.name != undefined, "Server should have a name");
Assert(ServerInfo.ip != undefined, "Server should have an IP");
Assert(ServerInfo.port != undefined, "Server should have a port to connect to");
this.serverInfos[ServerInfo.name] = ServerInfo;
}
Proxy.prototype.addServerList = function(ServerList){
Assert(ServerList instanceof Array, "Server list should be an array of server infos");
for(var i = 0; i < ServerList.length; i++){
this.addServer(ServerList[i]);
}
}