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