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