-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.js
200 lines (179 loc) · 6.69 KB
/
console.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
var inquirer = require("inquirer");
var config = require("./config");
var utils = require("./utils");
var runner = require("./runner");
var request = require('request');
var _ = require("underscore");
inquirer.prompt([
{
type: "list",
name: "menu",
message: "Main Menu",
choices: [
"1: Start Game",
"2: Install new version",
]
}], function( answers ) {
switch(answers.menu.substr(0,1)){
case "1":
startGame();
break;
case "2":
chooseInstallVersionType();
break;
}
});
function startGame(){
config.get("versions", function(versions){
versions = versions || ["No Versions Exist"];
inquirer.prompt([{
type: "list",
name: "menu",
message: "Please select version to launch",
choices: versions
}], function( answers ) {
chooseGamestartConfig(answers.menu);
});
});
};
function chooseGamestartConfig(version){
config.get("gameconfigs", function(gameconfigs){
gameconfigs = gameconfigs || [];
gameconfigs.unshift("Default");
inquirer.prompt([{
type: "list",
name: "menu",
message: "Please select version to launch",
choices: gameconfigs
}], function( answers ) {
var gameconfig = answers.menu;
runner.startGame(version, (gameconfig == "Default")?"Default": "./gameConfigs/" + gameconfig + ".cfg");
});
});
};
function chooseInstallVersionType(){
inquirer.prompt([{
type: "list",
name: "menu",
message: "Choose New Version Type",
choices: [
"Current Release",
"Current Nightly",
"Previous Releases",
]
}], function( answers ) {
switch(answers.menu){
case "Current Release":
// check new version against previous version - http://finger.openttd.org/versions.txt
checkNewRelease();
break;
case "Current Nightly":
// check nightly version against previous version - http://finger.openttd.org/versions.txt
checkNightlyRelease();
break;
case "Previous Releases":
// show a list of previous versions - http://finger.openttd.org/branches.txt
showVersionList();
break;
}
});
};
function checkNewRelease(){
request('http://finger.openttd.org/versions.txt', function (error, response, body) {
if (!error && response.statusCode == 200) {
var currentversion = body.split(/\r?\n/)[0].split(/\t/)[0];
config.get("versions", function(versions){
// filter out nightly versions
versions = versions.filter(function(d){
return (!(d[0]=="r"));
});
versions.sort();
versions.reverse()
var newestInstalled = versions[0];
if (utils.compareVersionIDs(newestInstalled, currentversion)){
inquirer.prompt([{
type: "confirm",
name: "confirm",
message: "Install version " + currentversion + "?",
}], function( answers ) {
if (answers.confirm){
runner.install("/releases/" + currentversion);
}
});
} else {
console.log("Installed Version is not newer than online version!");
}
});
} else {
throw new Error("Unable to reach openttd server");
}
});
}
function checkNightlyRelease(){
request('http://finger.openttd.org/versions.txt', function (error, response, body) {
if (!error && response.statusCode == 200) {
var currentversion = body.split(/\r?\n/)[2].split(/\t/)[0];
config.get("versions", function(versions){
// filter out nightly versions
versions = versions.filter(function(d){
return (d[0]=="r");
});
versions.sort();
versions.reverse()
var newestInstalled = versions[0];
if (utils.compareVersionIDs(newestInstalled, currentversion)){
inquirer.prompt([{
type: "confirm",
name: "confirm",
message: "Install version " + currentversion + "?",
}], function( answers ) {
if (answers.confirm){
runner.install("/nightlies/" + currentversion);
}
});
} else {
console.log("Installed Version is not newer than online version!");
}
});
} else {
throw new Error("Unable to reach openttd server");
}
});
}
function showVersionList(){
request('http://finger.openttd.org/tags.txt', function (error, response, body) {
if (!error && response.statusCode == 200) {
var versiondata = body.split(/\r?\n/);
var versionlist = versiondata.map(function(item){
return item.split(/\t/)[2];
});
config.get("versions", function(installedversions){
// clear out the junk and installed versions
versionlist = versionlist.filter(function(item){
return (item);
});
// remove installed versions
versionlist = _.difference(versionlist, installedversions);
inquirer.prompt([{
type: "list",
name: "menu",
message: "Choose New Version Type",
choices: versionlist
}], function( answers ) {
var installversion = answers.menu;
inquirer.prompt([{
type: "confirm",
name: "confirm",
message: "Install version " + installversion + "?",
}], function( answers ) {
if (answers.confirm){
runner.install("/releases/" + installversion);
}
});
});
});
} else {
throw new Error("Unable to reach openttd server");
}
});
}