aggiunta xcape
[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, 4);
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\\" 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\\" 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   for ( ; $i < PLAYERS_N ; $i++)
826     $content .= "<br>";
827
828   $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
829   
830   return ($ret);
831 }
832
833 function standup_content(&$bri, $user)
834 {
835   $ret = "";
836   $content = "";
837
838   if ($user->stat != 'room')
839     return;
840
841   $content .= '<table width=\\"100%\\">';
842   for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
843     if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
844       continue;
845
846
847     if ($bri->user[$e]->subst == "standup") {
848       if (($ct % 4) == 0) {
849         $content .= '<tr>';
850       }
851       if ($bri->user[$e] == $user) 
852         { $hilion = "<b>"; $hilioff = "</b>"; }
853       else
854         { $hilion = ""; $hilioff = ""; }
855
856       $content .= sprintf('<td style=\\"text-align: center\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
857       if (($ct % 4) == 3) {
858         $content .= '</tr>';
859       }
860       $ct++;
861     }
862   }
863   $content .= '</table>';
864         
865   $content .= '<div class=\\"esco\\"><input name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\"></div>';
866
867   $ret .= sprintf('$("standup").innerHTML = "%s";', $content);
868
869   return ($ret);
870 }
871
872 function standup_update(&$bri, &$user)
873 {
874   for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
875     $user_cur = &$bri->user[$i];
876     if ($user_cur->sess == '')
877       continue;
878
879     log_main("STANDUP START", $user_cur->stat);
880
881     if ($user_cur->stat == 'room') {
882       $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($bri, $user_cur);
883       if ($user_cur == $user)
884         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
885
886       log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
887
888       $user_cur->step++;
889     }
890   }
891 }
892
893
894
895
896
897 function show_notify($text, $tout, $butt)
898 {
899   log_main("SHOW_NOTIFY", $text);
900   return sprintf('var noti = new notify(gst,$("bg"),"%s",%d,"%s");', $text, $tout,$butt);
901 }
902
903
904
905
906 function briscola_show($bri, $table, $user)
907 {
908   $ptnadd = "";
909   $ret = "";
910
911   if ($table->asta_card == 9) 
912     $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
913   
914   /* text of caller cell */
915   if ($user->table_pos == $table->asta_win) 
916     $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
917   else 
918     $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";', 
919                     xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
920
921   $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
922                   $table->asta_win);
923   $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
924   $ret .= sprintf('$("caller").style.visibility = "visible";');
925   $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
926   $ret .= sprintf('$("asta").style.visibility = "hidden";');
927   $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
928   
929   return ($ret);
930 }
931
932
933 function game_result($asta_pnt, $pnt)
934 {
935   if ($asta_pnt == 61) {
936     if ($pnt > 60)
937       return (1);
938     else if ($pnt == 60)
939       return (0);
940     else
941       return (-1);
942   }
943   else {
944     if ($pnt >= $asta_pnt)
945       return (1);
946     else
947       return (-1);
948   }
949 }
950
951 function multoval($mult)
952 {
953   if ($mult == 2)
954     return ("doppio");
955   else if ($mult == 4)
956     return ("quadruplo");
957   else
958     return (sprintf("%d-plo", $mult));
959 }
960
961 function show_table_info(&$bri, &$table, $table_pos)
962 {
963   $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
964   $noty = sprintf('<table class=\"points\"><tr><th></th>');
965   
966   // Names.
967   for ($i = 0 ; $i < PLAYERS_N ; $i++) 
968     $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
969   $noty .= sprintf("</tr>");
970
971   // Points.
972   log_main("show_table_info", "pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
973
974   for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
975     $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
976     for ($e = 0 ; $e < PLAYERS_N ; $e++) 
977       $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
978     $noty .= "</tr>";
979   }
980
981   // Total points.
982   $noty .= '<tr><th class=\"td_points\">Tot.</th>';
983   for ($e = 0 ; $e < PLAYERS_N ; $e++) 
984     $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
985   $noty .= "</tr></table>";
986
987   if ($table->old_win != -1) {
988     $win = $table->player[$table->old_win];
989     $fri = $table->player[$table->old_friend];
990
991     $wol = game_result($table->old_asta_pnt, $table->old_pnt);
992
993     if ($win != $fri) {
994       $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", 
995                        xcape($bri->user[$win]->name),
996                        xcape($bri->user[$fri]->name));
997       if ($table->old_pnt == 120) {
998         $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
999       }
1000       else {
1001         $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1002                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1003                           'pi&ugrave; di 60'), $table->old_pnt,
1004                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1005       }
1006     }
1007     else {
1008       $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", 
1009                        xcape($bri->user[$win]->name));
1010       if ($table->old_pnt == 120) {
1011         $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1012       }
1013       else {
1014         $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1015                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1016                           'pi&ugrave; di 60'), $table->old_pnt,
1017                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1018       }
1019     }
1020   }
1021   if ($table->mazzo == $table_pos) 
1022     $noty .= "Fai <b>tu</b> il mazzo.";
1023   else {
1024     $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1025     $noty .= "Il mazzo a <b>$unam</b>.";
1026   }
1027
1028   if ($table->mult > 1) {
1029     $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1030   }
1031   $noty .= "<hr><br>";
1032
1033   $ret .= show_notify($noty, 3000, "torna alla partita");
1034   
1035   return ($ret);
1036 }
1037
1038
1039
1040 function show_room(&$bri, &$user)
1041 {
1042   $ret .= sprintf('gst.st = %d;',  $user->step);
1043   $ret .= sprintf('stat = "%s";',  $user->stat);
1044   $ret .= sprintf('subst = "%s";', $user->subst);
1045   $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1046   for ($i = 0 ; $i < TABLES_N ; $i++) {
1047     $ret .= table_content($bri, $user, $i);
1048     $act_content = table_act_content(($user->subst == 'standup'), 
1049                                      $bri->table[$i]->player_n, $i, $user->table);
1050     $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1051   }
1052   $ret .= standup_content($bri, $user);
1053   
1054   return ($ret);
1055 }
1056
1057
1058
1059 /* show table 
1060 is_transition (is from room to table ?)
1061 is_again      (is another game)
1062  */
1063 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1064 {
1065   $table_idx = $user->table;
1066   $table = &$bri->table[$table_idx];
1067
1068   /****************
1069    FOR RELOAD:
1070      DONE - user names
1071      handed cards
1072      tabled cards
1073      taked  cards
1074      remark on/off
1075      cards dnd (and gameable card if its turn)
1076      who call and what
1077   ****************/
1078  
1079
1080   $ret = "table_init();";
1081   
1082   if (!$is_again) {
1083     /* GENERAL STATUS */
1084     $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1085                      $sendstep, $user->stat, $user->subst, $user->table_pos);
1086     /* BACKGROUND */
1087     $ret .= "background_set();";
1088     
1089     /* USERS INFO */
1090     $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1091     $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1092                     xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1093                     xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1094                     xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1095                     (PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1096                     (PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1097   }
1098   /* NOTIFY FOR THE CARD MAKER */
1099   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1100     $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1101   }
1102
1103   /* CARDS */
1104   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1105     $ret .= "|";
1106     
1107     for ($i = 0 ; $i < 8 ; $i++) {
1108       for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1109         $ct = 0;
1110         for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1111           if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1112             $ct++;
1113             if ($ct == $i+1)
1114               break;
1115           }
1116         }
1117         log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1118         
1119         $ret .= sprintf( ' card_send(%d,%d,%d,%f,%d);|', ($table->gstart + $e) % PLAYERS_N, 
1120                          $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1121                               $table->card[$o]->value : -1), 
1122                          ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1123       }
1124     }    
1125   }
1126   else {
1127     $taked  = array(0,0,0,0,0);
1128     $inhand = array(0,0,0,0,0);
1129     $ontabl  = array(-1,-1,-1,-1,-1);
1130     $cards  = array();
1131
1132     for ($i = 0 ; $i < 40 ; $i++) {
1133       if ($table->card[$i]->stat == 'hand') {
1134         if ($table->card[$i]->owner == $user->table_pos) {
1135           $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1136         }
1137         $inhand[$table->card[$i]->owner]++;
1138       }
1139       else if ($table->card[$i]->stat == 'take') {
1140         log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1141         $taked[$table->card[$i]->owner]++;
1142       }
1143       else if ($table->card[$i]->stat == 'table') {
1144         $ontabl[$table->card[$i]->owner] = $i;
1145       }
1146     }
1147     $logg = "\n";
1148     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1149       $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1150     }
1151     log_main("Stat table:", $logg);
1152
1153     /* Set ours cards. */
1154     $oursarg = "";
1155     for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++) 
1156       $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1157     for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++) 
1158       $oursarg .= ($i == 0 ? "" : ", ")."-1";
1159     $ret .= sprintf('card_setours(%s);', $oursarg);
1160
1161     /* Dispose all cards */
1162     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1163       /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1164       $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1165                       $inhand[$i], $taked[$i]);
1166
1167       if ($ontabl[$i] != -1) {
1168         $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i], 
1169                         $table->card[$ontabl[$i]]->value, 
1170                         $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1171       }
1172     }
1173   }
1174
1175   /* Show auction */
1176   if ($user->subst == 'asta') {
1177
1178     /* show users auction status */
1179     $showst = "";
1180     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1181       $user_cur = &$bri->user[$table->player[$i]];
1182       $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
1183                          ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1184     }
1185     if (PLAYERS_N == 3)
1186         $showst .= ",-2,-2";
1187     $ret .= sprintf('show_astat(%s);', $showst);
1188
1189     if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1190       /* show card chooser */
1191       $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1192                       $table->asta_card);
1193     }
1194     else {
1195       /* show auction */
1196       if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1197           $table->asta_win == -1) 
1198         $ret .= sprintf('dispose_asta(%d,%d);', 
1199                         $table->asta_card + 1, $table->asta_pnt+1);
1200       else
1201         $ret .= sprintf('dispose_asta(%d,%d);',
1202                         $table->asta_card + 1, -($table->asta_pnt+1));
1203     }
1204
1205     /* Remark */
1206     if ($table->asta_win == -1) { // auction case
1207       if ($user->table_pos == ($table->gstart % PLAYERS_N)) 
1208         $ret .= "remark_on();";
1209       else
1210         $ret .= "remark_off();";
1211     }
1212     else { // chooseed case
1213       if ($user->table_pos == $table->asta_win) 
1214         $ret .= "remark_on();";
1215       else
1216         $ret .= "remark_off();";
1217     }
1218   }
1219   else if ($user->subst == 'game') {
1220     /* HIGHLIGHT */
1221     if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos) 
1222       $ret .= "is_my_time = true; remark_on();";
1223     else
1224       $ret .= "remark_off();";
1225     
1226     /* WHO CALL AND WATH */
1227     $ret .= briscola_show($bri, $table, $user);
1228     
1229   }
1230   return ($ret);
1231   }
1232
1233 function calculate_winner(&$table)
1234 {
1235   $briontab = FALSE;
1236   $ontab = array();
1237   $ontid = array();
1238   $cur_win  =  -1;
1239   $cur_val  = 100;
1240   $cur_seed = $table->briscola - ($table->briscola % 10);
1241
1242   for ($i = 0 ; $i < 40 ; $i++) {
1243     if ($table->card[$i]->stat != "table")
1244       continue;
1245
1246     log_wr($sess, sprintf("Card On table: [%d]", $i));
1247
1248     $v = $table->card[$i]->value; 
1249     $ontab[$table->card[$i]->owner] = $v;
1250     $ontid[$table->card[$i]->owner] = $i;
1251     /* se briscola setto il flag */
1252     if (($v - ($v % 10)) == $cur_seed)
1253       $briontab = TRUE;
1254   }
1255
1256   if ($briontab == FALSE) {
1257     $cur_win  = $table->gstart;
1258     $cur_val  = $ontab[$cur_win];
1259     $cur_seed = $cur_val - ($cur_val % 10);
1260   }
1261
1262   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1263     if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1264       if ($ontab[$i] < $cur_val) {
1265         $cur_val = $ontab[$i];
1266         $cur_win = $i;
1267       }
1268     }
1269   }
1270
1271   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1272     $table->card[$ontid[$i]]->owner = $cur_win;
1273     $table->card[$ontid[$i]]->stat =  "take";
1274   }
1275   return ($cur_win);
1276 }
1277
1278 function calculate_points(&$table)
1279 {
1280   $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1281
1282   $pro = 0;
1283
1284   if ($table->asta_pnt == 60)
1285     $table->asta_pnt = 61;
1286
1287   $table->old_win = $table->asta_win;
1288   $table->old_friend = $table->friend;
1289   $table->old_asta_pnt = $table->asta_pnt;
1290
1291   for ($i = 0 ; $i < 40 ; $i++) {
1292     $ctt = $table->card[$i]->value % 10;
1293     $own = $table->card[$i]->owner;
1294     if ($own == $table->asta_win || $own == $table->friend) 
1295       $pro += $all_points[$ctt];
1296   }
1297
1298   log_wr("XXX", sprintf("PRO: [%d]", $pro));
1299
1300   
1301   if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1302     $table->points[$table->points_n % MAX_POINTS] = array();
1303     for ($i = 0 ; $i < PLAYERS_N ; $i++) 
1304       $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1305     $table->points_n++;
1306     $table->old_pnt = $pro;
1307     $table->mult *= 2;
1308
1309     return;
1310   }
1311
1312   if ($pro >= $table->asta_pnt) 
1313     $sig = 1;
1314   else
1315     $sig = -1;
1316
1317   $table->points[$table->points_n % MAX_POINTS] = array();
1318   for ($i = 0 ; $i < 5 ; $i++) {
1319     if ($i == $table->asta_win) 
1320       $pt = ($i == $table->friend ? 4 : 2);
1321     else if ($i == $table->friend) 
1322       $pt = 1;
1323     else
1324       $pt = -1;
1325
1326     log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1327
1328     $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1329
1330     log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1331     
1332     $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1333     $table->total[$i] += $pt;
1334   }
1335   $table->points_n++;
1336   $table->old_pnt = $pro;
1337   $table->mult = 1;
1338 }
1339
1340 ?>