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