Delay_Management class added to avoid wrong timeout after temporarly server hungs
[brisk.git] / web / briskin5 / Obj / briskin5.phh
1 <?php
2 /*
3  *  brisk - briskin5.phh
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 define('BIN5_PLAYERS_N', 3);
25 define('BIN5_MAX_PLAYERS', BIN5_PLAYERS_N);
26 // define(BIN5_SHM_MIN', (50000 * BIN5_MAX_PLAYERS));
27 define('BIN5_SHM_MIN', 32768);
28 define('BIN5_SHM_MAX', (BIN5_SHM_MIN + 1048576));
29 define('BIN5_SHM_DLT', 32768);
30 define('BIN5_PROXY_PATH', PROXY_PATH."/bin5");
31
32 $mlang_bin5_bin5 = array( 
33                          'info_part' => array( 'it' => '<hr>Nell\'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>',
34                                                'en' => '<hr>In the last hand the declarer was <b>%s</b>, the partner was <b>%s</b>,<br>'),
35                          'info_capp' => array( 'it' => 'hanno fatto <b>cappotto</b> EBBRAVI!.<br>',
36                                                'en' => 'and they made <b>capot</b> WELL DONE!.<br>'),
37                          'info_pnt'  => array( 'it' => 'dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno %s.<br>',
38                                                'en' => 'they had to do <b>%s</b> points and they had made <b>%d</b>: they have %s.<br>'),
39                          'info_alea' => array( 'it' => 'almeno ',
40                                                'en' => 'at least '),
41                          'info_more' => array( 'it' => 'pi&ugrave; di 60',
42                                                'en' => 'over 60'),
43                          'info_win'  => array( 'it' => '<b>vinto</b>',
44                                                'en' => '<b>win</b>'),
45                          'info_peer' => array( 'it' => '<b>pareggiato</b>',
46                                                'en' => '<b>drew</b>'),
47                          'info_lost' => array( 'it' => '<b>perso</b>',
48                                                'en' => '<b>lost</b>'),
49
50                          'info_alon' => array( 'it' => '<hr>Nell\'ultima partita <b>%s</b> si &egrave; chiamato in mano,<br>',
51                                                'en' => '<hr>In the last hand <b>%s</b> play alone against each other,<br>'),
52                          'info_apnt' => array( 'it' => 'doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha %s.<br>',
53                                                'en' => 'he/she had to do <b>%s</b> points and they had made <b>%d</b>: he/she had %s.<br>'),
54                          'info_acap' => array( 'it' => 'ha fatto <b>cappotto</b> EBBRAVO!.<hr>',
55                                                'en' => 'and he/she made <b>capot</b> WELL DONE!.<hr>'),
56                          
57                          'info_omul' => array( 'it' => ' La partita valeva <b>%s</b>.',
58                                                'en' => ' EN: The game was worth <b>%s</b>.' ),
59
60                          'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
61                                                'en' => ' it\'s <b>your</b> turn.'),
62                          'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
63                                                'en' => 'it\'s the <b>%s</b>\'s turn.'),
64                          'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
65                                                'en' => ' The game worth <b>%s</b>.' ),
66                          'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
67                                                'en' => 'It\'s <b>your</b> shuffled the cards, '),
68                          'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
69                                                'en' => '<b>%s</b> shuffled the cards, '),
70                          'btn_bkgame'=> array( 'it' => 'torna alla partita',
71                                                'en' => 'back to the game'),
72                          'call_wptn' => array( 'it' => '<br>con %d punti',
73                                                'en' => '<br>with %d points'),
74                          'call_ycall'=> array( 'it' => 'Chiami%s:',
75                                                'en' => 'Call%s:'),
76                          'call_call' => array( 'it' => 'Chiama %s%s:',
77                                                'en' => 'The declarer is %s%s:')
78
79                          
80                           );
81
82 // MLANG
83 $table_wellarr = Array( 'it' => Array ( 'Benvenuto al tavolo. Se almeno tre giocatori non sbloccano l\'uscita cliccando il lucchetto, chi esce non pu&ograve; risedersi a un qualunque tavolo per '.floor(BAN_TIME/60).' minuti.'),
84                         'en' => Array ( 'EN Benvenuto al tavolo. Se almeno tre giocatori non sbloccano l\'uscita cliccando il lucchetto, chi esce non pu&ograve; risedersi a un qualunque tavolo per '.floor(BAN_TIME/60).' minuti.') );
85
86 function multoval($mult)
87 {
88     GLOBAL $G_lang;
89
90     if ($mult == 2)
91         return ($G_lang == 'en' ? 'double' : 'doppio');
92     if ($mult == 3)
93         return ($G_lang == 'en' ? 'triple' : 'triplo');
94     else if ($mult == 4)
95         return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
96     else
97         return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
98 }
99
100
101 class Card {
102     var $value; /* 0 - 39 card value */
103     var $stat;  /* 'bunch', 'hand', 'table', 'take' */
104     var $owner; /* (table position 0-4) */
105     // var $pos;   /* Pos in hand. */
106     var $x;     /* When played the X position on the table of the owner. */
107     var $y;     /* When played the Y position on the table of the owner. */
108
109     function Card($value, $stat, $owner)
110     {
111         $this->value = $value;
112         $this->stat  = $stat; // Card stat
113         $this->owner = $owner;
114     }
115
116     function assign($stat,$owner)
117     {
118         $this->stat  = $stat; // Card stat
119         $this->owner = $owner;
120     }
121
122     function setpos($pos)
123     {
124         $this->pos   = $pos;
125     }
126
127     function play($x,$y)
128     {
129         $this->stat = 'table'; // Card stat
130         $this->x = $x;
131         $this->y = $y;
132     }
133
134     function take($newown)
135     {
136         $this->stat = 'take'; // Card stat
137         $this->owner = $newown;
138     }
139 } // end class Card
140
141 class Bin5_table extends Table {
142     var $card;       // il mazzo di carte
143     var $mazzo;      // chi e' di mazzo
144     var $gstart;
145     var $turn;
146
147     var $asta_pla;
148     var $asta_pla_n;
149     var $asta_card;
150     var $asta_pnt;
151   
152     var $mult;
153     var $points;    // points array
154     var $points_n;  // number of row of points
155     var $total;
156
157     var $asta_win;
158     var $briscola;
159     var $friend;
160   
161     var $old_reason;
162     var $old_asta_pnt;
163     var $old_mult;
164     var $old_pnt;
165     var $old_win;
166     var $old_friend;
167
168     function Bin5_table() 
169     {
170     }
171
172
173     /* CREATE() NOT USED
174        function create($idx) 
175        {
176        if (($thiz =& new Bin5_table()) == FALSE)
177        return (FALSE);
178
179        $thiz->create($idx);
180
181        $thiz->card      =   FALSE;
182        $thiz->asta_pla  =   array(); // TRUE: in auction, FALSE: out of the auction
183        $thiz->asta_pla_n=  -1;
184        $thiz->asta_card =  -1;
185        $thiz->asta_pnt  =  -1;
186        $thiz->mult      =   0;
187     
188        $thiz->points    =   array( );
189        $thiz->points_n  =   0;
190        $thiz->total     =   array( 0, 0, 0, 0, 0);
191        $thiz->asta_win  =  -1;
192        $thiz->briscola  =  -1;
193        $thiz->friend    =  -1;
194        $thiz->turn      =   0;
195
196        $thiz->old_reason   = "";
197        $thiz->old_asta_pnt = -1;
198        $thiz->old_mult     = -1;
199        $thiz->old_pnt      = -1;
200        $thiz->old_win      = -1;
201        $thiz->old_friend   = -1;
202
203        return ($thiz);
204        }
205     */
206
207     /* CLONE() NOT USED
208        function myclone(&$from)
209        {
210        if (($thiz =& new Bin5_table()) == FALSE)
211        return (FALSE);
212     
213        parent::copy($from);
214
215        $thiz->card = $from->card;
216        $thiz->mazzo = $from->mazzo; // REVIEW
217        $thiz->gstart = $from->gstart;
218        $thiz->turn = $from->turn;
219
220        $thiz->asta_pla = $from->asta_pla;
221        $thiz->asta_pla_n = $from->asta_pla_n;
222        $thiz->asta_card = $from->asta_card;
223        $thiz->asta_pnt = $from->asta_pnt;
224     
225        $thiz->mult = $from->mult;
226        $thiz->points = $from->points;
227        $thiz->points_n = $from->points_n;
228        $thiz->total = $from->total;
229     
230        $thiz->asta_win = $from->asta_win;
231        $thiz->briscola = $from->briscola;
232        $thiz->friend = $from->friend;
233     
234        $thiz->old_reason = $from->old_reason;
235        $thiz->old_asta_pnt = $from->old_asta_pnt;
236        $thiz->old_mult = $from->mult;
237        $thiz->old_pnt = $from->old_pnt;
238        $thiz->old_win = $from->old_win;
239        $thiz->old_friend = $from->old_friend;
240
241        return ($thiz);
242        }
243     */
244
245     function parentcopy(&$from)
246     {
247         parent::copy($from);
248     }
249
250     function spawn(&$from)
251     {
252         if (($thiz =& new Bin5_table()) == FALSE)
253             return (FALSE);
254     
255         $thiz->parentcopy($from);
256
257         log_main("PLAYER_N - spawn.".$thiz->player_n);
258
259         $thiz->card = array();
260         $thiz->bunch_create();
261         $thiz->mazzo    = rand(0,PLAYERS_N-1);
262         $thiz->points_n = 0;
263         $thiz->mult     = 0;
264         $thiz->old_win    = -1;
265         $thiz->old_reason = "";
266
267         // players are rearranged in an dedicated array
268         $thiz->player = array();
269         for ($i = 0 ; $i < $from->player_n ; $i++)
270             $thiz->player[$i] = $i;
271
272         log_main("TABLE_OLD_WIN - spawn:".$thiz->old_win);
273
274         return ($thiz);
275     }
276
277     function asta2mult($asta_pnt)
278     {
279         if ($asta_pnt > 110)
280             return (6);
281         else if ($asta_pnt > 100)
282             return (5);
283         else if ($asta_pnt > 90)
284             return (4);
285         else if ($asta_pnt > 80)
286             return (3);
287         else if ($asta_pnt > 70)
288             return (2);
289         else
290             return (1);
291     }
292
293     function multer($is_new)
294     {
295         if ($is_new) {
296             return (pow(2, $this->mult) * $this->asta2mult($this->asta_pnt));
297         }
298         else {
299             return (pow(2, $this->old_mult) * $this->asta2mult($this->old_asta_pnt));
300         }
301     }
302
303
304     //   function bunch_create_old() function AND 
305     //   {
306     //     $ret = array();
307     // 
308     //     for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
309     //       // for ($i = 0 ; $i < 40 ; $i++) {
310     //       $ret[$i] =& new Card($i, 'bunch', 'no_owner');
311     //     }
312     // 
313     //     $oret = &$ret;
314     //     return ($oret);
315     //   }
316
317     function bunch_create()
318     {
319         $ret = array();
320       
321         for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
322             $this->card[$i] = new Card($i, 'bunch', 'no_owner');
323         }
324     }
325
326     function bunch_make()
327     {
328         log_main("bunch_make start");
329         $ct = array(0,0,0,0,0);
330     
331         mt_srand(make_seed());
332     
333         for ($i = (BIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--) 
334             $rest[$i] = $i;
335
336         for ($i = (BIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--) {
337             $rn = rand(0, $i);
338       
339             if ($rn == 0)
340                 log_main("RND ZERO");
341       
342             $id = $rest[$rn];
343
344             $owner = $i % BIN5_PLAYERS_N;
345             $this->card[$id]->assign('hand', $owner);
346
347             $rest[$rn] = $rest[$i];
348             // $pubbpos[$rn2] = $pubbpos[$i];
349         }
350         log_main("bunch_make end");
351     }
352
353     function init($userarr)
354     {
355         /* MOVED INTO SPAWN
356            $this->mazzo    =  rand(0,PLAYERS_N-1);
357            $this->points_n =  0;
358            $this->mult     =  0;
359            $this->old_win  = -1;
360            $this->old_reason = "";
361         */
362         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
363             $this->total[$i] = 0;
364             $user_cur = $userarr[$this->player[$i]];
365             $user_cur->exitislock = TRUE;
366         }
367
368         log_main("table::init: ci siamo");
369     }
370
371     function game_init($userarr)
372     {
373         log_rd2("GSTART 4");
374
375         $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
376         $this->bunch_make();
377     
378     
379         $this->asta_pla_n = BIN5_PLAYERS_N;
380         $this->asta_card = -1;
381         $this->asta_pnt  = 60;
382         $this->asta_win  = -1;
383         $this->briscola  = -1;
384         $this->friend    = -1;
385         $this->turn      =  0;
386     
387         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
388             $this->asta_pla[$i] = TRUE;
389             $user_cur = $userarr[$this->player[$i]];
390             $user_cur->subst = 'asta';
391             $user_cur->asta_card = -2;
392             $user_cur->asta_pnt  = -1;
393             $user_cur->handpt = $this->hand_points($i);
394             // SEE function calculate_points(&$table)
395         }
396         log_rd2("GEND 4");
397     }
398
399     function game_next()
400     {
401         $this->mazzo  = ($this->mazzo + 1) % BIN5_PLAYERS_N;
402     }
403
404
405     function hand_points($idx)
406     {
407         GLOBAL $G_all_points; 
408     
409         $tot = 0;
410     
411         for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
412             // for ($i = 0 ; $i < 40 ; $i++) {
413             if ($this->card[$i]->owner != $idx)
414                 continue;
415
416             $ctt = $this->card[$i]->value % 10;
417             $tot += $G_all_points[$ctt];
418         }
419
420         return ($tot);
421     }
422
423
424     function exitlock_show($userarr, $table_pos)
425     {
426         $ct = $this->exitlock_calc($userarr, $table_pos);
427
428         $ret = sprintf('exitlock_show(%d, %s);', $ct, 
429                        ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
430         return ($ret);
431     }
432
433     function exitlock_calc(&$userarr, $table_pos)
434     {
435         $ct = 0;
436
437         for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {        
438             if ($userarr[$this->player[$i]]->exitislock == FALSE)
439                 $ct++;
440         }
441
442         return ($ct);
443     }
444 } // end class Bin5_table
445
446
447
448
449 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
450
451 class Bin5_user extends User {
452     var $asta_card;  // 
453     var $asta_pnt;   //
454     var $handpt;     // Total card points at the beginning of the current hand.
455     var $exitislock; // Player can exit from the table ?
456     var $privflags;  // Flags for briskin5 only 
457
458     const BASE = "../";
459     
460     function User() {
461     }
462     
463     /* CREATE NOT USED
464        function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
465        if (($thiz =& new User()) == FALSE)
466        return (FALSE);
467      
468        $thiz->asta_card = -2;
469        $thiz->asta_pnt  = -1;
470        $thiz->handpt = -1;
471        $thiz->exitislock = TRUE;
472        $thiz->privflags = 0;
473      
474        return ($thiz);
475        }
476     */
477     
478     function parentcopy(&$from)
479     {
480         parent::copy($from);
481     }
482     
483     function copy(&$from)
484     {
485         $this->parentcopy($from);
486         
487         $this->asta_card  = $from->asta_card;
488         $this->asta_pnt   = $from->asta_pnt;
489         $this->handpt     = $from->handpt;
490         $this->exitislock = $from->exitislock;
491         $this->privflags  = $from->privflags;
492     }
493     
494     /* CLONE NOT USED
495        function myclone(&$from)
496        {
497        if (($thiz =& new User()) == FALSE)
498        return (FALSE);
499      
500        $thiz->copy($from);
501      
502        return ($thiz);
503        } 
504     */
505     
506     function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
507     {
508         if (($thiz = new Bin5_user()) == FALSE)
509             return (FALSE);
510
511         if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
512             $CO_bin5_pref_ring_endauct = "";
513         }
514
515         $thiz->parentcopy($from);
516         
517         /* NOTE: at this moment idx and table_pos fields have the same value 
518            but diffentent functions, we keep them separated for a while */
519         $thiz->room       = $bri;
520         $thiz->idx        = $table_pos;
521         $thiz->asta_card  = -2;
522         $thiz->asta_pnt   = -1;
523         $thiz->handpt     = -1;
524         $thiz->exitislock = TRUE;
525         
526         log_wr("Bin5 constructor");
527         
528         $this->privflags  = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
529
530         $thiz->table_orig = $table;
531         $thiz->table      = 0;
532         $thiz->table_pos  = $table_pos;
533         
534         $thiz->step_inc();
535
536         return ($thiz);
537     }
538
539     function step_set($step) 
540     {
541         $this->step = $step & 0x7fffffff;
542         
543         return TRUE;
544     }
545     
546     function step_inc($delta = 1) {
547         $this->step += $delta;
548         /* modularization because unpack() not manage unsigned 32bit int correctly */
549         $this->step &= 0x7fffffff;
550         
551         return (TRUE);
552     }
553
554     static function load_step($tab_id, $sess)
555     {
556         $fp = FALSE;
557         do {
558             if (validate_sess($sess) == FALSE)
559                 break;
560             
561             if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
562                 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
563             if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
564                 break;
565             if (($s = fread($fp, 8)) == FALSE)
566                 break;
567             if (mb_strlen($s, "ASCII") != 8)
568                 break;
569             $arr = unpack('Ls/Li', $s);
570             fclose($fp);
571             
572             // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
573             return ($arr);
574         } while (0);
575         
576         if ($fp != FALSE)
577             fclose($fp);
578         
579         log_rd2("STEP_GET [".$sess."]: return false ");
580         
581         return (FALSE);
582     }
583
584     function save_step() 
585     {
586         do {
587             if (validate_sess($this->sess) == FALSE)
588                 break;
589             if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
590                 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
591             if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
592                 break;
593             fwrite($fp, pack("LL",$this->step, $this->idx));
594             fclose($fp);
595             
596             log_main("step_set [".$this->sess. "] [".$this->step."]"); 
597             
598             return (TRUE);
599         } while (0);
600         
601         return (FALSE);
602     }
603     
604     static function unproxy_step($tab_id, $sess)
605     {
606         log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
607         if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
608             return;
609
610         @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
611     }
612     
613     function destroy_data($tab_id) 
614     {
615         do {
616             if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
617                 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
618                 break;
619             }
620           
621             if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
622                 log_crit("BIN5 USER DATA REMOVE FAILED 2");
623                 break;
624             }
625             if (shmop_delete($shm) == 0) {
626                 log_crit("BIN5 USER DATA REMOVE FAILED 3");
627                 break;
628             }
629             $shm = FALSE;
630
631             log_main("BIN5 USER DATA DESTROY SUCCESS");
632           
633             // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
634             $ret = TRUE;
635         } while (0);
636       
637         if ($shm)
638             shm_detach($shm);
639       
640         return ($ret);
641     }
642
643     static function blocking_error($is_unrecoverable)
644     {
645         log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
646         return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
647     }
648     
649     protected function page_sync($sess, $page)
650     {
651         log_rd2("PAGE_SYNC");
652         printf("xXx BIN5_USER::PAGE_SYNC\n");
653         return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
654     }
655
656     protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
657     {
658         GLOBAL $G_lang, $mlang_indrd;
659         GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
660         GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
661         $CO_splashdate = "CO_splashdate".$G_splash_idx;
662         $$CO_splashdate = $splashdate;
663         
664         GLOBAL $S_load_stat;
665         
666         log_rd("maincheck begin");
667         
668         $ret = FALSE;
669         $curtime = time();
670         
671       /* Nothing changed, return. */
672       if ($cur_step == $this->step) 
673           return (FALSE);
674       
675       log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
676       
677       if ($cur_step == -1) {
678           /*
679            *  if $cur_step == -1 load the current state from the main struct
680            */
681           
682           $S_load_stat['wR_minusone']++;
683           
684           // if ($this->the_end == TRUE) {
685           // log_rd2("main_check: the end".var_export(debug_backtrace()));
686           // }
687           
688           if ($this->trans_step != -1) {
689               log_rd2("TRANS USATO ".$this->trans_step);
690               $cur_step = $this->trans_step;
691               $this->trans_step = -1;
692           }
693           else {
694               log_rd2("TRANS NON ATTIVATO");
695           }
696       }
697       
698       
699       /* this part I suppose is read only on $this->room structure */
700       if ($cur_step == -1) {
701           log_rd2("PRE-NEWSTAT");
702               
703           /***************
704            *             *
705            *    TABLE    *
706            *             *
707            ***************/
708           if ($this->stat == 'table') {
709               log_load("RESYNC");
710               /* NOTE: $this->room is associated with the current $bri object */
711               printf("xXx CLASS NAME [%s]\n", get_class($this->room));
712               $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
713           }
714           log_rd2("NEWSTAT: ".$this->stat);
715
716           $new_stat =  $this->stat;
717           $new_subst = $this->subst;
718           $new_step =  $this->step;
719       } /* if ($cur_step == -1) { */
720       else {
721           /* $sem = Room::lock_data(FALSE); */
722           $S_load_stat['rU_heavy']++;
723           
724           if ($cur_step < $this->step) {
725               do {
726                   if ($cur_step + COMM_N < $this->step) {
727                       if (($cur_stat != $this->stat)) {
728                           $to_stat = $this->stat;
729                           /* Room::unlock_data($sem); */
730                           log_load("RESYNC");
731                           printf("xXx BIN5_USER::MAINCHECK\n");
732                           return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
733                       }
734                       log_rd2("lost history, refresh from scratch");
735                       printf("xXx LOST HISTORY!\n");
736                       $new_step = -1;
737                       break;
738                   } 
739                   for ($i = $cur_step ; $i < $this->step ; $i++) {
740                       $ii = $i % COMM_N;
741                       log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
742                       $ret .= $this->comm[$ii];
743                   }
744                   $new_stat =  $this->stat;
745                   $new_subst = $this->subst;
746                   $new_step =  $this->step;
747               } while (0);
748               
749               log_rd2($this->step, 'index_rd.php: after ret set');
750               
751               if ($this->the_end == TRUE) {
752                   log_rd2("LOGOUT BYE BYE!!");
753                   log_auth($this->sess, "Explicit logout.");
754                   
755                   if ($this->the_end == TRUE) {
756                       $this->reset();
757                       
758                       // FIXME !!! 
759                       /* if ($this->subst == 'sitdown') { */
760                       /*     log_load("ROOM WAKEUP"); */
761                       /*     $this->room->room_wakeup($this); */
762                       /* } */
763                       /* else if ($this->subst == 'standup') */
764                       /*     $this->room->room_outstandup($this); */
765                       /* else */
766                       /*     log_rd2("LOGOUT FROM WHAT ???"); */
767                       
768                   } /* if ($this->the_end == TRUE) { ... */
769               } /* if ($this->the_end == TRUE) { ... */
770           } /* if ($cur_step < $this->step) { */
771           
772           /* Room::unlock_data($sem); */
773       }  /* else of if ($cur_step == -1) { */
774       
775     
776       return ($ret);
777   }  //   function maincheck (...
778
779
780
781
782
783
784 } // end class Bin5_user
785
786
787
788 class Bin5 {
789     static $delta_t = array();
790
791     var $user;
792     var $table;
793     var $comm; // commands for many people
794     var $step; // current step of the comm array
795     var $garbage_timeout;
796     var $shm_sz;
797     
798     var $table_idx;
799     var $table_token;
800     
801     var $the_end;
802     var $tok;
803
804     var $delay_mgr;
805
806     static function page_manager($room, $header_out, $path, $method, $addr, $get, $post, $cookie) 
807     {
808         switch ($path) {
809         case "":
810         case "index.php":
811             ob_start();
812             bin5_index_main($room, $header_out, $addr, $get, $post, $cookie);
813             $content = ob_get_contents();
814             ob_end_clean();
815             
816             force_no_cache($header_out);
817
818             $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
819             
820             if ($pgflush->try_flush($curtime) == FALSE) {
821                 // Add $pgflush to the pgflush array
822                 array_push($pages_flush, $pgflush);
823             }
824             break;
825         }
826         return TRUE;
827     }
828
829     function Bin5 ($room, $table_idx, $table_token, $get, $post, $cookie) {
830         $this->user = array();
831         $this->table = array();
832         
833         $this->the_end = FALSE;
834         $this->shm_sz = BIN5_SHM_MIN;
835         if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
836             echo "FTOK FAILED";
837             exit;
838         }
839         
840         $user  = $room->user;
841         $table = $room->table[$table_idx];
842         
843         log_wr("Bin5 constructor");
844         
845         for ($i = 0 ; $i < $table->player_n ; $i++) {
846             $user[$table->player[$i]]->table_token = $table_token;
847             $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
848         }
849         $this->table[0] = Bin5_table::spawn(&$table);
850         
851         log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_win);
852         
853         $this->table_idx = $table_idx;
854         $this->table_token = $table_token;
855         $this->garbage_timeout = 0;
856         
857         $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
858
859         log_wr("Bin5 constructor end");
860     }
861     
862
863     function get_user($sess, &$idx)
864     {
865         GLOBAL $PHP_SELF;
866         
867         if (validate_sess($sess)) {
868             for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
869                 if (strcmp($sess, $this->user[$i]->sess) == 0) {
870                     // find it
871                     $idx = $i;
872                     $ret = &$this->user[$i];
873                     return ($ret);
874                 }
875             }
876             log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
877             // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) 
878             // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
879         }
880         else {
881             log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
882         }
883         
884         return (FALSE);
885     }
886     
887     
888     function garbage_manager($force)
889     {
890         GLOBAL $G_base;
891         
892         /* Garbage collector degli utenti in timeout */
893         $ismod = FALSE;
894         $curtime = time();
895
896         $delta = $this->delay_mgr->delta_get($curtime);
897
898         if ($force || $this->garbage_timeout < $curtime) {
899             for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
900                 $user_cur = $this->user[$i];
901                 if ($user_cur->sess == "" || 
902                     ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
903                     continue;
904                 
905                 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
906                     log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
907                     
908                     if ($user_cur->stat == 'table') {
909                         log_auth($user_cur->sess," bin5 Autologout session.");
910                         
911                         /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE 
912                            
913                            $tmp_sess = $user_cur->sess;
914                            $user_cur->sess = "";
915                            Bin5_user::step_unproxy($tmp_sess);
916                            $user_cur->name = "";
917                            $user_cur->the_end = FALSE;
918                            
919                         */
920                         
921                         /* se gli altri utenti non erano d'accordo questo utente viene bannato */
922                         $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
923                         if ($remcalc < 3) {
924                             require_once("${G_base}Obj/hardban.phh");
925                             Hardbans::add(($user_cur->flags & USER_FLAG_AUTH ? $user_cur->name : FALSE),
926                                           $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
927                         }
928                         //      $user->bantime = $user->laccwr + BAN_TIME;
929                         
930                         $this->table_wakeup($user_cur);
931                     }
932                 }
933             }
934             log_rd2($user_cur->sess." GARBAGE UPDATED!");
935             
936             $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
937             
938             $ismod = TRUE;
939         }
940         
941         $this->delay_mgr->lastcheck_set($curtime);
942         return ($ismod);
943     }
944     
945
946     function destroy_data() 
947     {
948         GLOBAL $sess;
949
950         $ret =   FALSE;
951         $shm =   FALSE;
952         log_main("DESTROY BRISKIN5 DATA");
953     
954         do {
955             log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
956             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
957                 $this->user[$i]->destroy_data($this->table_idx);
958                 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
959             }
960             if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1) 
961                 break;
962
963             if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
964                 break;
965       
966             if (shmop_delete($shm) == 0) {
967                 log_only("REMOVE FALLITA ");
968                 break;
969             }
970    
971             $shm = FALSE;
972             log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
973       
974             // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
975             $ret = TRUE;
976         } while (0);
977     
978         if ($shm)
979             shm_detach($shm);
980     
981         return ($ret);
982     }
983
984     static function lock_data($is_exclusive, $table_idx)
985     {
986         if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
987             self::$delta_t = microtime(TRUE);
988             log_lock("LOCK   table [".$table_idx."]         [".self::$delta_t[$table_idx]."]");
989           
990             return (new Vect(array('res' => $res, 'tab' => $table_idx)));
991         }
992       
993         return (FALSE);
994     }
995
996   
997     static function unlock_data($res_vect)
998     {
999         GLOBAL $sess; 
1000
1001         $res = $res_vect->getbyid('res');
1002         $tab = $res_vect->getbyid('tab');
1003
1004         log_lock("UNLOCK table [".$tab."]         [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1005
1006         file_unlock($res);
1007     }
1008
1009
1010     function chatt_send($user, $mesg)
1011     {
1012         GLOBAL $mlang_brisk, $G_lang;
1013
1014         if ($user->stat == 'table') {
1015             $table = &$this->table[$user->table];
1016         }
1017     
1018         $user_mesg = substr($mesg,6);
1019     
1020         $curtime = time();
1021     
1022         $dt = date("H:i ", $curtime);
1023         if (strncmp($user_mesg, "/nick ", 6) == 0) {
1024             log_main($user->sess." chatt_send BEGIN");
1025
1026             if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1027                 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1028                 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1029                 $user->step_inc();
1030
1031                 return;
1032             }
1033             $user_mesg = "COMMAND ".$user_mesg;
1034             // Search dup name
1035             // change
1036             // update local graph
1037             // update remote graphs
1038             for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1039                 $user_cur = $this->user[$i];
1040                 //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
1041                 if ($user_cur->sess == '')
1042                     continue;
1043                 if (strcasecmp($user_cur->name, $name_new) == 0) {
1044                     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1045
1046                     $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1047                     $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
1048                     $user->step_inc();
1049                     break;
1050                 }
1051             }
1052             if ($i == BIN5_MAX_PLAYERS) {
1053                 if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
1054                     if ($this->table[$user->table]->auth_only == TRUE) {
1055                         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1056                         $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1057                         $user->step_inc();
1058                         return;
1059                     }
1060                     else {
1061                         $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1062                     }
1063                 }
1064     
1065                 $user->name = $name_new;
1066
1067                 log_main($user->sess." chatt_send start set");
1068         
1069
1070                 for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1071                     log_main($user->sess." chatt_send set loop");
1072           
1073                     $user_cur = &$this->user[$i];
1074                     if ($user_cur->sess == '')
1075                         continue;
1076           
1077                     if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1078                         $table = &$this->table[$user->table];
1079             
1080                         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1081                         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1082                                                                              $this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->flags,
1083                                                                              xcape($this->user[$table->player[($user_cur->table_pos) % BIN5_PLAYERS_N]]->name),
1084
1085                                                                              $this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->flags,
1086                                                                              xcape($this->user[$table->player[($user_cur->table_pos+1) % BIN5_PLAYERS_N]]->name),
1087
1088                                                                              $this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->flags,
1089                                                                              xcape($this->user[$table->player[($user_cur->table_pos+2) % BIN5_PLAYERS_N]]->name),
1090
1091                                                                              (BIN5_PLAYERS_N == 3 ? 0:  $this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->flags),
1092                                                                              (BIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3) % BIN5_PLAYERS_N]]->name)),
1093
1094                                                                              (BIN5_PLAYERS_N == 3 ? 0:  $this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->flags),
1095                                                                              (BIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4) % BIN5_PLAYERS_N]]->name)));
1096                         if ($user_cur == $user) {
1097                             $user_cur->comm[$user_cur->step % COMM_N] .= $user_cur->myname_innerHTML();
1098                         }
1099                         $user_cur->step_inc();
1100                     }
1101                 }
1102             }
1103         }
1104         else {
1105             for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1106                 if ($user->stat == 'room') {
1107                     $user_cur = &$this->user[$i];
1108                     if ($user_cur->sess == '' || $user_cur->stat != 'room')
1109                         continue;
1110                 }
1111                 else {
1112                     $user_cur = &$this->user[$table->player[$i]];
1113                 }
1114         
1115                 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1116                 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1117                                                                      $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1118                 $user_cur->step_inc();
1119             }
1120             log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1121         }
1122     }
1123
1124     function table_wakeup($user)
1125     {
1126         $table = &$this->table[0];
1127
1128         log_main("BIN5_WAKEUP begin function table  stat: ".$user->stat."  subst: ".$user->subst);
1129
1130         $curtime = time();
1131
1132         log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1133     
1134         for ($i = 0 ; $i < $table->player_n ; $i++) {
1135             $user_cur = &$this->user[$i];
1136             log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1137       
1138             if ($user_cur == $user)
1139                 $user_cur->subst = "shutdowner";
1140             else
1141                 $user_cur->subst = "shutdowned";
1142             $user_cur->laccwr = $curtime;
1143
1144             $ret = "gst.st = ".($user_cur->step+1)."; ";
1145             $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1146
1147             log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1148             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1149             $user_cur->step_inc();
1150         }
1151
1152         $this->the_end = TRUE;
1153     }
1154   
1155     /*
1156      *  If all players are freezed the room garbage_manager clean up table and users.
1157      */ 
1158     function is_abandoned() 
1159     {
1160         $is_ab = TRUE;
1161         $curtime = time();
1162
1163         $table = &$this->table[0];
1164
1165         for ($i = 0 ; $i < $table->player_n ; $i++) {
1166             $user_cur = &$this->user[$i];
1167
1168             if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) { 
1169                 $is_ab = FALSE;
1170                 break;
1171             }
1172         }
1173
1174         return ($is_ab);
1175     }
1176
1177     static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1178     {
1179         printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1180         
1181         $enc = get_encoding($header);
1182         if (isset($header['User-Agent'])) {
1183             if (strstr($header['User-Agent'], "MSIE")) {
1184                 $transp_type = "htmlfile";
1185             }
1186             else {
1187                 $transp_type = "xhr";
1188             }
1189         }
1190         else {
1191             $transp_type = "iframe";
1192         }
1193         force_no_cache($header_out);
1194
1195         if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE) 
1196             unset($table_idx);
1197         
1198         if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE) 
1199             unset($table_token);
1200
1201
1202         switch ($path) {
1203         case "":
1204         case "index.php":
1205             ob_start();
1206             bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1207             $content = ob_get_contents();
1208             ob_end_clean();
1209         
1210             $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
1211             return TRUE;
1212         
1213         break;
1214         case "index_wr.php":
1215             // FIXME $content can be unset
1216             if (isset($table_idx) && isset($table_token)) {
1217                 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1218                     ob_start();
1219                     bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1220                     $content = ob_get_contents();
1221                     ob_end_clean();
1222                 }
1223                 else {
1224                     $content = "Bin5 Load data error";
1225                 }
1226             }
1227             else {
1228                 $content = "Bin5 Load data error";
1229             }
1230             $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
1231             return TRUE;
1232             
1233             break;
1234         case "index_rd_ifra.php":
1235             do {
1236                 if (!isset($table_idx)
1237                     || !isset($table_token)
1238                     || !isset($cookie['sess'])
1239                     || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1240                     || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1241
1242                     $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1243                     $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
1244
1245                     return TRUE;
1246                     break;
1247                 }
1248                 // close a previous opened index_read_ifra socket, if exists
1249                 if (($prev = $user->rd_socket_get()) != NULL) {
1250                     $s_a_p->socks_unset($user->rd_socket_get());
1251                     fclose($user->rd_socket_get());
1252                     printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1253                     $user->rd_socket_set(NULL);
1254                 }
1255                 
1256                 $content = "";
1257                 $user->stream_init($s_a_p->rndstr, $enc, $header_out, $content, $get, $post, $cookie);
1258                 $response = headers_render($header_out, -1).chunked_content($user->rd_zls_get(), $content);
1259                 $response_l = mb_strlen($response, "ASCII");
1260                 
1261                 $wret = @fwrite($new_socket, $response, $response_l);
1262                 if ($wret < $response_l) {
1263                     printf("TROUBLES WITH FWRITE: %d\n", $wret);
1264                     $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1265                 }
1266                 else {
1267                     $user->rd_cache_set("");
1268                 }
1269                 fflush($new_socket);
1270                 
1271                 
1272                 $s_a_p->socks_set($new_socket, $user);
1273                 $user->rd_socket_set($new_socket);
1274                 printf(" - qui ci siamo - ");
1275                 return TRUE;
1276             } while (FALSE);
1277             
1278             return FALSE;
1279             break;
1280             
1281         default:
1282             return FALSE;
1283             break;
1284       }
1285
1286       return (FALSE);
1287   }
1288
1289
1290
1291 } // end class Bin5
1292
1293 function locshm_exists($tok)
1294 {
1295     // return (TRUE);
1296
1297     if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1298         log_main($tok." SHM NOT exists");
1299
1300         return (FALSE);
1301     }
1302     else {
1303         shmop_close($id);
1304         log_main($tok." SHM exists");
1305
1306         return (TRUE);
1307     }
1308     
1309 }
1310
1311
1312 // rendiamo qui l'elenco dei punti come return della func
1313 function calculate_points(&$table)
1314 {
1315     GLOBAL $G_all_points; 
1316
1317     $ret = array();
1318     $pro = 0;
1319
1320     if ($table->asta_pnt == 60)
1321         $table->asta_pnt = 61;
1322
1323     $table->old_reason = "";
1324     $table->old_win = $table->asta_win;
1325     $table->old_friend = $table->friend;
1326     $table->old_asta_pnt = $table->asta_pnt;
1327     $table->old_mult = $table->mult;
1328
1329     // count points for the temporary 2 teams
1330     for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1331         // for ($i = 0 ; $i < 40 ; $i++) {
1332         $ctt = $table->card[$i]->value % 10;
1333         $own = $table->card[$i]->owner;
1334         if ($own == $table->asta_win || $own == $table->friend) 
1335             $pro += $G_all_points[$ctt];
1336     }
1337
1338     log_wr(sprintf("PRO: [%d]", $pro));
1339
1340     // PATTA case !
1341     if ($table->asta_pnt == 61 && $pro == 60) {
1342         $table->points[$table->points_n % MAX_POINTS] = array();
1343         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1344             $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1345             $ret[$i] = 0;
1346         }
1347         $table->points_n++;
1348         $table->old_pnt = $pro;
1349         $table->mult += 1;
1350
1351         return($ret);
1352     }
1353
1354     if ($pro >= $table->asta_pnt) 
1355         $sig = 1;
1356     else
1357         $sig = -1;
1358
1359     $table->points[$table->points_n % MAX_POINTS] = array();
1360     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1361         if ($i == $table->asta_win) 
1362             $pt = ($i == $table->friend ? 4 : 2);
1363         else if ($i == $table->friend) 
1364             $pt = 1;
1365         else
1366             $pt = -1;
1367     
1368     
1369
1370         log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1371
1372         $pt = $pt * $sig * $table->multer(TRUE) * ($pro == 120 ? 2 : 1);
1373
1374         log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->multer(TRUE), ($pro == 120 ? 2 : 1)));
1375     
1376         $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1377         $table->total[$i] += $pt;
1378         $ret[$i] = $pt;
1379     }
1380     $table->points_n++;
1381     $table->old_pnt = $pro;
1382     $table->mult = 0;
1383   
1384     return($ret);
1385 }
1386
1387 /* show table 
1388    is_transition (is from room to table ?)
1389    is_again      (is another game)
1390
1391    Examples                    of $is_transition, $is_again:
1392    from reload of the page:              FALSE, FALSE
1393    from sitdown in room:                  TRUE, FALSE
1394    from table: asta cmd e tutti passano:  TRUE, TRUE
1395    from table: fine partita:              TRUE, TRUE
1396 */
1397 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1398 {
1399     $table_idx = $user->table;
1400     $table     = $bri->table[$table_idx];
1401     $table_pos = $user->table_pos;
1402
1403     $ret = "table_init();";
1404     $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1405     if (!$is_again) {
1406         /* GENERAL STATUS */
1407         $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1408                          $sendstep, $user->stat, $user->subst, $table_pos);
1409
1410         log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1411
1412         /* BACKGROUND */
1413         $ret .= "background_set();";
1414     
1415         /* USERS INFO */
1416         $ret .= $user->myname_innerHTML();
1417         $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1418                         $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1419                         xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1420
1421                         $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1422                         xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1423
1424                         $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1425                         xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1426
1427                         (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1428                         (BIN5_PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1429
1430                         (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1431                         (BIN5_PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1432     }
1433     /* NOTIFY FOR THE CARD MAKER */
1434     if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1435         $ret .= show_table_info(&$bri, &$table, $table_pos);
1436         $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1437     }
1438     if (!$is_again) 
1439         $ret .= table_welcome($user);
1440
1441     if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1442         $ret .= playsound("cow.mp3");
1443     }
1444
1445
1446     /* CARDS */
1447     if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1448         $ret .= "|";
1449     
1450         for ($i = 0 ; $i < 8 ; $i++) {
1451             for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1452                 $ct = 0;
1453                 for ($o = 0 ; $o < (BIN5_PLAYERS_N == 5 ? 40 : 24) && $ct < $i+1 ; $o++) {
1454                     // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1455                     if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1456                         $ct++;
1457                         if ($ct == $i+1)
1458                             break;
1459                     }
1460                 }
1461                 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1462         
1463                 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N, 
1464                                  $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1465                                       $table->card[$o]->value : -1), 
1466                                  ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1467             }
1468         }    
1469     }
1470     else {
1471         $taked  = array(0,0,0,0,0);
1472         $inhand = array(0,0,0,0,0);
1473         $ontabl  = array(-1,-1,-1,-1,-1);
1474         $cards  = array();
1475
1476         for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1477             // for ($i = 0 ; $i < 40 ; $i++) {
1478             if ($table->card[$i]->stat == 'hand') {
1479                 if ($table->card[$i]->owner == $table_pos) {
1480                     $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1481                 }
1482                 $inhand[$table->card[$i]->owner]++;
1483             }
1484             else if ($table->card[$i]->stat == 'take') {
1485                 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1486                 $taked[$table->card[$i]->owner]++;
1487             }
1488             else if ($table->card[$i]->stat == 'table') {
1489                 $ontabl[$table->card[$i]->owner] = $i;
1490             }
1491         }
1492         $logg = "\n";
1493         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1494             $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1495         }
1496         log_main("Stat table: ".$logg);
1497
1498         /* Set ours cards. */
1499         $oursarg = "";
1500         for ($i = 0 ; $i < $inhand[$table_pos] ; $i++) 
1501             $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1502         for ($i = $inhand[$table_pos] ; $i < 8 ; $i++) 
1503             $oursarg .= ($i == 0 ? "" : ", ")."-1";
1504         $ret .= sprintf('card_setours(%s);', $oursarg);
1505
1506         /* Dispose all cards */
1507         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1508             /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1509             $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1510                             ($inhand[$i] <= 8 ? $inhand[$i] : 8)  , $taked[$i]);
1511
1512             if ($ontabl[$i] != -1) {
1513                 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i], 
1514                                 $table->card[$ontabl[$i]]->value, 
1515                                 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1516             }
1517         }
1518     }
1519
1520     /* Show auction */
1521     if ($user->subst == 'asta') {
1522
1523         /* show users auction status */
1524         $showst = "";
1525         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1526             $user_cur = &$bri->user[$table->player[$i]];
1527             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
1528                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1529         }
1530         if (BIN5_PLAYERS_N == 3)
1531             $showst .= ",-2,-2";
1532         $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1533         $ret .= sprintf('show_astat(%s);', $showst);
1534
1535         if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1536             /* show card chooser */
1537             $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1538                             $table->asta_card);
1539         }
1540         else {
1541             /* show auction */
1542             if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1543                 $table->asta_win == -1) 
1544                 $ret .= sprintf('dispose_asta(%d,%d, %s);', 
1545                                 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1546             else
1547                 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1548                                 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ?  "true" : "false"));
1549         }
1550
1551         /* Remark */
1552         if ($table->asta_win == -1) { // auction case
1553             if ($table_pos == ($table->gstart % BIN5_PLAYERS_N)) 
1554                 $ret .= "remark_on();";
1555             else
1556                 $ret .= "remark_off();";
1557         }
1558         else { // chooseed case
1559             if ($table_pos == $table->asta_win) 
1560                 $ret .= "remark_on();";
1561             else
1562                 $ret .= "remark_off();";
1563         }
1564     }
1565     else if ($user->subst == 'game') {
1566         /* HIGHLIGHT */
1567         if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos) 
1568             $ret .= "is_my_time = true; remark_on();";
1569         else
1570             $ret .= "remark_off();";
1571     
1572         /* WHO CALL AND WHAT */
1573         $ret .= briscola_show($bri, $table, $user);
1574     
1575     }
1576     return ($ret);
1577 } // end function show_table(...
1578
1579 function calculate_winner(&$table)
1580 {
1581     $briontab = FALSE;
1582     $ontab = array();
1583     $ontid = array();
1584     $cur_win  =  -1;
1585     $cur_val  = 100;
1586     $cur_seed = $table->briscola - ($table->briscola % 10);
1587
1588     for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1589         // for ($i = 0 ; $i < 40 ; $i++) {
1590         if ($table->card[$i]->stat != "table")
1591             continue;
1592
1593         log_wr(sprintf("Card On table: [%d]", $i));
1594
1595         $v = $table->card[$i]->value; 
1596         $ontab[$table->card[$i]->owner] = $v;
1597         $ontid[$table->card[$i]->owner] = $i;
1598         /* se briscola setto il flag */
1599         if (($v - ($v % 10)) == $cur_seed)
1600             $briontab = TRUE;
1601     }
1602
1603     if ($briontab == FALSE) {
1604         $cur_win  = $table->gstart;
1605         $cur_val  = $ontab[$cur_win];
1606         $cur_seed = $cur_val - ($cur_val % 10);
1607     }
1608
1609     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1610         if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1611             if ($ontab[$i] < $cur_val) {
1612                 $cur_val = $ontab[$i];
1613                 $cur_win = $i;
1614             }
1615         }
1616     }
1617
1618     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1619         $table->card[$ontid[$i]]->owner = $cur_win;
1620         $table->card[$ontid[$i]]->stat =  "take"; // Card stat
1621     }
1622     return ($cur_win);
1623 }
1624
1625 function show_table_info(&$bri, &$table, $table_pos)
1626 {
1627     GLOBAL $G_lang, $mlang_bin5_bin5;
1628
1629     $ret = "";
1630     $user = $bri->user[$table->player[$table_pos]];
1631
1632     $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1633     $noty = sprintf('<table class=\"points\"><tr><th></th>');
1634   
1635     // Names.
1636     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) 
1637         $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1638     $noty .= sprintf("</tr>");
1639
1640     // Points.
1641     log_main("show_table_info: pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
1642
1643     for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1644         $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1645         for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) 
1646             $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1647         $noty .= "</tr>";
1648     }
1649
1650     // Total points.
1651     $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1652     for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) 
1653         $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1654     $noty .= "</tr></table>";
1655
1656     if ($table->old_reason != "") {
1657         $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1658     }
1659
1660     if ($table->old_win != -1) {
1661         log_main("TABLE_OLD_WIN:".$table->old_win);
1662         $win = $table->player[$table->old_win];
1663         log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1664         $fri = $table->player[$table->old_friend];
1665
1666         $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1667
1668         if ($win != $fri) {
1669             /* MLANG: "<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", "hanno fatto <b>cappotto</b> EBBRAVI!.<hr>", "dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>", "<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", "ha fatto <b>cappotto</b> EBBRAVO!.<hr>", "doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>", ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt : 'pi&ugrave; di 60'), $table->old_pnt, ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso" */
1670       
1671             $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang], 
1672                              xcape($bri->user[$win]->name),
1673                              xcape($bri->user[$fri]->name));
1674             if ($table->old_pnt == 120) {
1675                 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
1676             }
1677             else {
1678                 $noty .= sprintf($mlang_bin5_bin5['info_pnt'][$G_lang],
1679                                  ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1680                                   $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1681                                  ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] : ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] : $mlang_bin5_bin5['info_lost'][$G_lang])));
1682             }
1683         }
1684         else {
1685             $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
1686                              xcape($bri->user[$win]->name));
1687             if ($table->old_pnt == 120) {
1688                 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang]);
1689             }
1690             else {
1691                 $noty .= sprintf($mlang_bin5_bin5['info_apnt'][$G_lang],
1692
1693                                  ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1694                                   $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1695
1696                                  ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] : ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] : $mlang_bin5_bin5['info_lost'][$G_lang])));
1697             }
1698         }
1699         $old_multer = $table->multer(FALSE);
1700         if ($old_multer > 1) {
1701             $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang], multoval($old_multer));
1702         }
1703         $noty .= "<hr><br>";
1704     }
1705     /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1706     if ($table->mazzo == $table_pos) 
1707         $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1708     else {
1709         $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1710         $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1711     }
1712
1713     if ($user->subst == 'asta') {
1714         if ($table->asta_win == -1)  // auction case
1715             $curplayer = $table->gstart % BIN5_PLAYERS_N;
1716         else 
1717             $curplayer = $table->asta_win;
1718     }
1719     else if ($user->subst == 'game') {
1720         $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
1721     }
1722
1723     /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1724     if ($curplayer == $table_pos) {
1725         $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1726     }
1727     else {
1728         $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1729         $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1730     }
1731   
1732     $multer = $table->multer(TRUE);
1733     if ($multer > 1) {
1734         $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
1735     }
1736     $noty .= "<hr><br>";
1737     $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1738     /* NOTE: show_notify($noty, 3000, "torna alla partita", 500, 
1739      *                   130 + ($table->points_n > 0 ? 50 : 0) + 
1740      *                   (120 * ($table->points_n / MAX_POINTS)));
1741      *       will be used when we refact notify js function following 
1742      *       photoo class logic 
1743      */ 
1744   
1745     return ($ret);
1746 }
1747
1748 function table_welcome($user)
1749 {
1750     GLOBAL $table_wellarr, $G_lang;
1751     $ret = "";
1752
1753     for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1754         $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1755
1756     return ($ret);
1757 }
1758
1759
1760 function briscola_show($bri, $table, $user)
1761 {
1762     GLOBAL $G_lang, $mlang_bin5_bin5;
1763     $ptnadd = "";
1764     $ret = "";
1765
1766     if ($table->asta_card == 9) 
1767         $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1768   
1769     /* text of caller cell */
1770     if ($user->table_pos == $table->asta_win) {
1771         $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1772         $ret .= sprintf($prestr, $ptnadd);
1773     }
1774     else {
1775         $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1776         $ret .= sprintf($prestr, 
1777                         xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1778     }
1779     $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
1780
1781     $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1782                     $table->asta_win);
1783     $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1784     $ret .= sprintf('$("caller").style.visibility = "visible";');
1785     $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1786     $ret .= sprintf('$("astalascio").style.visibility = "";');
1787     $ret .= sprintf('$("asta").style.visibility = "hidden";');
1788     $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1789   
1790     return ($ret);
1791 }
1792
1793
1794 function game_result($asta_pnt, $pnt)
1795 {
1796     if ($asta_pnt == 61) {
1797         if ($pnt > 60)
1798             return (1);
1799         else if ($pnt == 60)
1800             return (0);
1801         else
1802             return (-1);
1803     }
1804     else {
1805         if ($pnt >= $asta_pnt)
1806             return (1);
1807         else
1808             return (-1);
1809     }
1810 }
1811
1812 function log_points($remote_addr, $curtime, $user, $where, $mesg) 
1813 {
1814     if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
1815         /* Unix time | session | nickname | IP | where was | mesg */
1816         fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
1817                             ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
1818                             $user->name, $remote_addr, $where , $mesg));
1819         fclose($fp);
1820     }
1821 }
1822
1823 ?>