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