copyright updated and mop user status added
[brisk.git] / web / briskin5 / index_wr.php
1 <?php
2 /*
3  *  brisk - index_wr.php
4  *
5  *  Copyright (C) 2006-2011 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 require_once("../Obj/brisk.phh");
26 // require_once("../Obj/proxyscan.phh");
27 require_once("Obj/briskin5.phh");
28
29 // Use of proxies isn't allowed.
30 // if (is_proxy()) {
31 //   sleep(5);
32 //   exit;
33 //}
34
35 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
36   echo "Debugging time!";
37   exit;
38 }
39
40 log_load("bin5/index_wr.php");
41
42 /*
43  *  MAIN
44  */
45 log_wr('COMM: '.$mesg);
46
47 if ($table_idx < 0 || $table_idx >= TABLE_N)
48      exit;
49
50 log_mop(0, 'bin::index_wr.php: COMM: '.xcapemesg($mesg));
51
52 $sem = Briskin5::lock_data($table_idx);
53
54 if (($bri = &Briskin5::load_data($table_idx,$table_token)) == FALSE) {
55   echo "Bin5 Load data error";
56   log_wr("Bin5 Load data error");
57   Briskin5::unlock_data($sem);
58   exit;
59 }
60
61 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
62   echo "Get User Error";
63   log_wr("Get User Error");
64   Briskin5::unlock_data($sem);
65   exit;
66 }
67 $argz = explode('|', $mesg);
68
69 log_wr('POSTSPLIT: '.$argz[0]);
70 log_mop($user->step, 'bin::index_wr.php: after get_user()');
71
72 if (false && $argz[0] == 'shutdown') {
73   log_auth($user_cur->sess, "Shutdown session. delegate to room gc the autologout");
74   
75   log_rd2("bin5/index_wr.php: AUTO LOGOUT.");
76   if ($user->stat == 'table') {
77     $bri->table_wakeup(&$user);
78     // to force the logout
79     $user->lacc = 0;
80   }
81   else
82     log_rd2("SHUTDOWN FROM WHAT ???");
83 }
84 /*********************
85  *                   *
86  *    STAT: table    *
87  *                   *
88  *********************/
89 else if ($user->stat == 'table') {
90   $user->laccwr = time();
91   $table = &$bri->table[$user->table];
92
93   if ($argz[0] == 'tableinfo') {
94     log_wr("PER DI TABLEINFO");
95     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
96     $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
97     log_wr($user->comm[$user->step % COMM_N]);
98     $user->step_inc();
99   }
100   else if ($argz[0] == 'chatt') {
101     $bri->chatt_send(&$user,$mesg);
102   }
103   else if ($argz[0] == 'preferences_update') {
104     GLOBAL $CO_bin5_pref_ring_endauct;
105
106     log_wr("PER DI TABLEINFO");
107
108     if ($CO_bin5_pref_ring_endauct == "true")
109       $user->privflags |= BIN5_USER_FLAG_RING_ENDAUCT;
110     else
111       $user->privflags &= ~BIN5_USER_FLAG_RING_ENDAUCT;
112   }
113   else if ($argz[0] == 'logout') {
114     $remcalc = $argz[1];
115
116     if ($user->exitislock == TRUE) {
117       $remcalc++;
118       $user->exitislock = FALSE;
119     }
120
121     $logout_cont = TRUE;
122     if ($remcalc >= 3) {
123       $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
124       if ($lockcalc < 3) {
125         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
126         $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
127         $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);
128         
129         log_wr($user->comm[$user->step % COMM_N]);
130         $user->step_inc();
131         $logout_cont = FALSE;
132       }
133     }
134     else {
135       require_once("../Obj/hardban.phh");
136       Hardbans::add(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
137                     $user->ip, $user->sess, $user->laccwr + BAN_TIME);
138     }
139     //      $user->bantime = $user->laccwr + BAN_TIME;
140     
141     if ($logout_cont == TRUE) {
142       $bri->table_wakeup(&$user);
143     }
144   }
145   else if ($argz[0] == 'exitlock') {
146     if ($user->exitislock == TRUE) {
147       $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
148       for ($ct = 0, $i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {  
149         $user_cur[$i] = &$bri->user[$table->player[$i]];
150         if ($user_cur[$i]->exitislock == FALSE)
151           $ct++;
152       }
153       for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
154         $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
155         $ret .= sprintf('exitlock_show(%d, %s);', $ct, 
156                         ($user_cur[$i]->exitislock ? 'true' : 'false'));
157         $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
158         log_wr($user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
159         $user_cur[$i]->step_inc();
160       }
161     }
162   }
163   else if ($user->subst == 'asta') {
164     if ($argz[0] == 'lascio' && $user->handpt <= 2) {
165       $index_cur = $table->gstart % BRISKIN5_PLAYERS_N;
166     
167       log_wr(sprintf("GIOCO FINITO !!!"));
168     
169       $table->mult += 1; 
170       $table->old_reason = sprintf("Ha lasciato %s perché aveva al massimo 2 punti.", xcape($user->name));
171
172       $table->game_next();
173       $table->game_init(&$bri->user);
174     
175       for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {   
176         $user_cur = &$bri->user[$table->player[$i]];
177
178         $ret = sprintf('gst.st = %d;', $user_cur->step+1);
179         $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
180         $user_cur->comm[$user_cur->step % COMM_N] = $ret;
181         $user_cur->step_inc();      
182       }
183     }
184     else if ($argz[0] == 'asta') {
185       $again = TRUE;
186     
187       $index_cur = $table->gstart % BRISKIN5_PLAYERS_N;
188       if ($user->table_pos == $index_cur &&
189           $table->asta_pla[$index_cur]) {
190         $a_card = $argz[1];
191         $a_pnt  = $argz[2];
192       
193         log_wr("CI SIAMO  a_card ".$a_card."  asta_card ".$table->asta_card);
194       
195         // Abbandono dell'asta
196         if ($a_card <= -1) {
197           log_wr("Abbandona l'asta.");
198           $table->asta_pla[$index_cur] = FALSE;
199           $user->asta_card  = -1;
200           $table->asta_pla_n--;
201           $again = FALSE;
202         }
203         else if ($a_card <= 9) {
204           if ($a_card >= 0 && $a_card < 9 && $a_card > $table->asta_card)
205             $again = FALSE;
206           else if ($a_card == 9 && $a_pnt > ($table->asta_pnt >= 61 ? $table->asta_pnt : 60) && $a_pnt <= 120)
207             $again = FALSE;
208           
209
210           if ($again == FALSE) {
211             log_wr("NUOVI ORZI.");
212             $user->asta_card  = $a_card;
213             $table->asta_card = $a_card;
214             if ($a_card == 9) {
215               $user->asta_pnt   = $a_pnt;
216               $table->asta_pnt  = $a_pnt;
217             }
218           }
219         }
220       
221       
222       
223         if ($again) { // Qualcosa non andato bene, rifare
224           $ret = sprintf('gst.st = %d; asta_pnt_set(%d);', $user->step+1, 
225                          ($table->asta_pnt > 60 ? $table->asta_pnt + 1 : 61) );
226           $user->comm[$user->step % COMM_N] = $ret;
227           $user->step_inc();
228
229           log_wr("Ripetere.");
230         }
231         else {
232           /* next step */
233           $showst = "show_astat("; 
234           for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
235             $user_cur = &$bri->user[$table->player[$i]];
236             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
237                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
238           }
239           if (BRISKIN5_PLAYERS_N == 3)
240             $showst .= ",-2,-2";
241           $showst .= ");";
242
243           $maxcard = -2;
244           for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
245             $user_cur = &$bri->user[$table->player[$i]];
246             if ($maxcard < $user_cur->asta_card)
247               $maxcard = $user_cur->asta_card;
248           }
249
250           if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
251               !($table->asta_card == 9 && $table->asta_pnt == 120)) {
252             log_wr("ALLOPPA QUI");
253             for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
254               $index_next = ($table->gstart + $i) % BRISKIN5_PLAYERS_N;
255               if ($table->asta_pla[$index_next]) {
256                 log_wr("GSTART 1");
257                 $table->gstart += $i;
258                 break;
259               }
260             }
261           
262           
263             for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
264               $user_cur = &$bri->user[$table->player[$i]];
265               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
266               if ($user_cur->table_pos == ($table->gstart % BRISKIN5_PLAYERS_N)) 
267                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();', 
268                                 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
269               else
270                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
271                                 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
272               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
273               $user_cur->step_inc();
274             }
275           }
276           else if ($table->asta_pla_n == 0) {
277             log_wr("PASSANO TUTTI!");
278
279             log_wr(sprintf("GIOCO FINITO !!!"));
280           
281             $table->old_reason = "Hanno passato tutti.";
282             $table->mult += 1; 
283
284             $table->game_next();
285             $table->game_init(&$bri->user);
286           
287             for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {     
288               $user_cur = &$bri->user[$table->player[$i]];
289
290               $ret = sprintf('gst.st = %d;', $user_cur->step+1);
291               $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
292               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
293               $user_cur->step_inc();        
294             }
295           }
296           else {
297             log_wr("FINITA !");
298             // if a_pnt == 120 supergame ! else abbandono
299             if ($a_pnt == 120 || $user->asta_card != -1) {
300               $chooser = $index_cur;
301               for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++) 
302                 if ($i != $chooser)
303                   $table->asta_pla[$i] = FALSE;
304             }
305             else {
306               /*
307               $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();", 
308                                                            $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
309               $user->step_inc();
310               */
311               for ($i = 1 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
312                 $chooser = ($table->gstart + $i) % BRISKIN5_PLAYERS_N;
313                 if ($table->asta_pla[$chooser]) {
314                   break;
315                 }
316               }
317             }
318             $table->asta_win = $chooser;
319
320             for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
321               $user_cur = &$bri->user[$table->player[$i]];
322               $ret = sprintf('gst.st = %d; %s dispose_asta(%d, %d, false);', $user_cur->step+1, $showst,
323                              $table->asta_card + 1,-($table->asta_pnt));
324
325               if ($i == $chooser) {
326                 $ret .= "choose_seed(". $table->asta_card."); remark_on();";
327               }
328               else {
329                 $ret .= "remark_off();";
330               }
331
332               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
333               $user_cur->step_inc();        
334             }
335           }
336         }
337       }
338       else {
339         log_wr("NON CI SIAMO");
340       }
341     }
342     /*  asta::choose */
343     else if ($argz[0] == 'choose') {
344       if ($table->asta_win > -1 && 
345           $user->table_pos == $table->asta_win) {
346         $a_brisco = $argz[1];
347         if ($a_brisco >= 0 && $a_brisco < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24)) {
348           $table->briscola = $a_brisco;
349           $table->friend   = $table->card[$a_brisco]->owner;
350           log_wr("GSTART 2");
351           $table->gstart = ($table->mazzo+1) % BRISKIN5_PLAYERS_N;
352           log_wr("Setta la briscola a ".$a_brisco);
353
354           $chooser = $table->asta_win;
355           $user_chooser = &$bri->user[$table->player[$chooser]];
356           for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
357             $user_cur = &$bri->user[$table->player[$i]];
358             $user_cur->subst = 'game';
359             $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
360           
361             if ($user_cur->privflags & BIN5_USER_FLAG_RING_ENDAUCT) {
362               // $ret .= "var de_che= 33;";
363               $ret .= playsound("ringbell.mp3");
364             }
365             $ret .= sprintf('document.title = "Brisk - Tavolo %d";', $user->table_orig);
366
367             /* bg of caller cell */
368             $ret .= briscola_show($bri, $table, $user_cur);
369
370             /* first gamer */
371             if ($i == ($table->gstart % BRISKIN5_PLAYERS_N))
372               $ret .= "is_my_time = true; remark_on();";
373             else
374               $ret .= "is_my_time = false; remark_off();";
375
376             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
377             $user_cur->step_inc();          
378           }
379           /*
380             TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
381             VAR PER PASSARE ALLA FASE DI GIOCO
382           */
383         
384         }
385       }
386     }
387   }
388   else if ($user->subst == 'game') {
389     log_wr("state: table::game".$argz[0]);
390
391     if ($argz[0] == 'play') {
392       $a_play = $argz[1];
393       $a_x =    $argz[2];
394       $a_y =    $argz[3];
395
396       if (strpos($a_x, "px") != FALSE)
397         $a_x = substr($a_x,0,-2);
398       if (strpos($a_y, "px") != FALSE)
399         $a_y = substr($a_y,0,-2);
400
401       $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
402                        $a_play, $user->table_pos, ($table->gstart % BRISKIN5_PLAYERS_N),
403                        $table->mazzo, $table->gstart,
404                        $table->card[$a_play]->stat, $table->card[$a_play]->owner);
405       log_wr("CIC".$loggo);
406                           
407       /* se era il suo turno e la carta era sua ed era in mano */
408       if ($a_play >=0 && $a_play < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) &&
409           ($user->table_pos == (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N)) &&
410           $table->card[$a_play]->stat == 'hand' &&
411           $table->card[$a_play]->owner == $user->table_pos) {
412         log_wr(sprintf("User: %s Play: %d",$user->name, $a_play));
413
414         /* Change the card status. */
415         $table->card[$a_play]->play($a_x, $a_y);
416
417         /*
418          *  !!!! TURN INCREMENTED BEFORE !!!!
419          */
420         $turn_cur = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
421         $table->turn++;
422
423         $card_play = sprintf("card_play(%d,%d,%d,%d);|",
424                              $user->table_pos, $a_play, $a_x, $a_y);
425         if (($table->turn % BRISKIN5_PLAYERS_N) != 0) {     /* manche not finished */
426           $turn_nex = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
427
428           $player_cur = "remark_off();";
429           $player_nex = $card_play . "is_my_time = true; remark_on();";
430           $player_oth = $card_play;
431         }
432         else if ($table->turn <= (BRISKIN5_PLAYERS_N * 8)) { /* manche finished */
433           $winner = calculate_winner($table);
434           log_wr("GSTART 3");
435           $table->gstart = $winner;
436           $turn_nex = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
437
438           log_wr(sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
439           $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|", $winner);
440           $player_cur = "remark_off();" . $card_take; 
441           if ($turn_cur != $turn_nex)
442             $player_nex = $card_play . $card_take;
443           else
444             $player_nex = "";
445           if ($table->turn < (BRISKIN5_PLAYERS_N * 8))  /* game NOT finished */
446             $player_nex .= "is_my_time = true; remark_on();";
447           $player_oth = $card_play . $card_take;
448         }
449
450         log_wr(sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
451         for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) { 
452           $user_cur = &$bri->user[$table->player[$i]];
453
454           $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
455
456         
457           if ($i == $turn_cur) {
458             $ret .= $player_cur;          
459           }
460           if ($i == $turn_nex) {
461             $ret .= $player_nex;          
462           }
463           if ($i != $turn_cur && $i != $turn_nex) {
464             $ret .= $player_oth;
465           }
466
467           $retar[$i] = $ret;
468         }
469
470         if ($table->turn == (BRISKIN5_PLAYERS_N * 8)) { /* game finished */
471           log_wr(sprintf("GIOCO FINITO !!!"));
472
473
474           /* ************************************************ */
475           /*    PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO    */
476           /* ************************************************ */
477           $pt_cur = calculate_points(&$table);
478
479           $plist = "$table->table_token|$user->table_orig|$table->player_n";
480           $curtime = time();
481           for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
482             $user_cur = &$bri->user[$table->player[$i]];
483             $plist .= '|'.xcapelt($user_cur->name).'|'.$pt_cur[$i];
484           }
485           log_legal($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
486           if ($user->table_orig < TABLES_AUTH_N)
487             log_points($curtime, xcapelt($user->name), "STAT:BRISKIN5:FINISH_GAME", $plist);
488
489           $table->game_next();
490           $table->game_init(&$bri->user);
491           
492           for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
493             $user_cur = &$bri->user[$table->player[$i]];
494             $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
495           }
496         }
497
498
499         for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) { 
500           $user_cur = &$bri->user[$table->player[$i]];
501         
502           $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
503           $user_cur->step_inc();            
504         }
505
506         log_wr(sprintf("TURN: %d",$table->turn));
507         /* Have played all the players ? */
508         /* NO:  switch the focus and enable the next player to play. */
509       
510         /* YES: calculate who win and go to the next turn. */
511       }
512     }
513     else
514       log_wr("NOSENSE");
515   }
516 }
517 log_wr("before save data");
518 Briskin5::save_data($bri);
519 log_mop($user->step, 'bin::index_wr.php: after save_data()');
520
521 Briskin5::unlock_data($sem);
522 exit;
523 ?>