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