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