changes of 0.6.4
[brisk.git] / web / index_wr.php
1 <?php
2 /*
3  *  brisk - index_wr.php
4  *
5  *  Copyright (C) 2006 matteo.nastasi@milug.org
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details. You should have received a
16  * copy of the GNU General Public License along with this program; if
17  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18  * Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * $Id$
21  *
22  */
23
24 require_once("brisk.phh");
25 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
26   echo "Debugging time!";
27   exit;
28 }
29
30 log_load($sess, "LOAD: index_wr.php");
31
32 /*
33  *  MAIN
34  */
35 log_wr($sess, 'COMM: '.$mesg);
36
37 $sem = lock_data();
38 $bri = &load_data();
39 if (($user = &$bri->get_user($sess, &$idx)) == FALSE) {
40   echo "Get User Error";
41   log_wr($sess, "Get User Error");
42   unlock_data($sem);
43   exit;
44 }
45 $argz = explode('|', $mesg);
46
47 if ($argz[0] == 'shutdown') {
48   log_auth($user_cur->sess, "Shutdown session.");
49   
50   $user->sess = "";
51   $user->name = "";
52   $user->the_end = FALSE;
53   
54   log_rd2($user->sess, "AUTO LOGOUT.");
55   if ($user->subst == 'sitdown' || $user->stat == 'table')
56     $bri->room_wakeup(&$user);
57   else if ($user->subst == 'standup')
58     $bri->room_outstandup(&$user);
59   else
60     log_rd2($sess, "SHUTDOWN FROM WHAT ???");
61 }
62 /******************
63  *                *
64  *   STAT: room   *
65  *                *
66  ******************/
67 else if ($user->stat == 'room') {
68   $user->laccwr = time();
69
70   if ($argz[0] == 'help') {
71     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
72     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
73
74     log_wr($sess, $user->comm[$user->step % COMM_N]);
75     $user->step++;
76     
77   }
78   else if ($argz[0] == 'about') {
79     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
80     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
81
82     log_wr($sess, $user->comm[$user->step % COMM_N]);
83     $user->step++;
84     
85   }
86   /*  else if ($argz[0] == 'logout') {
87     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
88     $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
89     $user->the_end = TRUE;
90     $user->step++;
91   }*/
92   else if ($argz[0] == 'chatt') {
93     $bri->chatt_send(&$user,$mesg);
94   }
95   /*
96   else if ($argz[0] == 'sitdown') {
97     if ($user->stat != 'room' || $user->subst != 'standup') {
98       log_wr($sess, "Warning ! sitdown out fsm");
99       unlock_data($sem);
100       exit;
101     }
102                 
103     if ($user->bantime > $user->laccwr) {
104       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
105             $user->comm[$user->step % COMM_N] .= show_notify("<br>Ti sei alzato da un tavolo senza il consenso degli altri giocatori. Dovrai aspettare ancora ".secstoword($user->bantime - $user->laccwr)." prima di poterti sedere nuovamente.", 2000, "Torna in piedi.", 400, 100);
106       
107       $user->step++;
108       save_data($bri);
109       unlock_data($sem);
110       exit;
111     }
112     
113     // Take parameters
114     $table_idx = $argz[1];
115     $table = &$bri->table[$table_idx];
116     
117     if ($table->player_n == PLAYERS_N) {
118       log_wr($sess, "Warning ! unreachable, table full.");
119       unlock_data($sem);
120       exit;
121     } 
122
123     // set new status
124     $user->subst = "sitdown";
125     $user->table = $table_idx;
126     $user->table_pos = $table->user_add($idx);
127                 
128     if ($table->player_n == PLAYERS_N) {
129       // Start game for this table.
130       log_wr($sess, "Start game!");
131       
132       $table->init(&$bri->user);
133       $table->game_init(&$bri->user);
134       $curtime = time();
135
136       for ($i = 0 ; $i < $table->player_n ; $i++) {
137         $user_cur = &$bri->user[$table->player[$i]];
138         log_wr($sess, "Pre if!");
139         
140         $ret = "";
141         $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
142         
143         $user_cur->comm[$user_cur->step % COMM_N] = $ret;
144         $user_cur->trans_step = $user_cur->step + 1;
145         log_wr($sess, "TRANS ATTIVATO");
146         
147         $user_cur->stat =  'table';
148         $user_cur->subst = 'asta';
149         $user_cur->laccwr = $curtime;
150         $user_cur->step++;
151         
152         $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
153         $user_cur->step++;
154       }
155     }
156                 
157     $bri->room_sitdown(&$user, $table_idx); // COMMENTED
158   }
159   */
160   /*
161   else if ($argz[0] == 'wakeup') {
162     if ($user->stat != 'room' || $user->subst != 'sitdown') {
163       log_wr($sess, "Warning ! wakeup out fsm.");
164       unlock_data($sem);
165       exit;
166     }
167                 
168     // set new status
169     $user->subst = "standup";
170
171     $bri->room_wakeup(&$user);
172   }
173   */
174   /**********************
175    *                    *
176    *   SUBST: standup   *
177    *                    *
178    **********************/
179   else if ($user->subst == 'standup') {
180    
181     if ($argz[0] == 'sitdown') {
182       if ($user->the_end == TRUE) {
183         log_wr($sess, "INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
184         unlock_data($sem);
185         exit;
186       }
187       /* TODO: refact to a function */
188       if ($user->bantime > $user->laccwr) {
189         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
190         $user->comm[$user->step % COMM_N] .= show_notify("<br>Ti sei alzato da un tavolo senza il consenso degli altri giocatori. Dovrai aspettare ancora ".secstoword($user->bantime - $user->laccwr)." prima di poterti sedere nuovamente.", 2000, "Torna in piedi.", 400, 100);
191         
192         $user->step++;
193         save_data($bri);
194         unlock_data($sem);
195         exit;
196       }
197     
198       // Take parameters
199       $table_idx = $argz[1];
200       $table = &$bri->table[$table_idx];
201     
202       if ($table->player_n == PLAYERS_N) {
203         log_wr($sess, "WARN:FSM: Sitdown unreachable, table full.");
204         unlock_data($sem);
205         exit;
206       } 
207
208       // set new status
209       $user->subst = "sitdown";
210       $user->table = $table_idx;
211       $user->table_pos = $table->user_add($idx);
212       
213       if ($table->player_n == PLAYERS_N) {
214         // Start game for this table.
215         log_wr($sess, "Start game!");
216         
217         $table->init(&$bri->user);
218         $table->game_init(&$bri->user);
219         $curtime = time();
220         
221         for ($i = 0 ; $i < $table->player_n ; $i++) {
222           $user_cur = &$bri->user[$table->player[$i]];
223           log_wr($sess, "Pre if!");
224           
225           $ret = "";
226           $ret .= sprintf('gst.st_loc++; gst.st=%d; the_end=true; window.onunload = null ; document.location.assign("table.php");|', $user_cur->step+1);
227           
228           $user_cur->comm[$user_cur->step % COMM_N] = $ret;
229           $user_cur->trans_step = $user_cur->step + 1;
230           log_wr($sess, "TRANS ATTIVATO");
231           
232           $user_cur->stat =  'table';
233           $user_cur->subst = 'asta';
234           $user_cur->laccwr = $curtime;
235           $user_cur->step++;
236           
237           $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, FALSE);
238           $user_cur->step++;
239         }
240       }
241       
242       $bri->room_sitdown(&$user, $table_idx);
243     }
244     else if ($argz[0] == 'logout') {
245       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
246       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
247       $user->the_end = TRUE;
248       $user->step++;
249     }
250   }
251   /**********************
252    *                    *
253    *   SUBST: sitdown   *
254    *                    *
255    **********************/
256   else if ($user->subst == 'sitdown') {
257     if ($argz[0] == 'wakeup') {
258       $bri->room_wakeup(&$user);      
259     }
260     else if ($argz[0] == 'logout') {
261       $bri->room_wakeup(&$user);      
262       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
263       $user->comm[$user->step % COMM_N] .= sprintf('postact_logout();');
264       $user->the_end = TRUE;
265       $user->step++;
266     }
267   }
268 }
269 /*********************
270  *                   *
271  *    STAT: table    *
272  *                   *
273  *********************/
274 else if ($user->stat == 'table') {
275   $user->laccwr = time();
276   $table = &$bri->table[$user->table];
277
278   if ($argz[0] == 'tableinfo') {
279     log_wr($sess, "PER DI TABLEINFO");
280     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
281     $user->comm[$user->step % COMM_N] .= show_table_info(&$bri, &$table, $user->table_pos);
282     log_wr($sess, $user->comm[$user->step % COMM_N]);
283     $user->step++;
284   }
285   else if ($argz[0] == 'chatt') {
286     $bri->chatt_send(&$user,$mesg);
287   }
288   else if ($argz[0] == 'logout') {
289     $remcalc = $argz[1];
290
291     if ($user->exitislock == TRUE) {
292       $remcalc++;
293       $user->exitislock = FALSE;
294     }
295
296     $logout_cont = TRUE;
297     if ($remcalc >= 3) {
298       $lockcalc = $table->exitlock_calc(&$bri->user, $user->table_pos);
299       if ($lockcalc < 3) {
300         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
301         $user->comm[$user->step % COMM_N] .= $table->exitlock_show(&$bri->user, $user->table_pos);
302         $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);
303         
304         log_wr($sess, $user->comm[$user->step % COMM_N]);
305         $user->step++;
306         $logout_cont = FALSE;
307       }
308     }
309     else 
310       $user->bantime = $user->laccwr + BAN_TIME;
311     
312     if ($logout_cont == TRUE) {
313       $bri->room_wakeup(&$user);
314     }
315   }
316   else if ($argz[0] == 'exitlock') {
317     $user->exitislock = ($user->exitislock == TRUE ? FALSE : TRUE);
318     for ($ct = 0, $i = 0 ; $i < PLAYERS_N ; $i++) {     
319       $user_cur[$i] = &$bri->user[$table->player[$i]];
320       if ($user_cur[$i]->exitislock == FALSE)
321         $ct++;
322     }
323     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
324       $ret = sprintf('gst.st = %d;', $user_cur[$i]->step+1);
325       $ret .= sprintf('exitlock_show(%d, %s);', $ct, 
326                      ($user_cur[$i]->exitislock ? 'true' : 'false'));
327       $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N] = $ret;
328       log_wr($sess, $user_cur[$i]->comm[$user_cur[$i]->step % COMM_N]);
329       $user_cur[$i]->step++;
330     }
331   }
332   else if ($user->subst == 'asta') {
333     if ($argz[0] == 'lascio' && $user->handpt <= 2) {
334       $index_cur = $table->gstart % PLAYERS_N;
335     
336       log_wr($sess, sprintf("GIOCO FINITO !!!"));
337     
338       $table->mult *= 2; 
339       $table->old_reason = sprintf("Ha lasciato %s perche` aveva al massimo 2 punti.", $user->name);
340
341       $table->game_next();
342       $table->game_init(&$bri->user);
343     
344       for ($i = 0 ; $i < PLAYERS_N ; $i++) {    
345         $user_cur = &$bri->user[$table->player[$i]];
346
347         $ret = sprintf('gst.st = %d;', $user_cur->step+1);
348         $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
349         $user_cur->comm[$user_cur->step % COMM_N] = $ret;
350         $user_cur->step++;          
351       }
352     }
353     else if ($argz[0] == 'asta') {
354       $again = TRUE;
355     
356       $index_cur = $table->gstart % PLAYERS_N;
357       if ($user->table_pos == $index_cur &&
358           $table->asta_pla[$index_cur]) {
359         $a_card = $argz[1];
360         $a_pnt  = $argz[2];
361       
362         log_wr($sess, "CI SIAMO  a_card ".$a_card."  asta_card ".$table->asta_card);
363       
364         // Abbandono dell'asta
365         if ($a_card <= -1) {
366           log_wr($sess, "Abbandona l'asta.");
367           $table->asta_pla[$index_cur] = FALSE;
368           $user->asta_card  = -1;
369           $table->asta_pla_n--;
370           $again = FALSE;
371         }
372         else if ($a_card <= 9) {
373           if ($table->asta_card == 9) {
374             if ($a_card == 9 && $a_pnt <= 120 && $a_pnt > $table->asta_pnt)
375               $again = FALSE;
376           }
377           else {
378             if ($a_card >= 0 && $a_card <= 9 && $a_card > $table->asta_card)
379               $again = FALSE;
380           }
381
382           if ($again == FALSE) {
383             log_wr($sess, "NUOVI ORZI.");
384             $user->asta_card  = $a_card;
385             $table->asta_card = $a_card;
386             if ($a_card == 9) {
387               $user->asta_pnt   = $a_pnt;
388               $table->asta_pnt  = $a_pnt;
389             }
390           }
391         }
392       
393       
394       
395         if ($again) { // Qualcosa non andato bene, rifare
396           log_wr($sess, "Ripetere.");
397         }
398         else {
399           /* next step */
400           $showst = "show_astat("; 
401           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
402             $user_cur = &$bri->user[$table->player[$i]];
403             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
404                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
405           }
406           if (PLAYERS_N == 3)
407             $showst .= ",-2,-2";
408           $showst .= ");";
409
410           $maxcard = -2;
411           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
412             $user_cur = &$bri->user[$table->player[$i]];
413             if ($maxcard < $user_cur->asta_card)
414               $maxcard = $user_cur->asta_card;
415           }
416
417           if (($table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
418               !($table->asta_card == 9 && $table->asta_pnt == 120)) {
419             log_wr($sess,"ALLOPPA QUI");
420             for ($i = 1 ; $i < PLAYERS_N ; $i++) {
421               $index_next = ($table->gstart + $i) % PLAYERS_N;
422               if ($table->asta_pla[$index_next]) {
423                 log_wr($sess,"GSTART 1");
424                 $table->gstart += $i;
425                 break;
426               }
427             }
428           
429           
430             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
431               $user_cur = &$bri->user[$table->player[$i]];
432               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
433               if ($user_cur->table_pos == ($table->gstart % PLAYERS_N)) 
434                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_on();', 
435                                 $table->asta_card + 1, $table->asta_pnt+1, ($user_cur->handpt <= 2 ? "true" : "false"));
436               else
437                 $ret .= sprintf('dispose_asta(%d,%d, %s); remark_off();',
438                                 $table->asta_card + 1, -($table->asta_pnt+1), ($user_cur->handpt <= 2 ? "true" : "false"));
439               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
440               $user_cur->step++;
441             }
442           }
443           else if ($table->asta_pla_n == 0) {
444             log_wr($sess, "PASSANO TUTTI!");
445
446             log_wr($sess, sprintf("GIOCO FINITO !!!"));
447           
448             $table->old_reason = "Hanno passato tutti.";
449             $table->mult *= 2; 
450
451             $table->game_next();
452             $table->game_init(&$bri->user);
453           
454             for ($i = 0 ; $i < PLAYERS_N ; $i++) {      
455               $user_cur = &$bri->user[$table->player[$i]];
456
457               $ret = sprintf('gst.st = %d;', $user_cur->step+1);
458               $ret .= show_table(&$bri,&$user_cur,$user_cur->step+1, TRUE, TRUE);
459               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
460               $user_cur->step++;            
461             }
462           }
463           else {
464             log_wr($sess, "FINITA !");
465             // if a_pnt == 120 supergame ! else abbandono
466             if ($a_pnt == 120 || $user->asta_card != -1) {
467               $chooser = $index_cur;
468               for ($i = 1 ; $i < PLAYERS_N ; $i++) 
469                 if ($i != $chooser)
470                   $table->asta_pla[$i] = FALSE;
471             }
472             else {
473               //"gst.st = ".($user->step+1)."; dispose_asta(".($table->asta_card + 1).",".-($table->asta_pnt).", true); remark_off();";
474               $user->comm[$user->step % COMM_N] = sprintf( "gst.st = %d; dispose_asta(%d, %d, false); remark_off();", $user->step+1, $table->asta_card + 1,-($table->asta_pnt));
475               $user->step++;
476               for ($i = 1 ; $i < PLAYERS_N ; $i++) {
477                 $chooser = ($table->gstart + $i) % PLAYERS_N;
478                 if ($table->asta_pla[$chooser]) {
479                   break;
480                 }
481               }
482             }
483             $table->asta_win = $chooser;
484
485             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
486               $user_cur = &$bri->user[$table->player[$i]];
487               $ret = sprintf('gst.st = %d; %s', $user_cur->step+1, $showst);
488
489               if ($i == $chooser) {
490                 $ret .= "choose_seed(". $table->asta_card."); \$(\"asta\").style.visibility = \"hidden\"; remark_on();";
491               }
492               else {
493                 $ret .= "remark_off();";
494               }
495
496               $user_cur->comm[$user_cur->step % COMM_N] = $ret;
497               $user_cur->step++;            
498             }
499           }
500         }
501       }
502       else {
503         log_wr($sess, "NON CI SIAMO");
504       }
505     }
506     /*  asta::choose */
507     else if ($argz[0] == 'choose') {
508       if ($table->asta_win > -1 && 
509           $user->table_pos == $table->asta_win) {
510         $a_brisco = $argz[1];
511         if ($a_brisco >= 0 && $a_brisco < 40) {
512           $table->briscola = $a_brisco;
513           $table->friend   = $table->card[$a_brisco]->owner;
514           log_wr($sess,"GSTART 2");
515           $table->gstart = ($table->mazzo+1) % PLAYERS_N;
516           log_wr($sess, "Setta la briscola a ".$a_brisco);
517
518           $chooser = $table->asta_win;
519           $user_chooser = &$bri->user[$table->player[$chooser]];
520           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
521             $user_cur = &$bri->user[$table->player[$i]];
522             $user_cur->subst = 'game';
523             $ret = sprintf('gst.st = %d; subst = "game";', $user_cur->step+1);
524           
525
526             /* bg of caller cell */
527             $ret .= briscola_show($bri, $table, $user_cur);
528
529             /* first gamer */
530             if ($i == ($table->gstart % PLAYERS_N))
531               $ret .= "is_my_time = true; remark_on();";
532             else
533               $ret .= "is_my_time = false; remark_off();";
534
535             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
536             $user_cur->step++;      
537           }
538           /*
539             TUTTE LE VARIABILI DI STATO PER PASSARE A GIOCARE E LE
540             VAR PER PASSARE ALLA FASE DI GIOCO
541           */
542         
543         }
544       }
545     }
546   }
547   else if ($user->subst == 'game') {
548     log_wr($sess, "state: table::game".$argz[0]);
549
550     if ($argz[0] == 'play') {
551       $a_play = $argz[1];
552       $a_x =    $argz[2];
553       $a_y =    $argz[3];
554
555       if (strpos($a_x, "px") != FALSE)
556         $a_x = substr($a_x,0,-2);
557       if (strpos($a_y, "px") != FALSE)
558         $a_y = substr($a_y,0,-2);
559
560       $loggo = sprintf("A_play %s, table_pos %d == %d, mazzo %d, gstart %d, card_stat %d, card_own %d",
561                        $a_play, $user->table_pos, ($table->gstart % PLAYERS_N),
562                        $table->mazzo, $table->gstart,
563                        $table->card[$a_play]->stat, $table->card[$a_play]->owner);
564       log_wr($sess, "CIC".$loggo);
565                           
566       /* se era il suo turno e la carta era sua ed era in mano */
567       if ($a_play >=0 && $a_play < 40 &&
568           ($user->table_pos == (($table->gstart + $table->turn) % PLAYERS_N)) &&
569           $table->card[$a_play]->stat == 'hand' &&
570           $table->card[$a_play]->owner == $user->table_pos) {
571         log_wr($sess, sprintf("User: %s Play: %d",$user->name, $a_play));
572
573         /* Change the card status. */
574         $table->card[$a_play]->play($a_x, $a_y);
575
576         /*
577          *  !!!! TURN INCREMENTED BEFORE !!!!
578          */
579         $turn_cur = ($table->gstart + $table->turn) % PLAYERS_N;
580         $table->turn++;
581
582         $card_play = sprintf("card_play(%d,%d,%d,%d);|",
583                              $user->table_pos, $a_play, $a_x, $a_y);
584         if (($table->turn % PLAYERS_N) != 0) {     /* manche not finished */
585           $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
586
587           $player_cur = "remark_off();";
588           $player_nex = $card_play . "is_my_time = true; remark_on();";
589           $player_oth = $card_play;
590         }
591         else if ($table->turn <= (PLAYERS_N * 8)) { /* manche finished */
592           $winner = calculate_winner($table);
593           log_wr($sess,"GSTART 3");
594           $table->gstart = $winner;
595           $turn_nex = ($table->gstart + $table->turn) % PLAYERS_N;
596
597           log_wr($sess, sprintf("The winner is: [%d] [%s]", $winner, $bri->user[$table->player[$winner]]->name));
598           $card_take = sprintf("sleep(gst,2000);|cards_take(%d);|cards_hidetake($d);",
599                                $winner, $winner);
600           $player_cur = "remark_off();" . $card_take . "|"; 
601           if ($turn_cur != $turn_nex)
602             $player_nex = $card_play . $card_take . "|";
603           else
604             $player_nex = "";
605           if ($table->turn < (PLAYERS_N * 8))  /* game NOT finished */
606             $player_nex .= "is_my_time = true; remark_on();";
607           $player_oth = $card_play . $card_take;
608         }
609
610         log_wr($sess, sprintf("Turn Cur %d Turn Nex %d",$turn_cur, $turn_nex));
611         for ($i = 0 ; $i < PLAYERS_N ; $i++) {  
612           $user_cur = &$bri->user[$table->player[$i]];
613
614           $ret = sprintf('gst.st = %d; ', $user_cur->step+1);
615
616         
617           if ($i == $turn_cur) {
618             $ret .= $player_cur;          
619           }
620           if ($i == $turn_nex) {
621             $ret .= $player_nex;          
622           }
623           if ($i != $turn_cur && $i != $turn_nex) {
624             $ret .= $player_oth;
625           }
626
627           $retar[$i] = $ret;
628         }
629
630
631
632
633         if ($table->turn == (PLAYERS_N * 8)) { /* game finished */
634           log_wr($sess, sprintf("GIOCO FINITO !!!"));
635
636           /* ************************************************ */
637           /*    PRIMA LA PARTE PER LO SHOW DI CHI HA VINTO    */
638           /* ************************************************ */
639           calculate_points(&$table);
640
641           $table->game_next();
642           $table->game_init(&$bri->user);
643           
644           for ($i = 0 ; $i < PLAYERS_N ; $i++) {
645             $user_cur = &$bri->user[$table->player[$i]];
646             $retar[$i] .= show_table(&$bri,&$user_cur,$user_cur->step+1,TRUE, TRUE);
647           }
648         }
649
650
651         for ($i = 0 ; $i < PLAYERS_N ; $i++) {  
652           $user_cur = &$bri->user[$table->player[$i]];
653         
654           $user_cur->comm[$user_cur->step % COMM_N] = $retar[$i];
655           $user_cur->step++;        
656         }
657
658         log_wr($sess, sprintf("TURN: %d",$table->turn));
659         /* Have played all the players ? */
660         /* NO:  switch the focus and enable the next player to play. */
661       
662         /* YES: calculate who win and go to the next turn. */
663       }
664     }
665     else
666       log_wr($sess, "NOSENSE");
667   }
668 }
669 log_wr($sess, "before save data");
670 save_data($bri);
671
672 unlock_data($sem);
673 exit;
674 ?>