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