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