FSM correction
[brisk.git] / web / index_wr.php
1 <?php
2 /*
3  *  brisk - index_wr.php
4  *
5  *  Copyright (C) 2006 matteo.nastasi@milug.org
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details. You should have received a
16  * copy of the GNU General Public License along with this program; if
17  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18  * Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * $Id$
21  *
22  */
23
24 require_once("brisk.phh");
25 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
26   echo "Debugging time!";
27   exit;
28 }
29
30 log_load($sess, "LOAD: index_wr.php");
31
32 /*
33  *  MAIN
34  */
35 log_wr($sess, 'COMM: '.$mesg);
36
37 $sem = lock_data();
38 $bri = &load_data();
39 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
40   echo "Get User Error";
41   log_wr($sess, "Get User Error");
42   unlock_data($sem);
43   exit;
44 }
45 $argz = explode('|', $mesg);
46
47 if ($argz[0] == 'shutdown') {
48   log_auth($user_cur->sess, "Shutdown session.");
49   $tmp_sess = $user->sess;
50   $user->sess = "";
51   step_unproxy($tmp_sess);
52   $user->name = "";
53   $user->the_end = FALSE;
54   
55   log_rd2($user->sess, "AUTO LOGOUT.");
56   if ($user->subst == 'sitdown' || $user->stat == 'table')
57     $bri->room_wakeup(&$user);
58   else if ($user->subst == 'standup')
59     $bri->room_outstandup(&$user);
60   else
61     log_rd2($sess, "SHUTDOWN FROM WHAT ???");
62 }
63 /******************
64  *                *
65  *   STAT: room   *
66  *                *
67  ******************/
68 else if ($user->stat == 'room') {
69   $user->laccwr = time();
70
71   if ($argz[0] == 'help') {
72     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
73     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
74
75     log_wr($sess, $user->comm[$user->step % COMM_N]);
76     $user->step_inc();
77     
78   }
79   else if ($argz[0] == 'about') {
80     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
81     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
82
83     log_wr($sess, $user->comm[$user->step % COMM_N]);
84     $user->step_inc();
85     
86   }
87   else if ($argz[0] == 'chatt') {
88     $bri->chatt_send(&$user,$mesg);
89   }
90   /**********************
91    *                    *
92    *   SUBST: standup   *
93    *                    *
94    **********************/
95   else if ($user->subst == 'standup') {
96    
97     if ($argz[0] == 'sitdown') {
98       if ($user->the_end == TRUE) {
99         log_wr($sess, "INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
100         unlock_data($sem);
101         exit;
102       }
103       /* TODO: refact to a function */
104       if ($user->bantime > $user->laccwr) {
105         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
106         $user->comm[$user->step % COMM_N] .= show_notify("<br>Ti sei alzato da un tavolo senza il consenso degli altri giocatori. Dovrai aspettare ancora ".secstoword($user->bantime - $user->laccwr)." prima di poterti sedere nuovamente.", 2000, "Torna in piedi.", 400, 100);
107         
108         $user->step_inc();
109         save_data($bri);
110         unlock_data($sem);
111         exit;
112       }
113     
114       // Take parameters
115       $table_idx = $argz[1];
116       $table = &$bri->table[$table_idx];
117     
118       if ($table->player_n == PLAYERS_N) {
119         log_wr($sess, "WARN:FSM: Sitdown unreachable, table full.");
120         unlock_data($sem);
121         exit;
122       } 
123
124       // set new status
125       $user->subst = "sitdown";
126       $user->table = $table_idx;
127       $user->table_pos = $table->user_add($idx);
128       
129       if ($table->player_n == PLAYERS_N) {
130         // Start game for this table.
131         log_wr($sess, "Start game!");
132         
133         $table->init(&$bri->user);
134         $table->game_init(&$bri->user);
135         $curtime = time();
136         
137         for ($i = 0 ; $i < $table->player_n ; $i++) {
138           $user_cur = &$bri->user[$table->player[$i]];
139           log_wr($sess, "Pre if!");
140           
141           $ret = "";
142           $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
143           
144           $user_cur->comm[$user_cur->step % COMM_N] = $ret;
145           $user_cur->trans_step = $user_cur->step + 1;
146           log_wr($sess, "TRANS ATTIVATO");
147           
148
149           $user_cur->stat_set('table');
150           $user_cur->subst = 'asta';
151           $user_cur->laccwr = $curtime;
152           $user_cur->step_inc();
153           
154           $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
155           $user_cur->step_inc();
156         }
157       }
158       
159       $bri->room_sitdown(&$user, $table_idx);
160     }
161     else if ($argz[0] == 'logout') {
162       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
163       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
164       $user->the_end = TRUE;
165       $user->step_inc();
166     }
167   }
168   /**********************
169    *                    *
170    *   SUBST: sitdown   *
171    *                    *
172    **********************/
173   else if ($user->subst == 'sitdown') {
174     if ($argz[0] == 'wakeup') {
175       $bri->room_wakeup(&$user);      
176     }
177     else if ($argz[0] == 'logout') {
178       $bri->room_wakeup(&$user);      
179       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
180       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
181       $user->the_end = TRUE;
182       $user->step_inc();
183     }
184   }
185 }
186 /*********************
187  *                   *
188  *    STAT: table    *
189  *                   *
190  *********************/
191 else if ($user->stat == 'table') {
192   $user->laccwr = time();
193   $table = &$bri->table[$user->table];
194
195   if ($argz[0] == 'tableinfo') {
196     log_wr($sess, "PER DI TABLEINFO");
197     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
198     $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
199     log_wr($sess, $user->comm[$user->step % COMM_N]);
200     $user->step_inc();
201   }
202   else if ($argz[0] == 'chatt') {
203     $bri->chatt_send(&$user,$mesg);
204   }
205   else if ($argz[0] == 'logout') {
206     $remcalc = $argz[1];
207
208     if ($user->exitislock == TRUE) {
209       $remcalc++;
210       $user->exitislock = FALSE;
211     }
212
213     $logout_cont = TRUE;
214     if ($remcalc >= 3) {
215       $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
216       if ($lockcalc < 3) {
217         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
218         $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
219         $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);
220         
221         log_wr($sess, $user->comm[$user->step % COMM_N]);
222         $user->step_inc();
223         $logout_cont = FALSE;
224       }
225     }
226     else 
227       $user->bantime = $user->laccwr + BAN_TIME;
228     
229     if ($logout_cont == TRUE) {
230       $bri->room_wakeup(&$user);
231     }
232   }
233   else if ($argz[0] == 'exitlock') {
234     $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
235     for ($ct = 0, $i = 0 ; $i < PLAYERS_N ; $i++) {     
236       $user_cur[$i] = &$bri->user[$table->player[$i]];
237       if ($user_cur[$i]->exitislock == FALSE)
238         $ct++;
239     }
240     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
241       $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
242       $ret .= sprintf('exitlock_show(%d, %s);', $ct, 
243                      ($user_cur[$i]->exitislock ? 'true' : 'false'));
244       $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
245       log_wr($sess, $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
246       $user_cur[$i]->step_inc();
247     }
248   }
249   else if ($user->subst == 'asta') {
250     if ($argz[0] == 'lascio' && $user->handpt <= 2) {
251       $index_cur = $table->gstart % PLAYERS_N;
252     
253       log_wr($sess, sprintf("GIOCO FINITO !!!"));
254     
255       $table->mult *= 2; 
256       $table->old_reason = sprintf("Ha lasciato %s perche` aveva al massimo 2 punti.", $user->name);
257
258       $table->game_next();
259       $table->game_init(&$bri->user);
260     
261       for ($i = 0 ; $i < PLAYERS_N ; $i++) {    
262         $user_cur = &$bri->user[$table->player[$i]];
263
264         $ret = sprintf('gst.st = %d;', $user_cur->step+1);
265         $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
266         $user_cur->comm[$user_cur->step % COMM_N] = $ret;
267         $user_cur->step_inc();      
268       }
269     }
270     else if ($argz[0] == 'asta') {
271       $again = TRUE;
272     
273       $index_cur = $table->gstart % PLAYERS_N;
274       if ($user->table_pos == $index_cur &&
275           $table->asta_pla[$index_cur]) {
276         $a_card = $argz[1];
277         $a_pnt  = $argz[2];
278       
279         log_wr($sess, "CI SIAMO  a_card ".$a_card."  asta_card ".$table->asta_card);
280       
281         // Abbandono dell'asta
282         if ($a_card <= -1) {
283           log_wr($sess, "Abbandona l'asta.");
284           $table->asta_pla[$index_cur] = FALSE;
285           $user->asta_card  = -1;
286           $table->asta_pla_n--;
287           $again = FALSE;
288         }
289         else if ($a_card <= 9) {
290           if ($table->asta_card == 9) {
291             if ($a_card == 9 && $a_pnt <= 120 && $a_pnt > $table->asta_pnt)
292               $again = FALSE;
293           }
294           else {
295             if ($a_card >= 0 && $a_card <= 9 && $a_card > $table->asta_card)
296               $again = FALSE;
297           }
298
299           if ($again == FALSE) {
300             log_wr($sess, "NUOVI ORZI.");
301             $user->asta_card  = $a_card;
302             $table->asta_card = $a_card;
303             if ($a_card == 9) {
304               $user->asta_pnt   = $a_pnt;
305               $table->asta_pnt  = $a_pnt;
306             }
307           }
308         }
309       
310       
311       
312         if ($again) { // Qualcosa non andato bene, rifare
313           log_wr($sess, "Ripetere.");
314         }
315         else {
316           /* next step */
317           $showst = "show_astat("; 
318           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
319             $user_cur = &$bri->user[$table->player[$i]];
320             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
321                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
322           }
323           if (PLAYERS_N == 3)
324             $showst .= ",-2,-2";
325           $showst .= ");";
326
327           $maxcard = -2;
328           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
329             $user_cur = &$bri->user[$table->player[$i]];
330             if ($maxcard < $user_cur->asta_card)
331               $maxcard = $user_cur->asta_card;
332           }
333
334           if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
335               !($table->asta_card == 9 && $table->asta_pnt == 120)) {
336             log_wr($sess,"ALLOPPA QUI");
337             for ($i = 1 ; $i < PLAYERS_N ; $i++) {
338               $index_next = ($table->gstart + $i) % PLAYERS_N;
339               if ($table->asta_pla[$index_next]) {
340                 log_wr($sess,"GSTART 1");
341                 $table->gstart += $i;
342                 break;
343               }
344             }
345           
346           
347             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
348               $user_cur = &$bri->user[$table->player[$i]];
349               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
350               if ($user_cur->table_pos == ($table->gstart % PLAYERS_N)) 
351                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();', 
352                                 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
353               else
354                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
355                                 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
356               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
357               $user_cur->step_inc();
358             }
359           }
360           else if ($table->asta_pla_n == 0) {
361             log_wr($sess, "PASSANO TUTTI!");
362
363             log_wr($sess, sprintf("GIOCO FINITO !!!"));
364           
365             $table->old_reason = "Hanno passato tutti.";
366             $table->mult *= 2; 
367
368             $table->game_next();
369             $table->game_init(&$bri->user);
370           
371             for ($i = 0 ; $i < PLAYERS_N ; $i++) {      
372               $user_cur = &$bri->user[$table->player[$i]];
373
374               $ret = sprintf('gst.st = %d;', $user_cur->step+1);
375               $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
376               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
377               $user_cur->step_inc();        
378             }
379           }
380           else {
381             log_wr($sess, "FINITA !");
382             // if a_pnt == 120 supergame ! else abbandono
383             if ($a_pnt == 120 || $user->asta_card != -1) {
384               $chooser = $index_cur;
385               for ($i = 1 ; $i < PLAYERS_N ; $i++) 
386                 if ($i != $chooser)
387                   $table->asta_pla[$i] = FALSE;
388             }
389             else {
390               //"gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt).", true); remark_off();";
391               $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();", $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
392               $user->step_inc();
393               for ($i = 1 ; $i < PLAYERS_N ; $i++) {
394                 $chooser = ($table->gstart + $i) % PLAYERS_N;
395                 if ($table->asta_pla[$chooser]) {
396                   break;
397                 }
398               }
399             }
400             $table->asta_win = $chooser;
401
402             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
403               $user_cur = &$bri->user[$table->player[$i]];
404               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
405
406               if ($i == $chooser) {
407                 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
408               }
409               else {
410                 $ret .= "remark_off();";
411               }
412
413               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
414               $user_cur->step_inc();        
415             }
416           }
417         }
418       }
419       else {
420         log_wr($sess, "NON CI SIAMO");
421       }
422     }
423     /*  asta::choose */
424     else if ($argz[0] == 'choose') {
425       if ($table->asta_win > -1 && 
426           $user->table_pos == $table->asta_win) {
427         $a_brisco = $argz[1];
428         if ($a_brisco >= 0 && $a_brisco < 40) {
429           $table->briscola = $a_brisco;
430           $table->friend   = $table->card[$a_brisco]->owner;
431           log_wr($sess,"GSTART 2");
432           $table->gstart = ($table->mazzo+1) % PLAYERS_N;
433           log_wr($sess, "Setta la briscola a ".$a_brisco);
434
435           $chooser = $table->asta_win;
436           $user_chooser = &$bri->user[$table->player[$chooser]];
437           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
438             $user_cur = &$bri->user[$table->player[$i]];
439             $user_cur->subst = 'game';
440             $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
441           
442
443             /* bg of caller cell */
444             $ret .= briscola_show($bri, $table, $user_cur);
445
446             /* first gamer */
447             if ($i == ($table->gstart % PLAYERS_N))
448               $ret .= "is_my_time = true; remark_on();";
449             else
450               $ret .= "is_my_time = false; remark_off();";
451
452             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
453             $user_cur->step_inc();          
454           }
455           /*
456             TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
457             VAR PER PASSARE ALLA FASE DI GIOCO
458           */
459         
460         }
461       }
462     }
463   }
464   else if ($user->subst == 'game') {
465     log_wr($sess, "state: table::game".$argz[0]);
466
467     if ($argz[0] == 'play') {
468       $a_play = $argz[1];
469       $a_x =    $argz[2];
470       $a_y =    $argz[3];
471
472       if (strpos($a_x, "px") != FALSE)
473         $a_x = substr($a_x,0,-2);
474       if (strpos($a_y, "px") != FALSE)
475         $a_y = substr($a_y,0,-2);
476
477       $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
478                        $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
479                        $table->mazzo, $table->gstart,
480                        $table->card[$a_play]->stat, $table->card[$a_play]->owner);
481       log_wr($sess, "CIC".$loggo);
482                           
483       /* se era il suo turno e la carta era sua ed era in mano */
484       if ($a_play >=0 && $a_play < 40 &&
485           ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
486           $table->card[$a_play]->stat == 'hand' &&
487           $table->card[$a_play]->owner == $user->table_pos) {
488         log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
489
490         /* Change the card status. */
491         $table->card[$a_play]->play($a_x, $a_y);
492
493         /*
494          *  !!!! TURN INCREMENTED BEFORE !!!!
495          */
496         $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
497         $table->turn++;
498
499         $card_play = sprintf("card_play(%d,%d,%d,%d);|",
500                              $user->table_pos, $a_play, $a_x, $a_y);
501         if (($table->turn % PLAYERS_N) != 0) {     /* manche not finished */
502           $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
503
504           $player_cur = "remark_off();";
505           $player_nex = $card_play . "is_my_time = true; remark_on();";
506           $player_oth = $card_play;
507         }
508         else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
509           $winner = calculate_winner($table);
510           log_wr($sess,"GSTART 3");
511           $table->gstart = $winner;
512           $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
513
514           log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
515           $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
516                                $winner, $winner);
517           $player_cur = "remark_off();" . $card_take . "|"; 
518           if ($turn_cur != $turn_nex)
519             $player_nex = $card_play . $card_take . "|";
520           else
521             $player_nex = "";
522           if ($table->turn < (PLAYERS_N * 8))  /* game NOT finished */
523             $player_nex .= "is_my_time = true; remark_on();";
524           $player_oth = $card_play . $card_take;
525         }
526
527         log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
528         for ($i = 0 ; $i < PLAYERS_N ; $i++) {  
529           $user_cur = &$bri->user[$table->player[$i]];
530
531           $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
532
533         
534           if ($i == $turn_cur) {
535             $ret .= $player_cur;          
536           }
537           if ($i == $turn_nex) {
538             $ret .= $player_nex;          
539           }
540           if ($i != $turn_cur && $i != $turn_nex) {
541             $ret .= $player_oth;
542           }
543
544           $retar[$i] = $ret;
545         }
546
547
548
549
550         if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
551           log_wr($sess, sprintf("GIOCO FINITO !!!"));
552
553           /* ************************************************ */
554           /*    PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO    */
555           /* ************************************************ */
556           calculate_points(&$table);
557
558           $table->game_next();
559           $table->game_init(&$bri->user);
560           
561           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
562             $user_cur = &$bri->user[$table->player[$i]];
563             $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
564           }
565         }
566
567
568         for ($i = 0 ; $i < PLAYERS_N ; $i++) {  
569           $user_cur = &$bri->user[$table->player[$i]];
570         
571           $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
572           $user_cur->step_inc();            
573         }
574
575         log_wr($sess, sprintf("TURN: %d",$table->turn));
576         /* Have played all the players ? */
577         /* NO:  switch the focus and enable the next player to play. */
578       
579         /* YES: calculate who win and go to the next turn. */
580       }
581     }
582     else
583       log_wr($sess, "NOSENSE");
584   }
585 }
586 log_wr($sess, "before save data");
587 save_data($bri);
588
589 unlock_data($sem);
590 exit;
591 ?>