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