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