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