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