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