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