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