css skinned buttons
[brisk.git] / web / brisk.phh
1 <?php
2 /*
3  *  brisk - brisk.phh
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  */
21
22 define( FTOK_PATH, "/var/lib/brisk");
23 define(TABLES_N, 8);
24 define(PLAYERS_N, 3);
25 define(MAX_POINTS, 5);
26 define(MAX_PLAYERS, (PLAYERS_N * TABLES_N));
27 define(COMM_N, 12);
28 define(COMM_GEN_N, 50);
29 define(SESS_LEN, 13);
30 define(STREAM_TIMEOUT, 20);
31 define(EXPIRE_TIME, 180);
32 define(GARBAGE_TIMEOUT, 10);
33 define(NICKSERV, "<i>SERVER</i>");
34 define(BRISK_DEBUG, FALSE);
35 // define(DEBUGGING, "local");
36
37 function xcape($s)
38 {
39   return (str_replace('\\', '\\\\', str_replace('@', '&#64;', str_replace('|', '&brvbar;', htmlentities($s,ENT_COMPAT,"UTF-8")))));
40 }
41
42
43 class Card {
44   var $value; /* 0 - 39 card value */
45   var $stat;  /* 'bunch', 'hand', 'table', 'take' */
46   var $owner; /* (table position 0-4) */
47   // var $pos;   /* Pos in hand. */
48   var $x;     /* When played the X position on the table of the owner. */
49   var $y;     /* When played the Y position on the table of the owner. */
50
51   function Card($value, $stat, $owner)
52   {
53     $this->value = $value;
54     $this->stat  = $stat;
55     $this->owner = $owner;
56   }
57
58   function assign($stat,$owner)
59   {
60     $this->stat  = $stat;
61     $this->owner = $owner;
62   }
63
64   function setpos($pos)
65   {
66     $this->pos   = $pos;
67   }
68
69   function play($x,$y)
70   {
71     $this->stat = 'table';
72     $this->x = $x;
73     $this->y = $y;
74   }
75
76   function take($newown)
77   {
78     $this->stat = 'take';
79     $this->owner = $newown;
80   }
81 }
82
83 class Table {
84   var $player;
85   var $player_n;
86   var $card;
87   var $mazzo;
88   var $gstart;
89   var $turn;
90
91   var $asta_pla;
92   var $asta_pla_n;
93   var $asta_card;
94   var $asta_pnt;
95   
96   var $mult;
97   var $points;    // points array
98   var $points_n;  // number of row of points
99   var $total;
100
101   var $asta_win;
102   var $briscola;
103   var $friend;
104   
105   var $old_asta_pnt;
106   var $old_pnt;
107   var $old_win;
108   var $old_friend;
109
110   function Table() 
111   {
112     $this->player    =   array();
113     $this->player_n  =   0;
114     $this->card      =  &$this->bunch_create();
115     $this->asta_pla  =   array(); // TRUE: in auction, FALSE: out of the auction
116     $this->asta_pla_n=  -1;
117     $this->asta_card =  -1;
118     $this->asta_pnt  =  -1;
119     $this->mult      =   1;
120     $this->points    =   array( );
121     $this->points_n  =   0;
122     $this->total     =   array( 0, 0, 0, 0, 0);
123     $this->asta_win  =  -1;
124     $this->briscola  =  -1;
125     $this->friend    =  -1;
126     $this->turn      =   0;
127     $this->old_asta_pnt = -1;
128     $this->old_pnt      = -1;
129     $this->old_win   =  -1;
130     $this->old_friend=  -1;
131
132   }
133
134   function &bunch_create()
135   {
136     $ret = array();
137
138     for ($i = 0 ; $i < 40 ; $i++) {
139       $ret[$i] = new Card($i, 'bunch', 'no_owner');
140     }
141
142     return ($ret);
143   }
144
145   function bunch_make()
146   {
147     $ct = array(0,0,0,0,0);
148     
149     mt_srand(make_seed());
150     
151     for ($i = 39 ; $i >= 0 ; $i--) 
152       $rest[$i] = $i;
153
154     for ($i = 39 ; $i >= 0 ; $i--) {
155       $rn = rand(0, $i);
156
157       if ($rn == 0)
158         log_main("RND ZERO", "");
159       
160       $id = $rest[$rn];
161
162       $owner = $i % 5;
163       $this->card[$id]->assign('hand', $owner);
164
165       $rest[$rn] = $rest[$i];
166       $pubbpos[$rn2] = $pubbpos[$i];
167     }
168   }
169
170   function init()
171   {
172     $this->mazzo    = rand(0,PLAYERS_N-1);
173     $this->points_n = 0;
174     $this->mult     = 1;
175     $this->old_win  =-1;
176     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
177       $this->total[$i] = 0;
178     }
179
180     log_main("table::init","ci siamo");
181   }
182
183   function game_init(&$bri)
184   {
185     log_rd2($sess,"GSTART 4");
186
187     $this->gstart = ($this->mazzo+1) % PLAYERS_N;
188     $this->bunch_make();
189     
190     
191     $this->asta_pla_n = PLAYERS_N;
192     $this->asta_card = -1;
193     $this->asta_pnt  = 60;
194     $this->asta_win  = -1;
195     $this->briscola  = -1;
196     $this->friend    = -1;
197     $this->turn      =  0;
198     
199     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
200       $this->asta_pla[$i] = TRUE;
201       $user_cur = &$bri->user[$this->player[$i]];
202       $user_cur->subst = 'asta';
203       $user_cur->asta_card = -2;
204       $user_cur->asta_pnt  = -1;
205     }
206   }
207
208   function game_next()
209   {
210     $this->mazzo  = ($this->mazzo + 1) % PLAYERS_N;
211   }
212
213   function getPlayer($idx)
214   {
215     return ($this->player[$idx]);
216   }
217
218   function setPlayer($idx, $player)
219   {
220     $this->player[$idx] = $player;
221   }
222 }
223
224 class User {
225   var $name;       // name of the user
226   var $sess;       // session of the user
227   var $lacc;       // last access (for the cleanup)
228   var $stat;       // status (outdoor, room, table, game, ...)
229   var $subst;      // substatus for each status   
230   var $step;       // step of the current status
231   var $trans_step; // step to enable transition between pages (disable == -1)
232   var $comm;       // commands array
233   var $asta_card;  // 
234   var $asta_pnt;   //
235
236   var $table;      // id of the current table (if in table state)
237   var $table_pos;  // idx on the table
238
239   function User($name, $sess, $stat = "", $subst = "", $table = -1) {
240     $this->name  = $name;
241     $this->sess  = $sess;
242     $this->lacc  = time();
243     $this->stat  = $stat;
244     $this->subst  = $subst;
245     $this->step  = 1;
246     $this->trans_step  = -1;
247     $this->comm  = array();
248     $this->asta_card = -2;
249     $this->asta_pnt  = -1;
250
251     $this->table = $table;
252   }
253 }
254
255 class brisco {
256   var $user;
257   var $table;
258   var $comm; // commands for many people
259   var $step; // current step of the comm array
260   var $garbage_timeout;
261
262   function brisco () {
263     $this->user = array();
264
265     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
266       $this->user[$i] = new User("", "");
267     }
268     for ($i = 0 ; $i < TABLES_N ; $i++) 
269       $this->table[$i] = new Table();
270     $this->garbage_timeout = 0;
271   }
272
273   function garbage_manager($force)
274   {
275     
276     /* Garbage collector degli utenti in timeout */
277     $curtime = time();
278     if ($force || $this->garbage_timeout < $curtime) {
279       
280       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
281         $user_cur = &$this->user[$i];
282         if ($user_cur->sess == "")
283           continue;
284         
285         if ($user_cur->lacc < $curtime) { // Auto logout dell'utente
286           log_rd2($user_cur->sess, "AUTO LOGOUT.");
287           /*
288           if ($user_cur->stat == 'table') {
289               log_rd2($user_cur->sess, "AUTO LOGOUT: Yet not implemented in table stat!");
290               continue;
291             }
292           else
293           */
294           if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
295             log_auth($user_cur->sess, "Autologout session.");
296             
297             $user_cur->sess = "";
298             $user_cur->name = "";
299             $user_cur->the_end = FALSE;
300             
301             log_rd2($user_cur->sess, "AUTO LOGOUT.");
302             if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
303               $this->room_wakeup(&$user_cur);
304             else if ($user_cur->subst == 'standup')
305               $this->room_outstandup(&$user_cur);
306             else
307               log_rd2($sess, "LOGOUT FROM WHAT ???");
308           }
309           //    } // if (0 == 1) 
310           
311         }
312       }
313       log_rd2($user_cur->sess, "GARBAGE UPDATED!");
314       
315       $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
316     }
317   }
318
319
320   function room_wakeup(&$user)
321   {
322     $table_idx = $user->table;
323     $table = &$this->table[$table_idx];
324
325     log_main("WAKEUP", "begin function table:".$table_idx);
326
327     $from_table = ($user->stat == "table");
328     if ($from_table) {
329       log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
330
331       for ($i = 0 ; $i < $table->player_n ; $i++) {
332         $user_cur = &$this->user[$table->player[$i]];
333         log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
334
335         if ($user_cur != $user) {
336           $user_cur->stat = "room";
337           $user_cur->subst = "sitdown";
338         }
339         else if ($user->sess != "") {
340           $user_cur->stat = "room";
341           $user_cur->subst = "standup";
342           $user_cur->table = -1;
343         }
344       }
345     }
346     /* aggiorna l'array dei giocatori al tavolo. */
347     for ($i = $user->table_pos ; $i < $table->player_n-1 ; $i++) {
348       $table->player[$i] = $table->player[$i+1];
349       $user_cur = &$this->user[$table->player[$i]];
350       $user_cur->table_pos = $i;
351     }
352     $this->table[$table_idx]->player_n--;
353     
354     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
355       $user_cur = &$this->user[$i];
356       if ($user_cur->sess == '' || $user_cur->stat != 'room')
357         continue;
358       
359       log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table."  tab: ".$user_cur->table." taix: ".$table_idx."  ucur: ".$user_cur."  us: ".$user);
360
361       // function show_room(&$bri, &$user)
362
363       $ret = "gst.st = ".($user_cur->step+1)."; ";
364       if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
365         $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
366         // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
367         log_main("DOCUMENT.index.php", "from table");
368       }
369       else if ($user_cur->stat == "room") {
370         $ret .= table_content($this, $user_cur, $table_idx);
371         $ret .= standup_content($this, $user_cur);
372         
373         $act_content = table_act_content(FALSE, 0, $e, $user->table);
374         $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
375         
376         
377         if ($user_cur == $user) {
378           // set the new status 
379           $ret .=  'subst = "standup"; ';
380           // clean the action buttons in other tables
381           for ($e = 0 ; $e < TABLES_N ; $e++) {
382             if ($this->table[$e]->player_n < PLAYERS_N)
383               $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
384           }
385         }
386         else {
387           $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
388           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
389         }
390       }
391       log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
392       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
393       $user_cur->step++;
394     }
395   }
396   
397
398
399
400   function room_outstandup(&$user)
401   {
402     $this->room_sitdown(&$user, -1);
403   }
404   
405   function table_update(&$user)
406   {
407     log_main("table_update", "pre - USER: ".$user->name);
408
409     $table_idx = $user->table;
410
411     if ($table_idx > -1) 
412       $table = &$this->table[$table_idx];
413     
414     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
415       $ret = "";
416       $user_cur = &$this->user[$i];
417       if ($user_cur->sess == '' || $user_cur->stat != 'room')
418       continue;
419       
420       $ret = "gst.st = ".($user_cur->step+1)."; ";
421       if ($table_idx > -1)
422         $ret .= table_content($this, $user_cur, $table_idx);
423       
424       if ($user_cur == $user) {
425         $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
426       }
427       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
428       $user_cur->step++;
429     }
430
431     log_main("table_update", "post");
432   }
433
434   function room_sitdown(&$user, $table_idx)
435   {
436     log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
437
438     if ($table_idx > -1) 
439       $table = &$this->table[$table_idx];
440     
441     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
442       $ret = "";
443       $user_cur = &$this->user[$i];
444       if ($user_cur->sess == '' || $user_cur->stat != 'room')
445       continue;
446       
447       $ret = "gst.st = ".($user_cur->step+1)."; ";
448       if ($table_idx > -1)
449       $ret .= table_content($this, $user_cur, $table_idx);
450       $ret .= standup_content($this, $user_cur);
451       
452       if ($user_cur == $user) {
453         $ret .=  'subst = "sitdown"; ';
454         // clean the action buttons in other tables
455         for ($e = 0 ; $e < TABLES_N ; $e++) {
456           $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
457           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
458         }
459       }
460       else if ($table_idx > -1) {
461         if ($table->player_n == PLAYERS_N) {
462           $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
463           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
464         }
465       }
466       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
467       $user_cur->step++;
468     }
469   }
470
471   function chatt_send(&$user, $mesg)
472   {
473     if ($user->stat == 'table') {
474       $table = &$this->table[$user->table];
475     }
476     
477     $user_mesg = substr($mesg,6);
478     
479     $dt = date("H:i ",time());
480     if (strncmp($user_mesg, "/nick ", 6) == 0) {
481       log_main($user->sess, "chatt_send BEGIN");
482
483       $name_new = substr(trim(substr($user_mesg, 6)),0,12);
484       $user_mesg = "COMMAND ".$user_mesg;
485       // Search dup name
486       // change
487       // update local graph
488       // update remote graphs
489       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
490         $user_cur = &$this->user[$i];
491         if ($user_cur->sess == '' || $user_cur->stat != 'room')
492           continue;
493         if ($user_cur->name == $name_new) {
494           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
495           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> gi&agrave; in uso.");', $dt.NICKSERV, xcape($name_new));
496           $user->step++;
497           break;
498         }
499       }
500       if ($i == MAX_PLAYERS) {
501         $user->name = $name_new;
502
503       log_main($user->sess, "chatt_send start set");
504         
505
506         for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
507           log_main($user->sess, "chatt_send set loop");
508           
509           $user_cur = &$this->user[$i];
510           if ($user_cur->sess == '')
511             continue;
512           if ($user_cur->stat == 'room') {
513             if ($user->stat == 'room' && $user->subst == 'standup') {
514               standup_update(&$this,&$user);
515             }
516             else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
517                      $user->stat == 'table') {
518               log_main($user->sess, "chatt_send pre table update");
519
520               $this->table_update(&$user);
521
522               log_main($user->sess, "chatt_send post table update");
523             }
524           }
525           else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
526             $table = &$this->table[$user->table];
527             
528             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
529             $user_cur->comm[$user_cur->step % COMM_N] = sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
530                 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
531                 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
532                 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
533                 (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
534                 (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
535             if ($user_cur == $user)
536               $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";', 
537                                                                    xcape($user->name,ENT_COMPAT,"UTF-8"));
538             $user_cur->step++;
539           }
540         }
541       }
542     }
543     else {
544       for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
545         if ($user->stat == 'room') {
546           $user_cur = &$this->user[$i];
547           if ($user_cur->sess == '' || $user_cur->stat != 'room')
548             continue;
549         }
550         else {
551           $user_cur = &$this->user[$table->player[$i]];
552         }
553         
554         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
555         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
556                                                              $dt.xcape($user->name), xcape($user_mesg));
557         $user_cur->step++;
558       }
559     }
560   }
561   
562 } // end class brisco
563
564 function make_seed()
565 {
566   list($usec, $sec) = explode(' ', microtime());
567   return (float) $sec + ((float) $usec * 100000);
568 }
569
570 function log_main($sess, $log) {
571   if (BRISK_DEBUG != TRUE)
572     return;
573
574   $fp = fopen("/tmp/brisk_main.log", 'a');
575   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
576   fclose($fp);
577 }
578
579 function log_rd($sess, $log) {
580   if (BRISK_DEBUG != TRUE)
581     return;
582
583   $fp = fopen("/tmp/brisk_rd.log", 'a');
584   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
585   fclose($fp);
586 }
587
588 function log_rd2($sess, $log) {
589   if (BRISK_DEBUG != TRUE)
590     return;
591
592   $fp = fopen("/tmp/brisk_rd2.log", 'a');
593   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
594   fclose($fp);
595 }
596
597 function log_send($sess, $log) {
598   if (BRISK_DEBUG != TRUE)
599     return;
600
601   $fp = fopen("/tmp/brisk_send.log", 'a');
602   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
603   fclose($fp);
604 }
605
606 function log_auth($sess, $log) {
607   if (BRISK_DEBUG != TRUE)
608     return;
609
610   $fp = fopen("/tmp/brisk_auth.log", 'a');
611   fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
612   fclose($fp);
613 }
614
615 function log_wr($sess, $log) {
616   if (BRISK_DEBUG != TRUE)
617     return;
618
619   $fp = fopen("/tmp/brisk_wr.log", 'a');
620   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
621   fclose($fp);
622 }
623
624 function log_load($sess, $log) {
625   if (BRISK_DEBUG != TRUE)
626     return;
627
628   $fp = fopen("/tmp/brisk_load.log", 'a');
629   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
630   fclose($fp);
631 }
632
633 function init_data()
634 {
635   $brisco = new brisco();
636
637   return $brisco;
638 }
639
640 function lock_data()
641 {
642         //  echo "LOCK: ".FTOK_PATH."/main";
643         //  exit;
644   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
645     echo "FTOK FAILED";
646     exit;
647   }
648   // echo "FTOK ".$tok."<br>";
649   if (($res = sem_get($tok)) == FALSE) {
650     echo "SEM_GET FAILED";
651     exit;
652   }
653   if (sem_acquire($res)) 
654     return ($res);
655   else
656     return (false);
657 }
658
659 function unlock_data($res)
660 {
661   return (sem_release($res));
662 }
663
664
665 function &load_data() 
666 {
667   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
668     echo "FTOK FAILED";
669     exit;
670   }
671
672   if ($shm = shm_attach($tok,100000 * TABLES_N)) {
673     if(($bri = @shm_get_var($shm, $tok)) == false) {
674       log_main("XXX", "INIT MAIN DATA");
675
676       $bri = init_data();
677       shm_put_var($shm, $tok, $bri);
678     }
679     
680     shm_detach($shm);
681
682     return ($bri);
683   }
684
685   return (NULL);
686 }
687
688
689 function save_data(&$bri) 
690 {
691   $ret =   FALSE;
692   $shm =   FALSE;
693   $isacq = FALSE;
694
695   // var_dump($bri);
696
697   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) 
698     return (FALSE);
699
700   do {
701     $isacq = TRUE;
702     
703     if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
704       break;
705     
706     if (shm_put_var($shm, $tok, $bri) == FALSE)
707       break;
708     // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
709     $ret = TRUE;
710   } while (0);
711   
712   if ($shm)
713     shm_detach($shm);
714      
715   return ($ret);
716 }
717
718 function &get_user(&$bri, $sess, &$idx)
719 {
720   GLOBAL $PHP_SELF;
721
722   if (strlen($sess) == SESS_LEN) {
723     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
724       if (strcmp($sess, $bri->user[$i]->sess) == 0) {
725         // find it
726         $idx = $i;
727         return ($bri->user[$i]);
728       }
729     }
730     log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
731     // for ($i = 0 ; $i < MAX_PLAYERS ; $i++) 
732     // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$bri->user[$i]->sess));
733   }
734   else {
735     log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
736   }
737   return (FALSE);
738 }
739
740 /*
741  * function &add_user(&$bri, &$sess, &$idx, $name)
742  *
743  * RETURN VALUE:
744  *   if ($idx != -1 && ret == FALSE)  =>  duplicated nick
745  *   if ($idx == -1 && ret == FALSE)  =>  no space left
746  *   if (ret == TRUE)                 =>  SUCCESS
747  */
748 function &add_user(&$bri, &$sess, &$idx, $name)
749 {
750   $idx = -1;
751
752   for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
753     /* free user ? */
754     if (strcmp($sess, $bri->user[$i]->sess) == 0) {
755       if ($idx == -1)
756         $idx = $i;
757     }
758     if (strcmp($bri->user[$i]->name, $name) == 0) {
759       $idx = $i;
760       break;
761     }
762   }
763   if ($idx != -1 && $i == MAX_PLAYERS) {
764     /* SUCCESS */
765     $bri->user[$idx]->sess = uniqid("");
766     $sess = $bri->user[$idx]->sess;
767     $bri->user[$idx]->name = $name;
768     $bri->user[$idx]->stat = "room";
769     $bri->user[$idx]->subst = "standup";
770
771     log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
772       
773     return ($bri->user[$idx]);
774   }
775   else {
776     return (FALSE);
777   }
778 }
779
780 function table_act_content($isstanding, $sitted, $table, $cur_table)
781 {
782   $ret = "";
783
784   if ($isstanding) {
785     if ($sitted < PLAYERS_N) {
786       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\"  value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
787     }
788   }
789   else {
790     if ($table == $cur_table)
791       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\"  value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
792     else
793       $ret = "";
794   }
795   return ($ret);
796 }
797
798 function table_content($bri, $user, $table_idx)
799 {
800   $content = "";
801
802   // TODO
803   //
804   //   Si possono usare i dati nella classe table
805   //
806
807   $sess = $user->sess;
808   $table = &$bri->table[$table_idx];
809
810   if ($user->stat != 'room')
811     return;
812
813   for ($i = 0 ; $i < $table->player_n ; $i++) {
814     $user_cur = &$bri->user[$table->player[$i]];
815
816     if ($user_cur == $user) 
817         { $hilion = "<b>"; $hilioff = "</b>"; }
818       else
819         { $hilion = ""; $hilioff = ""; }
820
821     log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
822     
823     $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
824   }
825   /*
826   for ( ; $i < PLAYERS_N ; $i++)
827     $content .= "<br>";
828   */
829
830   $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
831   
832   return ($ret);
833 }
834
835 function standup_content(&$bri, $user)
836 {
837   $ret = "";
838   $content = "";
839
840   if ($user->stat != 'room')
841     return;
842
843   $content .= '<table class=\\"table_standup\\">';
844   for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
845     if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
846       continue;
847
848
849     if ($bri->user[$e]->subst == "standup") {
850       if (($ct % 4) == 0) {
851         $content .= '<tr>';
852       }
853       if ($bri->user[$e] == $user) 
854         { $hilion = "<b>"; $hilioff = "</b>"; }
855       else
856         { $hilion = ""; $hilioff = ""; }
857
858       $content .= sprintf('<td style=\\"text-align: center\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
859       if (($ct % 4) == 3) {
860         $content .= '</tr>';
861       }
862       $ct++;
863     }
864   }
865   $content .= '</table>';
866         
867   $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
868   $ret .= sprintf('$("standup").innerHTML = "%s";  $("esco").innerHTML = "%s";', 
869                   $content, $content2);
870
871   return ($ret);
872 }
873
874 function standup_update(&$bri, &$user)
875 {
876   for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
877     $user_cur = &$bri->user[$i];
878     if ($user_cur->sess == '')
879       continue;
880
881     log_main("STANDUP START", $user_cur->stat);
882
883     if ($user_cur->stat == 'room') {
884       $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($bri, $user_cur);
885       if ($user_cur == $user)
886         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
887
888       log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
889
890       $user_cur->step++;
891     }
892   }
893 }
894
895
896
897
898
899 function show_notify($text, $tout, $butt)
900 {
901   log_main("SHOW_NOTIFY", $text);
902   return sprintf('var noti = new notify(gst,$("bg"),"%s",%d,"%s");', $text, $tout,$butt);
903 }
904
905
906
907
908 function briscola_show($bri, $table, $user)
909 {
910   $ptnadd = "";
911   $ret = "";
912
913   if ($table->asta_card == 9) 
914     $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
915   
916   /* text of caller cell */
917   if ($user->table_pos == $table->asta_win) 
918     $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
919   else 
920     $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";', 
921                     xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
922
923   $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
924                   $table->asta_win);
925   $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
926   $ret .= sprintf('$("caller").style.visibility = "visible";');
927   $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
928   $ret .= sprintf('$("asta").style.visibility = "hidden";');
929   $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
930   
931   return ($ret);
932 }
933
934
935 function game_result($asta_pnt, $pnt)
936 {
937   if ($asta_pnt == 61) {
938     if ($pnt > 60)
939       return (1);
940     else if ($pnt == 60)
941       return (0);
942     else
943       return (-1);
944   }
945   else {
946     if ($pnt >= $asta_pnt)
947       return (1);
948     else
949       return (-1);
950   }
951 }
952
953 function multoval($mult)
954 {
955   if ($mult == 2)
956     return ("doppio");
957   else if ($mult == 4)
958     return ("quadruplo");
959   else
960     return (sprintf("%d-plo", $mult));
961 }
962
963 function show_table_info(&$bri, &$table, $table_pos)
964 {
965   $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
966   $noty = sprintf('<table class=\"points\"><tr><th></th>');
967   
968   // Names.
969   for ($i = 0 ; $i < PLAYERS_N ; $i++) 
970     $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
971   $noty .= sprintf("</tr>");
972
973   // Points.
974   log_main("show_table_info", "pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
975
976   for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
977     $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
978     for ($e = 0 ; $e < PLAYERS_N ; $e++) 
979       $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
980     $noty .= "</tr>";
981   }
982
983   // Total points.
984   $noty .= '<tr><th class=\"td_points\">Tot.</th>';
985   for ($e = 0 ; $e < PLAYERS_N ; $e++) 
986     $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
987   $noty .= "</tr></table>";
988
989   if ($table->old_win != -1) {
990     $win = $table->player[$table->old_win];
991     $fri = $table->player[$table->old_friend];
992
993     $wol = game_result($table->old_asta_pnt, $table->old_pnt);
994
995     if ($win != $fri) {
996       $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", 
997                        xcape($bri->user[$win]->name),
998                        xcape($bri->user[$fri]->name));
999       if ($table->old_pnt == 120) {
1000         $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1001       }
1002       else {
1003         $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1004                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1005                           'pi&ugrave; di 60'), $table->old_pnt,
1006                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1007       }
1008     }
1009     else {
1010       $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", 
1011                        xcape($bri->user[$win]->name));
1012       if ($table->old_pnt == 120) {
1013         $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1014       }
1015       else {
1016         $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1017                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1018                           'pi&ugrave; di 60'), $table->old_pnt,
1019                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1020       }
1021     }
1022   }
1023   if ($table->mazzo == $table_pos) 
1024     $noty .= "Fai <b>tu</b> il mazzo.";
1025   else {
1026     $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1027     $noty .= "Il mazzo a <b>$unam</b>.";
1028   }
1029
1030   if ($table->mult > 1) {
1031     $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1032   }
1033   $noty .= "<hr><br>";
1034
1035   $ret .= show_notify($noty, 3000, "torna alla partita");
1036   
1037   return ($ret);
1038 }
1039
1040
1041
1042 function show_room(&$bri, &$user)
1043 {
1044   $ret .= sprintf('gst.st = %d;',  $user->step);
1045   $ret .= sprintf('stat = "%s";',  $user->stat);
1046   $ret .= sprintf('subst = "%s";', $user->subst);
1047   $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1048   for ($i = 0 ; $i < TABLES_N ; $i++) {
1049     $ret .= table_content($bri, $user, $i);
1050     $act_content = table_act_content(($user->subst == 'standup'), 
1051                                      $bri->table[$i]->player_n, $i, $user->table);
1052     $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1053   }
1054   $ret .= standup_content($bri, $user);
1055   
1056   return ($ret);
1057 }
1058
1059
1060
1061 /* show table 
1062 is_transition (is from room to table ?)
1063 is_again      (is another game)
1064  */
1065 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1066 {
1067   $table_idx = $user->table;
1068   $table = &$bri->table[$table_idx];
1069
1070   /****************
1071    FOR RELOAD:
1072      DONE - user names
1073      handed cards
1074      tabled cards
1075      taked  cards
1076      remark on/off
1077      cards dnd (and gameable card if its turn)
1078      who call and what
1079   ****************/
1080  
1081
1082   $ret = "table_init();";
1083   
1084   if (!$is_again) {
1085     /* GENERAL STATUS */
1086     $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1087                      $sendstep, $user->stat, $user->subst, $user->table_pos);
1088     /* BACKGROUND */
1089     $ret .= "background_set();";
1090     
1091     /* USERS INFO */
1092     $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1093     $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1094                     xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1095                     xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1096                     xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1097                     (PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1098                     (PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1099   }
1100   /* NOTIFY FOR THE CARD MAKER */
1101   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1102     $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1103   }
1104
1105   /* CARDS */
1106   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1107     $ret .= "|";
1108     
1109     for ($i = 0 ; $i < 8 ; $i++) {
1110       for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1111         $ct = 0;
1112         for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1113           if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1114             $ct++;
1115             if ($ct == $i+1)
1116               break;
1117           }
1118         }
1119         log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1120         
1121         $ret .= sprintf( ' card_send(%d,%d,%d,%f,%d);|', ($table->gstart + $e) % PLAYERS_N, 
1122                          $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1123                               $table->card[$o]->value : -1), 
1124                          ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1125       }
1126     }    
1127   }
1128   else {
1129     $taked  = array(0,0,0,0,0);
1130     $inhand = array(0,0,0,0,0);
1131     $ontabl  = array(-1,-1,-1,-1,-1);
1132     $cards  = array();
1133
1134     for ($i = 0 ; $i < 40 ; $i++) {
1135       if ($table->card[$i]->stat == 'hand') {
1136         if ($table->card[$i]->owner == $user->table_pos) {
1137           $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1138         }
1139         $inhand[$table->card[$i]->owner]++;
1140       }
1141       else if ($table->card[$i]->stat == 'take') {
1142         log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1143         $taked[$table->card[$i]->owner]++;
1144       }
1145       else if ($table->card[$i]->stat == 'table') {
1146         $ontabl[$table->card[$i]->owner] = $i;
1147       }
1148     }
1149     $logg = "\n";
1150     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1151       $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1152     }
1153     log_main("Stat table:", $logg);
1154
1155     /* Set ours cards. */
1156     $oursarg = "";
1157     for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++) 
1158       $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1159     for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++) 
1160       $oursarg .= ($i == 0 ? "" : ", ")."-1";
1161     $ret .= sprintf('card_setours(%s);', $oursarg);
1162
1163     /* Dispose all cards */
1164     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1165       /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1166       $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1167                       $inhand[$i], $taked[$i]);
1168
1169       if ($ontabl[$i] != -1) {
1170         $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i], 
1171                         $table->card[$ontabl[$i]]->value, 
1172                         $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1173       }
1174     }
1175   }
1176
1177   /* Show auction */
1178   if ($user->subst == 'asta') {
1179
1180     /* show users auction status */
1181     $showst = "";
1182     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1183       $user_cur = &$bri->user[$table->player[$i]];
1184       $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
1185                          ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1186     }
1187     if (PLAYERS_N == 3)
1188         $showst .= ",-2,-2";
1189     $ret .= sprintf('show_astat(%s);', $showst);
1190
1191     if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1192       /* show card chooser */
1193       $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1194                       $table->asta_card);
1195     }
1196     else {
1197       /* show auction */
1198       if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1199           $table->asta_win == -1) 
1200         $ret .= sprintf('dispose_asta(%d,%d);', 
1201                         $table->asta_card + 1, $table->asta_pnt+1);
1202       else
1203         $ret .= sprintf('dispose_asta(%d,%d);',
1204                         $table->asta_card + 1, -($table->asta_pnt+1));
1205     }
1206
1207     /* Remark */
1208     if ($table->asta_win == -1) { // auction case
1209       if ($user->table_pos == ($table->gstart % PLAYERS_N)) 
1210         $ret .= "remark_on();";
1211       else
1212         $ret .= "remark_off();";
1213     }
1214     else { // chooseed case
1215       if ($user->table_pos == $table->asta_win) 
1216         $ret .= "remark_on();";
1217       else
1218         $ret .= "remark_off();";
1219     }
1220   }
1221   else if ($user->subst == 'game') {
1222     /* HIGHLIGHT */
1223     if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos) 
1224       $ret .= "is_my_time = true; remark_on();";
1225     else
1226       $ret .= "remark_off();";
1227     
1228     /* WHO CALL AND WATH */
1229     $ret .= briscola_show($bri, $table, $user);
1230     
1231   }
1232   return ($ret);
1233   }
1234
1235 function calculate_winner(&$table)
1236 {
1237   $briontab = FALSE;
1238   $ontab = array();
1239   $ontid = array();
1240   $cur_win  =  -1;
1241   $cur_val  = 100;
1242   $cur_seed = $table->briscola - ($table->briscola % 10);
1243
1244   for ($i = 0 ; $i < 40 ; $i++) {
1245     if ($table->card[$i]->stat != "table")
1246       continue;
1247
1248     log_wr($sess, sprintf("Card On table: [%d]", $i));
1249
1250     $v = $table->card[$i]->value; 
1251     $ontab[$table->card[$i]->owner] = $v;
1252     $ontid[$table->card[$i]->owner] = $i;
1253     /* se briscola setto il flag */
1254     if (($v - ($v % 10)) == $cur_seed)
1255       $briontab = TRUE;
1256   }
1257
1258   if ($briontab == FALSE) {
1259     $cur_win  = $table->gstart;
1260     $cur_val  = $ontab[$cur_win];
1261     $cur_seed = $cur_val - ($cur_val % 10);
1262   }
1263
1264   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1265     if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1266       if ($ontab[$i] < $cur_val) {
1267         $cur_val = $ontab[$i];
1268         $cur_win = $i;
1269       }
1270     }
1271   }
1272
1273   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1274     $table->card[$ontid[$i]]->owner = $cur_win;
1275     $table->card[$ontid[$i]]->stat =  "take";
1276   }
1277   return ($cur_win);
1278 }
1279
1280 function calculate_points(&$table)
1281 {
1282   $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1283
1284   $pro = 0;
1285
1286   if ($table->asta_pnt == 60)
1287     $table->asta_pnt = 61;
1288
1289   $table->old_win = $table->asta_win;
1290   $table->old_friend = $table->friend;
1291   $table->old_asta_pnt = $table->asta_pnt;
1292
1293   for ($i = 0 ; $i < 40 ; $i++) {
1294     $ctt = $table->card[$i]->value % 10;
1295     $own = $table->card[$i]->owner;
1296     if ($own == $table->asta_win || $own == $table->friend) 
1297       $pro += $all_points[$ctt];
1298   }
1299
1300   log_wr("XXX", sprintf("PRO: [%d]", $pro));
1301
1302   
1303   if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1304     $table->points[$table->points_n % MAX_POINTS] = array();
1305     for ($i = 0 ; $i < PLAYERS_N ; $i++) 
1306       $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1307     $table->points_n++;
1308     $table->old_pnt = $pro;
1309     $table->mult *= 2;
1310
1311     return;
1312   }
1313
1314   if ($pro >= $table->asta_pnt) 
1315     $sig = 1;
1316   else
1317     $sig = -1;
1318
1319   $table->points[$table->points_n % MAX_POINTS] = array();
1320   for ($i = 0 ; $i < 5 ; $i++) {
1321     if ($i == $table->asta_win) 
1322       $pt = ($i == $table->friend ? 4 : 2);
1323     else if ($i == $table->friend) 
1324       $pt = 1;
1325     else
1326       $pt = -1;
1327
1328     log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1329
1330     $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1331
1332     log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1333     
1334     $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1335     $table->total[$i] += $pt;
1336   }
1337   $table->points_n++;
1338   $table->old_pnt = $pro;
1339   $table->mult = 1;
1340 }
1341
1342 ?>