-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
245 lines (235 loc) · 6.95 KB
/
main.ts
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
import Initializer from "./Module/Initializer";
import {CmdHandlerType} from "./Module/interface";
import inquirer, {Question} from "inquirer";
import downloader from "./Module/Downloader";
import DBManager from "./Module/DBManager";
import ServerManager from "./Module/ServerManager";
let server!: ServerManager;
interface HandlerType {
[i: string]: (args: string) => Promise<Number>;
}
const entryQuestion = [
{
type: "list",
name: "entry",
message: "请选择操作: ",
choices: [
{
value: CmdHandlerType.choiceName.ADD_A_SERVER,
name: "添加并下载一个服务端"
},
{
name: "选择并开启一个服务器",
value: CmdHandlerType.choiceName.RUN_A_SERVER
},
{
name: "退出",
value: CmdHandlerType.choiceName.EXIT
}
]
}
];
const addServerQuestion = [
{
type: "input",
name: "serverName",
message: "给你的新服务器取一个名字吧: "
},
{
type: "list",
name: "serverType",
message: "选择服务器类型: ",
choices: [
{
value: CmdHandlerType.choiceName.FABRIC,
name: "Fabric"
},
{
name: "原版",
value: CmdHandlerType.choiceName.VANILLA
}
]
}
];
const addServerHandler: HandlerType = {
"FABRIC": async (serverName: string) => {
try {
await downloader.requestFabricInfo();
} catch (e) {
console.log(e);
return 1;
}
const versionQuestion = [
{
type: "list",
name: "game",
message: "请选择一个游戏版本: ",
choices: downloader._fabricGameList
},
{
type: "list",
name: "installer",
message: "请选择一个Fabric Installer版本: ",
choices: downloader._fabricInstallerList
},
{
type: "list",
name: "loader",
message: "请选择一个Fabric Loader版本: ",
choices: downloader._fabricLoaderList
}
];
const baseAns = await inquirer.prompt(versionQuestion);
// 处理并寻找版本号对应的Fabric API版本
let gameVer = baseAns.game;
let apiVersions: Array<string>;
let temp = [];
let flag = false;
if(downloader._fabricAPIVersions.has(gameVer)) {
apiVersions = <Array<string>>downloader._fabricAPIVersions.get(gameVer);
} else {
temp = gameVer.split(".");
temp.splice(temp.length - 1, 1);
temp = temp.join(".");
apiVersions = <Array<string>>downloader._fabricAPIVersions.get(<string>temp);
flag = true;
}
const apiQuestion = [
{
type: "list",
name: "api",
message: "请选择一个Fabric API版本",
choices: apiVersions
}
];
let apiAns = (await inquirer.prompt(apiQuestion)).api;
if (flag) {
apiAns += `+${temp}`;
} else {
apiAns += `+${gameVer}`;
}
await downloader.getFabricServer(serverName, baseAns.game, baseAns.loader, baseAns.installer, apiAns);
return -1;
},
"VANILLA": async (serverName) => {
try {
await downloader.requestPureInfo();
const ques = [
{
type: "list",
message: "请选择一个版本: ",
choices: downloader._pureServerList,
name: "game"
}
];
const ans = (await inquirer.prompt(ques)).game;
await downloader.getPureServer(ans, serverName);
return -1;
} catch (e) {
console.log(e);
return 1;
}
}
}
async function addServer() {
try {
const answer = await inquirer.prompt(addServerQuestion);
console.log(answer);
if (answer.serverName) {
const checkRes = await DBManager.selectSingleServerInfo(answer.serverName);
if (checkRes.length > 0) {
console.log("服务器名已存在");
return 1;
}
return await addServerHandler[<string>answer.serverType](answer.serverName);
} else {
console.log("不合法的服务器名");
return -1;
}
} catch (e) {
console.log(e);
return 1;
}
}
async function runServer() {
try {
const list = (await DBManager.selectAllServerInfo()).map(i => {
return {
name: i.server_name,
value: i
}
});
if (list.length) {
const ans = (await inquirer.prompt({
type: "list",
name: "serverN",
message: "请选择一个服务器",
choices: list
})).serverN;
server = new ServerManager(ans.server_name, ans.dir);
await server.run();
return 1;
} else {
console.log("您大概还没有安装MC服务端");
return -1;
}
} catch (e) {
console.log(e);
return 1;
}
}
function* questionGenerator() {
let answer = <string>(yield entryQuestion);
while(1) {
let target;
switch (answer) {
case CmdHandlerType.choiceName.RUN_A_SERVER: {
target = runServer;
} break;
case CmdHandlerType.choiceName.ADD_A_SERVER: {
target = addServer;
} break;
default:
target = entryQuestion;break;
}
if (answer === CmdHandlerType.choiceName.EXIT) {
break;
} else {
answer = yield target;
}
}
yield 0;
}
async function main() {
await Initializer.init();
process.on("error", (e) => {
console.log(e);
});
}
main().then(async () => {
try {
let status = -1;
let ans = "";
const gen = questionGenerator();
while (1) {
if (status < 0) {
const question = <Array<Question>>gen.next("entry").value;
ans = (await inquirer.prompt(question)).entry;
if (ans === CmdHandlerType.choiceName.EXIT) {
status = 1;
} else {
status = 0;
}
} else if (!status) {
const operation = <Function>gen.next(ans).value;
status = await operation();
} else if (status){
break;
}
}
} catch (e) {
console.log(e);
}
}, (err) => {
console.log(err);
});