enabled deck select at table preferences
[brisk.git] / web / briskin5 / briskin5.js
1 /*
2  *  brisk - briskin5.js
3  *
4  *  Copyright (C) 2006-2012 Matteo Nastasi
5  *                          mailto: nastasi@alternativeoutput.it 
6  *                                  matteo.nastasi@milug.org
7  *                          web: http://www.alternativeoutput.it
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details. You should have received a
18  * copy of the GNU General Public License along with this program; if
19  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
20  * Suite 330, Boston, MA 02111-1307, USA.
21  *
22  */
23
24 var mlang_briskin5 = { 'is_calling' : { 'it' : ' sta chiamando', 
25                                         'en' : ' is calling' } }
26
27 function Preferences(ring_endauct, deck, deck_old)
28 {
29     this.ring_endauct = ring_endauct;
30     this.deck = deck;
31     this.deck_old = deck_old;
32 }
33
34 Preferences.prototype = {
35     ring_endauct: true,
36     deck: null,
37     deck_old: null
38 }
39
40 function background_set()
41 {
42     $("bg").style.backgroundImage = 'url("img/brisk_table_sand'+table_pos+'.jpg")'; 
43 }
44
45 /* Stat: TABLE  Subst: ASTA */
46 function act_asta(card,pnt)
47 {
48     send_mesg("asta|"+card+"|"+pnt);
49 }
50
51 var asta_xarr = new Array(0,66,132);
52
53 /* TODO: impostare gli onclick */
54 function dispose_asta(idx, pnt, nopoint)
55 {
56     var i, btn, pass;
57     var btn;
58     var lng = langtolng(g_lang);
59
60     for (i = 0 ; i < 10 ; i++) {
61         btn = $("asta"+i);
62         if (i < idx) {
63             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+lng+".png";
64             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
65             pass = -1;
66         }
67         else {
68             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+lng+".png";
69             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
70             pass = i;
71         }
72         if (i < 19)
73             btn.style.left = asta_xarr[i % 3];
74         else
75             btn.style.left = asta_xarr[(i+1) % 3];
76         
77         btn.style.top  = parseInt(i / 3) * 50 + (i == 9 ? 0 : 1);
78
79         if (pnt >= 0) {
80             eval("btn.onclick = function () { act_asta("+pass+",61); }");
81             btn.style.cursor = "pointer";
82         }
83         else {
84             btn.onclick = null;
85             btn.style.cursor = "default";
86         }
87     }
88     
89     
90     btn = $("astaptdiv");
91     btn.style.left = asta_xarr[i % 3];
92     btn.style.top = parseInt(i / 3) * 50 - 2;
93     // btn.style.visibility  = "visible";
94     
95     btn = $("astapt");
96     var rpnt = (pnt < 0 ? -pnt : pnt);
97     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
98     
99     btn = $("astaptsub");
100     btn.style.left = asta_xarr[i % 3];
101     btn.style.top = 25 + parseInt(i / 3) * 50 - 1;
102     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+lng+".png";
103     btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
104     if (pnt >= 0) {
105         btn.onclick = function () { act_asta(9,$("astapt").value); };
106         btn.style.cursor = "pointer";
107     }
108     else {
109         btn.onclick = null;
110         btn.style.cursor = "default";
111     }
112     
113     i+=1;
114     if (nopoint) {
115         btn = $("astapasso");
116         btn.style.left = asta_xarr[i % 3];
117         btn.style.top = parseInt(i / 3) * 50;
118         btn.src = "img/astapashalf"+(pnt >= 0 ? "" : "_ro")+lng+".png";
119         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
120         if (pnt >= 0) {
121             btn.onclick = function () { act_asta(-1,0); };
122         }
123         else {          
124             btn.onclick = null;
125         }
126
127         btn = $("astalascio");
128         btn.style.left = asta_xarr[i % 3];
129         btn.style.top = parseInt(i / 3) * 50 + 24;
130         btn.src = "img/astalascio"+lng+".png";
131         btn.style.visibility = "";
132         btn.onclick = function () { safelascio(); };
133         }
134     else {
135         btn = $("astapasso");
136         btn.style.left = asta_xarr[i % 3];
137         btn.style.top = parseInt(i / 3) * 50;;
138         btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+lng+".png";
139         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
140         if (pnt >= 0) {
141             btn.onclick = function () { act_asta(-1,0); };
142         }
143         else {
144             btn.onclick = null;
145         }
146
147         btn = $("astalascio");
148         btn.style.visibility = "hidden";
149         btn.onclick = null;
150     }
151     // btn.style.visibility  = "visible";
152     $("asta").style.visibility = "visible";
153 }
154
155 function asta_pnt_set(pnt)
156 {
157     btn = $("astapt");
158     var rpnt = (pnt < 0 ? -pnt : pnt);
159     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
160 }
161
162 function hide_asta()
163 {
164     $("asta").style.visibility = "hidden"; 
165 }
166
167 function choose_seed(card)
168 {
169     var i;
170
171     $("asta").style.visibility = "hidden"; 
172     $("astalascio").style.visibility = "hidden"; 
173     $("chooseed").style.visibility = "visible";
174     for (i = 0 ; i < 4 ; i++) {
175         $("seed"+i).src = "img/"+i+""+card+".png";
176         seed=$("seed"+i);
177         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
178     }
179 }
180
181 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
182
183 function show_astat(zer,uno,due,tre,qua)
184 {
185     var astat = new Array(zer,uno,due,tre,qua);
186     var lng = langtolng(g_lang);
187
188     for (i = 0 ; i < PLAYERS_N ; i++) {
189         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
190
191         if (astat[i] == -2) {
192             $("public"+astat_suffix[idx]).style.visibility = "hidden";
193         }
194         else if (astat[i] == -1) {
195             $("public"+astat_suffix[idx]).style.visibility = "visible";
196             $("pubacard"+astat_suffix[idx]).src = "img/astapasso"+lng+".png";
197             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
198             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
199         }
200         else if (astat[i] <= 10) {
201             $("public"+astat_suffix[idx]).style.visibility = "visible";
202             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+lng+".png";
203             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
204         }
205         else if (astat[i] <= 120) {
206             $("public"+astat_suffix[idx]).style.visibility = "visible";
207             $("pubacard"+astat_suffix[idx]).src = "img/asta9"+lng+".png";
208             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
209             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
210         }
211     }
212 }
213
214
215 function table_init() {
216     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
217
218     // console.log("table_init");
219
220     remark_off();
221     $("asta").style.visibility = "hidden";
222     $("caller").style.visibility = "hidden";
223     show_astat(-2,-2,-2,-2,-2);
224     set_iscalling(-1);
225
226     for (i=0 ; i < CARD_HAND ; i++) {
227         Drag.init($("card" + i), card_mouseup_cb);
228         for (e = 0 ; e < PLAYERS_N ; e++)
229             $("card"+sux[e]+i).style.visibility = "hidden";
230     }
231     for (i=0 ; i < PLAYERS_N ; i++) {
232         // console.log("shut: "+"takes"+sux[i]);
233         $("takes"+sux[i]).style.visibility = "hidden";
234         }
235
236     for (i=0 ; i < CARD_HAND ; i++) {
237         cards_pos[i] = i;
238         cards_ea_pos[i] = i;
239         cards_ne_pos[i] = i;
240         cards_nw_pos[i] = i;
241         cards_we_pos[i] = i;
242     }
243
244 }
245   
246 function act_choose(card)
247 {
248     send_mesg("choose|"+card);
249 }
250
251 function act_play(card,x,y)
252 {
253     send_mesg("play|"+card+"|"+x+"|"+y);
254 }
255
256 function act_tableinfo()
257 {
258     send_mesg("tableinfo");
259 }
260
261 function act_exitlock()
262 {
263     send_mesg("exitlock");
264 }
265
266 function safelogout()
267 {
268     var res;
269     
270     if (g_exitlock < 2) 
271         res = window.confirm("Sei sicuro di volere abbandonare la partita?\nATTENZIONE: se esci adesso senza il consenso degli altri giocatori non potrai sederti ai tavoli per "+(Math.floor(EXIT_BAN_TIME/60))+" minuti.");    
272     else 
273         res = window.confirm("Sei sicuro di volere abbandonare la partita?");
274     if (res)
275         act_logout(g_exitlock);
276 }
277
278 function act_reload()
279 {
280     window.onunload = null;
281     window.onbeforeunload = null;
282     // alert(document.location.toString());
283     document.location.assign("index.php");
284     // document.location.reload();
285 }
286
287 function set_names(so,ea,ne,nw,we)
288 {
289     // alert("SET NAME");
290     $("name").innerHTML    = user_decorator(so, false);
291     $("name").title    = unescapeHTML(so[1]); 
292     $("name_ea").innerHTML = user_decorator(ea, false);
293     $("name_ea").title = unescapeHTML(ea[1]);
294     $("name_ne").innerHTML = user_decorator(ne, false);
295     $("name_ne").title = unescapeHTML(ne[1]);
296     $("name_nw").innerHTML = user_decorator(nw, false);
297     $("name_nw").title = unescapeHTML(nw[1]);
298     $("name_we").innerHTML = user_decorator(we, false);
299     $("name_we").title = unescapeHTML(we[1]);
300
301     for (i = 0 ; i < PLAYERS_N ; i++) 
302         $("name"+astat_suffix[i]).title_orig = $("name"+astat_suffix[i]).title;
303
304     return;
305 }
306
307 function set_iscalling(idx)
308 {
309     var i;
310
311     for (i = 0 ; i < PLAYERS_N ; i++) {
312         $("name"+astat_suffix[i]).className = "pubinfo"+astat_suffix[i]+(i == idx ? "_iscalling" : "");
313         $("name"+astat_suffix[i]).title = $("name"+astat_suffix[i]).title_orig + (i == idx ? mlang_briskin5['is_calling'][g_lang] : "");
314     }
315 }
316
317 var preferences = new Preferences(true, 'xx', 'xx');
318 // FIXME move it in the html dynamic generation scope
319 var deck_list = { 'xx': 'Normal cards',
320                   'yy': 'Slim cards' }
321 function preferences_init()
322 {
323     var rd;
324
325     if ((rd = readCookie("CO_bin5_pref_ring_endauct")) != null) {
326         preferences.ring_endauct = $('pref_ring_endauct').checked = (rd == "true" ? true : false);
327     }
328     else {
329         preferences.ring_endauct = $('pref_ring_endauct').checked = true;
330     }
331
332     if ((rd = readCookie("CO_bin5_pref_deck")) != null && rd in deck_list) {
333         $$('#pref_deck').val(rd);
334         preferences.deck = rd;
335         preferences.deck_old = rd;
336     }
337     else {
338         rd = 'xx';
339         $$('#pref_deck').val(rd);
340         preferences.deck = rd;
341         preferences.deck_old = rd;
342     }
343 }
344
345 function preferences_update()
346 {
347     var ret;
348     createCookie("CO_bin5_pref_ring_endauct", (preferences.ring_endauct ? "true" : "false"), 24*3650, cookiepath);
349     createCookie("CO_bin5_pref_deck", preferences.deck, 24*3650, cookiepath);
350     ret = server_request('mesg', 'preferences_update');
351 }
352
353 function act_preferences_update()
354 {
355     preferences_update();
356     if (preferences.deck != preferences.deck_old) {
357         // FIXME: with dynamic text
358         x = new notify(gst,'<br/>Per rendere visibile il nuovo mazzo di carte<br/><br/>occorre fare reload della pagina.',
359                        0, "Close", 400, 110);
360     }
361     preferences_showhide();
362 }
363
364 function pref_ring_endauct_set(obj)
365 {
366     preferences.ring_endauct = obj.checked;
367 }
368
369 function pref_deck_set(obj)
370 {
371     preferences.deck = $$(obj).val();
372 }
373
374
375 function preferences_show()
376 {
377     var no;
378
379     div_show($('preferences'));
380 }
381
382 function preferences_showhide()
383 {
384     if ($('preferences').style.visibility == 'hidden') {
385         preferences_init();
386         
387         $('preferences').style.top = parseInt((document.body.clientHeight - 
388                                                parseInt(getStyle($('preferences'), "height","height"))
389                                                ) / 2) + document.body.scrollTop;
390         $('preferences').style.visibility = 'visible';
391     }
392     else
393         $('preferences').style.visibility = 'hidden';
394 }
395
396 function act_select_rules(rule_id)
397 {
398     send_mesg("chatt|/rules " + rule_id);
399 }
400
401 function act_select_deck(deck_id)
402 {
403     preferences.deck = deck_id;
404 }
405
406 function rules_set(id)
407 {
408     $('select_rules').value = id;
409 }