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