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