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