update last access on any action
[brisk.git] / web / briskin5 / index_wr.php
1 <?php
2 /*
3  *  brisk - index_wr.php
4  *
5  *  Copyright (C) 2006-2011 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(&$bri, $remote_addr, $get, $post, $cookie)
42 {
43     GLOBAL $G_base, $G_dbasetype;
44
45     $curtime = time();
46     if ($bri == NULL) {
47         return FALSE;
48     }
49     
50     if (($mesg = gpcs_var('mesg', $get, $post, $cookie)) === FALSE) 
51         unset($mesg);
52     
53     if (($sess = gpcs_var('sess', $get, $post, $cookie)) === FALSE) 
54         $sess = "";
55
56     log_wr('COMM: '.$mesg);
57
58
59     if (($CO_bin5_pref_ring_endauct = gpcs_var('CO_bin5_pref_ring_endauct', $get, $post, $cookie)) === FALSE) 
60         $CO_bin5_pref_ring_endauct = "";
61
62
63     
64     log_wr(0, 'bin::index_wr.php: COMM: '.xcapemesg($mesg));
65     
66     
67     if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
68         echo "Get User Error";
69         log_wr("Get User Error");
70         return FALSE;
71     }
72     $argz = explode('|', $mesg);
73     
74     log_wr('POSTSPLIT: '.$argz[0].'  user->stat: ['.$user->stat.']');
75     log_wr($user->step, 'bin::index_wr.php: after get_user()');
76
77     $user->lacc = $curtime;
78     
79     if ($argz[0] == 'ping') {
80         log_wr("PING RECEIVED");
81     }
82     else if (false && $argz[0] == 'shutdown') {
83         log_auth($user_cur->sess, "Shutdown session. delegate to room gc the autologout");
84         
85         log_rd2("bin5/index_wr.php: AUTO LOGOUT.");
86         if ($user->stat == 'table') {
87             $bri->table_wakeup($user);
88             // to force the logout
89             $user->lacc = 0;
90         }
91         else
92             log_rd2("SHUTDOWN FROM WHAT ???");
93     }
94     /*********************
95      *                   *
96      *    STAT: table    *
97      *                   *
98      *********************/
99     else if ($user->stat == 'table') {
100         $user->laccwr = time();
101         $table = $bri->table[$user->table];
102         
103         if ($argz[0] == 'tableinfo') {
104             log_wr("PER DI TABLEINFO");
105             $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
106             $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
107             log_wr($user->comm[$user->step % COMM_N]);
108             $user->step_inc();
109         }
110         else if ($argz[0] == 'chatt') {
111             $bri->chatt_send(&$user,$mesg);
112         }
113         else if ($argz[0] == 'preferences_update') {
114             GLOBAL $CO_bin5_pref_ring_endauct;
115             
116             log_wr("PER DI PREFERENCES_UPDATE");
117             
118             if ($CO_bin5_pref_ring_endauct == "true")
119                 $user->privflags |= BIN5_USER_FLAG_RING_ENDAUCT;
120             else
121                 $user->privflags &= ~BIN5_USER_FLAG_RING_ENDAUCT;
122         }
123         else if ($argz[0] == 'logout') {
124             $remcalc = $argz[1];
125             
126             if ($user->exitislock == TRUE) {
127                 $remcalc++;
128                 $user->exitislock = FALSE;
129             }
130             
131             $logout_cont = TRUE;
132             if ($remcalc >= 3) {
133                 $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
134                 if ($lockcalc < 3) {
135                     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
136                     $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
137                     $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);
138                     
139                     log_wr($user->comm[$user->step % COMM_N]);
140                     $user->step_inc();
141                     $logout_cont = FALSE;
142                 }
143             }
144             else {
145                 require_once("../Obj/hardban.phh");
146                 Hardbans::add(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
147                               $user->ip, $user->sess, $user->laccwr + BAN_TIME);
148             }
149             //      $user->bantime = $user->laccwr + BAN_TIME;
150             
151             if ($logout_cont == TRUE) {
152                 $bri->table_wakeup(&$user);
153             }
154         }
155         else if ($argz[0] == 'exitlock') {
156             if ($user->exitislock == TRUE) {
157                 $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
158                 for ($ct = 0, $i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {    
159                     $user_cur[$i] = &$bri->user[$table->player[$i]];
160                     if ($user_cur[$i]->exitislock == FALSE)
161                         $ct++;
162                 }
163                 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
164                     $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
165                     $ret .= sprintf('exitlock_show(%d, %s);', $ct, 
166                                     ($user_cur[$i]->exitislock ? 'true' : 'false'));
167                     $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
168                     log_wr($user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
169                     $user_cur[$i]->step_inc();
170                 }
171             }
172         }
173         else if ($user->subst == 'asta') {
174             if ($argz[0] == 'lascio' && $user->handpt <= 2) {
175                 $index_cur = $table->gstart % BIN5_PLAYERS_N;
176                 
177                 log_wr(sprintf("GIOCO FINITO !!!"));
178                 
179                 $table->mult += 1; 
180                 $table->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name));
181                 
182                 $table->game_next();
183                 $table->game_init(&$bri->user);
184                 
185                 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {     
186                     $user_cur = &$bri->user[$table->player[$i]];
187                     
188                     $ret = sprintf('gst.st = %d;', $user_cur->step+1);
189                     $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
190                     $user_cur->comm[$user_cur->step % COMM_N] = $ret;
191                     $user_cur->step_inc();          
192                 }
193             }
194             else if ($argz[0] == 'asta') {
195                 $again = TRUE;
196                 
197                 $index_cur = $table->gstart % BIN5_PLAYERS_N;
198                 if ($user->table_pos == $index_cur &&
199                     $table->asta_pla[$index_cur]) {
200                     $a_card = $argz[1];
201                     $a_pnt  = $argz[2];
202                     
203                     log_wr("CI SIAMO  a_card ".$a_card."  asta_card ".$table->asta_card);
204                     
205                     // Abbandono dell'asta
206                     if ($a_card <= -1) {
207                         log_wr("Abbandona l'asta.");
208                         $table->asta_pla[$index_cur] = FALSE;
209                         $user->asta_card  = -1;
210                         $table->asta_pla_n--;
211                         $again = FALSE;
212                     }
213                     else if ($a_card <= 9) {
214                         if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
215                             $again = FALSE;
216                         else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
217                             $again = FALSE;
218                         
219                         
220                         if ($again == FALSE) {
221                             log_wr("NUOVI ORZI.");
222                             $user->asta_card  = $a_card;
223                             $table->asta_card = $a_card;
224                             if ($a_card == 9) {
225                                 $user->asta_pnt   = $a_pnt;
226                                 $table->asta_pnt  = $a_pnt;
227                             }
228                         }
229                     }
230                     
231       
232       
233                     if ($again) { // Qualcosa non andato bene, rifare
234                         $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1, 
235                                        ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
236                         $user->comm[$user->step % COMM_N] = $ret;
237                         $user->step_inc();
238                         
239                         log_wr("Ripetere.");
240                     }
241                     else {
242                         /* next step */
243                         $showst = "show_astat("; 
244                         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
245                             $user_cur = &$bri->user[$table->player[$i]];
246                             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
247                                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
248                         }
249                         if (BIN5_PLAYERS_N == 3)
250                             $showst .= ",-2,-2";
251                         $showst .= ");";
252                         
253                         $maxcard = -2;
254                         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
255                             $user_cur = &$bri->user[$table->player[$i]];
256                             if ($maxcard < $user_cur->asta_card)
257                                 $maxcard = $user_cur->asta_card;
258                         }
259                         
260                         if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
261                             !($table->asta_card == 9 && $table->asta_pnt == 120)) {
262                             log_wr("ALLOPPA QUI");
263                             for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
264                                 $index_next = ($table->gstart + $i) % BIN5_PLAYERS_N;
265                                 if ($table->asta_pla[$index_next]) {
266                                     log_wr("GSTART 1");
267                                     $table->gstart += $i;
268                                     break;
269                                 }
270                             }
271                             
272                             
273                             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
274                                 $user_cur = &$bri->user[$table->player[$i]];
275                                 $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
276                                 if ($user_cur->table_pos == ($table->gstart % BIN5_PLAYERS_N)) 
277                                     $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();', 
278                                                     $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
279                                 else
280                                     $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
281                                                     $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
282                                 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
283                                 $user_cur->step_inc();
284                             }
285                         }
286                         else if ($table->asta_pla_n == 0) {
287                             log_wr("PASSANO TUTTI!");
288                             
289                             log_wr(sprintf("GIOCO FINITO !!!"));
290                             
291                             $table->old_reason = "Hanno passato tutti.";
292                             $table->mult += 1; 
293                             
294                             $table->game_next();
295                             $table->game_init(&$bri->user);
296                             
297                             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) { 
298                                 $user_cur = &$bri->user[$table->player[$i]];
299                                 
300                                 $ret = sprintf('gst.st = %d;', $user_cur->step+1);
301                                 $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
302                                 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
303                                 $user_cur->step_inc();      
304                             }
305                         }
306                         else {
307                             log_wr("FINITA !");
308                             // if a_pnt == 120 supergame ! else abbandono
309                             if ($a_pnt == 120 || $user->asta_card != -1) {
310                                 $chooser = $index_cur;
311                                 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) 
312                                     if ($i != $chooser)
313                                         $table->asta_pla[$i] = FALSE;
314                             }
315                             else {
316                                 /*
317                                   $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();", 
318                                   $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
319                                   $user->step_inc();
320                                 */
321                                 for ($i = 1 ; $i < BIN5_PLAYERS_N ; $i++) {
322                                     $chooser = ($table->gstart + $i) % BIN5_PLAYERS_N;
323                                     if ($table->asta_pla[$chooser]) {
324                                         break;
325                                     }
326                                 }
327                             }
328                             $table->asta_win = $chooser;
329                             
330                             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
331                                 $user_cur = &$bri->user[$table->player[$i]];
332                                 $ret = sprintf('gst.st = %d; %s dispose_asta(%d, %d, false);', $user_cur->step+1, $showst,
333                                                $table->asta_card + 1,-($table->asta_pnt));
334                                 
335                                 if ($i == $chooser) {
336                                     $ret .= "choose_seed(". $table->asta_card."); remark_on();";
337                                 }
338                                 else {
339                                     $ret .= "remark_off();";
340                                 }
341                                 
342                                 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
343                                 $user_cur->step_inc();      
344                             }
345                         }
346                     }
347                 }
348                 else {
349                     log_wr("NON CI SIAMO");
350                 }
351             }
352             /*  asta::choose */
353             else if ($argz[0] == 'choose') {
354                 if ($table->asta_win > -1 && 
355                     $user->table_pos == $table->asta_win) {
356                     $a_brisco = $argz[1];
357                     if ($a_brisco >= 0 && $a_brisco < 40) {
358                         $table->briscola = $a_brisco;
359                         $table->friend   = $table->card[$a_brisco]->owner;
360                         log_wr("GSTART 2");
361                         $table->gstart = ($table->mazzo+1) % BIN5_PLAYERS_N;
362                         log_wr("Setta la briscola a ".$a_brisco);
363                         
364                         $chooser = $table->asta_win;
365                         $user_chooser = &$bri->user[$table->player[$chooser]];
366                         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
367                             $user_cur = &$bri->user[$table->player[$i]];
368                             $user_cur->subst = 'game';
369                             $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
370                             
371                             if ($user_cur->privflags & BIN5_USER_FLAG_RING_ENDAUCT) {
372                                 // $ret .= "var de_che= 33;";
373                                 $ret .= playsound("ringbell.mp3");
374                             }
375                             $ret .= sprintf('document.title = "Brisk - Tavolo %d";', $user->table_orig);
376                             
377                             /* bg of caller cell */
378                             $ret .= briscola_show($bri, $table, $user_cur);
379                             
380                             /* first gamer */
381                             if ($i == ($table->gstart % BIN5_PLAYERS_N))
382                                 $ret .= "is_my_time = true; remark_on();";
383                             else
384                                 $ret .= "is_my_time = false; remark_off();";
385                             
386                             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
387                             $user_cur->step_inc();          
388                         }
389                         /*
390                           TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
391                           VAR PER PASSARE ALLA FASE DI GIOCO
392                         */
393                         
394                     }
395                 }
396             }
397         }
398         else if ($user->subst == 'game') {
399             log_wr("state: table::game".$argz[0]);
400             
401             if ($argz[0] == 'play') {
402                 $a_play = $argz[1];
403                 $a_x =    $argz[2];
404                 $a_y =    $argz[3];
405                 
406                 if (strpos($a_x, "px") != FALSE)
407                     $a_x = substr($a_x,0,-2);
408                 if (strpos($a_y, "px") != FALSE)
409                     $a_y = substr($a_y,0,-2);
410                 
411                 $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
412                                  $a_play, $user->table_pos, ($table->gstart % BIN5_PLAYERS_N),
413                                  $table->mazzo, $table->gstart,
414                                  $table->card[$a_play]->stat, $table->card[$a_play]->owner);
415                 log_wr("CIC".$loggo);
416                 
417                 /* se era il suo turno e la carta era sua ed era in mano */
418                 if ($a_play >=0 && $a_play < (BIN5_PLAYERS_N == 5 ? 40 : 24) &&
419                     ($user->table_pos == (($table->gstart + $table->turn) % BIN5_PLAYERS_N)) &&
420                     $table->card[$a_play]->stat == 'hand' &&
421                     $table->card[$a_play]->owner == $user->table_pos) {
422                     log_wr(sprintf("User: %s Play: %d",$user->name, $a_play));
423                     
424                     /* Change the card status. */
425                     $table->card[$a_play]->play($a_x, $a_y);
426                     
427                     /*
428                      *  !!!! TURN INCREMENTED BEFORE !!!!
429                      */
430                     $turn_cur = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
431                     $table->turn++;
432                     
433                     $card_play = sprintf("card_play(%d,%d,%d,%d);|",
434                                          $user->table_pos, $a_play, $a_x, $a_y);
435                     if (($table->turn % BIN5_PLAYERS_N) != 0) {     /* manche not finished */
436                         $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
437                         
438                         $player_cur = "remark_off();";
439                         $player_nex = $card_play . "is_my_time = true; remark_on();";
440                         $player_oth = $card_play;
441                     }
442                     else if ($table->turn <= (BIN5_PLAYERS_N * 8)) { /* manche finished */
443                         $winner = calculate_winner($table);
444                         log_wr("GSTART 3");
445                         $table->gstart = $winner;
446                         $turn_nex = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
447                         
448                         log_wr(sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
449                         $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|", $winner);
450                         $player_cur = "remark_off();" . $card_take; 
451                         if ($turn_cur != $turn_nex)
452                             $player_nex = $card_play . $card_take;
453                         else
454                             $player_nex = "";
455                         if ($table->turn < (BIN5_PLAYERS_N * 8))  /* game NOT finished */
456                             $player_nex .= "is_my_time = true; remark_on();";
457                         $player_oth = $card_play . $card_take;
458                     }
459                     
460                     log_wr(sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
461                     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) { 
462                         $user_cur = &$bri->user[$table->player[$i]];
463                         
464                         $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
465                         
466                         
467                         if ($i == $turn_cur) {
468                             $ret .= $player_cur;          
469                         }
470                         if ($i == $turn_nex) {
471                             $ret .= $player_nex;          
472                         }
473                         if ($i != $turn_cur && $i != $turn_nex) {
474                             $ret .= $player_oth;
475                         }
476                         
477                         $retar[$i] = $ret;
478                     }
479                     
480                     if ($table->turn == (BIN5_PLAYERS_N * 8)) { /* game finished */
481                         log_wr(sprintf("GIOCO FINITO !!!"));
482                         
483                         
484                         /* ************************************************ */
485                         /*    PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO    */
486                         /* ************************************************ */
487                         $pt_cur = calculate_points(&$table);
488                         
489                         $plist = "$table->table_token|$user->table_orig|$table->player_n";
490                         $ucodes = array();
491                         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
492                             $user_cur = &$bri->user[$table->player[$i]];
493                             $plist .= '|'.xcapelt($user_cur->name).'|'.$pt_cur[$i];
494                             $ucodes[$i] = $user_cur->code_get();
495                         }
496                         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
497                             $plist .= '|'.xcapelt($ucodes[$i]);
498                         }
499                         log_legal($curtime, 'xxx', $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
500                         if ($user->table_orig < TABLES_AUTH_N) {
501                             require_once("../Obj/dbase_".$G_dbasetype.".phh");
502                             
503                             if (($bdb = BriskDB::create()) != FALSE) {
504                                 $bdb->bin5_points_save($curtime, $table->table_token, $user->table_orig, $ucodes, $pt_cur);
505                                 unset($bdb);
506                             }
507                             else {
508                                 log_points($remote_addr, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
509                             }
510                             log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
511                         }
512                         
513                         $table->game_next();
514                         $table->game_init(&$bri->user);
515                         
516                         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
517                             $user_cur = &$bri->user[$table->player[$i]];
518                             $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
519                         }
520                     }
521                     
522                     
523                     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) { 
524                         $user_cur = &$bri->user[$table->player[$i]];
525                         
526                         $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
527                         $user_cur->step_inc();      
528                     }
529                     
530                     log_wr(sprintf("TURN: %d",$table->turn));
531                     /* Have played all the players ? */
532                     /* NO:  switch the focus and enable the next player to play. */
533                     
534                     /* YES: calculate who win and go to the next turn. */
535                 }
536             }
537             else
538                 log_wr("NOSENSE");
539         }
540     }
541     log_wr("before save data");
542     log_wr($user->step, 'bin::index_wr.php: after save_data()');
543     return TRUE;
544 }
545 ?>