forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.ts
More file actions
145 lines (121 loc) · 5 KB
/
Copy pathfunctions.ts
File metadata and controls
145 lines (121 loc) · 5 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
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
export {};
const config = require("./config");
const API_REQUEST_TIMEOUT_MS = 8000;
const funct = new (Funct as any)();
function Funct(this: any) {
this.jsonDecode = function <T>(this: any, data: string): T {
return JSON.parse(data) as T;
};
this.dumpError = function (this: any, err: unknown): void {
if (typeof err === "object") {
if (err && "message" in err && typeof err.message === "string") {
console.log("\nMessage: " + err.message);
}
if (err && "stack" in err && typeof err.stack === "string") {
console.log("\nStacktrace:");
console.log("====================");
console.log(err.stack);
}
} else {
console.log("dumpError :: argument is not an object");
}
};
this.randomIntFromInterval = function (this: any, min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1) + min);
};
this.sign = function (this: any, x: number): number {
return x > 0 ? 1 : x < 0 ? -1 : 0;
};
this.dateFormat = function (this: any, date: Date, fstr: string, utc?: boolean): string {
const accessorPrefix = utc ? "getUTC" : "get";
return fstr.replace(/%[YmdHMS]/g, function (m: string) {
const dateWithDynamicGetters = date as unknown as Record<string, () => number>;
switch (m) {
case "%Y":
return String(dateWithDynamicGetters[accessorPrefix + "FullYear"]());
case "%m":
m = String(1 + dateWithDynamicGetters[accessorPrefix + "Month"]());
break;
case "%d":
m = String(dateWithDynamicGetters[accessorPrefix + "Date"]());
break;
case "%H":
m = String(dateWithDynamicGetters[accessorPrefix + "Hours"]());
break;
case "%M":
m = String(dateWithDynamicGetters[accessorPrefix + "Minutes"]());
break;
case "%S":
m = String(dateWithDynamicGetters[accessorPrefix + "Seconds"]());
break;
default:
return m.slice(1); // unknown code, remove %
}
// add leading zero if required
return ("0" + m).slice(-2);
});
};
this.sendTelegramMessage = (_message: string): void => {
void _message;
// Open-source builds do not send operational notifications to private channels.
};
this.logOnlineRecord = (): void => {
const vars = require("./vars");
const handleProtocol = require("./handleProtocol");
const onlineOpenWorld = Number(vars.usuariosOnline) || 0;
const onlineArena = Number(vars.usuariosOnlinePvP) || 0;
const onlineTotal = onlineOpenWorld + onlineArena;
if (onlineTotal <= vars.maxUsersOnline) {
return;
}
vars.maxUsersOnline = onlineTotal;
const message = `[Online Record] Nuevo record: ${onlineTotal} jugadores en simultáneo (mundo abierto: ${onlineOpenWorld}, arena: ${onlineArena})`;
console.log(message);
this.sendTelegramMessage(message);
handleProtocol.consoleToAll(message, "#E69500", 1, 0);
};
this.logCharacterActivity = (_payload: unknown): void => {
void _payload;
};
this.logChallengeHistory = (payload: unknown): void => {
const vars = require("./vars");
void this.fetchUrl("/internal/challenges/history", {
method: "POST",
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json",
Authorization: vars.tokenAuth,
},
}).catch((error: unknown) => {
this.dumpError(error);
});
};
this.fetchUrl = async <T>(url: string, options: RequestInit = {}): Promise<T> => {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), API_REQUEST_TIMEOUT_MS);
let response: Response;
try {
response = await fetch(config.apiBaseUrl + url, {
...options,
signal: controller.signal,
});
} catch (error) {
if (error instanceof Error && error.name === "AbortError") {
throw new Error(`API request timed out after ${API_REQUEST_TIMEOUT_MS}ms`);
}
throw error;
} finally {
clearTimeout(timeoutId);
}
const result = await response.json();
if (!response.ok) {
const message =
typeof result === "object" && result && "error" in result && typeof result.error === "string"
? result.error
: `Request failed with status ${response.status}`;
throw new Error(message);
}
return result as T;
};
}
module.exports = funct;