0d2259a811e17de3044dc86e187a42344c1c6dba
[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     }
33
34     static function asta2mult($asta_pnt)
35     {
36         if ($asta_pnt > 110)
37             return (6);
38         else if ($asta_pnt > 100)
39             return (5);
40         else if ($asta_pnt > 90)
41             return (4);
42         else if ($asta_pnt > 80)
43             return (3);
44         else if ($asta_pnt > 70)
45             return (2);
46         else
47             return (1);
48     }
49     static function s_multer($mult, $pnt)
50     {
51         return (pow(2, $mult) * static::asta2mult($pnt));
52     }
53
54     static function s_point_calc($pnt_done, $mult, $pnt_req, $is_allpoints)
55     {
56         return ($pnt_done * static::s_multer($mult, $pnt_req) * ($is_allpoints ? 2 : 1));
57     }
58
59     function multer($is_new)
60     {
61         if ($is_new) {
62             return (static::s_multer($this->table->mult, $this->table->asta_pnt));
63         }
64         else {
65             return (static::s_multer($this->table->old_mult, $this->table->old_asta_pnt));
66         }
67     }
68
69     static function game_result($asta_pnt, $pnt)
70     {
71         $sixty = (BIN5_PLAYERS_N == 3 ? 30 : 60);
72         if ($asta_pnt == 61) {
73             if ($pnt > $sixty)
74                 return (1);
75             else if ($pnt == $sixty)
76                 return (0);
77             else
78                 return (-1);
79         }
80         else {
81             if ($pnt >= $asta_pnt)
82                 return (1);
83             else
84                 return (-1);
85         }
86     }
87
88     function rules_asta(&$bri, $curtime, $action, $user, &$ret_s, $a_card, $a_pnt) {
89         if ($a_card >= 0 && $a_card < 9 && $a_card > $this->table->asta_card)
90             return TRUE;
91         else if ($a_card == 9 && $a_pnt > ($this->table->asta_pnt >= 61 ? $this->table->asta_pnt : 60) && $a_pnt <= 120)
92             return TRUE;
93
94         return FALSE;
95     }
96
97     function engine(&$bri, $curtime, $action, $user, &$opt1 = NULL, &$opt2 = NULL, &$opt3 = NULL)
98     {
99         GLOBAL $G_all_points, $G_dbasetype;
100
101         $table = $this->table;
102         $pnts_sav = array();
103
104         if ($action == BIN5_RULES_ASTA) {
105             return ($this->rules_asta($bri, $curtime, $action, $user, &$opt1, $opt2, $opt3));
106         }
107         if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
108             $table->old_act = $action;
109             $table->old_asta_win = -1;
110             $table->old_pnt = 0;
111             $table->mult_inc(1);
112             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
113                 $pnts_sav[$i] = 0;
114             }
115
116             $game_delta = 1;
117             // $table->game_next(1);
118             $table->game_init(&$bri->user);
119         }
120         else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
121             if (!($user->handpt <= 2)) {
122                 return (FALSE);
123             }
124             $table->old_act = $action;
125             log_wr(sprintf("GIOCO FINITO !!!"));
126             $table->old_asta_win = $user->table_pos;
127             $table->old_pnt = 0;
128             $table->mult_inc(1);
129
130             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
131                 $pnts_sav[$i] = 0;
132             }
133
134             // Non si cambia mazzo se si abbandona la partita
135             $game_delta = 0;
136             // $table->game_next(0);
137             $table->game_init(&$bri->user);
138         }
139         else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
140             $table->old_act = $action;
141             do {
142                 $pro = 0;
143
144                 if ($table->asta_pnt == 60)
145                     $table->asta_pnt = 61;
146
147                 $table->old_reason = "";
148
149                 // count points for the temporary 2 teams
150                 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
151                     $ctt = $table->card[$i]->value % 10;
152                     $own = $table->card[$i]->owner;
153                     if ($own == $table->asta_win || $own == $table->friend)
154                         $pro += $G_all_points[$ctt];
155                 }
156
157                 log_wr(sprintf("PRO: [%d]", $pro));
158
159                 // PATTA case !
160                 if (static::game_result($table->asta_pnt, $pro) == 0) {
161                     $table->points[$table->points_n % MAX_POINTS] = array();
162                     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
163                         $table->points[$table->points_n % MAX_POINTS][$i] = 0;
164                         $pnts_sav[$i] = 0;
165                     }
166                     $table->points_n++;
167                     $table->old_pnt = $pro;
168                     $table->old_asta_win = $table->asta_win;
169                     $table->mult_inc(1);
170
171                     break;
172                     }
173
174                 if (static::game_result($table->asta_pnt, $pro) == 1)
175                     $sig = 1;
176                 else
177                     $sig = -1;
178
179                 // TAG: POINTS_MANAGEMENT
180                 $table->points[$table->points_n % MAX_POINTS] = array();
181                 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
182                     if ($i == $table->asta_win)
183                         $pnt = ($i == $table->friend ? 4 : 2);
184                     else if ($i == $table->friend)
185                         $pnt = 1;
186                     else
187                         $pnt = -1;
188
189                     log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pnt));
190
191                     $pnt_sav = static::s_point_calc($pnt * $sig,           0, $table->asta_pnt, ($pro == 120));
192                     $pnt_tab = static::s_point_calc($pnt * $sig, $table->mult, $table->asta_pnt, ($pro == 120));
193
194                     $table->points[$table->points_n % MAX_POINTS][$i] = $pnt_tab;
195                     $table->total[$i] += $pnt_tab;
196                     $pnts_sav[$i] = $pnt_sav;
197                 }
198                 $table->points_n++;
199                 $table->old_pnt = $pro;
200                 $table->old_asta_win = $table->asta_win;
201                 $table->mult_set(0);
202             } while (0);
203             $game_delta = 1;
204         }
205         else {
206             return (FALSE);
207         }
208         $table->game_next($game_delta);
209
210         $plist = "$table->table_token|$user->table_orig|$table->player_n";
211         $ucodes = array();
212         $codes = "";
213         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
214             $user_cur = &$bri->user[$table->player[$i]];
215
216             /* pro db */
217             $ucodes[$i] = $user_cur->code_get();
218
219             /* pro log */
220             $plist .= '|'.xcapelt($user_cur->name).'|'.$pnts_sav[$i];
221             $codes .= '|'.xcapelt($user_cur->code_get());
222         }
223         $plist .= $codes;
224         log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
225
226         $table->old_asta_pnt = $table->asta_pnt;
227         // $table->old_mazzo is managed by ->game_next();
228         // $table->old_mult, $table->old_pnt, $table->old_reason and $table->old_asta_win are specific
229
230         $table->old_friend = $table->friend;
231         $table->old_tourn_pts = $table->tourn_pts;
232
233         $table->old_reason = static::game_description($action, 'html', $table->old_mult,
234                                              $table->old_asta_win,
235                                              ($table->old_asta_win != -1 ?
236                                               $bri->user[$table->player[$table->old_asta_win]]->name : ""),
237                                              $table->old_friend,
238                                              ($table->old_friend != -1 ?
239                                               $bri->user[$table->player[$table->old_friend]]->name : ""),
240                                              $table->old_pnt, $table->old_asta_pnt, $table->old_tourn_pts);
241
242
243         if ($user->table_orig < TABLES_AUTH_N) {
244             require_once("../Obj/dbase_".$G_dbasetype.".phh");
245
246             if (($bdb = BriskDB::create()) != FALSE) {
247                 $bdb->bin5_points_save($curtime, $table, $user->table_orig, $action, $ucodes, $pnts_sav);
248                 unset($bdb);
249             }
250             else {
251                 log_points($user->ip, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
252             }
253             log_points($user->ip, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
254         }
255
256         $table->game_init(&$bri->user);
257
258         return (TRUE);
259     }
260
261     static function game_description($act, $form, $old_mult, $win = -1, $win_name = "?1?", $fri = -1, $fri_name = "?2?",
262                               $old_pnt = -1, $old_asta_pnt = -1, $old_tourn_pts = -1)
263     {
264         GLOBAL $G_lang, $mlang_bin5_bin5;
265
266         if ($form == 'html') {
267             $tg_br = "<br>";
268             $tg_hr = "<hr>";
269             $tg_bo = "<b>";
270             $tg_bc = "</b>";
271             $win_name = xcape($win_name);
272             $fri_name = xcape($fri_name);
273         }
274         else {
275             $tg_br = " ";
276             $tg_hr = " ";
277             $tg_bo = "";
278             $tg_bc = "";
279         }
280
281         if ($act == BIN5_RULES_OLDSCHEMA) {
282             return ("");
283         }
284         else if ($act == BIN5_RULES_ALLPASS) {
285             return (sprintf($mlang_bin5_rules['info_alpa'][$G_lang],
286                             $tg_br, $tg_hr, $tg_bo, $tg_bc));
287         }
288         else if ($act == BIN5_RULES_ABANDON) {
289             return (sprintf($mlang_bin5_rules['info_aban'][$G_lang],
290                             $tg_br, $tg_hr, $tg_bo, $tg_bc,
291                             $win_name));
292         }
293         else {
294             $wol = static::game_result($old_asta_pnt, $old_pnt);
295
296             $noty = "";
297
298             if ($win != $fri) { // not alone case
299                 /* 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" */
300
301                 $noty .= sprintf($mlang_bin5_rules['info_part'][$G_lang],
302                                  $tg_br, $tg_hr, $tg_bo, $tg_bc,
303                                  $win_name,
304                                  $fri_name,
305                                  $old_tourn_pts);
306                 if ($old_pnt == 120) {
307                     $noty .= sprintf($mlang_bin5_rules['info_capp'][$G_lang],
308                                      $tg_br, $tg_hr, $tg_bo, $tg_bc );
309                 }
310                 else {
311                     if ($old_asta_pnt > 61) {
312                         $noty .= sprintf($mlang_bin5_rules['info_alea'][$G_lang],
313                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
314                                          $old_asta_pnt, $old_pnt,
315                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
316                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
317                                            $mlang_bin5_rules['info_lost'][$G_lang])));
318                     }
319                     else {
320                         $noty .= sprintf($mlang_bin5_rules['info_more'][$G_lang],
321                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
322                                          $old_pnt,
323                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
324                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
325                                            $mlang_bin5_rules['info_lost'][$G_lang])));
326                     } // else of if ($old_asta_pnt > 61) {
327                 } // else of if ($old_pnt == 120) {
328             } // if ($win != $fri) { // not alone case
329             else {
330                 $noty .= sprintf($mlang_bin5_rules['info_alon'][$G_lang],
331                                  $tg_br, $tg_hr, $tg_bo, $tg_bc,
332                                  $win_name,
333                                  $old_tourn_pts);
334                 if ($old_pnt == 120) {
335                     $noty .= sprintf($mlang_bin5_rules['info_acap'][$G_lang],
336                                      $tg_br, $tg_hr, $tg_bo, $tg_bc);
337                 }
338                 else {
339                     if ($old_asta_pnt > 61) {
340                         $noty .= sprintf($mlang_bin5_rules['info_aleaa'][$G_lang],
341                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
342                                          $old_asta_pnt, $old_pnt,
343                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
344                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
345                                            $mlang_bin5_rules['info_lost'][$G_lang])));
346                     }
347                     else {
348                         $noty .= sprintf($mlang_bin5_rules['info_morea'][$G_lang],
349                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
350                                          $old_pnt,
351                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
352                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
353                                            $mlang_bin5_rules['info_lost'][$G_lang])));
354                     }
355                 }
356             }
357         }
358
359         $old_multer = static::s_multer($old_mult, $old_asta_pnt);
360         if ($old_multer > 1) {
361             $noty .= sprintf($mlang_bin5_rules['info_omul'][$G_lang],
362                              $tg_br, $tg_hr, $tg_bo, $tg_bc,
363                              multoval($old_multer));
364         }
365         $noty .= sprintf('%2$s', $tg_br, $tg_hr);
366
367         return ($noty);
368     }
369 } // class Rules_old_rules
370
371 ?>