ffe6c8b14ff814b85be8f83290945d4a0c0e550d
[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 background_set()
28 {
29     $("bg").style.backgroundImage = 'url("img/brisk_table_sand'+table_pos+'.jpg")'; 
30 }
31
32 /* Stat: TABLE  Subst: ASTA */
33 function act_asta(card,pnt)
34 {
35     send_mesg("asta|"+card+"|"+pnt);
36 }
37
38 var asta_xarr = new Array(0,66,132);
39
40 /* TODO: impostare gli onclick */
41 function dispose_asta(idx, pnt, nopoint)
42 {
43     var i, btn, pass;
44     var btn;
45     var lng = langtolng(g_lang);
46
47     for (i = 0 ; i < 10 ; i++) {
48         btn = $("asta"+i);
49         if (i < idx) {
50             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+lng+".png";
51             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
52             pass = -1;
53         }
54         else {
55             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+lng+".png";
56             btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
57             pass = i;
58         }
59         if (i < 19)
60             btn.style.left = asta_xarr[i % 3];
61         else
62             btn.style.left = asta_xarr[(i+1) % 3];
63         
64         btn.style.top  = parseInt(i / 3) * 50 + (i == 9 ? 0 : 1);
65
66         if (pnt >= 0) {
67             eval("btn.onclick = function () { act_asta("+pass+",61); }");
68             btn.style.cursor = "pointer";
69         }
70         else {
71             btn.onclick = null;
72             btn.style.cursor = "default";
73         }
74     }
75     
76     
77     btn = $("astaptdiv");
78     btn.style.left = asta_xarr[i % 3];
79     btn.style.top = parseInt(i / 3) * 50 - 2;
80     // btn.style.visibility  = "visible";
81     
82     btn = $("astapt");
83     var rpnt = (pnt < 0 ? -pnt : pnt);
84     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
85     
86     btn = $("astaptsub");
87     btn.style.left = asta_xarr[i % 3];
88     btn.style.top = 25 + parseInt(i / 3) * 50 - 1;
89     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+lng+".png";
90     btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
91     if (pnt >= 0) {
92         btn.onclick = function () { act_asta(9,$("astapt").value); };
93         btn.style.cursor = "pointer";
94     }
95     else {
96         btn.onclick = null;
97         btn.style.cursor = "default";
98     }
99     
100     i+=1;
101     if (nopoint) {
102         btn = $("astapasso");
103         btn.style.left = asta_xarr[i % 3];
104         btn.style.top = parseInt(i / 3) * 50;
105         btn.src = "img/astapashalf"+(pnt >= 0 ? "" : "_ro")+lng+".png";
106         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
107         if (pnt >= 0) {
108             btn.onclick = function () { act_asta(-1,0); };
109         }
110         else {          
111             btn.onclick = null;
112         }
113
114         btn = $("astalascio");
115         btn.style.left = asta_xarr[i % 3];
116         btn.style.top = parseInt(i / 3) * 50 + 24;
117         btn.src = "img/astalascio"+lng+".png";
118         btn.style.visibility = "";
119         btn.onclick = function () { safelascio(); };
120         }
121     else {
122         btn = $("astapasso");
123         btn.style.left = asta_xarr[i % 3];
124         btn.style.top = parseInt(i / 3) * 50;;
125         btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+lng+".png";
126         btn.style.cursor = (pnt >= 0 ? "pointer" : "default");
127         if (pnt >= 0) {
128             btn.onclick = function () { act_asta(-1,0); };
129         }
130         else {
131             btn.onclick = null;
132         }
133
134         btn = $("astalascio");
135         btn.style.visibility = "hidden";
136         btn.onclick = null;
137     }
138     // btn.style.visibility  = "visible";
139     $("asta").style.visibility = "visible";
140 }
141
142 function asta_pnt_set(pnt)
143 {
144     btn = $("astapt");
145     var rpnt = (pnt < 0 ? -pnt : pnt);
146     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
147 }
148
149 function hide_asta()
150 {
151     $("asta").style.visibility = "hidden"; 
152 }
153
154 function choose_seed(card)
155 {
156     var i;
157
158     $("asta").style.visibility = "hidden"; 
159     $("astalascio").style.visibility = "hidden"; 
160     $("chooseed").style.visibility = "visible";
161     for (i = 0 ; i < 4 ; i++) {
162         $("seed"+i).src = "img/"+i+""+card+".png";
163         seed=$("seed"+i);
164         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
165     }
166 }
167
168 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
169
170 function show_astat(zer,uno,due,tre,qua)
171 {
172     var astat = new Array(zer,uno,due,tre,qua);
173     var lng = langtolng(g_lang);
174
175     for (i = 0 ; i < PLAYERS_N ; i++) {
176         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
177
178         if (astat[i] == -2) {
179             $("public"+astat_suffix[idx]).style.visibility = "hidden";
180         }
181         else if (astat[i] == -1) {
182             $("public"+astat_suffix[idx]).style.visibility = "visible";
183             $("pubacard"+astat_suffix[idx]).src = "img/astapasso"+lng+".png";
184             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
185             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
186         }
187         else if (astat[i] <= 10) {
188             $("public"+astat_suffix[idx]).style.visibility = "visible";
189             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+lng+".png";
190             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
191         }
192         else if (astat[i] <= 120) {
193             $("public"+astat_suffix[idx]).style.visibility = "visible";
194             $("pubacard"+astat_suffix[idx]).src = "img/asta9"+lng+".png";
195             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
196             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
197         }
198     }
199 }
200
201
202 function table_init() {
203     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
204
205     // console.log("table_init");
206
207     remark_off();
208     $("asta").style.visibility = "hidden";
209     $("caller").style.visibility = "hidden";
210     show_astat(-2,-2,-2,-2,-2);
211     set_iscalling(-1);
212
213     for (i=0 ; i < CARD_HAND ; i++) {
214         Drag.init($("card" + i), card_mouseup_cb);
215         for (e = 0 ; e < PLAYERS_N ; e++)
216             $("card"+sux[e]+i).style.visibility = "hidden";
217     }
218     for (i=0 ; i < PLAYERS_N ; i++) {
219         // console.log("shut: "+"takes"+sux[i]);
220         $("takes"+sux[i]).style.visibility = "hidden";
221         }
222
223     for (i=0 ; i < CARD_HAND ; i++) {
224         cards_pos[i] = i;
225         cards_ea_pos[i] = i;
226         cards_ne_pos[i] = i;
227         cards_nw_pos[i] = i;
228         cards_we_pos[i] = i;
229     }
230
231 }
232   
233 function act_choose(card)
234 {
235     send_mesg("choose|"+card);
236 }
237
238 function act_play(card,x,y)
239 {
240     send_mesg("play|"+card+"|"+x+"|"+y);
241 }
242
243 function act_tableinfo()
244 {
245     send_mesg("tableinfo");
246 }
247
248 function act_exitlock()
249 {
250     send_mesg("exitlock");
251 }
252
253 function safelogout()
254 {
255     var res;
256     
257     if (g_exitlock < 2) 
258         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.");    
259     else 
260         res = window.confirm("Sei sicuro di volere abbandonare la partita?");
261     if (res)
262         act_logout(g_exitlock);
263 }
264
265 function act_reload()
266 {
267     window.onunload = null;
268     window.onbeforeunload = null;
269     // alert(document.location.toString());
270     document.location.assign("index.php");
271     // document.location.reload();
272 }
273
274 function set_names(so,ea,ne,nw,we)
275 {
276     // alert("SET NAME");
277     $("name").innerHTML    = user_decorator(so);
278     $("name").title    = unescapeHTML(so[1]); 
279     $("name_ea").innerHTML = user_decorator(ea);
280     $("name_ea").title = unescapeHTML(ea[1]);
281     $("name_ne").innerHTML = user_decorator(ne);
282     $("name_ne").title = unescapeHTML(ne[1]);
283     $("name_nw").innerHTML = user_decorator(nw);
284     $("name_nw").title = unescapeHTML(nw[1]);
285     $("name_we").innerHTML = user_decorator(we);
286     $("name_we").title = unescapeHTML(we[1]);
287
288     for (i = 0 ; i < PLAYERS_N ; i++) 
289         $("name"+astat_suffix[i]).title_orig = $("name"+astat_suffix[i]).title;
290
291     return;
292 }
293
294 function set_iscalling(idx)
295 {
296     var i;
297
298     for (i = 0 ; i < PLAYERS_N ; i++) {
299         $("name"+astat_suffix[i]).className = "pubinfo"+astat_suffix[i]+(i == idx ? "_iscalling" : "");
300         $("name"+astat_suffix[i]).title = $("name"+astat_suffix[i]).title_orig + (i == idx ? mlang_briskin5['is_calling'][g_lang] : "");
301     }
302 }
303
304 function preferences_init()
305 {
306     var rd;
307
308     if ((rd = readCookie("CO_bin5_pref_ring_endauct")) != null) {
309         $('pref_ring_endauct').checked = (rd == "true" ? true : false);
310     }
311     else {
312         $('pref_ring_endauct').checked = false;
313     }
314     $('preferences').ring_endauct = $('pref_ring_endauct').checked;
315 }
316
317 function preferences_update()
318 {
319     var ret;
320     createCookie("CO_bin5_pref_ring_endauct", ($('preferences').ring_endauct ? "true" : "false"), 24*3650, cookiepath); 
321     ret = server_request('mesg', 'preferences_update');
322 }
323
324 function act_preferences_update()
325 {
326     preferences_update()
327     preferences_showhide();
328 }
329
330 function pref_ring_endauct_set(obj)
331 {
332     $('preferences').ring_endauct = obj.checked;
333 }
334
335
336 function preferences_show()
337 {
338     var no;
339
340     div_show($('preferences'));
341 }
342
343 function preferences_showhide()
344 {
345     if ($('preferences').style.visibility == 'hidden') {
346         preferences_init();
347         
348         $('preferences').style.top = parseInt((document.body.clientHeight - 
349                                                parseInt(getStyle($('preferences'), "height","height"))
350                                                ) / 2) + document.body.scrollTop;
351         $('preferences').style.visibility = 'visible';
352     }
353     else
354         $('preferences').style.visibility = 'hidden';
355 }