user is_auth() is_cert() methods added
[brisk.git] / web / briskin5 / index_wr.php
1 <?php
2 /*
3  *  brisk - briskin5/index_wr.php
4  *
5  *  Copyright (C) 2006-2012 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25
26 $G_base = "../";
27
28 require_once("../Obj/brisk.phh");
29 // require_once("../Obj/proxyscan.phh");
30 require_once("Obj/briskin5.phh");
31
32 // Use of proxies isn't allowed.
33 // if (is_proxy()) {
34 //   sleep(5);
35 //   exit;
36 //}
37
38 /*
39  *  MAIN
40  */
41 function bin5_index_wr_main(&$bin5, $remote_addr_full, $get, $post, $cookie)
42 {
43     GLOBAL $G_base, $G_dbasetype, $G_ban_list, $G_black_list;
44
45     $remote_addr = addrtoipv4($remote_addr_full);
46
47     $curtime = time();
48     if ($bin5 == NULL) {
49         return FALSE;
50     }
51
52     if (($mesg = gpcs_var('mesg', $get, $post, $cookie)) === FALSE)
53         unset($mesg);
54
55     if (($sess = gpcs_var('sess', $get, $post, $cookie)) === FALSE)
56         $sess = "";
57
58     log_wr('COMM: '.$mesg);
59
60
61     if (($CO_bin5_pref_ring_endauct = gpcs_var('CO_bin5_pref_ring_endauct', $get, $post, $cookie)) === FALSE)
62         $CO_bin5_pref_ring_endauct = "";
63
64
65
66     log_wr(0, 'bin::index_wr.php: COMM: '.xcapemesg($mesg));
67
68
69     if (($user = &$bin5->get_user($sess, &$idx)) == FALSE) {
70         echo "Get User Error";
71         log_wr("Get User Error");
72         return FALSE;
73     }
74     $bin5->brisk->sess_cur_set($user->sess);
75     if ( ( ! $user->is_auth() ) &&
76         $bin5->brisk->ban_check($user->ip)) {
77         // TODO: waiting async 5 sec before close
78         return (FALSE);
79     }
80
81     $argz = explode('|', $mesg);
82
83     log_wr('POSTSPLIT: '.$argz[0].'  user->stat: ['.$user->stat.']');
84     log_wr($user->step, 'bin::index_wr.php: after get_user()');
85
86     $user->lacc = $curtime;
87
88     if ($argz[0] == 'ping') {
89         log_wr("PING RECEIVED");
90     }
91     else if (false && $argz[0] == 'shutdown') {
92         log_auth($user_cur->sess, "Shutdown session. delegate to room gc the autologout");
93
94         log_rd2("bin5/index_wr.php: AUTO LOGOUT.");
95         if ($user->stat == 'table') {
96             $bin5->table_wakeup($user);
97             // to force the logout
98             $user->lacc = 0;
99         }
100         else
101             log_rd2("SHUTDOWN FROM WHAT ???");
102     }
103     /*********************
104      *                   *
105      *    STAT: table    *
106      *                   *
107      *********************/
108     else if ($user->stat == 'table') {
109         $user->laccwr = time();
110         $table = $bin5->table[$user->table];
111
112         if ($argz[0] == 'tableinfo') {
113             log_wr("PER DI TABLEINFO");
114             $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
115             $user->comm[$user->step % COMM_N] .= show_table_info(&$bin5, &$table, $user->table_pos);
116             log_wr($user->comm[$user->step % COMM_N]);
117             $user->step_inc();
118         }
119         else if ($argz[0] == 'chatt') {
120             $bin5->chatt_send(&$user,$mesg);
121         }
122         else if ($argz[0] == 'preferences_update') {
123             log_wr("PER DI PREFERENCES_UPDATE");
124
125             if ($CO_bin5_pref_ring_endauct == "true")
126                 $user->privflags |= BIN5_USER_FLAG_RING_ENDAUCT;
127             else
128                 $user->privflags &= ~BIN5_USER_FLAG_RING_ENDAUCT;
129         }
130         else if ($argz[0] == 'logout') {
131             $remcalc = $argz[1];
132
133             if ($user->exitislock == TRUE) {
134                 $remcalc++;
135                 $user->exitislock = FALSE;
136             }
137
138             $logout_cont = TRUE;
139             if ($remcalc >= 3) {
140                 $lockcalc = $table->exitlock_calc(&$bin5->user, $user->table_pos);
141                 if ($lockcalc < 3) {
142                     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
143                     $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bin5->user, $user->table_pos);
144                     $user->comm[$user->step % COMM_N] .=  show_notify("<br>I dati presenti sul server non erano allineati con quelli inviati dal tuo browser, adesso lo sono. Riprova ora.", 2000, "torna alla partita.", 400, 100);
145
146                     log_wr($user->comm[$user->step % COMM_N]);
147                     $user->step_inc();
148                     $logout_cont = FALSE;
149                 }
150             }
151             else {
152                 require_once("../Obj/hardban.phh");
153                 Hardbans::add(($user->is_auth() ? $user->name : FALSE),
154                               $user->ip, $user->sess, $user->laccwr + BAN_TIME);
155             }
156             //      $user->bantime = $user->laccwr + BAN_TIME;
157
158             if ($logout_cont == TRUE) {
159                 $bin5->table_wakeup(&$user);
160             }
161         }
162         else if ($argz[0] == 'exitlock') {
163             if ($user->exitislock == TRUE) {
164                 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
165                 for ($ct = 0, $i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
166                     $user_cur[$i] = &$bin5->user[$table->player[$i]];
167                     if ($user_cur[$i]->exitislock == FALSE)
168                         $ct++;
169                 }
170                 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
171                     $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
172                     $ret .= sprintf('exitlock_show(%d, %s);', $ct,
173                                     ($user_cur[$i]->exitislock ? 'true' : 'false'));
174                     $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
175                     log_wr($user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
176                     $user_cur[$i]->step_inc();
177                 }
178             }
179         }
180         else if ($user->subst == 'asta') {
181             if ($argz[0] == 'lascio') {
182                 //  && $user->handpt <= 2) {
183                 /* $index_cur = $table->gstart % BIN5_PLAYERS_N; */
184
185                 /* log_wr(sprintf("GIOCO FINITO !!!")); */
186
187                 /* $table->mult += 1;  */
188                 /* $table->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name)); */
189
190                 /* // Non si cambia mazzo se si abbandona la partita */
191                 /* $table->game_next(0); */
192
193                 /* if ($user->table_orig < TABLES_AUTH_N) { */
194                 /*     require_once("../Obj/dbase_".$G_dbasetype.".phh"); */
195
196                 /*     if (($bdb = BriskDB::create()) != FALSE) { */
197                 /*         $bdb->bin5_points_save($curtime, $table, $user->table_orig, $ucodes, $pt_cur); */
198                 /*         unset($bdb); */
199                 /*     } */
200                 /*     else { */
201                 /*         log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED"); */
202                 /*     } */
203                 /*     log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist); */
204                 /* } */
205
206                 /* $table->game_init(&$bin5->user); */
207
208                 if ($table->rules_engine(&$bin5, $curtime, BIN5_RULES_ABANDON, $user)) {
209                     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
210                         $user_cur = &$bin5->user[$table->player[$i]];
211
212                         $ret = sprintf('gst.st = %d;', $user_cur->step+1);
213                         $ret .= show_table(&$bin5,&$user_cur,$user_cur->step+1, TRUE, TRUE);
214                         $user_cur->comm[$user_cur->step % COMM_N] = $ret;
215                         $user_cur->step_inc();
216                     }
217                 }
218             }
219             else if ($argz[0] == 'asta') {
220                 $again = TRUE;
221
222                 $index_cur = $table->gstart % BIN5_PLAYERS_N;
223                 if ($user->table_pos == $index_cur &&
224                     $table->asta_pla[$index_cur]) {
225                     $a_card = $argz[1];
226                     $a_pnt  = $argz[2];
227
228                     log_wr("CI SIAMO  a_card ".$a_card."  asta_card ".$table->asta_card);
229
230                     // Abbandono dell'asta
231                     if ($a_card <= -1) {
232                         log_wr("Abbandona l'asta.");
233                         $table->asta_pla[$index_cur] = FALSE;
234                         $user->asta_card  = -1;
235                         $table->asta_pla_n--;
236                         $again = FALSE;
237                     }
238                     else if ($a_card <= 9) {
239                         if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
240                             $again = FALSE;
241                         else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
242                             $again = FALSE;
243
244
245                         if ($again == FALSE) {
246                             log_wr("NUOVI ORZI.");
247                             $user->asta_card  = $a_card;
248                             $table->asta_card = $a_card;
249                             if ($a_card == 9) {
250                                 $user->asta_pnt   = $a_pnt;
251                                 $table->asta_pnt  = $a_pnt;
252                             }
253                         }
254                     }
255
256                     if ($again) { // Qualcosa non andato bene, rifare
257                         $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1,
258                                        ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
259                         $user->comm[$user->step % COMM_N] = $ret;
260                         $user->step_inc();
261
262                         log_wr("Ripetere.");
263                     }
264                     else {
265                         /* next step */
266                         $showst = "show_astat(";
267                         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
268                             $user_cur = &$bin5->user[$table->player[$i]];
269                             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
270                                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
271                         }
272                         if (BIN5_PLAYERS_N == 3)
273                             $showst .= ",-2,-2";
274                         $showst .= ");";
275
276                         $maxcard = -2;
277                         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
278                             $user_cur = &$bin5->user[$table->player[$i]];
279                             if ($maxcard < $user_cur->asta_card)
280                                 $maxcard = $user_cur->asta_card;
281                         }
282
283                         if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
284                             !($table->asta_card == 9 && $table->asta_pnt == 120)) {
285                             log_wr("ALLOPPA QUI");
286                             for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
287                                 $index_next = ($table->gstart + $i) % BIN5_PLAYERS_N;
288                                 if ($table->asta_pla[$index_next]) {
289                                     log_wr("GSTART 1");
290                                     $table->gstart += $i;
291                                     break;
292                                 }
293                             }
294
295
296                             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
297                                 $user_cur = &$bin5->user[$table->player[$i]];
298                                 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
299                                 if ($user_cur->table_pos == ($table->gstart % BIN5_PLAYERS_N))
300                                     $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();',
301                                                     $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
302                                 else
303                                     $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
304                                                     $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
305                                 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
306                                 $user_cur->step_inc();
307                             }
308                         }
309                         else if ($table->asta_pla_n == 0) {
310                             log_wr("PASSANO TUTTI!");
311
312                             if ($table->rules_engine(&$bin5, $curtime, BIN5_RULES_ALLPASS, $user)) {
313                                 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
314                                     $user_cur = &$bin5->user[$table->player[$i]];
315
316                                     $ret = sprintf('gst.st = %d;', $user_cur->step+1);
317                                     $ret .= show_table(&$bin5,&$user_cur,$user_cur->step+1, TRUE, TRUE);
318                                     $user_cur->comm[$user_cur->step % COMM_N] = $ret;
319                                     $user_cur->step_inc();
320                                 }
321                             }
322                         }
323                         else {
324                             log_wr("FINITA !");
325                             // if a_pnt == 120 supergame ! else abbandono
326                             if ($a_pnt == 120 || $user->asta_card != -1) {
327                                 $chooser = $index_cur;
328                                 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++)
329                                     if ($i != $chooser)
330                                         $table->asta_pla[$i] = FALSE;
331                             }
332                             else {
333                                 /*
334                                   $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();",
335                                   $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
336                                   $user->step_inc();
337                                 */
338                                 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
339                                     $chooser = ($table->gstart + $i) % BIN5_PLAYERS_N;
340                                     if ($table->asta_pla[$chooser]) {
341                                         break;
342                                     }
343                                 }
344                             }
345                             $table->asta_win = $chooser;
346
347                             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
348                                 $user_cur = &$bin5->user[$table->player[$i]];
349                                 $ret = sprintf('gst.st = %d; %s dispose_asta(%d, %d, false);', $user_cur->step+1, $showst,
350                                                $table->asta_card + 1,-($table->asta_pnt));
351
352                                 if ($i == $chooser) {
353                                     $ret .= "choose_seed(". $table->asta_card."); remark_on();";
354                                 }
355                                 else {
356                                     $ret .= "remark_off();";
357                                 }
358
359                                 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
360                                 $user_cur->step_inc();
361                             }
362                         }
363                     }
364                 }
365                 else {
366                     log_wr("NON CI SIAMO");
367                 }
368             }
369             /*  asta::choose */
370             else if ($argz[0] == 'choose') {
371                 if ($table->asta_win > -1 &&
372                     $user->table_pos == $table->asta_win) {
373                     $a_brisco = $argz[1];
374                     if ($a_brisco >= 0 && $a_brisco < 40) {
375                         $table->briscola = $a_brisco;
376                         $table->friend   = $table->card[$a_brisco]->owner;
377                         log_wr("GSTART 2");
378                         $table->gstart = ($table->mazzo+1) % BIN5_PLAYERS_N;
379                         log_wr("Setta la briscola a ".$a_brisco);
380
381                         $chooser = $table->asta_win;
382                         $user_chooser = &$bin5->user[$table->player[$chooser]];
383                         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
384                             $user_cur = &$bin5->user[$table->player[$i]];
385                             $user_cur->subst = 'game';
386                             $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
387
388                             if ($user_cur->privflags & BIN5_USER_FLAG_RING_ENDAUCT) {
389                                 // $ret .= "var de_che= 33;";
390                                 $ret .= playsound("ringbell.mp3");
391                             }
392                             $ret .= sprintf('document.title = "Brisk - Tavolo %d";', $user->table_orig);
393
394                             /* bg of caller cell */
395                             $ret .= briscola_show($bin5, $table, $user_cur);
396
397                             /* first gamer */
398                             if ($i == ($table->gstart % BIN5_PLAYERS_N))
399                                 $ret .= "is_my_time = true; remark_on();";
400                             else
401                                 $ret .= "is_my_time = false; remark_off();";
402
403                             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
404                             $user_cur->step_inc();
405                         }
406                         /*
407                           TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
408                           VAR PER PASSARE ALLA FASE DI GIOCO
409                         */
410
411                     }
412                 }
413             }
414         }
415         else if ($user->subst == 'game') {
416             log_wr("state: table::game".$argz[0]);
417
418             if ($argz[0] == 'play') {
419                 $a_play = $argz[1];
420                 $a_x =    $argz[2];
421                 $a_y =    $argz[3];
422
423                 if (strpos($a_x, "px") != FALSE)
424                     $a_x = substr($a_x,0,-2);
425                 if (strpos($a_y, "px") != FALSE)
426                     $a_y = substr($a_y,0,-2);
427
428                 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
429                                  $a_play, $user->table_pos, ($table->gstart % BIN5_PLAYERS_N),
430                                  $table->mazzo, $table->gstart,
431                                  $table->card[$a_play]->stat, $table->card[$a_play]->owner);
432                 log_wr("CIC".$loggo);
433
434                 /* se era il suo turno e la carta era sua ed era in mano */
435                 if ($a_play >=0 && $a_play < (BIN5_CARD_HAND * BIN5_PLAYERS_N) &&
436                     ($user->table_pos == (($table->gstart + $table->turn) % BIN5_PLAYERS_N)) &&
437                     $table->card[$a_play]->stat == 'hand' &&
438                     $table->card[$a_play]->owner == $user->table_pos) {
439                     log_wr(sprintf("User: %s Play: %d",$user->name, $a_play));
440
441                     /* Change the card status. */
442                     $table->card[$a_play]->play($a_x, $a_y);
443
444                     /*
445                      *  !!!! TURN INCREMENTED BEFORE !!!!
446                      */
447                     $turn_cur = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
448                     $table->turn++;
449
450                     $card_play = sprintf("card_play(%d,%d,%d,%d);|",
451                                          $user->table_pos, $a_play, $a_x, $a_y);
452                     if (($table->turn % BIN5_PLAYERS_N) != 0) {     /* manche not finished */
453                         $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
454
455                         $player_cur = "remark_off();";
456                         $player_nex = $card_play . "is_my_time = true; remark_on();";
457                         $player_oth = $card_play;
458                     }
459                     else if ($table->turn <= (BIN5_PLAYERS_N * BIN5_CARD_HAND)) { /* manche finished */
460                         $winner = calculate_winner($table);
461                         log_wr("GSTART 3");
462                         $table->gstart = $winner;
463                         $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
464
465                         log_wr(sprintf("The winner is: [%d] [%s]", $winner, $bin5->user[$table->player[$winner]]->name));
466                         $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|", $winner);
467                         $player_cur = "remark_off();" . $card_take;
468                         if ($turn_cur != $turn_nex)
469                             $player_nex = $card_play . $card_take;
470                         else
471                             $player_nex = "";
472                         if ($table->turn < (BIN5_PLAYERS_N * BIN5_CARD_HAND))  /* game NOT finished */
473                             $player_nex .= "is_my_time = true; remark_on();";
474                         $player_oth = $card_play . $card_take;
475                     }
476
477                     log_wr(sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
478                     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
479                         $user_cur = &$bin5->user[$table->player[$i]];
480
481                         $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
482
483
484                         if ($i == $turn_cur) {
485                             $ret .= $player_cur;
486                         }
487                         if ($i == $turn_nex) {
488                             $ret .= $player_nex;
489                         }
490                         if ($i != $turn_cur && $i != $turn_nex) {
491                             $ret .= $player_oth;
492                         }
493
494                         $retar[$i] = $ret;
495                     }
496
497                     if ($table->turn == (BIN5_PLAYERS_N * BIN5_CARD_HAND)) { /* game finished */
498                         log_wr(sprintf("GIOCO FINITO !!!"));
499
500                         if ($table->rules_engine(&$bin5, $curtime, BIN5_RULES_FINISH, $user)) {
501                             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
502                                 $user_cur = &$bin5->user[$table->player[$i]];
503                                 $retar[$i] .= show_table(&$bin5,&$user_cur,$user_cur->step+1,TRUE, TRUE);
504                             }
505                         }
506                     }
507
508                     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
509                         $user_cur = &$bin5->user[$table->player[$i]];
510
511                         $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
512                         $user_cur->step_inc();
513                     }
514
515                     log_wr(sprintf("TURN: %d",$table->turn));
516                     /* Have played all the players ? */
517                     /* NO:  switch the focus and enable the next player to play. */
518
519                     /* YES: calculate who win and go to the next turn. */
520                 }
521             }
522             else
523                 log_wr("NOSENSE");
524         }
525     }
526     return TRUE;
527 }
528 ?>