fix rules for 'together for fun' tournament
[brisk.git] / web / briskin5 / Obj / rules_together17.phh
1 <?php
2 /*
3  *  brisk - rules_together17.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
29 $mlang_bin5_toge17_rules =
30     array(
31           // br, hr, b, /b, win, fri
32           'info_part' => array( 'it' => 'Ha chiamato %3$s%5$s%4$s (punti torneo %7$d), il socio era %3$s%6$s%4$s,%1$s',
33                                 'en' => 'The declarer was %3$s%5$s%4$s (tournment points %7$d), the partner was %3$s%6$s%4$s,%1$s'),
34           // br, hr, b, /b, win_name
35           'info_alon' => array( 'it' => '%3$s%5$s%4$s si è chiamato in mano (punti torneo %6$d),%1$s',
36                                 'en' => '%3$s%5$s%4$s play alone against each other (tournment points %6$d),%1$s')
37           );
38
39 class Rules_together17 extends Rules_no_draw {
40     function __construct($table)
41     {
42         parent::__construct($table);
43         $this->id = 4;
44     }
45
46     function tourn_points(&$user, $user_pos)
47     {
48         $card = $this->table->card;
49         unset($user->asta_tourn_pts);
50         $user->asta_tourn_pts = array(0, 0, 0, 0);
51         if (BIN5_CARD_HAND == 8)
52             $tourn_values = array(11, 10, 4,3,2, 1,1,1,1,1);
53         else
54             $tourn_values = array(33, 30, 12,9,6, 3,3,3,3,3);
55
56         for ($i = 0 ; $i < BIN5_PLAYERS_N * BIN5_CARD_HAND ; $i++) {
57
58             if ($card[$i]->owner == $user_pos) {
59                 $user->asta_tourn_pts[(int)($i / 10)] += $tourn_values[$i % 10];
60             }
61         }
62     }
63
64     function tourn_points_max($user)
65     {
66         $mx = 0;
67         for ($i = 0 ; $i < 4 ; $i++) {
68             if ($user->asta_tourn_pts[$i] > $mx) {
69                 $mx = $user->asta_tourn_pts[$i];
70             }
71         }
72
73         return $mx;
74     }
75
76     function min_tourn_points($a_pnt)
77     {
78         if ($a_pnt < 62)
79             $min_pts = 0;
80         else if ($a_pnt < 71)
81             $min_pts = 13;
82         else if ($a_pnt < 80)
83             $min_pts = 19;
84         else if ($a_pnt < 90)
85             $min_pts = 23;
86         else if ($a_pnt < 100)
87             $min_pts = 25;
88         else
89             $min_pts = 28;
90
91         return $min_pts;
92     }
93
94     function check_tourn_points($hand_pts, $a_pnt, $a_seed, &$ret_s) {
95         $min_pts = $this->min_tourn_points($a_pnt);
96
97         if ($hand_pts < $min_pts) {
98             // FIXME_LANG
99             $seeds = array('coppe', 'ori', 'spade', 'bastoni');
100             $ret_s = sprintf("Hai chiamato un due di %s a %d ma in mano per quel seme hai %d punti-torneo, non puoi.",
101                              $seeds[$a_seed], $a_pnt, $hand_pts);
102             return FALSE;
103         }
104         return TRUE;
105     }
106
107     function rules_asta(&$bri, $curtime, $action, $user, &$ret_s, $a_card, $a_pnt) {
108         /*
109 Per le chiamate da 62 a 70 bisognerà avere 13 punti ( esempi.: asso e fante oppure tre
110 e cavallo oppure re, cavallo, fante, sette, sei, cinque, quattro) ;
111 Per chiamate da 71 ad 80 bisognerà avere 19 punti ( esempi.: asso, re, cavallo e 6
112 oppure tre, re, fante, 7, 6 e 5 );
113 Per chiamate da 81 a 90 bisognerà avere 23 punti ( esempi.: asso, tre e fante oppure
114 asso, re, cavallo, fante, 6, 5 e 4);
115 Per chiamate da 91 a 100 bisognerà avere 25 punti ( esempi.: asso, tre, re e 7 oppure
116 asso, tre, cavallo e fante );
117 Per chiamate da 101 a 120 bisognerà avere 28 punti ( esempi.:asso, tre, re e cavallo
118 oppure asso, tre, cavallo, 7, 6, 5 e 4 ).
119         */
120
121         $index_cur = $this->table->gstart % BIN5_PLAYERS_N;
122
123         // Abbandono dell'asta
124         if ($a_card <= -1) {
125             log_wr("Abbandona l'asta.");
126             $this->table->asta_pla[$index_cur] = FALSE;
127             $user->asta_card  = -1;
128             $this->table->asta_pla_n--;
129
130             if ($this->table->asta_card == -1 && $this->table->asta_pla_n == 1) {
131                 // all abandon auction except one => move next bid to two and 71 points
132                 $this->table->asta_card = 9;
133                 $this->table->asta_pnt = 70;
134             }
135
136             return TRUE;
137         }
138         else if ($a_card <= 9) {
139             $ret_s = "";
140
141             do {
142                 if ($a_card >= 0 && $a_card < 9 && $a_card > $this->table->asta_card) {
143                     ;
144                 }
145                 else if ($a_card == 9 &&
146                          $a_pnt > ($this->table->asta_pnt >= 61 ? $this->table->asta_pnt : 60)
147                          && $a_pnt <= 120) {
148                     $min_pts = $this->min_tourn_points($a_pnt);
149
150                     $max_hand_pts = $this->tourn_points_max($user);
151                     if ($max_hand_pts < $min_pts) {
152                         // FIXME_LANG
153                         $ret_s = sprintf("Hai chiamato un due a %d ma in mano hai al massimo %d punti-torneo, non puoi.",
154                                          $a_pnt, $max_hand_pts);
155                         break;
156                     }
157                 }
158                 else {
159                     break;
160                 }
161
162                 $user->asta_card  = $a_card;
163                 $this->table->asta_card = $a_card;
164                 if ($a_card == 9) {
165                     $user->asta_pnt   = $a_pnt;
166                     $this->table->asta_pnt  = $a_pnt;
167                 }
168
169                 return TRUE;
170             } while (0);
171         }
172
173         return FALSE;
174     }
175
176     function rules_nextauct(&$bri, $curtime, $action, $user, $maxcard)
177     {
178         if (($this->table->asta_pla_n > ($maxcard > -1 ? 1 : 0)) &&
179             !($this->table->asta_card == 9 && $this->table->asta_pnt == 120)) {
180             return TRUE;
181         }
182         else {
183             return FALSE;
184         }
185
186     }
187
188     function rules_checkchoose(&$bri, $curtime, $action, $user, &$ret_s, $a_brisco)
189     {
190         if (! parent::rules_checkchoose(&$bri, $curtime, $action, $user, &$ret_s, $a_brisco)) {
191             return FALSE;
192         }
193
194         if ($this->table->asta_card == 9) {
195             $bri_seed = (int)($a_brisco / 10);
196             return $this->check_tourn_points($user->asta_tourn_pts[$bri_seed],
197                                              $this->table->asta_pnt, $bri_seed, $ret_s);
198         }
199
200         return TRUE;
201     }
202
203
204     static function game_description($act, $form, $old_mult, $win = -1, $win_name = "?1?", $fri = -1, $fri_name = "?2?",
205                               $old_pnt = -1, $old_asta_pnt = -1, $old_tourn_pts = -1)
206     {
207         GLOBAL $G_lang, $mlang_bin5_rules, $mlang_bin5_toge17_rules;
208
209         if ($form == 'html') {
210             $tg_br = "<br>";
211             $tg_hr = "<hr>";
212             $tg_bo = "<b>";
213             $tg_bc = "</b>";
214             $win_name = xcape($win_name);
215             $fri_name = xcape($fri_name);
216         }
217         else {
218             $tg_br = " ";
219             $tg_hr = " ";
220             $tg_bo = "";
221             $tg_bc = "";
222         }
223
224         if ($act == BIN5_RULES_OLDSCHEMA) {
225             return ("");
226         }
227         else if ($act == BIN5_RULES_ALLPASS) {
228             return (sprintf($mlang_bin5_rules['info_alpa'][$G_lang],
229                             $tg_br, $tg_hr, $tg_bo, $tg_bc));
230         }
231         else if ($act == BIN5_RULES_ABANDON) {
232             return (sprintf($mlang_bin5_rules['info_aban'][$G_lang],
233                             $tg_br, $tg_hr, $tg_bo, $tg_bc,
234                             $win_name));
235         }
236         else {
237             $wol = static::game_result($old_asta_pnt, $old_pnt);
238
239             $noty = "";
240
241             if ($win != $fri) { // not alone case
242                 /* 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" */
243
244                 $noty .= sprintf($mlang_bin5_toge17_rules['info_part'][$G_lang],
245                                  $tg_br, $tg_hr, $tg_bo, $tg_bc,
246                                  $win_name,
247                                  $fri_name,
248                                  $old_tourn_pts);
249                 if ($old_pnt == 120) {
250                     $noty .= sprintf($mlang_bin5_rules['info_capp'][$G_lang],
251                                      $tg_br, $tg_hr, $tg_bo, $tg_bc );
252                 }
253                 else {
254                     if ($old_asta_pnt > 61) {
255                         $noty .= sprintf($mlang_bin5_rules['info_alea'][$G_lang],
256                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
257                                          $old_asta_pnt, $old_pnt,
258                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
259                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
260                                            $mlang_bin5_rules['info_lost'][$G_lang])));
261                     }
262                     else {
263                         $noty .= sprintf($mlang_bin5_rules['info_more'][$G_lang],
264                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
265                                          $old_pnt,
266                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
267                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
268                                            $mlang_bin5_rules['info_lost'][$G_lang])));
269                     } // else of if ($old_asta_pnt > 61) {
270                 } // else of if ($old_pnt == 120) {
271             } // if ($win != $fri) { // not alone case
272             else {
273                 $noty .= sprintf($mlang_bin5_toge17_rules['info_alon'][$G_lang],
274                                  $tg_br, $tg_hr, $tg_bo, $tg_bc,
275                                  $win_name,
276                                  $old_tourn_pts);
277                 if ($old_pnt == 120) {
278                     $noty .= sprintf($mlang_bin5_rules['info_acap'][$G_lang],
279                                      $tg_br, $tg_hr, $tg_bo, $tg_bc);
280                 }
281                 else {
282                     if ($old_asta_pnt > 61) {
283                         $noty .= sprintf($mlang_bin5_rules['info_aleaa'][$G_lang],
284                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
285                                          $old_asta_pnt, $old_pnt,
286                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
287                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
288                                            $mlang_bin5_rules['info_lost'][$G_lang])));
289                     }
290                     else {
291                         $noty .= sprintf($mlang_bin5_rules['info_morea'][$G_lang],
292                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
293                                          $old_pnt,
294                                          ($wol == 1 ? $mlang_bin5_rules['info_win'][$G_lang] :
295                                           ($wol == 0 ? $mlang_bin5_rules['info_peer'][$G_lang] :
296                                            $mlang_bin5_rules['info_lost'][$G_lang])));
297                     }
298                 }
299             }
300         }
301
302         $old_multer = static::s_multer($old_mult, $old_asta_pnt);
303         if ($old_multer > 1) {
304             $noty .= sprintf($mlang_bin5_rules['info_omul'][$G_lang],
305                              $tg_br, $tg_hr, $tg_bo, $tg_bc,
306                              multoval($old_multer));
307         }
308         $noty .= sprintf('%2$s', $tg_br, $tg_hr);
309
310         return ($noty);
311     }
312 } // class Rules_no_draw
313
314 ?>