-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeRepository.js
More file actions
105 lines (96 loc) · 3.45 KB
/
Copy pathCodeRepository.js
File metadata and controls
105 lines (96 loc) · 3.45 KB
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
scriptLoadError = (scriptName) => game_log(`Error Loading Script: ${scriptName}`);
loadCode = (scriptName) => load_code(scriptName, () => scriptLoadError(scriptName));
const repoURL = "https://raw.githubusercontent.com/:user/:repo/:branch/";
function CodeFile(slot, name, extension = ".js") {
this.slot = slot;
this.name = name;
this.extension = extension;
}
getRepoUrl = (user, repo, branch) => {
return repoURL.replace(":user", user).replace(":repo", repo).replace(":branch", branch);
}
const CodeFiles = [
new CodeFile(1, "CodeRepository"),
new CodeFile(2, "Main"),
new CodeFile(3, "Constants"),
new CodeFile(4, "Config"),
new CodeFile(5, "Enemies"),
new CodeFile(6, "Inventory"),
new CodeFile(7, "Locations"),
new CodeFile(8, "SafeSay"),
new CodeFile(9, "Shop"),
new CodeFile(10, "Commands"),
new CodeFile(11, "Party"),
new CodeFile(12, "Targeting"),
new CodeFile(13, "Combat"),
new CodeFile(14, "StateMachine"),
new CodeFile(15, "ConsoleUtilities")
];
fetchCodeFromRemoteBranch = (user, repo, branch, version) => {
let recievedCount = 0;
parent.api_call("list_codes", {
callback: () => {
game_log(`Fetching updated code from remote branch ${branch}`);
CodeFiles.forEach(file => {
let request = new XMLHttpRequest();
let filePath = `${getRepoUrl(user, repo, branch)}${file.name}${file.extension}`;
request.open("GET", filePath);
request.onreadystatechange = () => {
if (request.readyState === 4 && request.status === 200) {
let data = {
name: file.name,
slot: file.slot,
code: request.responseText
};
parent.api_call("save_code", data);
game_log(`Recieved ${file.slot} - ${file.name}`);
recievedCount++;
if (recievedCount == CodeFiles.length) {
game_log(`Update to ${version} Completed. Starting Bot.`);
setVersion(version);
startBot();
}
}
};
request.send();
});
}
});
}
getVersionFromRemoteBranch = (user, repo, branch) => {
let version = getVersion();
game_log(`Checking for updated code from remote branch ${branch}`);
game_log(`Current Version: ${version}`);
let request = new XMLHttpRequest();
let filePath = `${getRepoUrl(user, repo, branch)}Version.txt`;
request.open("GET", filePath);
request.onreadystatechange = () => {
if (request.readyState === 4 && request.status === 200) {
let remoteVersion = request.responseText;
game_log(`Remote Version: ${remoteVersion}`);
if (remoteVersion !== version) {
fetchCodeFromRemoteBranch(user, repo, branch, remoteVersion);
} else {
game_log('Up To Date. Starting Bot.');
startBot();
}
}
};
request.send();
}
getVersion = () => {
let value = pget('VERSION');
if (value === undefined) {
return '';
}
return value;
}
setVersion = (newVersion) => {
if (!newVersion) {
newVersion = '';
}
pset('VERSION', newVersion);
}
startBot = () => {
loadCode("Main");
}