proxyscan to check (and deny) proxy connections
[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/proxyscan.phh");
26 require_once("Obj/briskin5.phh");
27
28 // Use of proxies isn't allowed.
29 if (is_proxy()) {
30   sleep(5);
31   exit;
32 }
33
34 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
35   echo "Debugging time!";
36   exit;
37 }
38
39 log_load("bin5/index_wr.php");
40
41 /*
42  *  MAIN
43  */
44 log_wr('COMM: '.$mesg);
45
46 if ($table_idx < 0 || $table_idx >= TABLE_N)
47      exit;
48
49 $sem = Briskin5::lock_data($table_idx);
50
51 if (($bri = &Briskin5::load_data($table_idx,$table_token)) == FALSE) {
52   echo "Bin5 Load data error";
53   log_wr("Bin5 Load data error");
54   Briskin5::unlock_data($sem);
55   exit;
56 }
57
58 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
59   echo "Get User Error";
60   log_wr("Get User Error");
61   Briskin5::unlock_data($sem);
62   exit;
63 }
64 $argz = explode('|', $mesg);
65
66 log_wr('POSTSPLIT: '.$argz[0]);
67
68 if ($argz[0] == 'shutdown') {
69   log_auth($user_cur->sess, "Shutdown session. delegate to room gc the autologout");
70   
71   log_rd2("bin5/index_wr.php: AUTO LOGOUT.");
72   if ($user->stat == 'table') {
73     $bri->table_wakeup(&$user);
74     // to force the logout
75     $user->lacc = 0;
76   }
77   else
78     log_rd2("SHUTDOWN FROM WHAT ???");
79 }
80 /*********************
81  *                   *
82  *    STAT: table    *
83  *                   *
84  *********************/
85 else if ($user->stat == 'table') {
86   $user->laccwr = time();
87   $table = &$bri->table[$user->table];
88
89   if ($argz[0] == 'tableinfo') {
90     log_wr("PER DI TABLEINFO");
91     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
92     $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
93     log_wr($user->comm[$user->step % COMM_N]);
94     $user->step_inc();
95   }
96   else if ($argz[0] == 'chatt') {
97     $bri->chatt_send(&$user,$mesg);
98   }
99   else if ($argz[0] == 'logout') {
100     $remcalc = $argz[1];
101
102     if ($user->exitislock == TRUE) {
103       $remcalc++;
104       $user->exitislock = FALSE;
105     }
106
107     $logout_cont = TRUE;
108     if ($remcalc >= 3) {
109       $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
110       if ($lockcalc < 3) {
111         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
112         $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
113         $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);
114         
115         log_wr($user->comm[$user->step % COMM_N]);
116         $user->step_inc();
117         $logout_cont = FALSE;
118       }
119     }
120     else 
121       $user->bantime = $user->laccwr + BAN_TIME;
122     
123     if ($logout_cont == TRUE) {
124       $bri->table_wakeup(&$user);
125     }
126   }
127   else if ($argz[0] == 'exitlock') {
128     $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
129     for ($ct = 0, $i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {    
130       $user_cur[$i] = &$bri->user[$table->player[$i]];
131       if ($user_cur[$i]->exitislock == FALSE)
132         $ct++;
133     }
134     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
135       $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
136       $ret .= sprintf('exitlock_show(%d, %s);', $ct, 
137                      ($user_cur[$i]->exitislock ? 'true' : 'false'));
138       $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
139       log_wr($user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
140       $user_cur[$i]->step_inc();
141     }
142   }
143   else if ($user->subst == 'asta') {
144     if ($argz[0] == 'lascio' && $user->handpt <= 2) {
145       $index_cur = $table->gstart % BRISKIN5_PLAYERS_N;
146     
147       log_wr(sprintf("GIOCO FINITO !!!"));
148     
149       $table->mult *= 2; 
150       $table->old_reason = sprintf("Ha lasciato %s perche` aveva al massimo 2 punti.", $user->name);
151
152       $table->game_next();
153       $table->game_init(&$bri->user);
154     
155       for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {   
156         $user_cur = &$bri->user[$table->player[$i]];
157
158         $ret = sprintf('gst.st = %d;', $user_cur->step+1);
159         $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
160         $user_cur->comm[$user_cur->step % COMM_N] = $ret;
161         $user_cur->step_inc();      
162       }
163     }
164     else if ($argz[0] == 'asta') {
165       $again = TRUE;
166     
167       $index_cur = $table->gstart % BRISKIN5_PLAYERS_N;
168       if ($user->table_pos == $index_cur &&
169           $table->asta_pla[$index_cur]) {
170         $a_card = $argz[1];
171         $a_pnt  = $argz[2];
172       
173         log_wr("CI SIAMO  a_card ".$a_card."  asta_card ".$table->asta_card);
174       
175         // Abbandono dell'asta
176         if ($a_card <= -1) {
177           log_wr("Abbandona l'asta.");
178           $table->asta_pla[$index_cur] = FALSE;
179           $user->asta_card  = -1;
180           $table->asta_pla_n--;
181           $again = FALSE;
182         }
183         else if ($a_card <= 9) {
184           if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
185             $again = FALSE;
186           else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
187             $again = FALSE;
188           
189
190           if ($again == FALSE) {
191             log_wr("NUOVI ORZI.");
192             $user->asta_card  = $a_card;
193             $table->asta_card = $a_card;
194             if ($a_card == 9) {
195               $user->asta_pnt   = $a_pnt;
196               $table->asta_pnt  = $a_pnt;
197             }
198           }
199         }
200       
201       
202       
203         if ($again) { // Qualcosa non andato bene, rifare
204           $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1, 
205                          ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
206           $user->comm[$user->step % COMM_N] = $ret;
207           $user->step_inc();
208
209           log_wr("Ripetere.");
210         }
211         else {
212           /* next step */
213           $showst = "show_astat("; 
214           for ($i = 0 ; $i < BRISKIN5_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 (BRISKIN5_PLAYERS_N == 3)
220             $showst .= ",-2,-2";
221           $showst .= ");";
222
223           $maxcard = -2;
224           for ($i = 0 ; $i < BRISKIN5_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             log_wr("ALLOPPA QUI");
233             for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
234               $index_next = ($table->gstart + $i) % BRISKIN5_PLAYERS_N;
235               if ($table->asta_pla[$index_next]) {
236                 log_wr("GSTART 1");
237                 $table->gstart += $i;
238                 break;
239               }
240             }
241           
242           
243             for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
244               $user_cur = &$bri->user[$table->player[$i]];
245               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
246               if ($user_cur->table_pos == ($table->gstart % BRISKIN5_PLAYERS_N)) 
247                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();', 
248                                 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
249               else
250                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
251                                 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
252               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
253               $user_cur->step_inc();
254             }
255           }
256           else if ($table->asta_pla_n == 0) {
257             log_wr("PASSANO TUTTI!");
258
259             log_wr(sprintf("GIOCO FINITO !!!"));
260           
261             $table->old_reason = "Hanno passato tutti.";
262             $table->mult *= 2; 
263
264             $table->game_next();
265             $table->game_init(&$bri->user);
266           
267             for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {     
268               $user_cur = &$bri->user[$table->player[$i]];
269
270               $ret = sprintf('gst.st = %d;', $user_cur->step+1);
271               $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
272               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
273               $user_cur->step_inc();        
274             }
275           }
276           else {
277             log_wr("FINITA !");
278             // if a_pnt == 120 supergame ! else abbandono
279             if ($a_pnt == 120 || $user->asta_card != -1) {
280               $chooser = $index_cur;
281               for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++) 
282                 if ($i != $chooser)
283                   $table->asta_pla[$i] = FALSE;
284             }
285             else {
286               //"gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt).", true); remark_off();";
287               $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));
288               $user->step_inc();
289               for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
290                 $chooser = ($table->gstart + $i) % BRISKIN5_PLAYERS_N;
291                 if ($table->asta_pla[$chooser]) {
292                   break;
293                 }
294               }
295             }
296             $table->asta_win = $chooser;
297
298             for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
299               $user_cur = &$bri->user[$table->player[$i]];
300               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
301
302               if ($i == $chooser) {
303                 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
304               }
305               else {
306                 $ret .= "remark_off();";
307               }
308
309               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
310               $user_cur->step_inc();        
311             }
312           }
313         }
314       }
315       else {
316         log_wr("NON CI SIAMO");
317       }
318     }
319     /*  asta::choose */
320     else if ($argz[0] == 'choose') {
321       if ($table->asta_win > -1 && 
322           $user->table_pos == $table->asta_win) {
323         $a_brisco = $argz[1];
324         if ($a_brisco >= 0 && $a_brisco < 40) {
325           $table->briscola = $a_brisco;
326           $table->friend   = $table->card[$a_brisco]->owner;
327           log_wr("GSTART 2");
328           $table->gstart = ($table->mazzo+1) % BRISKIN5_PLAYERS_N;
329           log_wr("Setta la briscola a ".$a_brisco);
330
331           $chooser = $table->asta_win;
332           $user_chooser = &$bri->user[$table->player[$chooser]];
333           for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
334             $user_cur = &$bri->user[$table->player[$i]];
335             $user_cur->subst = 'game';
336             $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
337           
338
339             /* bg of caller cell */
340             $ret .= briscola_show($bri, $table, $user_cur);
341
342             /* first gamer */
343             if ($i == ($table->gstart % BRISKIN5_PLAYERS_N))
344               $ret .= "is_my_time = true; remark_on();";
345             else
346               $ret .= "is_my_time = false; remark_off();";
347
348             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
349             $user_cur->step_inc();          
350           }
351           /*
352             TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
353             VAR PER PASSARE ALLA FASE DI GIOCO
354           */
355         
356         }
357       }
358     }
359   }
360   else if ($user->subst == 'game') {
361     log_wr("state: table::game".$argz[0]);
362
363     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 % BRISKIN5_PLAYERS_N),
375                        $table->mazzo, $table->gstart,
376                        $table->card[$a_play]->stat, $table->card[$a_play]->owner);
377       log_wr("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) % BRISKIN5_PLAYERS_N)) &&
382           $table->card[$a_play]->stat == 'hand' &&
383           $table->card[$a_play]->owner == $user->table_pos) {
384         log_wr(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) % BRISKIN5_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 % BRISKIN5_PLAYERS_N) != 0) {     /* manche not finished */
398           $turn_nex = ($table->gstart + $table->turn) % BRISKIN5_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 <= (BRISKIN5_PLAYERS_N * 8)) { /* manche finished */
405           $winner = calculate_winner($table);
406           log_wr("GSTART 3");
407           $table->gstart = $winner;
408           $turn_nex = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
409
410           log_wr(sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
411           $card_take = sprintf("sleep(gst,2000);|cards_take(%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 < (BRISKIN5_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(sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
424         for ($i = 0 ; $i < BRISKIN5_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 == (BRISKIN5_PLAYERS_N * 8)) { /* game finished */
447           log_wr(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->user);
456           
457           for ($i = 0 ; $i < BRISKIN5_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 < BRISKIN5_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_inc();            
469         }
470
471         log_wr(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("NOSENSE");
480   }
481 }
482 log_wr("before save data");
483 Briskin5::save_data($bri);
484
485 Briskin5::unlock_data($sem);
486 exit;
487 ?>