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