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