-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.js
244 lines (185 loc) · 6.88 KB
/
Game.js
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
/**
* Only start New if there is no game in the progress
* if there is in the progress, then the game in the progress will be loaded
*/
function sendGame(ctx) {
ctx.replyWithChatAction("typing");
const progress = getProgressOfTheGame();
if (progress === "ON") { return loadGameInProgress(ctx) }
setProgressOfTheGame("ON")
const emoji_and_name = getEmojiAndName()
const emoji = emoji_and_name.emoji;
const name = emoji_and_name.name;
const difficulty = emoji_and_name.difficulty;
const string = generateHiddenString(name)
const template = gameTemplate(emoji, string, null, difficulty, null)
ctx.replyWithHtml(template, { reply_markup: kbds() })
setImojiAndName(emoji, name, difficulty)
}
/**
* Load Game which is already in progress.
*/
function loadGameInProgress(ctx) {
const data = getData()
const emoji = data.emoji;
const difficulty = data.difficulty;
const handler = getUpdatedString();
const getPlayerTemplate = getPlayersTemplate() || '';
const template = gameTemplate(emoji, handler, null, difficulty, getPlayerTemplate)
ctx.replyWithHtml(template, { reply_markup: kbds() })
}
//Get Winner username if possible,
//unless get the first name only
function getWinner(ctx) {
const username = (ctx.from.username) ? '@' + ctx.from.username : ''
const first_name = ctx.from.first_name
return username || first_name;
}
/**
* Game Manager it self.
*/
function game(ctx) {
var lock = LockService.getScriptLock();
try {
// wait up to 0.5 seconds for other processes
lock.tryLock(500);
ctx.replyWithChatAction("typing");
let cbk_data = getCbkdata(ctx).toUpperCase();
const cbk_text = getTextCbk(ctx);
const string = updatedString(cbk_text)
const data = getData()
const emoji = data.emoji;
const name = data.name;
const update = data.update.toUpperCase();
const difficulty = data.difficulty;
if (!emoji) { return }
let updated_string = '';
let playersTemp = '';
let stat = '';
const old_player_template = getPlayersTemplate() || '';
//get Index of character where to replace
const index = indexOfElementInString(cbk_data, update)
//Pizza will be #izza after "p" is detected
//this is important when identical characters found
const chars_replaced_with_hashtags = replaceFirstOccurrence(update, cbk_data, "#")
//if the character is found
if (index) {
//Replace The Correct Character to the Correct Index of "-"
updated_string = replaceSubstringAtIndex(string, index - 1, cbk_data)
stat = '✅'
const playerName = getWinner(ctx)
const playerTemp = generatePlayersTemplate(playerName, cbk_data)
playersTemp = old_player_template + playerTemp;
} else {
updated_string = string
stat = '❌';
playersTemp = old_player_template
}
//when all characters except spaces are replaced with "#"
if (isGameEnded(chars_replaced_with_hashtags)) {
const winner = getWinner(ctx)
const winner_template = winnerTemplate(winner, playersTemp, emoji, name)
realTimeUpdateTheWinnerFromAllChat(winner_template, ctx)
return setTheNextGame()
}
//generate Game template With updates
const template = gameTemplate(
emoji,
updated_string,
stat,
difficulty,
playersTemp)
try {
ctx.replyWithEditedMessage(template, {
parse_mode: "HTML",
reply_markup: kbds()
});
} catch (err) { }
//save chats from where the game is being played.
saveChats(ctx)
setStringWithHashtagUpdates(chars_replaced_with_hashtags);
saveUpdatedString(updated_string)
//save players template
if (index) { setPlayersTemplate(playersTemp); }
checkIfTheGameIsAlreadyEnded(emoji, cbk_text, ctx)
} finally {
lock.releaseLock();
}
}
/**
* Show If the Game is already expired!
*/
function checkIfTheGameIsAlreadyEnded(emoji, cbktext, ctx) {
const text = cbktext.split("Emoji:")[1];
const emojiFromBot = text.split("\n\n")[0].trim()
if (emojiFromBot !== emoji) {
return ctx.replyWithEditedMessage("THE GAME IS EXPIRED ALREADY." +
"\n\n USE /start TO START THE NEW GAME.")
}
}
/**
* SETTING UP FOR NEXT GAME
*/
function setTheNextGame() {
const next = getNext()
PropertiesService.getScriptProperties().setProperties(
{
next: Number(next) + 1,
progres: "OFF"
})
//Delete Those Property, for the Next Game.
PropertiesService.getScriptProperties().deleteProperty('emoji')
PropertiesService.getScriptProperties().deleteProperty('name')
PropertiesService.getScriptProperties().deleteProperty('update')
PropertiesService.getScriptProperties().deleteProperty('difficulty')
PropertiesService.getScriptProperties().deleteProperty('player')
PropertiesService.getScriptProperties().deleteProperty('handler')
}
/**
* Get The Updated String Which is Replaced
* With The Correct Character
*/
function updatedString(cbkText) {
var string = ''
const last_saved_string = getUpdatedString();
if (!last_saved_string) {
const text = cbkText.split("Status:")[1];
string = text.split("\n\n")[0].trim()
} else string = last_saved_string
return string;
}
/**
* Save chats Id of all chats, from which the Game is being played
*/
function saveChats(ctx) {
const chatId = ctx.message.message.chat.id;
const messageId = ctx.message.message.message_id;
const getChatIds = PropertiesService.getScriptProperties().getProperty("chats") || '';
if (!getChatIds) {
PropertiesService.getScriptProperties().setProperty("chats", `${chatId}_${messageId}`)
}
else if (getChatIds.includes(chatId)) { return }
PropertiesService.getScriptProperties().setProperty("chats", `${chatId}_${messageId}#${getChatIds}`)
}
/**
* Update The Winner From All Chats In real time.
*/
function realTimeUpdateTheWinnerFromAllChat(template, ctx) {
const getChats = PropertiesService.getScriptProperties().getProperty("chats") || '';
if (!getChats) { return }
const chats = getChats.split("#");
try {
for (i = 0; i <= chats.length - 2; i++) {
const element = chats[i];
const chatId = element.split("_")[0];
const message_id = element.split("_")[1]
ctx.editMessageText({
chat_id: chatId,
message_id: message_id,
text: template,
parse_mode: "HTML",
});
}
PropertiesService.getScriptProperties().deleteProperty("chats")
} catch (err) { console.log(err) }
}