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