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