renamed class brisco to Room, some free funcs moved to static class funcs, split...
[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 = Room::lock_data();
38 $room = &Room::load_data();
39 if (($user = &$room->get_user($sess, &$idx)) == FALSE) {
40   echo "Get User Error";
41   log_wr($sess, "Get User Error");
42   Room::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     $room->room_wakeup(&$user);
58   else if ($user->subst == 'standup')
59     $room->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     $room->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         Room::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         Room::save_data($room);
110         Room::unlock_data($sem);
111         exit;
112       }
113     
114       // Take parameters
115       $table_idx = $argz[1];
116       $table = &$room->table[$table_idx];
117     
118       if ($table->player_n == PLAYERS_N) {
119         log_wr($sess, "WARN:FSM: Sitdown unreachable, table full.");
120         Room::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         //
134         //  START THE SPAWN HERE!!!!
135         //
136
137         if (TRUE) { // WITH SPAWN
138         }
139         else { // BEFORE SPAWN
140           // init table
141           $table->init(&$room->user);
142           $table->game_init(&$room->user);
143           $curtime = time();
144           
145           // init users
146           for ($i = 0 ; $i < $table->player_n ; $i++) {
147             $user_cur = &$room->user[$table->player[$i]];
148             log_wr($sess, "Pre if!");
149             
150             $ret = "";
151             $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
152             
153             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
154             $user_cur->trans_step = $user_cur->step + 1;
155             log_wr($sess, "TRANS ATTIVATO");
156             
157             
158             $user_cur->stat_set('table');
159             $user_cur->subst = 'asta';
160             $user_cur->laccwr = $curtime;
161             $user_cur->step_inc();
162             
163             $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$room,&$user_cur,$user_cur->step+1,TRUE, FALSE);
164             $user_cur->step_inc();
165           }
166         } // end else {  BEFORE SPAWN
167         
168         // change room
169         $room->room_sitdown(&$user, $table_idx);
170       }
171     }
172     else if ($argz[0] == 'logout') {
173       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
174       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
175       $user->the_end = TRUE;
176       $user->step_inc();
177     }
178   }
179   /**********************
180    *                    *
181    *   SUBST: sitdown   *
182    *                    *
183    **********************/
184   else if ($user->subst == 'sitdown') {
185     if ($argz[0] == 'wakeup') {
186       $room->room_wakeup(&$user);      
187     }
188     else if ($argz[0] == 'logout') {
189       $room->room_wakeup(&$user);      
190       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
191       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
192       $user->the_end = TRUE;
193       $user->step_inc();
194     }
195   }
196 }
197 /*********************
198  *                   *
199  *    STAT: table    *
200  *                   *
201  *********************/
202 else if ($user->stat == 'table') {
203   $user->laccwr = time();
204   $table = &$room->table[$user->table];
205
206   if ($argz[0] == 'tableinfo') {
207     log_wr($sess, "PER DI TABLEINFO");
208     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
209     $user->comm[$user->step % COMM_N] .= show_table_info(&$room, &$table, $user->table_pos);
210     log_wr($sess, $user->comm[$user->step % COMM_N]);
211     $user->step_inc();
212   }
213   else if ($argz[0] == 'chatt') {
214     $room->chatt_send(&$user,$mesg);
215   }
216   else if ($argz[0] == 'logout') {
217     $remcalc = $argz[1];
218
219     if ($user->exitislock == TRUE) {
220       $remcalc++;
221       $user->exitislock = FALSE;
222     }
223
224     $logout_cont = TRUE;
225     if ($remcalc >= 3) {
226       $lockcalc = $table->exitlock_calc(&$room->user, $user->table_pos);
227       if ($lockcalc < 3) {
228         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
229         $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$room->user, $user->table_pos);
230         $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);
231         
232         log_wr($sess, $user->comm[$user->step % COMM_N]);
233         $user->step_inc();
234         $logout_cont = FALSE;
235       }
236     }
237     else 
238       $user->bantime = $user->laccwr + BAN_TIME;
239     
240     if ($logout_cont == TRUE) {
241       $room->room_wakeup(&$user);
242     }
243   }
244   else if ($argz[0] == 'exitlock') {
245     $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
246     for ($ct = 0, $i = 0 ; $i < PLAYERS_N ; $i++) {     
247       $user_cur[$i] = &$room->user[$table->player[$i]];
248       if ($user_cur[$i]->exitislock == FALSE)
249         $ct++;
250     }
251     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
252       $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
253       $ret .= sprintf('exitlock_show(%d, %s);', $ct, 
254                      ($user_cur[$i]->exitislock ? 'true' : 'false'));
255       $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
256       log_wr($sess, $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
257       $user_cur[$i]->step_inc();
258     }
259   }
260   else if ($user->subst == 'asta') {
261     if ($argz[0] == 'lascio' && $user->handpt <= 2) {
262       $index_cur = $table->gstart % PLAYERS_N;
263     
264       log_wr($sess, sprintf("GIOCO FINITO !!!"));
265     
266       $table->mult *= 2; 
267       $table->old_reason = sprintf("Ha lasciato %s perche` aveva al massimo 2 punti.", $user->name);
268
269       $table->game_next();
270       $table->game_init(&$room->user);
271     
272       for ($i = 0 ; $i < PLAYERS_N ; $i++) {    
273         $user_cur = &$room->user[$table->player[$i]];
274
275         $ret = sprintf('gst.st = %d;', $user_cur->step+1);
276         $ret .= show_table(&$room,&$user_cur,$user_cur->step+1, TRUE, TRUE);
277         $user_cur->comm[$user_cur->step % COMM_N] = $ret;
278         $user_cur->step_inc();      
279       }
280     }
281     else if ($argz[0] == 'asta') {
282       $again = TRUE;
283     
284       $index_cur = $table->gstart % PLAYERS_N;
285       if ($user->table_pos == $index_cur &&
286           $table->asta_pla[$index_cur]) {
287         $a_card = $argz[1];
288         $a_pnt  = $argz[2];
289       
290         log_wr($sess, "CI SIAMO  a_card ".$a_card."  asta_card ".$table->asta_card);
291       
292         // Abbandono dell'asta
293         if ($a_card <= -1) {
294           log_wr($sess, "Abbandona l'asta.");
295           $table->asta_pla[$index_cur] = FALSE;
296           $user->asta_card  = -1;
297           $table->asta_pla_n--;
298           $again = FALSE;
299         }
300         else if ($a_card <= 9) {
301           if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
302             $again = FALSE;
303           else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
304             $again = FALSE;
305           
306
307           if ($again == FALSE) {
308             log_wr($sess, "NUOVI ORZI.");
309             $user->asta_card  = $a_card;
310             $table->asta_card = $a_card;
311             if ($a_card == 9) {
312               $user->asta_pnt   = $a_pnt;
313               $table->asta_pnt  = $a_pnt;
314             }
315           }
316         }
317       
318       
319       
320         if ($again) { // Qualcosa non andato bene, rifare
321           $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1, 
322                          ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
323           $user->comm[$user->step % COMM_N] = $ret;
324           $user->step_inc();
325
326           log_wr($sess, "Ripetere.");
327         }
328         else {
329           /* next step */
330           $showst = "show_astat("; 
331           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
332             $user_cur = &$room->user[$table->player[$i]];
333             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
334                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
335           }
336           if (PLAYERS_N == 3)
337             $showst .= ",-2,-2";
338           $showst .= ");";
339
340           $maxcard = -2;
341           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
342             $user_cur = &$room->user[$table->player[$i]];
343             if ($maxcard < $user_cur->asta_card)
344               $maxcard = $user_cur->asta_card;
345           }
346
347           if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
348               !($table->asta_card == 9 && $table->asta_pnt == 120)) {
349             log_wr($sess,"ALLOPPA QUI");
350             for ($i = 1 ; $i < PLAYERS_N ; $i++) {
351               $index_next = ($table->gstart + $i) % PLAYERS_N;
352               if ($table->asta_pla[$index_next]) {
353                 log_wr($sess,"GSTART 1");
354                 $table->gstart += $i;
355                 break;
356               }
357             }
358           
359           
360             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
361               $user_cur = &$room->user[$table->player[$i]];
362               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
363               if ($user_cur->table_pos == ($table->gstart % PLAYERS_N)) 
364                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();', 
365                                 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
366               else
367                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
368                                 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
369               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
370               $user_cur->step_inc();
371             }
372           }
373           else if ($table->asta_pla_n == 0) {
374             log_wr($sess, "PASSANO TUTTI!");
375
376             log_wr($sess, sprintf("GIOCO FINITO !!!"));
377           
378             $table->old_reason = "Hanno passato tutti.";
379             $table->mult *= 2; 
380
381             $table->game_next();
382             $table->game_init(&$room->user);
383           
384             for ($i = 0 ; $i < PLAYERS_N ; $i++) {      
385               $user_cur = &$room->user[$table->player[$i]];
386
387               $ret = sprintf('gst.st = %d;', $user_cur->step+1);
388               $ret .= show_table(&$room,&$user_cur,$user_cur->step+1, TRUE, TRUE);
389               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
390               $user_cur->step_inc();        
391             }
392           }
393           else {
394             log_wr($sess, "FINITA !");
395             // if a_pnt == 120 supergame ! else abbandono
396             if ($a_pnt == 120 || $user->asta_card != -1) {
397               $chooser = $index_cur;
398               for ($i = 1 ; $i < PLAYERS_N ; $i++) 
399                 if ($i != $chooser)
400                   $table->asta_pla[$i] = FALSE;
401             }
402             else {
403               //"gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt).", true); remark_off();";
404               $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));
405               $user->step_inc();
406               for ($i = 1 ; $i < PLAYERS_N ; $i++) {
407                 $chooser = ($table->gstart + $i) % PLAYERS_N;
408                 if ($table->asta_pla[$chooser]) {
409                   break;
410                 }
411               }
412             }
413             $table->asta_win = $chooser;
414
415             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
416               $user_cur = &$room->user[$table->player[$i]];
417               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
418
419               if ($i == $chooser) {
420                 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
421               }
422               else {
423                 $ret .= "remark_off();";
424               }
425
426               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
427               $user_cur->step_inc();        
428             }
429           }
430         }
431       }
432       else {
433         log_wr($sess, "NON CI SIAMO");
434       }
435     }
436     /*  asta::choose */
437     else if ($argz[0] == 'choose') {
438       if ($table->asta_win > -1 && 
439           $user->table_pos == $table->asta_win) {
440         $a_brisco = $argz[1];
441         if ($a_brisco >= 0 && $a_brisco < 40) {
442           $table->briscola = $a_brisco;
443           $table->friend   = $table->card[$a_brisco]->owner;
444           log_wr($sess,"GSTART 2");
445           $table->gstart = ($table->mazzo+1) % PLAYERS_N;
446           log_wr($sess, "Setta la briscola a ".$a_brisco);
447
448           $chooser = $table->asta_win;
449           $user_chooser = &$room->user[$table->player[$chooser]];
450           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
451             $user_cur = &$room->user[$table->player[$i]];
452             $user_cur->subst = 'game';
453             $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
454           
455
456             /* bg of caller cell */
457             $ret .= briscola_show($room, $table, $user_cur);
458
459             /* first gamer */
460             if ($i == ($table->gstart % PLAYERS_N))
461               $ret .= "is_my_time = true; remark_on();";
462             else
463               $ret .= "is_my_time = false; remark_off();";
464
465             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
466             $user_cur->step_inc();          
467           }
468           /*
469             TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
470             VAR PER PASSARE ALLA FASE DI GIOCO
471           */
472         
473         }
474       }
475     }
476   }
477   else if ($user->subst == 'game') {
478     log_wr($sess, "state: table::game".$argz[0]);
479
480     if ($argz[0] == 'play') {
481       $a_play = $argz[1];
482       $a_x =    $argz[2];
483       $a_y =    $argz[3];
484
485       if (strpos($a_x, "px") != FALSE)
486         $a_x = substr($a_x,0,-2);
487       if (strpos($a_y, "px") != FALSE)
488         $a_y = substr($a_y,0,-2);
489
490       $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
491                        $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
492                        $table->mazzo, $table->gstart,
493                        $table->card[$a_play]->stat, $table->card[$a_play]->owner);
494       log_wr($sess, "CIC".$loggo);
495                           
496       /* se era il suo turno e la carta era sua ed era in mano */
497       if ($a_play >=0 && $a_play < 40 &&
498           ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
499           $table->card[$a_play]->stat == 'hand' &&
500           $table->card[$a_play]->owner == $user->table_pos) {
501         log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
502
503         /* Change the card status. */
504         $table->card[$a_play]->play($a_x, $a_y);
505
506         /*
507          *  !!!! TURN INCREMENTED BEFORE !!!!
508          */
509         $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
510         $table->turn++;
511
512         $card_play = sprintf("card_play(%d,%d,%d,%d);|",
513                              $user->table_pos, $a_play, $a_x, $a_y);
514         if (($table->turn % PLAYERS_N) != 0) {     /* manche not finished */
515           $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
516
517           $player_cur = "remark_off();";
518           $player_nex = $card_play . "is_my_time = true; remark_on();";
519           $player_oth = $card_play;
520         }
521         else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
522           $winner = calculate_winner($table);
523           log_wr($sess,"GSTART 3");
524           $table->gstart = $winner;
525           $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
526
527           log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $room->user[$table->player[$winner]]->name));
528           $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
529                                $winner, $winner);
530           $player_cur = "remark_off();" . $card_take . "|"; 
531           if ($turn_cur != $turn_nex)
532             $player_nex = $card_play . $card_take . "|";
533           else
534             $player_nex = "";
535           if ($table->turn < (PLAYERS_N * 8))  /* game NOT finished */
536             $player_nex .= "is_my_time = true; remark_on();";
537           $player_oth = $card_play . $card_take;
538         }
539
540         log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
541         for ($i = 0 ; $i < PLAYERS_N ; $i++) {  
542           $user_cur = &$room->user[$table->player[$i]];
543
544           $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
545
546         
547           if ($i == $turn_cur) {
548             $ret .= $player_cur;          
549           }
550           if ($i == $turn_nex) {
551             $ret .= $player_nex;          
552           }
553           if ($i != $turn_cur && $i != $turn_nex) {
554             $ret .= $player_oth;
555           }
556
557           $retar[$i] = $ret;
558         }
559
560
561
562
563         if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
564           log_wr($sess, sprintf("GIOCO FINITO !!!"));
565
566           /* ************************************************ */
567           /*    PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO    */
568           /* ************************************************ */
569           calculate_points(&$table);
570
571           $table->game_next();
572           $table->game_init(&$room->user);
573           
574           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
575             $user_cur = &$room->user[$table->player[$i]];
576             $retar[$i] .= show_table(&$room,&$user_cur,$user_cur->step+1,TRUE, TRUE);
577           }
578         }
579
580
581         for ($i = 0 ; $i < PLAYERS_N ; $i++) {  
582           $user_cur = &$room->user[$table->player[$i]];
583         
584           $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
585           $user_cur->step_inc();            
586         }
587
588         log_wr($sess, sprintf("TURN: %d",$table->turn));
589         /* Have played all the players ? */
590         /* NO:  switch the focus and enable the next player to play. */
591       
592         /* YES: calculate who win and go to the next turn. */
593       }
594     }
595     else
596       log_wr($sess, "NOSENSE");
597   }
598 }
599 log_wr($sess, "before save data");
600 Room::save_data($room);
601
602 Room::unlock_data($sem);
603 exit;
604 ?>