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