consistency to add minute to apprentices tables
[brisk.git] / web / briskin5 / Obj / rules_old_rules.phh
1 <?php
2 /*
3  *  brisk - rules_old_rules.phh
4  *
5  *  Copyright (C) 2017 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 require_once("briskin5.phh");
26 require_once("rules_base.phh");
27
28 class Rules_old_rules extends Rules {
29     function __construct($table)
30     {
31         parent::__construct($table);
32         $this->id = 1;
33     }
34
35     static function asta2mult($asta_pnt)
36     {
37         if ($asta_pnt > 110)
38             return (6);
39         else if ($asta_pnt > 100)
40             return (5);
41         else if ($asta_pnt > 90)
42             return (4);
43         else if ($asta_pnt > 80)
44             return (3);
45         else if ($asta_pnt > 70)
46             return (2);
47         else
48             return (1);
49     }
50     static function s_multer($mult, $pnt)
51     {
52         return (pow(2, $mult) * static::asta2mult($pnt));
53     }
54
55     static function s_point_calc($pnt_done, $mult, $pnt_req, $is_allpoints)
56     {
57         return ($pnt_done * static::s_multer($mult, $pnt_req) * ($is_allpoints ? 2 : 1));
58     }
59
60     function multer($is_new)
61     {
62         if ($is_new) {
63             return (static::s_multer($this->table->mult, $this->table->asta_pnt));
64         }
65         else {
66             return (static::s_multer($this->table->old_mult, $this->table->old_asta_pnt));
67         }
68     }
69
70     static function game_result($asta_pnt, $pnt)
71     {
72         $sixty = (BIN5_PLAYERS_N == 3 ? 30 : 60);
73         if ($asta_pnt == 61) {
74             if ($pnt > $sixty)
75                 return (1);
76             else if ($pnt == $sixty)
77                 return (0);
78             else
79                 return (-1);
80         }
81         else {
82             if ($pnt >= $asta_pnt)
83                 return (1);
84             else
85                 return (-1);
86         }
87     }
88
89     function rules_asta(&$bri, $curtime, $action, $user, &$ret_s, $a_card, $a_pnt)
90     {
91         $index_cur = $this->table->gstart % BIN5_PLAYERS_N;
92
93         // Abbandono dell'asta
94         if ($a_card <= -1) {
95             log_wr("Abbandona l'asta.");
96             $this->table->asta_pla[$index_cur] = FALSE;
97             $user->asta_card  = -1;
98             $this->table->asta_pla_n--;
99
100             return TRUE;
101         }
102         else if ($a_card <= 9) {
103             $ret_s = "";
104
105             do {
106                 if ($a_card >= 0 && $a_card < 9 && $a_card > $this->table->asta_card)
107                     ;
108                 else if ($a_card == 9 && $a_pnt > ($this->table->asta_pnt >= 61 ? $this->table->asta_pnt : 60)
109                          && $a_pnt <= 120)
110                     ;
111                 else
112                     break;
113
114                 $user->asta_card  = $a_card;
115                 $this->table->asta_card = $a_card;
116                 if ($a_card == 9) {
117                     $user->asta_pnt   = $a_pnt;
118                     $this->table->asta_pnt  = $a_pnt;
119                 }
120
121                 return TRUE;
122             } while (0);
123         }
124
125         return FALSE;
126     }
127
128     function rules_checkchoose(&$bri, $curtime, $action, $user, &$ret_s, $a_brisco)
129     {
130         if ( !($a_brisco >= 0 && $a_brisco < 40)) // (BIN5_CARD_HAND * BIN5_PLAYERS_N)) )
131             return FALSE;
132
133         if ( !($this->table->asta_win > -1 &&
134                $user->table_pos == $this->table->asta_win) )
135             return FALSE;
136
137         return TRUE;
138     }
139
140     function rules_nextauct(&$bri, $curtime, $action, $user, $maxcard)
141     {
142         if (($this->table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
143             !($this->table->asta_card == 9 && $this->table->asta_pnt == 120)) {
144             return TRUE;
145         }
146         else {
147             return FALSE;
148         }
149
150     }
151
152     function engine(&$bri, $curtime, $action, $user, &$opt1 = NULL, &$opt2 = NULL, &$opt3 = NULL)
153     {
154         GLOBAL $G_all_points, $G_dbasetype;
155
156         $table = $this->table;
157         $pnts_sav = array();
158
159         if ($action == BIN5_RULES_ASTA) {
160             return ($this->rules_asta($bri, $curtime, $action, $user, $opt1, $opt2, $opt3));
161         }
162         else if ($action == BIN5_RULES_NEXTAUCT) {
163             return ($this->rules_nextauct($bri, $curtime, $action, $user, $opt1));
164         }
165         else if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
166             $table->old_act = $action;
167             $table->old_asta_win = -1;
168             $table->old_pnt = 0;
169             $table->mult_inc(1);
170             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
171                 $pnts_sav[$i] = 0;
172             }
173
174             $game_delta = 1;
175             // $table->game_next(1);
176             $table->game_init($bri->user);
177         }
178         else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
179             if (!($user->handpt <= 2)) {
180                 return (FALSE);
181             }
182             $table->old_act = $action;
183             log_wr(sprintf("GIOCO FINITO !!!"));
184             $table->old_asta_win = $user->table_pos;
185             $table->old_pnt = 0;
186             $table->mult_inc(1);
187
188             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
189                 $pnts_sav[$i] = 0;
190             }
191
192             // Non si cambia mazzo se si abbandona la partita
193             $game_delta = 0;
194             // $table->game_next(0);
195             $table->game_init($bri->user);
196         }
197         else if ($action == BIN5_RULES_CHECKCHOOSE) {
198             return ($this->rules_checkchoose($bri, $curtime, $action, $user, $opt1, $opt2));
199         }
200         else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
201             $table->old_act = $action;
202             do {
203                 $pro = 0;
204
205                 if ($table->asta_pnt == 60)
206                     $table->asta_pnt = 61;
207
208                 $table->old_reason = "";
209
210                 // count points for the temporary 2 teams
211                 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
212                     $ctt = $table->card[$i]->value % 10;
213                     $own = $table->card[$i]->owner;
214                     if ($own == $table->asta_win || $own == $table->friend)
215                         $pro += $G_all_points[$ctt];
216                 }
217
218                 log_wr(sprintf("PRO: [%d]", $pro));
219
220                 // PATTA case !
221                 if (static::game_result($table->asta_pnt, $pro) == 0) {
222                     $table->points[$table->points_n % MAX_POINTS] = array();
223                     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
224                         $table->points[$table->points_n % MAX_POINTS][$i] = 0;
225                         $pnts_sav[$i] = 0;
226                     }
227                     $table->points_n++;
228                     $table->old_pnt = $pro;
229                     $table->old_asta_win = $table->asta_win;
230                     $table->mult_inc(1);
231
232                     break;
233                     }
234
235                 if (static::game_result($table->asta_pnt, $pro) == 1)
236                     $sig = 1;
237                 else
238                     $sig = -1;
239
240                 // TAG: POINTS_MANAGEMENT
241                 $table->points[$table->points_n % MAX_POINTS] = array();
242                 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
243                     if ($i == $table->asta_win)
244                         $pnt = ($i == $table->friend ? 4 : 2);
245                     else if ($i == $table->friend)
246                         $pnt = 1;
247                     else
248                         $pnt = -1;
249
250                     log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pnt));
251
252                     $pnt_sav = static::s_point_calc($pnt * $sig,           0, $table->asta_pnt, ($pro == 120));
253                     $pnt_tab = static::s_point_calc($pnt * $sig, $table->mult, $table->asta_pnt, ($pro == 120));
254
255                     $table->points[$table->points_n % MAX_POINTS][$i] = $pnt_tab;
256                     $table->total[$i] += $pnt_tab;
257                     $pnts_sav[$i] = $pnt_sav;
258                 }
259                 $table->points_n++;
260                 $table->old_pnt = $pro;
261                 $table->old_asta_win = $table->asta_win;
262                 $table->mult_set(0);
263             } while (0);
264             $game_delta = 1;
265         }
266         else {
267             return (FALSE);
268         }
269         $table->game_next($game_delta);
270
271         $plist = "$table->table_token|$user->table_orig|$table->player_n";
272         $ucodes = array();
273         $codes = "";
274         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
275             $user_cur = &$bri->user[$table->player[$i]];
276
277             /* pro db */
278             $ucodes[$i] = $user_cur->code_get();
279
280             /* pro log */
281             $plist .= '|'.xcapelt($user_cur->name).'|'.$pnts_sav[$i];
282             $codes .= '|'.xcapelt($user_cur->code_get());
283         }
284         $plist .= $codes;
285         log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
286
287         $table->old_asta_pnt = $table->asta_pnt;
288         // $table->old_mazzo is managed by ->game_next();
289         // $table->old_mult, $table->old_pnt, $table->old_reason and $table->old_asta_win are specific
290
291         $table->old_friend = $table->friend;
292         $table->old_tourn_pts = $table->tourn_pts;
293
294         $table->old_reason = static::game_description($action, 'html', $table->old_mult,
295                                              $table->old_asta_win,
296                                              ($table->old_asta_win != -1 ?
297                                               $bri->user[$table->player[$table->old_asta_win]]->name : ""),
298                                              $table->old_friend,
299                                              ($table->old_friend != -1 ?
300                                               $bri->user[$table->player[$table->old_friend]]->name : ""),
301                                              $table->old_pnt, $table->old_asta_pnt, $table->old_tourn_pts);
302
303
304         if ($user->table_orig < TABLES_APPR_N) {
305             require_once("../Obj/dbase_".$G_dbasetype.".phh");
306
307             if (($bdb = BriskDB::create()) != FALSE) {
308                 $bdb->bin5_points_save($curtime, $table, $user->table_orig, $action, $ucodes, $pnts_sav);
309                 unset($bdb);
310             }
311             else {
312                 log_points($user->ip, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
313             }
314             log_points($user->ip, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
315         }
316
317         $table->game_init($bri->user);
318
319         return (TRUE);
320     }
321
322     static function game_description($act, $form, $old_mult, $win = -1, $win_name = "?1?", $fri = -1, $fri_name = "?2?",
323                               $old_pnt = -1, $old_asta_pnt = -1, $old_tourn_pts = -1)
324     {
325         GLOBAL $G_lang, $mlang_bin5_rules;
326
327         if ($form == 'html') {
328             $tg_br = "<br>";
329             $tg_hr = "<hr>";
330             $tg_bo = "<b>";
331             $tg_bc = "</b>";
332             $win_name = xcape($win_name);
333             $fri_name = xcape($fri_name);
334         }
335         else {
336             $tg_br = " ";
337             $tg_hr = " ";
338             $tg_bo = "";
339             $tg_bc = "";
340         }
341
342         if ($act == BIN5_RULES_OLDSCHEMA) {
343             return ("");
344         }
345         else if ($act == BIN5_RULES_ALLPASS) {
346             return (sprintf($mlang_bin5_rules['info_alpa'][$G_lang],
347                             $tg_br, $tg_hr, $tg_bo, $tg_bc));
348         }
349         else if ($act == BIN5_RULES_ABANDON) {
350             return (sprintf($mlang_bin5_rules['info_aban'][$G_lang],
351                             $tg_br, $tg_hr, $tg_bo, $tg_bc,
352                             $win_name));
353         }
354         else {
355             $wol = static::game_result($old_asta_pnt, $old_pnt);
356
357             $noty = "";
358
359             if ($win != $fri) { // not alone case
360                 /* MLANG: "<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", "hanno fatto <b>cappotto</b> EBBRAVI!.<hr>", "dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>", "<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", "ha fatto <b>cappotto</b> EBBRAVO!.<hr>", "doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>", ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt : 'pi&ugrave; di 60'), $table->old_pnt, ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso" */
361
362                 $noty .= sprintf($mlang_bin5_rules['info_part'][$G_lang],
363                                  $tg_br, $tg_hr, $tg_bo, $tg_bc,
364                                  $win_name,
365                                  $fri_name);
366                 if ($old_pnt == 120) {
367                     $noty .= sprintf($mlang_bin5_rules['info_capp'][$G_lang],
368                                      $tg_br, $tg_hr, $tg_bo, $tg_bc );
369                 }
370                 else {
371                     if ($old_asta_pnt > 61) {
372                         $noty .= sprintf($mlang_bin5_rules['info_alea'][$G_lang],
373                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
374                                          $old_asta_pnt, $old_pnt,
375                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
376                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
377                                            $mlang_bin5_rules['info_lost'][$G_lang])));
378                     }
379                     else {
380                         $noty .= sprintf($mlang_bin5_rules['info_more'][$G_lang],
381                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
382                                          $old_pnt,
383                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
384                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
385                                            $mlang_bin5_rules['info_lost'][$G_lang])));
386                     } // else of if ($old_asta_pnt > 61) {
387                 } // else of if ($old_pnt == 120) {
388             } // if ($win != $fri) { // not alone case
389             else {
390                 $noty .= sprintf($mlang_bin5_rules['info_alon'][$G_lang],
391                                  $tg_br, $tg_hr, $tg_bo, $tg_bc,
392                                  $win_name);
393                 if ($old_pnt == 120) {
394                     $noty .= sprintf($mlang_bin5_rules['info_acap'][$G_lang],
395                                      $tg_br, $tg_hr, $tg_bo, $tg_bc);
396                 }
397                 else {
398                     if ($old_asta_pnt > 61) {
399                         $noty .= sprintf($mlang_bin5_rules['info_aleaa'][$G_lang],
400                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
401                                          $old_asta_pnt, $old_pnt,
402                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
403                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
404                                            $mlang_bin5_rules['info_lost'][$G_lang])));
405                     }
406                     else {
407                         $noty .= sprintf($mlang_bin5_rules['info_morea'][$G_lang],
408                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
409                                          $old_pnt,
410                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
411                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
412                                            $mlang_bin5_rules['info_lost'][$G_lang])));
413                     }
414                 }
415             }
416         }
417
418         $old_multer = static::s_multer($old_mult, $old_asta_pnt);
419         if ($old_multer > 1) {
420             $noty .= sprintf($mlang_bin5_rules['info_omul'][$G_lang],
421                              $tg_br, $tg_hr, $tg_bo, $tg_bc,
422                              multoval($old_multer));
423         }
424         $noty .= sprintf('%2$s', $tg_br, $tg_hr);
425
426         return ($noty);
427     }
428 } // class Rules_old_rules
429
430 ?>