-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.mjs
215 lines (174 loc) · 6.28 KB
/
Helpers.mjs
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
//---------------------------------------------//
// Copyright (c) 2020 Ernesto Leonel Argüello.
// This file is covered by LICENSE at the root of this project.
//---------------------------------------------//
import fs from "fs";
import {ReadPrefix, ReadBot} from './Bot.mjs';
//---------------------------------------------//
// Helper functions.
//---------------------------------------------//
export function ParseMsgObj(MsgObj, toLower) { // Strips the message content from the prefix + the space.
if (toLower) {
return (MsgObj.content.slice(ReadPrefix().length + 1)).toLowerCase();
}
else {
return MsgObj.content.slice(ReadPrefix().length + 1);
}
}
//---------------------------------------------//
export function RandomNum(Min, Max) { // Vanilla random.
return Math.floor(Math.random() * (Max - Min) + Min);
}
//---------------------------------------------//
export function TourneyJSON() { // Shortcut for the Tourney.json.
return JSON.parse(fs.readFileSync("Tourney.json", {encoding: "utf8"}));
}
//---------------------------------------------//
export function RemoveSpecialChars(String, AllowSpaces) { // Simple regex to remove non-alphanumeric characters from string.
if (AllowSpaces) {
return String.replace(/[^a-zA-Z0-9 ]/g, ''); //Includes spaces in the allowed characters.
}
return String.replace(/[^a-zA-Z0-9]/g, '');
}
//---------------------------------------------//
export function CheckIfOnlyNum(Str) { // Checks that every character in a string is a number.
let i = 0;
let arr = Array.from(String(Str))
for (i in arr) {
if (arr[i] === "0" || arr[i] === "1" || arr[i] === "2" || arr[i] === "3" || arr[i] === "4" || arr[i] === "5" || arr[i] === "6" || arr[i] === "7" || arr[i] === "8" || arr[i] === "9") {
continue;
}
else {
return false;
}
}
return true;
}
//---------------------------------------------//
export function CheckEvenNum(Int) { // Checks that an int ends with an even number.
let arr = Array.from(String(Int));
let num = arr[arr.length - 1];
if (num == 0 || num == 2 || num == 4 || num == 6 || num == 8) {
return true;
}
return false;
}
//---------------------------------------------//
export function SkipFirstSpace(String) { // Allows commands to have a little more freedom by disregarding
let i = 0;
for (i in Array.from(String)) {
if (Array.from(String)[i] == " ") {
continue;
}
String = String.slice(i);
return String;
}
}
//---------------------------------------------//
export function SliceFromFirstSpace(String) { // Reads all the content, beginning from the first space.
let i = 0;
let NewString = "";
for (i in Array.from(String)) {
if (Array.from(String)[i] == " ") {
break;
}
NewString = NewString + Array.from(String)[i];
}
return NewString;
}
//---------------------------------------------//
export function GetFirstNumber(String) { // Gets the first number.
let i = 0;
let NewString = "";
for (i in Array.from(String)) {
if (CheckIfOnlyNum(Array.from(String)[i])) {
NewString = NewString + Array.from(String)[i];
continue;
}
break;
}
return NewString;
}
//---------------------------------------------//
export function ShuffleArray(array) { // Randomizes the entries inside an array.
let shuffled = array
.map((a) => ({sort: Math.random(), value: a}))
.sort((a, b) => a.sort - b.sort)
.map((a) => a.value);
return shuffled;
}
//---------------------------------------------//
export function RandomGoodLuck() { // Flavor added when successfully joining a tournament.
let file = JSON.parse(fs.readFileSync("GoodLuck.json", {encoding: "utf8"}));
return file.Phrases[RandomNum(0, file.Phrases.length)];
}
//---------------------------------------------//
export function RandomPointsPhrase() { // Flavor added when requesting points.
let file = JSON.parse(fs.readFileSync("PointsPhrases.json", {encoding: "utf8"}));
return file.Phrases[RandomNum(0, file.Phrases.length)];
}
//---------------------------------------------//
export async function FetchParticipantByNickname(Nickname) { // Fetches user object by reading participant nickname.
let i = 0;
let Participants = TourneyJSON().Tourneys[0].Participants;
let id;
for (i in Participants) {
if (Nickname == Participants[i].Nickname) {
id = Participants[i].ID
break;
}
}
let User = await (ReadBot().fetchUser(id))
return User;
}
//---------------------------------------------//
export function FindPairOpponentIndex(IndexInput) { // Opposing player in the same pair.
let OpponentIndex;
if (CheckEvenNum(IndexInput)) {
OpponentIndex = +IndexInput + +1
}
else {
OpponentIndex = IndexInput - 1
}
return OpponentIndex;
}
//---------------------------------------------//
export function FindBracketPlayersIndex(IndexInput) { // Translates a long array of pairs into the organized
let ArrayIndex; // ways of Brackets[] at Tourney.json.
let PlayerIndex;
let OpponentIndex;
if (IndexInput < 14) {
ArrayIndex = 2
PlayerIndex = IndexInput - 12
}
if (IndexInput < 12) {
ArrayIndex = 1
PlayerIndex = IndexInput - 8
}
if (IndexInput < 8) {
ArrayIndex = 0
PlayerIndex = IndexInput
}
if (CheckEvenNum(IndexInput)) {
OpponentIndex = PlayerIndex + 1
}
else {
OpponentIndex = PlayerIndex - 1
}
return [PlayerIndex, OpponentIndex, ArrayIndex];
}
//---------------------------------------------//
export function CommandName(Event) { // Human readable way to refer to a function when requesting an event while having
switch(Event) { // a non finished event already awaiting for a response.
case ChangePrefixRedir:
return '"cambio de prefijo"';
case CreateTourneyRedir:
return '"creación de torneo"';
case SendMsg:
return '"SendMsg"';
case JoinTourneyRedir:
return '"unirse a torneo"';
}
return;
}
//---------------------------------------------//