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