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