-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.ts
230 lines (204 loc) · 6.1 KB
/
helpers.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
import * as crypto from 'crypto';
import * as fs from 'fs';
export const generateSixSymbolHash = (): string => {
const hash = crypto.randomBytes(20).toString('hex');
return hash.substring(0, 6).toUpperCase();
};
export const getRandomIndex = (maxIndex: number) => {
return Math.floor(Math.random() * maxIndex);
};
/**
* Set random to generate smaller numbers more often
* @param minIndex
* @param maxIndex
* @returns
*/
export const getRandomIndexInRange = (minIndex: number, maxIndex: number) => {
minIndex = minIndex ** 0.8;
maxIndex = maxIndex ** 0.8;
return Math.floor(
Math.floor(Math.random() * (maxIndex - minIndex) + minIndex) ** 1.25,
);
};
export const isset = (val: any) => {
return val !== null && val !== undefined;
};
export const countCharacteristics = () => {
let jsonData: { [x: string]: any };
try {
const data = fs.readFileSync('./characteristics.json', 'utf8');
jsonData = JSON.parse(data);
} catch (error) {
console.log(`ERROR: ${error}`);
}
const res = {};
for (const char in jsonData) {
const count = jsonData[char].length;
res[char] = count;
}
return res;
};
export const generateFromCharacteristics = (
type: 'charList' | 'conditions' | 'specialCard',
): any => {
let jsonData: { [x: string]: any };
try {
const data = fs.readFileSync('./characteristics.json', 'utf8');
jsonData = JSON.parse(data);
} catch (error) {
console.log(`ERROR: ${error}`);
}
const _getRandomChar = (data: any, type: string) => {
if (type === 'gender') {
const age = getRandomIndexInRange(18, 101);
const gender = ['Чоловік', 'Жінка'][getRandomIndex(2)];
return `${gender}(Вік:${age})`; // example: Чоловік(Вік:101)
}
const charsByType = data[type];
const randomIndex = Math.floor(Math.random() * charsByType.length);
return charsByType[randomIndex];
};
if (type === 'conditions') {
const shelter = _getRandomChar(jsonData, 'shelter');
const catastrophe = _getRandomChar(jsonData, 'catastrophe');
return { shelter, catastrophe };
}
if (type === 'specialCard') {
const specialCardObj1 = _getRandomChar(jsonData, 'specialCard');
const filteredJsonData = jsonData['specialCard'].filter(
(sc) => sc.id !== specialCardObj1.id,
); // remove to avoid dublicates in one user
const randomIndex = Math.floor(Math.random() * filteredJsonData.length);
const specialCardObj2 = filteredJsonData[randomIndex];
const res = [
{
type: 'specialCard1',
text: specialCardObj1.text,
id: specialCardObj1.id,
isUsed: false,
onContestant: specialCardObj1.onContestant || false,
},
{
type: 'specialCard2',
text: specialCardObj2.text,
id: specialCardObj2.id,
isUsed: false,
onContestant: specialCardObj2.onContestant || false,
},
];
return res;
}
const charList = [
{
type: 'gender',
text: _getRandomChar(jsonData, 'gender'),
icon: 'genderIcon',
isRevealed: false,
},
{
type: 'health',
stage: ['тяжка форма', 'критична форма', 'легка форма', 'середняя форма'][
getRandomIndex(4)
],
text: _getRandomChar(jsonData, 'health'),
icon: 'healthIcon',
isRevealed: false,
},
{
type: 'hobby',
text: _getRandomChar(jsonData, 'hobby'),
icon: 'hobbyIcon',
isRevealed: false,
},
{
type: 'job',
text: _getRandomChar(jsonData, 'job'),
icon: 'jobIcon',
isRevealed: false,
},
{
type: 'phobia',
text: _getRandomChar(jsonData, 'phobia'),
icon: 'phobiaIcon',
isRevealed: false,
},
{
type: 'backpack',
text: _getRandomChar(jsonData, 'backpack'),
icon: 'backpackIcon',
isRevealed: false,
},
{
type: 'fact',
text: _getRandomChar(jsonData, 'fact'),
icon: 'factIcon',
isRevealed: false,
},
];
return charList;
};
export const countOccurrences = (arr: string[]) => {
const counts = {};
arr.forEach((id: string) => {
counts[id] = (counts[id] || 0) + 1;
});
return counts;
};
export const getKeysWithHighestValue = (obj: object) => {
let maxCount = -Infinity;
let keysWithMaxCount = [];
// Find the maximum count
for (const key in obj) {
const count = obj[key];
if (count > maxCount) {
maxCount = count;
keysWithMaxCount = [key];
} else if (count === maxCount) {
keysWithMaxCount.push(key);
}
}
return keysWithMaxCount;
};
export const getTime = () => {
const date = new Date();
const hour = date.getHours().toString().padStart(2, '0');
const minute = date.getMinutes().toString().padStart(2, '0');
const timeStr = `${hour}:${minute}`;
return timeStr;
};
export const getRandomGreeting = (list: string[]) => {
const greeting = list[getRandomIndex(list.length)];
return greeting;
};
export function extractJustificationInfo(text: string): {
characteristics: string[];
argument: string;
} {
console.log('extractJustificationInfo', text);
// Regular expression patterns to match characteristics and argument
const characteristicsPattern = /Характеристики:\s*(.+?)\./;
const argumentPattern = /Аргумент:\s*(.+)/;
// Match characteristics and argument
const characteristicsMatch = text.match(characteristicsPattern);
const argumentMatch = text.match(argumentPattern);
// Extract characteristics and argument
const characteristics = characteristicsMatch
? characteristicsMatch[1].split(', ').map((item) => item.trim())
: [];
const argument = argumentMatch ? argumentMatch[1].trim() : '';
return { characteristics, argument };
}
// Example: '@Leonardo da Vinci, hello!'
export function parseMessage(message: string): {
displayName: string | null;
userMessage: string;
} {
const regex = /^@([^,]+),\s*(.*)/;
const match = regex.exec(message);
if (match) {
const displayName = match[1].trim();
const userMessage = match[2].trim();
return { displayName, userMessage };
}
return { displayName: null, userMessage: '' };
}