-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp2.js
261 lines (201 loc) · 5.49 KB
/
tcp2.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
256
257
258
259
260
261
/* Copyright (c) 2006 YourNameHere
See the file LICENSE.txt for licensing information. */
var net = require('net');
var fs = require('fs');
var people = [];
var config = [];
var planets = {};
var cfgFile = "cfg.txt"; //config file.
var pfile = "planets.txt";
//load first
//read server config file into var config = []; - will be JSON text file.
/*
JSON in file:
{
server_name : "My Server Name Here",
server_version_in_use : 0.0.1, //will be what ever version of NODEJS version of Mekwars server
server_owner : "Heath Carruthers",
server_contact : "fireflyx@bigpond.net.au",
server_era : "AoW|SL|SCW|ECL|GECL|CI|FCCW|JIHAD|DA", (refer to below for explination of accronyms)
server_status : "Active|BETA|Closed_Alpha|Closed_BETA|Closed",
server_planet_data_file : "planets.xml" - perhaps change to JSON as well? - going to be interesting how to handle this - BIG FILE data stream
AoW = Age of War
SL = Star League
SCW = Succession Wars
ECL = Early Clan Years
GECL = Golden Era Clan
CI = Clan Invasion
FCCW = Fed Com Civil War
Jihad
DA = Dark Ages
}
*/
var readSrvConfig = function(serverConfigFile)
{
fs.readFile(serverConfigFile, function(err, data) {
/*
var tmp_cfg_data = data.toString().split("\n");
//config file is read correctly.
for(var i = 0; i < tmp_cfg_data.length; i++)
{
console.log(tmp_cfg_data[i]+"\n");
}
*/
var tmp_cfg_data = JSON.parse(data.toString());
console.log(tmp_cfg_data.server_name+ " - "+ tmp_cfg_data.server_owner);
});
};
var readBuildTable = function(username, buildTableName, socket)
{
fs.readFile(buildTableName+".txt", function(err, data) {
var buildTable = JSON.parse(data.toString());
return buildTable;
});
};
/*
example planets file:
{
"planets" : [
"name" : "Terra",
"x" : "0",
"y" : "0",
"original_owner" : "CS",
"current_owner" : "WOB",
"control_points" : "100"
"factory_file" : "terra_factory_list.txt",
]
}
*/
var planetsMaker = function(planetsFile) {
fs.readFile(planetsFile, function(err, data) {
//will be too big overall, might make it smaller initialy
if(err) {
console.log(err);
}
planets.uni = JSON.parse(data.toString());
console.log(planets.uni[0].name);
});
};
readSrvConfig(cfgFile);
planetsMaker(pfile);
//actual server
net.createServer(function (s) {
s.setEncoding('utf8');
var user = {};
user.socket = s;
user.id = s.remotePort+s.remoteAddress;//+ "" + Math.random(Math.floor(1000));
user.addy = s.remoteAddress;
people.push(user);
s.write("Welcome to the server\n\r");
broadcast("b:User Connected>"+user.id+"\n\r",s);
s.on('data', function(d) {
var data = d.split(":");//JSON.parse(d);
if(data[0] == "broadcast")
{
broadcast(d, s);
console.log(d);
} else if(data[0] == "pm")
{
//find userid.
var info = data[1].split("|");
for (var x = 0; x < people.length; x++)
{
if(people[x].id == info[0])
{
people[x].socket.write("PM: "+user.id+"> "+info[1]);
console.log("PM: "+user.id+ " > "+info[0]+ " : "+info[1]);
}
}
} else if(data[0] == "setadmin")
{
//console.log("setadmin");
for(var l = 0; l < people.length; l++)
{
var tmpsp = data[1].split("|");
//console.log(tmpsp[0]);
if(people[l].id == tmpsp[0])
{
people[l].adminlevel = tmpsp[1];
console.log(people[l]);
}
}
} else if(data[0] == "unitmake")
{
var unitData = data[1].split("|");
generateUnits("casperionx", s, unitData[0], unitData[1]);
} else if(data[0] == "purge_planets")
{
planets = {}; //kill planets DB
s.write(planets.toString());
console.log(planets);
}
}) ;
s.on('end', function() {
//var index = people.indexOf(s);
for(var i = 0; i < people.length; i++)
{
if(people[i].socket === s)
{
people.splice(i, 1);
}
}
});
}).listen(8125);
var broadcast = function (msg,socket)
{
for (var i = 0; i < people.length; i++)
{
//if(people[i].socket === socket) continue;
people[i].socket.write(socket.remotePort+socket.remoteAddress + '> ');
var tmpmsg = msg.split(":");
people[i].socket.write(tmpmsg[1]);
}
};
var generateUnits = function(username, socket, lc, mc)
{
unitsListing = {
light : [
{name : "commando", weight : 20, filename : "Commando COM-2D.MTF"},
{name : "flea", weight : 20, filename : "Jenner FLE-4.MTF"},
{name : "jenner", weight : 30, filename : "Jenner JR7-K.MTF"},
{name : "javelin", weight : 35, filename : "Javelin JVN-10F.MTF"}
],
medium : [
{name : "enforcer", weight : 50, filename : "Enforcer ENF-4R.MTF"},
{name : "dervish", weight : 55, filename : "Dervish DV-6M.MTF"},
{name : "wolverine", weight : 55, filename : "Wolverine WVR-6R.MTF"},
{name : "Hoplite", weight : 55, filename : "Hoplite HOP-4B.MTF"},
{name : "Gladiator", weight : 55, filename : "Gladiator GLD-4R.MTF"}
]
};
//build army:
var lightmechs = [];
var mediummechs = [];
//lightmech input
for (var i = 0; i < lc; i++)
{
var index_l = Math.floor(Math.random() * lc);
lightmechs.push(unitsListing.light[index_l]);
console.log(index_l);
}
//mediummech input
for (var j = 0; j < mc; j++)
{
var index_m = Math.floor(Math.random() * mc);
mediummechs.push(unitsListing.medium[index_m]);
console.log(index_m);
}
for (var cc = 0; cc < people.length; cc++)
{
if(people[cc].socket === socket)
{
people[cc].mechs = {
light : lightmechs,
medium : mediummechs
}
socket.write(JSON.stringify(people[cc].mechs));
}
}
console.log(lightmechs + " " + mediummechs);
};
//var