-
Notifications
You must be signed in to change notification settings - Fork 0
/
ut3.js
161 lines (135 loc) · 3.37 KB
/
ut3.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
//var io = require('socket.io');
var util = require('util');
var io = require('socket.io-client');
var spawn = require('child_process').spawn;
// ############################################################
// Globals
// ############################################################
var Config = {
host: "ec2-67-202-34-109.compute-1.amazonaws.com",
port: 9000,
home: "/home/ubuntu/ut3-dedicated/Binaries",
//url: function(){ return "http://" + this.host + ":" + this.port; },
url: function(){
return "ws://" + this.host + ":" + this.port;
},
ut3: "ut3",
exe: function() {
return this.home + "/" + this.ut3 ;
},
arguments: [
"server",
],
level: 'DM-Heatray',
parameters: {
MinPlayers: 1,
password: "",
},
getParameters: function(){
var final = this.level;
for(var p in this.parameters){
final += "?" + p + "=" + this.parameters[p];
}
return final;
},
interval: 5000,
}
var socket = {};
var connected =false;
// ############################################################
// Callbacks
// ############################################################
function onUT3exit(code)
{
console.log("UT3 server exited with code "+code);
}
function onUT3stdout(data)
{
console.log("\tut3 out\t:" + data);
}
function onUT3stderr(data)
{
console.log("\tut3 err\t:" + data);
}
// Start the UT3 server
function onStart(data)
{
console.log('Received start request.');
console.log(data);
var args = Config.arguments ;
// If we specified a leve
var level ;
if(data.level)
level = data.level;
else
level = Config.level;
//Configure UT3 server parameters
Config.parameters.password = generateGUID();
args.push(Config.getParameters());
var ut3 = spawn(Config.exe(),args);
// reguster ccallbacks
ut3.on('exit',onUT3exit);
ut3.stderr.on('data',onUT3stderr);
ut3.stdout.on('data',onUT3stdout);
console.log('Started game server process');
//Configure the return message
var message = {
password: Config.password,
}
socket.emit('started',message);
}
function onDisconnect(data)
{
console.log('Disconnected');
var connected=false;
}
function onConnect(data)
{
console.log('Connected to ' + Config.url());
connected=true;
}
// Start the UT3 server
function execCB (error,stdout,stderr)
{
console.log(stdout);
}
// Attempt to connect to the server
function connect(){
console.log('Attempting to connect to ' + Config.url());
socket = new io.connect(Config.url());
socket.on('start',onStart);
socket.on('disconnect',onDisconnect);
socket.on('connect',onConnect);
}
// ############################################################
// Utility functions
// ############################################################
/**
* Generate GUIDs for conferences
*/
function generateGUID() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
function generateArgString(level,args){
var final = level;
for(var arg in args){
final += "?" + args[arg];
}
return final;
}
// Think
// Check the connection and attempt to reconnect if disconnected
function think()
{
if(!connected)
connect();
}
// ############################################################
// "Main"
// ############################################################
// Check the connection to the sever and attempt to connect
// on a timer
setInterval(think,Config.interval);