step_proxy and banlist started
[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 = &$bri->get_user($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 ($argz[0] == 'shutdown') {
48   log_auth($user_cur->sess, "Shutdown session.");
49   $tmp_sess = $user->sess;
50   $user->sess = "";
51   step_unproxy($tmp_sess);
52   $user->name = "";
53   $user->the_end = FALSE;
54   
55   log_rd2($user->sess, "AUTO LOGOUT.");
56   if ($user->subst == 'sitdown' || $user->stat == 'table')
57     $bri->room_wakeup(&$user);
58   else if ($user->subst == 'standup')
59     $bri->room_outstandup(&$user);
60   else
61     log_rd2($sess, "SHUTDOWN FROM WHAT ???");
62 }
63 else if ($user->stat == 'room') {
64   $user->laccwr = time();
65
66   if ($argz[0] == 'help') {
67     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
68     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
69
70     log_wr($sess, $user->comm[$user->step % COMM_N]);
71     $user->step_inc();
72     
73   }
74   else if ($argz[0] == 'about') {
75     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
76     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
77
78     log_wr($sess, $user->comm[$user->step % COMM_N]);
79     $user->step_inc();
80     
81   }
82   else if ($argz[0] == 'logout') {
83     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
84     $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
85     $user->the_end = TRUE;
86     $user->step_inc();
87   }
88   else if ($argz[0] == 'chatt') {
89     $bri->chatt_send(&$user,$mesg);
90   }
91   else if ($argz[0] == 'sitdown') {
92     if ($user->stat != 'room' || $user->subst != 'standup') {
93       log_wr($sess, "Warning ! sitdown out fsm");
94       unlock_data($sem);
95       exit;
96     }
97                 
98     // BAN_IP_CHECK
99     if ($user->bantime > $user->laccwr) {
100       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
101       $user->comm[$user->step % COMM_N] .= show_notify("<br>Ti sei alzato da un tavolo senza il consenso degli altri giocatori. Dovrai aspettare ancora ".secstoword($user->bantime - $user->laccwr)." prima di poterti sedere nuovamente.", 2000, "Torna in piedi.", 400, 100);
102       
103       $user->step_inc();
104       save_data($bri);
105       unlock_data($sem);
106       exit;
107     }
108     
109     // Take parameters
110     $table_idx = $argz[1];
111     $table = &$bri->table[$table_idx];
112     
113     if ($table->player_n == PLAYERS_N) {
114       log_wr($sess, "Warning ! unreachable, table full.");
115       unlock_data($sem);
116       exit;
117     } 
118
119     // set new status
120     $user->subst = "sitdown";
121     $user->table = $table_idx;
122     $user->table_pos = $table->user_add($idx);
123                 
124     if ($table->player_n == PLAYERS_N) {
125       // Start game for this table.
126       log_wr($sess, "Start game!");
127       
128       $table->init(&$bri->user);
129       $table->game_init(&$bri->user);
130       $curtime = time();
131
132       for ($i = 0 ; $i < $table->player_n ; $i++) {
133         $user_cur = &$bri->user[$table->player[$i]];
134         log_wr($sess, "Pre if!");
135         
136         $ret = "";
137         $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
138         
139         $user_cur->comm[$user_cur->step % COMM_N] = $ret;
140         $user_cur->trans_step = $user_cur->step + 1;
141         log_wr($sess, "TRANS ATTIVATO");
142         
143         $user_cur->stat_set('table');
144         $user_cur->subst = 'asta';
145         $user_cur->laccwr = $curtime;
146         $user_cur->step_inc();
147         
148         $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
149         $user_cur->step_inc();
150       }
151     }
152                 
153     $bri->room_sitdown(&$user, $table_idx);
154   }
155   else if ($argz[0] == 'wakeup') {
156     if ($user->stat != 'room' || $user->subst != 'sitdown') {
157       log_wr($sess, "Warning ! wakeup out fsm.");
158       unlock_data($sem);
159       exit;
160     }
161                 
162     // set new status
163
164     $user->subst = "standup";
165
166     $bri->room_wakeup(&$user);
167   }
168 }
169 /***************
170  *             *
171  *    TABLE    *
172  *             *
173  ***************/
174 else if ($user->stat == 'table') {
175   $user->laccwr = time();
176   $table = &$bri->table[$user->table];
177
178   if ($argz[0] == 'tableinfo') {
179     log_wr($sess, "PER DI TABLEINFO");
180     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
181     $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
182     log_wr($sess, $user->comm[$user->step % COMM_N]);
183     $user->step_inc();
184   }
185   else if ($argz[0] == 'chatt') {
186     $bri->chatt_send(&$user,$mesg);
187   }
188   else if ($argz[0] == 'logout') {
189     $remcalc = $argz[1];
190
191     if ($user->exitislock == TRUE) {
192       $remcalc++;
193       $user->exitislock = FALSE;
194     }
195
196     $logout_cont = TRUE;
197     if ($remcalc >= 3) {
198       $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
199       if ($lockcalc < 3) {
200         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
201         $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
202         $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);
203         
204         log_wr($sess, $user->comm[$user->step % COMM_N]);
205         $user->step_inc();
206         $logout_cont = FALSE;
207       }
208     }
209     else {
210       $user->bantime = $user->laccwr + BAN_TIME;
211       // BAN_IP_SET
212     }
213     if ($logout_cont == TRUE) {
214       $bri->room_wakeup(&$user);
215     }
216   }
217   else if ($argz[0] == 'exitlock') {
218     $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
219     for ($ct = 0, $i = 0 ; $i < PLAYERS_N ; $i++) {     
220       $user_cur[$i] = &$bri->user[$table->player[$i]];
221       if ($user_cur[$i]->exitislock == FALSE)
222         $ct++;
223     }
224     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
225       $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
226       $ret .= sprintf('exitlock_show(%d, %s);', $ct, 
227                      ($user_cur[$i]->exitislock ? 'true' : 'false'));
228       $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
229       log_wr($sess, $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
230       $user_cur[$i]->step_inc();
231     }
232   }
233   else if ($user->subst == 'asta') {
234     if ($argz[0] == 'lascio' && $user->handpt <= 2) {
235       $index_cur = $table->gstart % PLAYERS_N;
236     
237       log_wr($sess, sprintf("GIOCO FINITO !!!"));
238     
239       $table->mult *= 2; 
240       $table->old_reason = sprintf("Ha lasciato %s perche` aveva al massimo 2 punti.", $user->name);
241
242       $table->game_next();
243       $table->game_init(&$bri->user);
244     
245       for ($i = 0 ; $i < PLAYERS_N ; $i++) {    
246         $user_cur = &$bri->user[$table->player[$i]];
247
248         $ret = sprintf('gst.st = %d;', $user_cur->step+1);
249         $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
250         $user_cur->comm[$user_cur->step % COMM_N] = $ret;
251         $user_cur->step_inc();      
252       }
253     }
254     else if ($argz[0] == 'asta') {
255       $again = TRUE;
256     
257       $index_cur = $table->gstart % PLAYERS_N;
258       if ($user->table_pos == $index_cur &&
259           $table->asta_pla[$index_cur]) {
260         $a_card = $argz[1];
261         $a_pnt  = $argz[2];
262       
263         log_wr($sess, "CI SIAMO  a_card ".$a_card."  asta_card ".$table->asta_card);
264       
265         // Abbandono dell'asta
266         if ($a_card <= -1) {
267           log_wr($sess, "Abbandona l'asta.");
268           $table->asta_pla[$index_cur] = FALSE;
269           $user->asta_card  = -1;
270           $table->asta_pla_n--;
271           $again = FALSE;
272         }
273         else if ($a_card <= 9) {
274           if ($table->asta_card == 9) {
275             if ($a_card == 9 && $a_pnt <= 120 && $a_pnt > $table->asta_pnt)
276               $again = FALSE;
277           }
278           else {
279             if ($a_card >= 0 && $a_card <= 9 && $a_card > $table->asta_card)
280               $again = FALSE;
281           }
282
283           if ($again == FALSE) {
284             log_wr($sess, "NUOVI ORZI.");
285             $user->asta_card  = $a_card;
286             $table->asta_card = $a_card;
287             if ($a_card == 9) {
288               $user->asta_pnt   = $a_pnt;
289               $table->asta_pnt  = $a_pnt;
290             }
291           }
292         }
293       
294       
295       
296         if ($again) { // Qualcosa non andato bene, rifare
297           log_wr($sess, "Ripetere.");
298         }
299         else {
300           /* next step */
301           $showst = "show_astat("; 
302           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
303             $user_cur = &$bri->user[$table->player[$i]];
304             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
305                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
306           }
307           if (PLAYERS_N == 3)
308             $showst .= ",-2,-2";
309           $showst .= ");";
310
311           $maxcard = -2;
312           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
313             $user_cur = &$bri->user[$table->player[$i]];
314             if ($maxcard < $user_cur->asta_card)
315               $maxcard = $user_cur->asta_card;
316           }
317
318           if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
319               !($table->asta_card == 9 && $table->asta_pnt == 120)) {
320             log_wr($sess,"ALLOPPA QUI");
321             for ($i = 1 ; $i < PLAYERS_N ; $i++) {
322               $index_next = ($table->gstart + $i) % PLAYERS_N;
323               if ($table->asta_pla[$index_next]) {
324                 log_wr($sess,"GSTART 1");
325                 $table->gstart += $i;
326                 break;
327               }
328             }
329           
330           
331             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
332               $user_cur = &$bri->user[$table->player[$i]];
333               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
334               if ($user_cur->table_pos == ($table->gstart % PLAYERS_N)) 
335                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();', 
336                                 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
337               else
338                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
339                                 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
340               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
341               $user_cur->step_inc();
342             }
343           }
344           else if ($table->asta_pla_n == 0) {
345             log_wr($sess, "PASSANO TUTTI!");
346
347             log_wr($sess, sprintf("GIOCO FINITO !!!"));
348           
349             $table->old_reason = "Hanno passato tutti.";
350             $table->mult *= 2; 
351
352             $table->game_next();
353             $table->game_init(&$bri->user);
354           
355             for ($i = 0 ; $i < PLAYERS_N ; $i++) {      
356               $user_cur = &$bri->user[$table->player[$i]];
357
358               $ret = sprintf('gst.st = %d;', $user_cur->step+1);
359               $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
360               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
361               $user_cur->step_inc();        
362             }
363           }
364           else {
365             log_wr($sess, "FINITA !");
366             // if a_pnt == 120 supergame ! else abbandono
367             if ($a_pnt == 120 || $user->asta_card != -1) {
368               $chooser = $index_cur;
369               for ($i = 1 ; $i < PLAYERS_N ; $i++) 
370                 if ($i != $chooser)
371                   $table->asta_pla[$i] = FALSE;
372             }
373             else {
374               //"gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt).", true); remark_off();";
375               $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));
376               $user->step_inc();
377               for ($i = 1 ; $i < PLAYERS_N ; $i++) {
378                 $chooser = ($table->gstart + $i) % PLAYERS_N;
379                 if ($table->asta_pla[$chooser]) {
380                   break;
381                 }
382               }
383             }
384             $table->asta_win = $chooser;
385
386             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
387               $user_cur = &$bri->user[$table->player[$i]];
388               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
389
390               if ($i == $chooser) {
391                 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
392               }
393               else {
394                 $ret .= "remark_off();";
395               }
396
397               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
398               $user_cur->step_inc();        
399             }
400           }
401         }
402       }
403       else {
404         log_wr($sess, "NON CI SIAMO");
405       }
406     }
407     /*  asta::choose */
408     else if ($argz[0] == 'choose') {
409       if ($table->asta_win > -1 && 
410           $user->table_pos == $table->asta_win) {
411         $a_brisco = $argz[1];
412         if ($a_brisco >= 0 && $a_brisco < 40) {
413           $table->briscola = $a_brisco;
414           $table->friend   = $table->card[$a_brisco]->owner;
415           log_wr($sess,"GSTART 2");
416           $table->gstart = ($table->mazzo+1) % PLAYERS_N;
417           log_wr($sess, "Setta la briscola a ".$a_brisco);
418
419           $chooser = $table->asta_win;
420           $user_chooser = &$bri->user[$table->player[$chooser]];
421           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
422             $user_cur = &$bri->user[$table->player[$i]];
423             $user_cur->subst = 'game';
424             $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
425           
426
427             /* bg of caller cell */
428             $ret .= briscola_show($bri, $table, $user_cur);
429
430             /* first gamer */
431             if ($i == ($table->gstart % PLAYERS_N))
432               $ret .= "is_my_time = true; remark_on();";
433             else
434               $ret .= "is_my_time = false; remark_off();";
435
436             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
437             $user_cur->step_inc();          
438           }
439           /*
440             TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
441             VAR PER PASSARE ALLA FASE DI GIOCO
442           */
443         
444         }
445       }
446     }
447   }
448   else if ($user->subst == 'game') {
449     log_wr($sess, "state: table::game".$argz[0]);
450
451     if ($argz[0] == 'play') {
452       $a_play = $argz[1];
453       $a_x =    $argz[2];
454       $a_y =    $argz[3];
455
456       if (strpos($a_x, "px") != FALSE)
457         $a_x = substr($a_x,0,-2);
458       if (strpos($a_y, "px") != FALSE)
459         $a_y = substr($a_y,0,-2);
460
461       $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
462                        $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
463                        $table->mazzo, $table->gstart,
464                        $table->card[$a_play]->stat, $table->card[$a_play]->owner);
465       log_wr($sess, "CIC".$loggo);
466                           
467       /* se era il suo turno e la carta era sua ed era in mano */
468       if ($a_play >=0 && $a_play < 40 &&
469           ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
470           $table->card[$a_play]->stat == 'hand' &&
471           $table->card[$a_play]->owner == $user->table_pos) {
472         log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
473
474         /* Change the card status. */
475         $table->card[$a_play]->play($a_x, $a_y);
476
477         /*
478          *  !!!! TURN INCREMENTED BEFORE !!!!
479          */
480         $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
481         $table->turn++;
482
483         $card_play = sprintf("card_play(%d,%d,%d,%d);|",
484                              $user->table_pos, $a_play, $a_x, $a_y);
485         if (($table->turn % PLAYERS_N) != 0) {     /* manche not finished */
486           $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
487
488           $player_cur = "remark_off();";
489           $player_nex = $card_play . "is_my_time = true; remark_on();";
490           $player_oth = $card_play;
491         }
492         else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
493           $winner = calculate_winner($table);
494           log_wr($sess,"GSTART 3");
495           $table->gstart = $winner;
496           $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
497
498           log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
499           $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
500                                $winner, $winner);
501           $player_cur = "remark_off();" . $card_take . "|"; 
502           if ($turn_cur != $turn_nex)
503             $player_nex = $card_play . $card_take . "|";
504           else
505             $player_nex = "";
506           if ($table->turn < (PLAYERS_N * 8))  /* game NOT finished */
507             $player_nex .= "is_my_time = true; remark_on();";
508           $player_oth = $card_play . $card_take;
509         }
510
511         log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
512         for ($i = 0 ; $i < PLAYERS_N ; $i++) {  
513           $user_cur = &$bri->user[$table->player[$i]];
514
515           $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
516
517         
518           if ($i == $turn_cur) {
519             $ret .= $player_cur;          
520           }
521           if ($i == $turn_nex) {
522             $ret .= $player_nex;          
523           }
524           if ($i != $turn_cur && $i != $turn_nex) {
525             $ret .= $player_oth;
526           }
527
528           $retar[$i] = $ret;
529         }
530
531
532
533
534         if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
535           log_wr($sess, sprintf("GIOCO FINITO !!!"));
536
537           /* ************************************************ */
538           /*    PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO    */
539           /* ************************************************ */
540           calculate_points(&$table);
541
542           $table->game_next();
543           $table->game_init(&$bri->user);
544           
545           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
546             $user_cur = &$bri->user[$table->player[$i]];
547             $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
548           }
549         }
550
551
552         for ($i = 0 ; $i < PLAYERS_N ; $i++) {  
553           $user_cur = &$bri->user[$table->player[$i]];
554         
555           $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
556           $user_cur->step_inc();            
557         }
558
559         log_wr($sess, sprintf("TURN: %d",$table->turn));
560         /* Have played all the players ? */
561         /* NO:  switch the focus and enable the next player to play. */
562       
563         /* YES: calculate who win and go to the next turn. */
564       }
565     }
566     else
567       log_wr($sess, "NOSENSE");
568   }
569 }
570 log_wr($sess, "before save data");
571 save_data($bri);
572
573 unlock_data($sem);
574 exit;
575 ?>