-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.php
362 lines (349 loc) · 13.1 KB
/
bot.php
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
require_once("config.php");
require_once("logger.php");
require_once("telegram.php");
require_once("organizer.php");
set_error_handler("app_error_logger");
$json = GetUpdate();
CheckChatSupported($json);
$message = GetTextMessage($json);
$chatId = GetChatId($json);
$userId = GetUserId($json);
$isBotCommand = IsBotCommand($json);
$parameters = array();
if($isBotCommand)
{
if(strpos($message, " ")!==false)
{
$parameters = explode(" ", $message);
$lowmsg = $parameters[0];
}
else
$lowmsg = $message;
}
else
$lowmsg = strtolower($message);
switch($lowmsg)
{
case "/nuova":
case "/nuova@".BOT_NAME:
if(!IsAdmin($json))
break;
if(!IsGameEnded($chatId))
{
SendMessage($chatId, "Una partita è ancora in corso. Terminala per crearne un'altra");
return;
}
if(CreateNewGame($chatId, $userId))
{
SendMessage($chatId, "Partita creata");
SendInfoGame($chatId);
}
else
SendMessage($chatId, "Non è stato possibile creare una nuova partita");
break;
case "/termina":
case "/termina@".BOT_NAME:
if(!IsAdmin($json))
break;
if(EndGame($chatId))
SendMessage($chatId, "Partita terminata con *SUCCESSO*", true);
else
SendMessage($chatId, "Non sono riuscito a terminare la partita");
break;
case "/info":
case "/info@".BOT_NAME:
SendInfoGame($chatId);
break;
case "presente":
case "ci sono":
case "/ci_sono":
case "/ci_sono@".BOT_NAME:
if(IsGameEnded($chatId))
return;
$userId = GetUserId($json);
$firstname = GetFirstName($json);
$lastName = GetLastName($json);
$nickname = GetNickName($json);
$game = GetLastGame($chatId);
if($game == null)
return;
$players = GetPlayers($chatId, $game["id"]);
if(count($players) >= $game["players"])
{
SendMessage($chatId, "Mi dispiace $firstname, ma la partita è già al completo");
break;
}
if(AddPlayer($chatId, $userId, $firstname, $lastName, $nickname))
{
SendMessage($chatId, "Si è unito anche $firstname");
SendInfoGame($chatId);
}
break;
case "assente":
case "non ci sono":
case "non ci sono più":
case "non ci sono piu":
case "/non_ci_sono":
case "/non_ci_sono@".BOT_NAME:
if(IsGameEnded($chatId))
return;
$userId = GetUserId($json);
if(RemovePlayer($chatId, $userId))
{
$firstname = GetFirstName($json);
SendMessage($chatId, "$firstname non verrà alla partita");
SendInfoGame($chatId);
}
break;
case "/info_esterni":
case "/info_esterni@".BOT_NAME:
$game = GetLastGame($chatId);
if($game != null)
SendMessage($chatId, ($game["external"] ? "Giocatori esterni al gruppo *ABILITATO*" : "Giocatori esterni al gruppo *NON ABILITATO*"), true);
break;
case "/cambia_esterni":
case "/cambia_esterni@".BOT_NAME:
if(!IsAdmin($json))
return;
if(ChangeExternalPlayerStatus($chatId))
{
$game = GetLastGame($chatId);
if($game != null)
SendMessage($chatId, $game["external"] ?
"Giocatori esterni al gruppo *ABILITATO*" :
"Giocatori esterni al gruppo *NON ABILITATO*", true);
}
break;
case "/riserva_posto":
case "/riserva_posto@".BOT_NAME:
if(IsGameEnded($chatId))
return;
$game = GetLastGame($chatId);
if($game==null || !$game["external"])
return;
$spots = 1;
$externalName = NULL;
$userFullName = trim(GetFirstName($json)." ".GetLastName($json));
if(count($parameters) > 1)
{
if(is_numeric($parameters[1]))
{
$tempVal = intval($parameters[1]);
$spots = $tempVal > 0 ? $tempVal : 1;
}
else
{
unset($parameters[0]);
$externalName = implode(" ", $parameters);
}
}
$players = GetPlayers($chatId, $game["id"]);
$freeSpots = $game["players"]-count($players);
if($spots > $freeSpots)
{
SendMessage($chatId, "Posti non riservati. Sono disponibili $freeSpots posti");
break;
}
$spotsReserved = ReservePlayer($userId, $chatId, $externalName, $userFullName, GetNickname($json), $spots);
SendMessage($chatId,"Riservato/i $spotsReserved posto/i da *$userFullName*", true);
if($spotsReserved > 0)
SendInfoGame($chatId);
break;
case "/libera_posto":
case "/libera_posto@".BOT_NAME:
if(IsGameEnded($chatId))
return;
$userFullName = trim(GetFirstName($json)." ".GetLastName($json));
$spots = 1;
$externalName = NULL;
if(count($parameters) > 1)
{
if(is_numeric($parameters[1]))
{
$tempVal = intval($parameters[1]);
$spots = $tempVal > 0 ? $tempVal : 1;
}
else
{
unset($parameters[0]);
$externalName = implode(" ", $parameters);
}
}
$spotsFreed = $externalName != NULL ? FreeSpotByName($chatId, $userId, $externalName) : FreeSpot($chatId, $userId, $spots);
SendMessage($chatId,"Liberato/i $spotsFreed posto/i da *$userFullName*", true);
if($spotsFreed > 0)
SendInfoGame($chatId);
break;
case "/imposta_giorno":
case "/imposta_giorno@".BOT_NAME:
if(!IsAdmin($json))
return;
if(isset($parameters) && count($parameters) > 1){
$newDay = $parameters[1];
if(SetDay($chatId, $newDay))
SendInfoGame($chatId);
else
SendMessage($chatId, "Giorno non cambiato");
}
else
SendKeyboard($chatId, "Scegli il giorno dall'elenco", 2,
"/imposta_giorno Lunedi",
"/imposta_giorno Martedi",
"/imposta_giorno Mercoledi",
"/imposta_giorno Giovedi",
"/imposta_giorno Venerdi",
"/imposta_giorno Sabato",
"/imposta_giorno Domenica");
break;
case "/imposta_ora":
case "/imposta_ora@".BOT_NAME:
if(!IsAdmin($json))
return;
if(isset($parameters) && count($parameters) > 1){
$hour = $parameters[1]; //verificare il formato dell'ora
if(SetHour($chatId, $hour))
SendInfoGame($chatId);
else
SendMessage($chatId, "Ora non cambiata");
}
else
SendMessage($chatId, "Modalità d'uso: /imposta_ora HH:MM");
break;
case "/imposta_giocatori":
case "/imposta_giocatori@".BOT_NAME:
if(!IsAdmin($json))
return;
if(isset($parameters) && count($parameters) > 1 && is_numeric($parameters[1]))
{
$numPlayers = intval($parameters[1]);
if(SetPlayers($chatId, $numPlayers))
SendInfoGame($chatId);
else
SendMessage($chatId, "Si è verificato un errore");
}
else
SendMessage($chatId, "Modalità d'uso: /imposta_giocatori N");
break;
case "/imposta_campo":
case "/imposta_campo@".BOT_NAME:
if(!IsAdmin($json))
return;
if(isset($parameters))
{
unset($parameters[0]);
$fieldName = implode(" ", $parameters);
if(SetField($chatId, $fieldName))
SendInfoGame($chatId);
else
SendMessage($chatId, "Si è verificato un errore");
}
else
SendMessage($chatId, "Modalità d'uso: /imposta_campo NomeCampo");
break;
case "/rimuovi":
case "/rimuovi@".BOT_NAME:
if(!isAdmin($json))
return;
SendMessage($chatId, "Non implementato");
// rimuove un giocatore in base al nome ed il cognome
break;
case "/rimuovi_esterni":
case "/rimuovi_esterni@".BOT_NAME:
if(!isAdmin($json))
return;
SendMessage($chatId, "Non implementato");
break;
case "/recupera":
case "/recupera@".BOT_NAME:
if(!isAdmin($json))
return;
SendMessage($chatId, "Non implementato");
// cancella l'ultima gara creata ed utilizza la penultima
break;
case "/statistiche":
case "/statistiche@".BOT_NAME:
if(!isAdmin($json))
return;
SendMessage($chatId, "Non implementato");
break;
default:
ManageMessage($chatId, $json);
break;
}
function SendInfoGame($chatId)
{
$game = GetLastGame($chatId);
if($game == NULL)
{
SendMessage($chatId, "Al momento non ci sono partite in programma");
return;
}
$players = GetPlayers($chatId);
$elenco = "";
$indexPlayer = 1;
foreach ($players as $player) {
$playerName = GetPlayerNameForList($player, $player["userId"]<0);
$elenco.=$indexPlayer." - ".$playerName."\n";
$indexPlayer++;
}
$date_item = DateTime::createFromFormat("Y-m-d", $game["startDay"]);
$formattedData = date("d-m-Y", $date_item->getTimestamp());
$italianDay = GetItalianDay(date("l", $date_item->getTimestamp()));
$hour_split = explode(":", $game["startHour"]);
unset($hour_split[2]);
$formattedTime = implode(":", $hour_split);
SendMessage($chatId,"La prossima partita si giocherà *$italianDay*, *$formattedData* alle ore *".$formattedTime."*"
.(!empty($game["field"]) ? " al campo *".$game["field"]."*\n" : "\n")
.(count($players) > 0 ? "*\nElenco partecipanti:*\n$elenco\n" : "\n")
.(count($players) == $game["players"] ? "*Siamo al completo*" : "Mancano *".($game["players"]-count($players)."* giocatori")), true);
}
function GetPlayerNameForList($user, $isFriend=false)
{
if($isFriend)
{
$name = "Amico di "
.(empty($user["lastname"]) ? $user["nickname"] : $user["lastname"])
.(empty($user["firstname"]) ? "" : " (".$user["firstname"].")");
}
else
{
$name = trim(($user["firstname"]!=NULL ? $user["firstname"] : "")." ".($user["lastname"]!=NULL ? $user["lastname"] : ""));
if(empty($name))
$name = !empty($user["nickname"]) ? $user["nickname"] : "Utente sconosciuto";
}
return $name;
}
function CheckChatSupported($messageJson)
{
$chatId = GetChatId($messageJson);
if(!IsGroup($messageJson) && !IsSupergroup($messageJson))
{
SendMessage($chatId, "Questo bot supporta soltanto i gruppi e i supergruppi");
exit;
}
}
function ManageMessage($chatId, $json)
{
$newChatId = CheckChangeChatId($json);
if($newChatId !== NULL && $chatId != $newChatId)
{
if(!ChangeGroupId($chatId, $newChatId))
file_put_contents("errore_cambio_id.txt", "origine: $chatId destinazione: $newChatId");
}
}
function IsGameEnded($chatId)
{
$game = GetLastGame($chatId);
if($game == NULL)
return true;
$timeNow = time()-600;
$timeEnd = GetTimestampFromDateTime($game["startDay"], $game["startHour"]);
return $timeNow > $timeEnd;
}
function GetTimestampFromDateTime($date,$time)
{
$date_item = DateTime::createFromFormat("Y-m-d H:i:s", $date." ".$time);
return $date_item->getTimestamp();
}
?>