spostate add_user e get_user nella classe brisk, modificata la gestione del cookie...
[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   function &get_user($sess, &$idx)
563   {
564     GLOBAL $PHP_SELF;
565     
566     if (strlen($sess) == SESS_LEN) {
567       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
568         if (strcmp($sess, $this->user[$i]->sess) == 0) {
569           // find it
570           $idx = $i;
571           return ($this->user[$i]);
572         }
573       }
574       log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
575       // for ($i = 0 ; $i < MAX_PLAYERS ; $i++) 
576       // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
577     }
578     else {
579       log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
580     }
581     return (FALSE);
582   }
583
584   /*
585    * function &add_user(&$bri, &$sess, &$idx, $name)
586    *
587    * RETURN VALUE:
588    *   if ($idx != -1 && ret == FALSE)  =>  duplicated nick
589    *   if ($idx == -1 && ret == FALSE)  =>  no space left
590    *   if (ret == TRUE)                 =>  SUCCESS
591    */
592   function &add_user(&$sess, &$idx, $name)
593   {
594     $idx = -1;
595     $idfree = -1;
596     
597     log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
598     if (validate_sess($sess) == FALSE) 
599       $sess = "";
600
601     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
602       /* free user ? */
603       if (strcmp($sess, $this->user[$i]->sess) == 0) {
604         if ($idx == -1)
605           $idx = $i;
606       }
607       if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
608         $idfree = $i;
609       }
610       if (strcmp($this->user[$i]->name, $name) == 0) {
611         $idx = $i;
612         break;
613       }
614     }
615     if ($idx == -1)
616       $idx = $idfree;
617
618     log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name));
619
620     if ($idx != -1 && $i == MAX_PLAYERS) {
621       /* SUCCESS */
622       if ($sess == "") {
623         $this->user[$idx]->sess = uniqid("");
624         $sess = $this->user[$idx]->sess;
625         
626       }
627       else {
628         $this->user[$idx]->sess = $sess;
629       }
630       $this->user[$idx]->name = $name;
631       $this->user[$idx]->stat = "room";
632       $this->user[$idx]->subst = "standup";
633       
634       log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name));
635       
636       return ($this->user[$idx]);
637     }
638
639     return (FALSE);
640   }
641   
642
643   
644 } // end class brisco
645
646 function make_seed()
647 {
648   list($usec, $sec) = explode(' ', microtime());
649   return (float) $sec + ((float) $usec * 100000);
650 }
651
652 function log_main($sess, $log) {
653   if (BRISK_DEBUG != TRUE)
654     return;
655
656   $fp = fopen("/tmp/brisk_main.log", 'a');
657   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
658   fclose($fp);
659 }
660
661 function log_rd($sess, $log) {
662   if (BRISK_DEBUG != TRUE)
663     return;
664
665   $fp = fopen("/tmp/brisk_rd.log", 'a');
666   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
667   fclose($fp);
668 }
669
670 function log_rd2($sess, $log) {
671   if (BRISK_DEBUG != TRUE)
672     return;
673
674   $fp = fopen("/tmp/brisk_rd2.log", 'a');
675   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
676   fclose($fp);
677 }
678
679 function log_send($sess, $log) {
680   if (BRISK_DEBUG != TRUE)
681     return;
682
683   $fp = fopen("/tmp/brisk_send.log", 'a');
684   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
685   fclose($fp);
686 }
687
688 function log_auth($sess, $log) {
689   if (BRISK_DEBUG != TRUE)
690     return;
691
692   $fp = fopen("/tmp/brisk_auth.log", 'a');
693   fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
694   fclose($fp);
695 }
696
697 function log_wr($sess, $log) {
698   if (BRISK_DEBUG != TRUE)
699     return;
700
701   $fp = fopen("/tmp/brisk_wr.log", 'a');
702   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
703   fclose($fp);
704 }
705
706 function log_load($sess, $log) {
707   if (BRISK_DEBUG != TRUE)
708     return;
709
710   $fp = fopen("/tmp/brisk_load.log", 'a');
711   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
712   fclose($fp);
713 }
714
715 function init_data()
716 {
717   $brisco = new brisco();
718
719   return $brisco;
720 }
721
722 function lock_data()
723 {
724         //  echo "LOCK: ".FTOK_PATH."/main";
725         //  exit;
726   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
727     echo "FTOK FAILED";
728     exit;
729   }
730   // echo "FTOK ".$tok."<br>";
731   if (($res = sem_get($tok)) == FALSE) {
732     echo "SEM_GET FAILED";
733     exit;
734   }
735   if (sem_acquire($res)) 
736     return ($res);
737   else
738     return (false);
739 }
740
741 function unlock_data($res)
742 {
743   return (sem_release($res));
744 }
745
746
747 function &load_data() 
748 {
749   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
750     echo "FTOK FAILED";
751     exit;
752   }
753
754   if ($shm = shm_attach($tok,100000 * TABLES_N)) {
755     if(($bri = @shm_get_var($shm, $tok)) == false) {
756       log_main("XXX", "INIT MAIN DATA");
757
758       $bri = init_data();
759       shm_put_var($shm, $tok, $bri);
760     }
761     
762     shm_detach($shm);
763
764     return ($bri);
765   }
766
767   return (NULL);
768 }
769
770
771 function save_data(&$bri) 
772 {
773   $ret =   FALSE;
774   $shm =   FALSE;
775   $isacq = FALSE;
776
777   // var_dump($bri);
778
779   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) 
780     return (FALSE);
781
782   do {
783     $isacq = TRUE;
784     
785     if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
786       break;
787     
788     if (shm_put_var($shm, $tok, $bri) == FALSE)
789       break;
790     // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
791     $ret = TRUE;
792   } while (0);
793   
794   if ($shm)
795     shm_detach($shm);
796      
797   return ($ret);
798 }
799
800 function table_act_content($isstanding, $sitted, $table, $cur_table)
801 {
802   $ret = "";
803
804   if ($isstanding) {
805     if ($sitted < PLAYERS_N) {
806       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\"  value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
807     }
808   }
809   else {
810     if ($table == $cur_table)
811       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\"  value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
812     else
813       $ret = "";
814   }
815   return ($ret);
816 }
817
818 function table_content($bri, $user, $table_idx)
819 {
820   $content = "";
821
822   // TODO
823   //
824   //   Si possono usare i dati nella classe table
825   //
826
827   $sess = $user->sess;
828   $table = &$bri->table[$table_idx];
829
830   if ($user->stat != 'room')
831     return;
832
833   for ($i = 0 ; $i < $table->player_n ; $i++) {
834     $user_cur = &$bri->user[$table->player[$i]];
835
836     if ($user_cur == $user) 
837         { $hilion = "<b>"; $hilioff = "</b>"; }
838       else
839         { $hilion = ""; $hilioff = ""; }
840
841     log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
842     
843     $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
844   }
845   /*
846   for ( ; $i < PLAYERS_N ; $i++)
847     $content .= "<br>";
848   */
849
850   $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
851   
852   return ($ret);
853 }
854
855 function standup_content(&$bri, $user)
856 {
857   $ret = "";
858   $content = "";
859
860   if ($user->stat != 'room')
861     return;
862
863   $content .= '<table class=\\"table_standup\\">';
864   for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
865     if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
866       continue;
867
868
869     if ($bri->user[$e]->subst == "standup") {
870       if (($ct % 4) == 0) {
871         $content .= '<tr>';
872       }
873       if ($bri->user[$e] == $user) 
874         { $hilion = "<b>"; $hilioff = "</b>"; }
875       else
876         { $hilion = ""; $hilioff = ""; }
877
878       $content .= sprintf('<td style=\\"text-align: center\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
879       if (($ct % 4) == 3) {
880         $content .= '</tr>';
881       }
882       $ct++;
883     }
884   }
885   $content .= '</table>';
886         
887   $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
888   $ret .= sprintf('$("standup").innerHTML = "%s";  $("esco").innerHTML = "%s";', 
889                   $content, $content2);
890
891   return ($ret);
892 }
893
894 function standup_update(&$bri, &$user)
895 {
896   for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
897     $user_cur = &$bri->user[$i];
898     if ($user_cur->sess == '')
899       continue;
900
901     log_main("STANDUP START", $user_cur->stat);
902
903     if ($user_cur->stat == 'room') {
904       $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($bri, $user_cur);
905       if ($user_cur == $user)
906         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
907
908       log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
909
910       $user_cur->step++;
911     }
912   }
913 }
914
915
916
917
918
919 function show_notify($text, $tout, $butt)
920 {
921   log_main("SHOW_NOTIFY", $text);
922   return sprintf('var noti = new notify(gst,$("bg"),"%s",%d,"%s");', $text, $tout,$butt);
923 }
924
925
926
927
928 function briscola_show($bri, $table, $user)
929 {
930   $ptnadd = "";
931   $ret = "";
932
933   if ($table->asta_card == 9) 
934     $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
935   
936   /* text of caller cell */
937   if ($user->table_pos == $table->asta_win) 
938     $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
939   else 
940     $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";', 
941                     xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
942
943   $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
944                   $table->asta_win);
945   $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
946   $ret .= sprintf('$("caller").style.visibility = "visible";');
947   $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
948   $ret .= sprintf('$("asta").style.visibility = "hidden";');
949   $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
950   
951   return ($ret);
952 }
953
954
955 function game_result($asta_pnt, $pnt)
956 {
957   if ($asta_pnt == 61) {
958     if ($pnt > 60)
959       return (1);
960     else if ($pnt == 60)
961       return (0);
962     else
963       return (-1);
964   }
965   else {
966     if ($pnt >= $asta_pnt)
967       return (1);
968     else
969       return (-1);
970   }
971 }
972
973 function multoval($mult)
974 {
975   if ($mult == 2)
976     return ("doppio");
977   else if ($mult == 4)
978     return ("quadruplo");
979   else
980     return (sprintf("%d-plo", $mult));
981 }
982
983 function show_table_info(&$bri, &$table, $table_pos)
984 {
985   $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
986   $noty = sprintf('<table class=\"points\"><tr><th></th>');
987   
988   // Names.
989   for ($i = 0 ; $i < PLAYERS_N ; $i++) 
990     $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
991   $noty .= sprintf("</tr>");
992
993   // Points.
994   log_main("show_table_info", "pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
995
996   for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
997     $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
998     for ($e = 0 ; $e < PLAYERS_N ; $e++) 
999       $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1000     $noty .= "</tr>";
1001   }
1002
1003   // Total points.
1004   $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1005   for ($e = 0 ; $e < PLAYERS_N ; $e++) 
1006     $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1007   $noty .= "</tr></table>";
1008
1009   if ($table->old_win != -1) {
1010     $win = $table->player[$table->old_win];
1011     $fri = $table->player[$table->old_friend];
1012
1013     $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1014
1015     if ($win != $fri) {
1016       $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", 
1017                        xcape($bri->user[$win]->name),
1018                        xcape($bri->user[$fri]->name));
1019       if ($table->old_pnt == 120) {
1020         $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1021       }
1022       else {
1023         $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1024                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1025                           'pi&ugrave; di 60'), $table->old_pnt,
1026                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1027       }
1028     }
1029     else {
1030       $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", 
1031                        xcape($bri->user[$win]->name));
1032       if ($table->old_pnt == 120) {
1033         $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1034       }
1035       else {
1036         $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1037                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1038                           'pi&ugrave; di 60'), $table->old_pnt,
1039                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1040       }
1041     }
1042   }
1043   if ($table->mazzo == $table_pos) 
1044     $noty .= "Fai <b>tu</b> il mazzo.";
1045   else {
1046     $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1047     $noty .= "Il mazzo a <b>$unam</b>.";
1048   }
1049
1050   if ($table->mult > 1) {
1051     $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1052   }
1053   $noty .= "<hr><br>";
1054
1055   $ret .= show_notify($noty, 3000, "torna alla partita");
1056   
1057   return ($ret);
1058 }
1059
1060
1061
1062 function show_room(&$bri, &$user)
1063 {
1064   $ret .= sprintf('gst.st = %d;',  $user->step);
1065   $ret .= sprintf('stat = "%s";',  $user->stat);
1066   $ret .= sprintf('subst = "%s";', $user->subst);
1067   $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1068   for ($i = 0 ; $i < TABLES_N ; $i++) {
1069     $ret .= table_content($bri, $user, $i);
1070     $act_content = table_act_content(($user->subst == 'standup'), 
1071                                      $bri->table[$i]->player_n, $i, $user->table);
1072     $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1073   }
1074   $ret .= standup_content($bri, $user);
1075   
1076   return ($ret);
1077 }
1078
1079
1080
1081 /* show table 
1082 is_transition (is from room to table ?)
1083 is_again      (is another game)
1084  */
1085 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1086 {
1087   $table_idx = $user->table;
1088   $table = &$bri->table[$table_idx];
1089
1090   /****************
1091    FOR RELOAD:
1092      DONE - user names
1093      handed cards
1094      tabled cards
1095      taked  cards
1096      remark on/off
1097      cards dnd (and gameable card if its turn)
1098      who call and what
1099   ****************/
1100  
1101
1102   $ret = "table_init();";
1103   
1104   if (!$is_again) {
1105     /* GENERAL STATUS */
1106     $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1107                      $sendstep, $user->stat, $user->subst, $user->table_pos);
1108     /* BACKGROUND */
1109     $ret .= "background_set();";
1110     
1111     /* USERS INFO */
1112     $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1113     $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1114                     xcape($bri->user[$table->player[($user->table_pos)%PLAYERS_N]]->name),
1115                     xcape($bri->user[$table->player[($user->table_pos+1)%PLAYERS_N]]->name),
1116                     xcape($bri->user[$table->player[($user->table_pos+2)%PLAYERS_N]]->name),
1117                     (PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($user->table_pos+3)%PLAYERS_N]]->name)),
1118                     (PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($user->table_pos+4)%PLAYERS_N]]->name)));
1119   }
1120   /* NOTIFY FOR THE CARD MAKER */
1121   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1122     $ret .= show_table_info(&$bri, &$table, $user->table_pos);
1123   }
1124
1125   /* CARDS */
1126   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1127     $ret .= "|";
1128     
1129     for ($i = 0 ; $i < 8 ; $i++) {
1130       for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1131         $ct = 0;
1132         for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1133           if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1134             $ct++;
1135             if ($ct == $i+1)
1136               break;
1137           }
1138         }
1139         log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1140         
1141         $ret .= sprintf( ' card_send(%d,%d,%d,%f,%d);|', ($table->gstart + $e) % PLAYERS_N, 
1142                          $i, ((($e + PLAYERS_N - $user->table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1143                               $table->card[$o]->value : -1), 
1144                          ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1145       }
1146     }    
1147   }
1148   else {
1149     $taked  = array(0,0,0,0,0);
1150     $inhand = array(0,0,0,0,0);
1151     $ontabl  = array(-1,-1,-1,-1,-1);
1152     $cards  = array();
1153
1154     for ($i = 0 ; $i < 40 ; $i++) {
1155       if ($table->card[$i]->stat == 'hand') {
1156         if ($table->card[$i]->owner == $user->table_pos) {
1157           $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1158         }
1159         $inhand[$table->card[$i]->owner]++;
1160       }
1161       else if ($table->card[$i]->stat == 'take') {
1162         log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1163         $taked[$table->card[$i]->owner]++;
1164       }
1165       else if ($table->card[$i]->stat == 'table') {
1166         $ontabl[$table->card[$i]->owner] = $i;
1167       }
1168     }
1169     $logg = "\n";
1170     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1171       $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1172     }
1173     log_main("Stat table:", $logg);
1174
1175     /* Set ours cards. */
1176     $oursarg = "";
1177     for ($i = 0 ; $i < $inhand[$user->table_pos] ; $i++) 
1178       $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1179     for ($i = $inhand[$user->table_pos] ; $i < 8 ; $i++) 
1180       $oursarg .= ($i == 0 ? "" : ", ")."-1";
1181     $ret .= sprintf('card_setours(%s);', $oursarg);
1182
1183     /* Dispose all cards */
1184     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1185       /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1186       $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1187                       $inhand[$i], $taked[$i]);
1188
1189       if ($ontabl[$i] != -1) {
1190         $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i], 
1191                         $table->card[$ontabl[$i]]->value, 
1192                         $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1193       }
1194     }
1195   }
1196
1197   /* Show auction */
1198   if ($user->subst == 'asta') {
1199
1200     /* show users auction status */
1201     $showst = "";
1202     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1203       $user_cur = &$bri->user[$table->player[$i]];
1204       $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
1205                          ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1206     }
1207     if (PLAYERS_N == 3)
1208         $showst .= ",-2,-2";
1209     $ret .= sprintf('show_astat(%s);', $showst);
1210
1211     if ($table->asta_win != -1 && $table->asta_win == $user->table_pos) {
1212       /* show card chooser */
1213       $ret .= sprintf('choose_seed(%s); $("asta").style.visibility = "hidden";',
1214                       $table->asta_card);
1215     }
1216     else {
1217       /* show auction */
1218       if ($user->table_pos == ($table->gstart % PLAYERS_N) &&
1219           $table->asta_win == -1) 
1220         $ret .= sprintf('dispose_asta(%d,%d);', 
1221                         $table->asta_card + 1, $table->asta_pnt+1);
1222       else
1223         $ret .= sprintf('dispose_asta(%d,%d);',
1224                         $table->asta_card + 1, -($table->asta_pnt+1));
1225     }
1226
1227     /* Remark */
1228     if ($table->asta_win == -1) { // auction case
1229       if ($user->table_pos == ($table->gstart % PLAYERS_N)) 
1230         $ret .= "remark_on();";
1231       else
1232         $ret .= "remark_off();";
1233     }
1234     else { // chooseed case
1235       if ($user->table_pos == $table->asta_win) 
1236         $ret .= "remark_on();";
1237       else
1238         $ret .= "remark_off();";
1239     }
1240   }
1241   else if ($user->subst == 'game') {
1242     /* HIGHLIGHT */
1243     if (($table->gstart + $table->turn) % PLAYERS_N == $user->table_pos) 
1244       $ret .= "is_my_time = true; remark_on();";
1245     else
1246       $ret .= "remark_off();";
1247     
1248     /* WHO CALL AND WATH */
1249     $ret .= briscola_show($bri, $table, $user);
1250     
1251   }
1252   return ($ret);
1253   }
1254
1255 function calculate_winner(&$table)
1256 {
1257   $briontab = FALSE;
1258   $ontab = array();
1259   $ontid = array();
1260   $cur_win  =  -1;
1261   $cur_val  = 100;
1262   $cur_seed = $table->briscola - ($table->briscola % 10);
1263
1264   for ($i = 0 ; $i < 40 ; $i++) {
1265     if ($table->card[$i]->stat != "table")
1266       continue;
1267
1268     log_wr($sess, sprintf("Card On table: [%d]", $i));
1269
1270     $v = $table->card[$i]->value; 
1271     $ontab[$table->card[$i]->owner] = $v;
1272     $ontid[$table->card[$i]->owner] = $i;
1273     /* se briscola setto il flag */
1274     if (($v - ($v % 10)) == $cur_seed)
1275       $briontab = TRUE;
1276   }
1277
1278   if ($briontab == FALSE) {
1279     $cur_win  = $table->gstart;
1280     $cur_val  = $ontab[$cur_win];
1281     $cur_seed = $cur_val - ($cur_val % 10);
1282   }
1283
1284   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1285     if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1286       if ($ontab[$i] < $cur_val) {
1287         $cur_val = $ontab[$i];
1288         $cur_win = $i;
1289       }
1290     }
1291   }
1292
1293   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1294     $table->card[$ontid[$i]]->owner = $cur_win;
1295     $table->card[$ontid[$i]]->stat =  "take";
1296   }
1297   return ($cur_win);
1298 }
1299
1300 function calculate_points(&$table)
1301 {
1302   $all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
1303
1304   $pro = 0;
1305
1306   if ($table->asta_pnt == 60)
1307     $table->asta_pnt = 61;
1308
1309   $table->old_win = $table->asta_win;
1310   $table->old_friend = $table->friend;
1311   $table->old_asta_pnt = $table->asta_pnt;
1312
1313   for ($i = 0 ; $i < 40 ; $i++) {
1314     $ctt = $table->card[$i]->value % 10;
1315     $own = $table->card[$i]->owner;
1316     if ($own == $table->asta_win || $own == $table->friend) 
1317       $pro += $all_points[$ctt];
1318   }
1319
1320   log_wr("XXX", sprintf("PRO: [%d]", $pro));
1321
1322   
1323   if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1324     $table->points[$table->points_n % MAX_POINTS] = array();
1325     for ($i = 0 ; $i < PLAYERS_N ; $i++) 
1326       $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1327     $table->points_n++;
1328     $table->old_pnt = $pro;
1329     $table->mult *= 2;
1330
1331     return;
1332   }
1333
1334   if ($pro >= $table->asta_pnt) 
1335     $sig = 1;
1336   else
1337     $sig = -1;
1338
1339   $table->points[$table->points_n % MAX_POINTS] = array();
1340   for ($i = 0 ; $i < 5 ; $i++) {
1341     if ($i == $table->asta_win) 
1342       $pt = ($i == $table->friend ? 4 : 2);
1343     else if ($i == $table->friend) 
1344       $pt = 1;
1345     else
1346       $pt = -1;
1347
1348     log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1349
1350     $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1351
1352     log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1353     
1354     $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1355     $table->total[$i] += $pt;
1356   }
1357   $table->points_n++;
1358   $table->old_pnt = $pro;
1359   $table->mult = 1;
1360 }
1361
1362 function validate_sess($sess) {
1363   if (strlen($sess) == SESS_LEN) 
1364     return (TRUE);
1365   else
1366     return (FALSE);
1367 }
1368 ?>