6c33bafecc7a33968569a4888977285a5163a96b
[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 ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
291             $again = FALSE;
292           else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
293             $again = FALSE;
294           
295
296           if ($again == FALSE) {
297             log_wr($sess, "NUOVI ORZI.");
298             $user->asta_card  = $a_card;
299             $table->asta_card = $a_card;
300             if ($a_card == 9) {
301               $user->asta_pnt   = $a_pnt;
302               $table->asta_pnt  = $a_pnt;
303             }
304           }
305         }
306       
307       
308       
309         if ($again) { // Qualcosa non andato bene, rifare
310           $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1, 
311                          ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
312           $user->comm[$user->step % COMM_N] = $ret;
313           $user->step_inc();
314
315           log_wr($sess, "Ripetere.");
316         }
317         else {
318           /* next step */
319           $showst = "show_astat("; 
320           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
321             $user_cur = &$bri->user[$table->player[$i]];
322             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
323                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
324           }
325           if (PLAYERS_N == 3)
326             $showst .= ",-2,-2";
327           $showst .= ");";
328
329           $maxcard = -2;
330           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
331             $user_cur = &$bri->user[$table->player[$i]];
332             if ($maxcard < $user_cur->asta_card)
333               $maxcard = $user_cur->asta_card;
334           }
335
336           if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
337               !($table->asta_card == 9 && $table->asta_pnt == 120)) {
338             log_wr($sess,"ALLOPPA QUI");
339             for ($i = 1 ; $i < PLAYERS_N ; $i++) {
340               $index_next = ($table->gstart + $i) % PLAYERS_N;
341               if ($table->asta_pla[$index_next]) {
342                 log_wr($sess,"GSTART 1");
343                 $table->gstart += $i;
344                 break;
345               }
346             }
347           
348           
349             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
350               $user_cur = &$bri->user[$table->player[$i]];
351               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
352               if ($user_cur->table_pos == ($table->gstart % PLAYERS_N)) 
353                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();', 
354                                 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
355               else
356                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
357                                 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
358               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
359               $user_cur->step_inc();
360             }
361           }
362           else if ($table->asta_pla_n == 0) {
363             log_wr($sess, "PASSANO TUTTI!");
364
365             log_wr($sess, sprintf("GIOCO FINITO !!!"));
366           
367             $table->old_reason = "Hanno passato tutti.";
368             $table->mult *= 2; 
369
370             $table->game_next();
371             $table->game_init(&$bri->user);
372           
373             for ($i = 0 ; $i < PLAYERS_N ; $i++) {      
374               $user_cur = &$bri->user[$table->player[$i]];
375
376               $ret = sprintf('gst.st = %d;', $user_cur->step+1);
377               $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
378               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
379               $user_cur->step_inc();        
380             }
381           }
382           else {
383             log_wr($sess, "FINITA !");
384             // if a_pnt == 120 supergame ! else abbandono
385             if ($a_pnt == 120 || $user->asta_card != -1) {
386               $chooser = $index_cur;
387               for ($i = 1 ; $i < PLAYERS_N ; $i++) 
388                 if ($i != $chooser)
389                   $table->asta_pla[$i] = FALSE;
390             }
391             else {
392               //"gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt).", true); remark_off();";
393               $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));
394               $user->step_inc();
395               for ($i = 1 ; $i < PLAYERS_N ; $i++) {
396                 $chooser = ($table->gstart + $i) % PLAYERS_N;
397                 if ($table->asta_pla[$chooser]) {
398                   break;
399                 }
400               }
401             }
402             $table->asta_win = $chooser;
403
404             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
405               $user_cur = &$bri->user[$table->player[$i]];
406               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
407
408               if ($i == $chooser) {
409                 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
410               }
411               else {
412                 $ret .= "remark_off();";
413               }
414
415               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
416               $user_cur->step_inc();        
417             }
418           }
419         }
420       }
421       else {
422         log_wr($sess, "NON CI SIAMO");
423       }
424     }
425     /*  asta::choose */
426     else if ($argz[0] == 'choose') {
427       if ($table->asta_win > -1 && 
428           $user->table_pos == $table->asta_win) {
429         $a_brisco = $argz[1];
430         if ($a_brisco >= 0 && $a_brisco < 40) {
431           $table->briscola = $a_brisco;
432           $table->friend   = $table->card[$a_brisco]->owner;
433           log_wr($sess,"GSTART 2");
434           $table->gstart = ($table->mazzo+1) % PLAYERS_N;
435           log_wr($sess, "Setta la briscola a ".$a_brisco);
436
437           $chooser = $table->asta_win;
438           $user_chooser = &$bri->user[$table->player[$chooser]];
439           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
440             $user_cur = &$bri->user[$table->player[$i]];
441             $user_cur->subst = 'game';
442             $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
443           
444
445             /* bg of caller cell */
446             $ret .= briscola_show($bri, $table, $user_cur);
447
448             /* first gamer */
449             if ($i == ($table->gstart % PLAYERS_N))
450               $ret .= "is_my_time = true; remark_on();";
451             else
452               $ret .= "is_my_time = false; remark_off();";
453
454             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
455             $user_cur->step_inc();          
456           }
457           /*
458             TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
459             VAR PER PASSARE ALLA FASE DI GIOCO
460           */
461         
462         }
463       }
464     }
465   }
466   else if ($user->subst == 'game') {
467     log_wr($sess, "state: table::game".$argz[0]);
468
469     if ($argz[0] == 'play') {
470       $a_play = $argz[1];
471       $a_x =    $argz[2];
472       $a_y =    $argz[3];
473
474       if (strpos($a_x, "px") != FALSE)
475         $a_x = substr($a_x,0,-2);
476       if (strpos($a_y, "px") != FALSE)
477         $a_y = substr($a_y,0,-2);
478
479       $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
480                        $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
481                        $table->mazzo, $table->gstart,
482                        $table->card[$a_play]->stat, $table->card[$a_play]->owner);
483       log_wr($sess, "CIC".$loggo);
484                           
485       /* se era il suo turno e la carta era sua ed era in mano */
486       if ($a_play >=0 && $a_play < 40 &&
487           ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
488           $table->card[$a_play]->stat == 'hand' &&
489           $table->card[$a_play]->owner == $user->table_pos) {
490         log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
491
492         /* Change the card status. */
493         $table->card[$a_play]->play($a_x, $a_y);
494
495         /*
496          *  !!!! TURN INCREMENTED BEFORE !!!!
497          */
498         $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
499         $table->turn++;
500
501         $card_play = sprintf("card_play(%d,%d,%d,%d);|",
502                              $user->table_pos, $a_play, $a_x, $a_y);
503         if (($table->turn % PLAYERS_N) != 0) {     /* manche not finished */
504           $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
505
506           $player_cur = "remark_off();";
507           $player_nex = $card_play . "is_my_time = true; remark_on();";
508           $player_oth = $card_play;
509         }
510         else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
511           $winner = calculate_winner($table);
512           log_wr($sess,"GSTART 3");
513           $table->gstart = $winner;
514           $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
515
516           log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
517           $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
518                                $winner, $winner);
519           $player_cur = "remark_off();" . $card_take . "|"; 
520           if ($turn_cur != $turn_nex)
521             $player_nex = $card_play . $card_take . "|";
522           else
523             $player_nex = "";
524           if ($table->turn < (PLAYERS_N * 8))  /* game NOT finished */
525             $player_nex .= "is_my_time = true; remark_on();";
526           $player_oth = $card_play . $card_take;
527         }
528
529         log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
530         for ($i = 0 ; $i < PLAYERS_N ; $i++) {  
531           $user_cur = &$bri->user[$table->player[$i]];
532
533           $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
534
535         
536           if ($i == $turn_cur) {
537             $ret .= $player_cur;          
538           }
539           if ($i == $turn_nex) {
540             $ret .= $player_nex;          
541           }
542           if ($i != $turn_cur && $i != $turn_nex) {
543             $ret .= $player_oth;
544           }
545
546           $retar[$i] = $ret;
547         }
548
549
550
551
552         if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
553           log_wr($sess, sprintf("GIOCO FINITO !!!"));
554
555           /* ************************************************ */
556           /*    PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO    */
557           /* ************************************************ */
558           calculate_points(&$table);
559
560           $table->game_next();
561           $table->game_init(&$bri->user);
562           
563           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
564             $user_cur = &$bri->user[$table->player[$i]];
565             $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
566           }
567         }
568
569
570         for ($i = 0 ; $i < PLAYERS_N ; $i++) {  
571           $user_cur = &$bri->user[$table->player[$i]];
572         
573           $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
574           $user_cur->step_inc();            
575         }
576
577         log_wr($sess, sprintf("TURN: %d",$table->turn));
578         /* Have played all the players ? */
579         /* NO:  switch the focus and enable the next player to play. */
580       
581         /* YES: calculate who win and go to the next turn. */
582       }
583     }
584     else
585       log_wr($sess, "NOSENSE");
586   }
587 }
588 log_wr($sess, "before save data");
589 save_data($bri);
590
591 unlock_data($sem);
592 exit;
593 ?>