aggiunto metodo act_help
[brisk.git] / web / commons.js
1 /*
2  *  brisk - commons.js
3  *
4  *  Copyright (C) 2006 matteo.nastasi@milug.org
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details. You should have received a
15  * copy of the GNU General Public License along with this program; if
16  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
17  * Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * $Id$
20  *
21  */
22
23 var PLAYERS_N = 3;
24
25 function $(id) { return document.getElementById(id); }
26
27 /* replacement of setInterval on IE */
28 (function(){
29     /*if not IE, do nothing*/
30     if(!document.uniqueID){return;};
31
32     /*Copy the default setInterval behavior*/
33     var nativeSetInterval = window.setInterval;
34     window.setInterval = function(fn,ms) {              
35         var param = [];
36         if(arguments.length <= 2)       {
37             return nativeSetInterval(fn,ms);
38         }
39         else {
40             for(var i=2;i<arguments.length;i+=1) {
41                 param[i-2] =  arguments[i];
42             }   
43         }
44         
45         if(typeof(fn)=='function') {
46             
47             return (function (fn,ms,param) {
48                 var fo = function () {                                                          
49                     fn.apply(window,param);
50                 };                      
51                 return nativeSetInterval(fo,ms); 
52             })(fn,ms,param);
53         }
54         else if(typeof(fn)=='string')
55         {
56             return  nativeSetInterval(fn,ms);
57         }
58         else
59         {
60             throw Error('setInterval Error\nInvalid function type');
61         };
62     };
63 })()
64
65     // var card_pos = RANGE 0 <= x < cards_ea_n
66
67 function rnd_int(min, max) {
68   return Math.floor(Math.random() * (max - min + 1) + min);
69 }
70
71 function error_images()
72 {
73     alert("GHESEMU!");
74 }
75
76 function abort_images()
77 {
78     alert("ABORTAIMAGES");
79 }
80
81 function unload_images()
82 {
83     alert("ABORTAIMAGES");
84 }
85
86 function reset_images()
87 {
88     alert("ABORTAIMAGES");
89 }
90
91 function update_images()
92 {
93     // $("imgct").innerHTML = "Immagini caricate "+g_preload_imgsz_arr[g_imgct]+"%.";
94     if (g_imgct < g_preload_img_arr.length)
95         setTimeout(preload_images, 100, g_preload_img_arr, g_imgct);
96     g_imgct++;
97     // $("imgct").innerHTML += "U";
98 }
99
100 function preload_images(arr,idx)
101 {
102     var im = new Image;
103     
104     // $("imgct").innerHTML = "Stiamo caricando "+arr[idx]+"%.<br>";
105     im.onload =   update_images;
106     im.onerror =  error_images;
107     im.onabort =  abort_images;
108     im.onunload = unload_images;
109     im.onreset =  reset_images;
110     im.src =      arr[idx];
111     // $("imgct").innerHTML += "P";
112 }
113
114 function safestatus(a)
115 {
116     try{
117         return (a.status);
118     } catch(b)
119         { return (-1); }
120 }
121
122 function createXMLHttpRequest() {
123     try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
124     try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
125     try { return new XMLHttpRequest();                   } catch(e) {}
126     alert("XMLHttpRequest not supported");
127     return null;
128 }
129
130 function send_mesg(mesg)
131 {
132     var xhr_wr = createXMLHttpRequest();
133     // xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+encodeURIComponent(mesg), true);
134     xhr_wr.open('GET', 'index_wr.php?sess='+sess+'&mesg='+mesg, true);
135     xhr_wr.onreadystatechange = function() { return; };
136     xhr_wr.send(null);
137
138 }
139
140 /* Stat: CHAT and TABLE */
141
142 function chatt_checksend(obj,e)
143 {
144     var keynum;
145     var keychar;
146     var numcheck;
147
148     if(window.event) { // IE
149         keynum = e.keyCode;
150     }
151     else if(e.which) { // Netscape/Firefox/Opera
152         keynum = e.which;
153     }
154     // alert("OBJ: "+obj);
155     if (keynum == 13 && obj.value != "") { // Enter
156         act_chatt(obj.value);
157         obj.value = "";
158     }
159 }
160 function act_chatt(value)
161 {
162     send_mesg("chatt|"+encodeURIComponent(value));
163     /*
164     obj.disabled = true;
165     obj.value = "";
166     obj.disabled = false;
167     obj.focus();
168     */
169     return false;
170 }
171
172 /* Stat: ROOM */
173 function act_sitdown(table)
174 {
175     send_mesg("sitdown|"+table);
176 }
177
178 function act_wakeup()
179 {
180     send_mesg("wakeup");
181 }
182
183 /* Stat: TABLE  Subst: ASTA */
184 function act_asta(card,pnt)
185 {
186     send_mesg("asta|"+card+"|"+pnt);
187 }
188
189 function act_choose(card)
190 {
191     // alert("sitdown");
192     send_mesg("choose|"+card);
193 }
194
195 /* Stat: TABLE  Subst: GAME */
196 function act_play(card,x,y)
197 {
198     // alert("sitdown");
199     send_mesg("play|"+card+"|"+x+"|"+y);
200 }
201
202 function act_tableinfo()
203 {
204     send_mesg("tableinfo");
205 }
206
207 function act_help()
208 {
209     send_mesg("help");
210 }
211
212 function safelogout()
213 {
214     var res;
215
216     res = window.confirm("Sei sicuro di volere abbandonare la partita?");
217     if (res)
218         act_logout();
219 }
220 function act_logout()
221 {
222     send_mesg("logout");
223 }
224
225 function act_reload()
226 {
227     window.onunload = null;
228     document.location.reload();
229 }
230
231 function act_shutdown()
232 {
233     var c = 0;
234
235     send_mesg("shutdown");
236     while (xhr_wr.readyState != 4)
237         c++;
238 }
239
240 function act_preout()
241 {
242     act_logout();
243 }
244
245 function postact_logout()
246 {
247     // alert("postact_logout");
248     try { 
249         xhr_rd.abort();
250     } catch (e) {}
251
252     // eraseCookie("sess");
253     document.location.assign("index.php");
254 }
255
256 /*
257   function slowimg(img,x1,y1,deltat,free,action,srcend)
258   img    - image to move
259   x1,y1  - destination coords
260   deltat - time for each frame (in msec)
261   free   - when the release the local block for other operations (range: 0 - 1)
262   action - function to run when the image is moved
263   srcend - image to switch when the image is moved
264 */
265
266 function sleep(st, delay)
267 {
268     // alert("LOC_NEW PRE: "+st.st_loc_new);
269
270     st.st_loc_new++;
271
272     setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; }},
273                delay, st);
274 }
275
276 function slowimg(img,x1,y1,deltat,free,action,srcend) {
277     this.img = img;
278
279     this.x0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("left"));
280     // alert("img.x0 = "+this.x0);
281     this.y0  = parseInt(document.defaultView.getComputedStyle(this.img, "").getPropertyValue("top"));
282     this.x1  = x1;
283     this.y1  = y1;
284     this.deltat = deltat;
285     this.free = free;
286     this.action = action;
287     this.srcend = srcend;
288 }
289
290 slowimg.prototype = {
291     img: null, 
292     st: null,
293     x0: 0,
294     y0: 0,
295     x1: 0,
296     y1: 0,
297     dx: 0,
298     dy: 0,
299     free: 0,
300     step_n:    0,
301     step_cur:  0,
302     step_free: 0,
303     time:      0,
304     deltat:   40,
305     tout: 0,
306     action: null,
307     srcend: null,
308     
309     setstart: function(x0,y0)
310     {
311         this.x0 = x0;
312         this.y0 = y0;
313     },
314     
315     setaction: function(act)
316     {
317         this.action = act;
318     },
319     
320     settime: function(time) 
321     {
322         this.time = time;
323         this.step_n = parseInt(time / this.deltat);
324         this.dx = (this.x1 - this.x0) / this.step_n;
325         this.dy = (this.y1 - this.y0) / this.step_n;
326         if (this.step_n * this.deltat == time) {
327             this.step_n--;
328         }
329         this.step_free = parseInt(this.step_n * this.free);
330     },
331     
332     start: function(st)
333     {
334         // $("logz").innerHTML += "               xxxxxxxxxxxxxxxxxxxxxSTART<br>";
335         this.st = st;
336         this.st.st_loc_new++;
337         
338         this.img.style.visibility = "visible";
339         setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
340     },
341     
342     animate: function()
343     {
344         // $("log").innerHTML = "Val " + this.step_cur + " N: " + this.step_n + "<br>";
345         if (this.step_cur == 0) {
346             var date = new Date();
347             // $("logz").innerHTML = "Timestart: " + date + "<br>";
348         }
349         if (this.step_cur <= this.step_n) {
350             this.img.style.left = this.x0 + this.dx * this.step_cur;
351             this.img.style.top  = this.y0 + this.dy * this.step_cur;
352             this.step_cur++;
353             setTimeout(function(obj){ obj.animate(); }, this.deltat, this);
354             if (this.step_cur == this.step_free && this.st != null) {
355                 if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
356                     // alert("QUI1  " + this.step_cur + "  ZZ  "+  this.step_free);
357                     this.st.st_loc++;
358                     this.st = null;
359                 }
360             }
361         }
362         else {
363             this.img.style.left = this.x1;
364             this.img.style.top  = this.y1;
365             // $("logz").innerHTML += "xxxxxxxxxxxxxxxCLEAR<br>";
366             var date = new Date();
367             // $("logz").innerHTML += "Timestop: " + date + "<br>";
368             if (this.st != null && this.st.st_loc < this.st.st_loc_new) {
369                 // alert("QUI2");
370                 this.st.st_loc++;
371                 this.st = null;
372             }
373             if (this.action != null) {
374                 eval(this.action);
375             }
376             if (this.srcend != null) {
377                 this.img.src = this.srcend;
378             }
379         }
380     }
381 }
382
383 var asta_xarr = new Array(0,66,133);
384
385 /* TODO: impostare gli onclick */
386 function dispose_asta(idx, pnt)
387 {
388     var i, btn, pass;
389     
390     var btn;
391     for (i = 0 ; i < 10 ; i++) {
392         btn = $("asta"+i);
393         if (i < idx) {
394             btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
395             pass = -1;
396         }
397         else {
398             btn.src = "img/asta"+i+(pnt >= 0 ? "" : "_ro")+".png";
399             pass = i;
400         }
401         if (i < 19)
402             btn.style.left = asta_xarr[i % 3];
403         else
404             btn.style.left = asta_xarr[(i+1) % 3];
405         
406         btn.style.top  = parseInt(i / 3) * 50+1;
407         // btn.style.visibility  = "visible";
408         
409         if (pnt >= 0)
410             eval("btn.onclick = function () { act_asta("+pass+",61); }");
411         else
412             btn.onclick = null;
413     }
414     
415     
416     btn = $("astaptdiv");
417     btn.style.left = asta_xarr[i % 3];
418     btn.style.top = parseInt(i / 3) * 50;
419     // btn.style.visibility  = "visible";
420     
421     btn = $("astapt");
422     var rpnt = (pnt < 0 ? -pnt : pnt);
423     btn.value = (rpnt < 61 ? 61 : (rpnt > 120 ? 120 : rpnt));
424     
425     btn = $("astaptsub");
426     btn.style.left = asta_xarr[i % 3];
427     btn.style.top = 25 + parseInt(i / 3) * 50;;
428     btn.src = "img/astaptsub"+(pnt >= 0 ? "" : "_ro")+".png";
429     // btn.style.visibility  = "visible";
430     if (pnt >= 0)
431         btn.onclick = function () { act_asta(9,$("astapt").value); };
432     else
433         btn.onclick = null;
434     
435     i+=1;
436     btn = $("astapasso2");
437     btn.style.left = asta_xarr[i % 3];
438     btn.style.top = parseInt(i / 3) * 50;;
439     btn.src = "img/astapasso"+(pnt >= 0 ? "" : "_ro")+".png";
440     // btn.style.visibility  = "visible";
441     if (pnt >= 0)
442         btn.onclick = function () { act_asta(-1,0); };
443     else
444         btn.onclick = null;
445     $("asta").style.visibility = "visible";
446 }
447
448 function hide_asta()
449 {
450     $("asta").style.visibility = "hidden"; 
451 }
452
453 function notify(st, ancestor, text, tout, butt)
454 {
455     var clo, box;
456     var t = this;
457     
458     this.st = st;
459
460     this.ancestor = ancestor;
461     
462     this.st.st_loc_new++;
463
464     clo = document.createElement("input");
465     clo.type = "submit";
466     clo.className = "button";
467     clo.value = butt;
468     clo.obj = this;
469     clo.onclick = this.input_hide;
470
471     box = document.createElement("div");
472     box.className = "notify";
473     box.innerHTML = text;
474     box.style.zIndex = 200;
475     box.appendChild(clo);
476     box.style.visibility = "visible";
477     
478     this.notitag = box;
479     
480     this.ancestor.appendChild(box);
481     
482     this.toutid = setTimeout(function(obj){ obj.unblock(); }, tout, this);
483 }
484
485 notify.prototype = {
486     ancestor: null,
487     st: null,
488     notitag: null,
489     toutid: null,
490     
491     unblock: function()
492     {
493         if (this.st.st_loc < this.st.st_loc_new) {
494             this.st.st_loc++;
495         }
496     },
497     
498     hide: function()
499     {
500         clearTimeout(this.toutid);
501         this.ancestor.removeChild(this.notitag);
502         this.unblock();
503     },
504
505     input_hide: function()
506     {
507         clearTimeout(this.obj.toutid);
508         this.obj.ancestor.removeChild(this.obj.notitag);
509         this.obj.unblock();
510     }
511 }
512         
513
514 function $(id) { 
515     return document.getElementById(id); 
516 }
517
518 function globst() {
519     this.st = -1;
520     this.st_loc = -1;
521     this.st_loc_new = -1;
522     this.comms  = new Array;
523 }
524
525
526
527 function remark_step()
528 {
529     var ct = $("remark").l_remct;
530     
531     if (ct != 0) {
532         ct++;
533         if (ct > 2)
534             ct = 1;
535         $("remark").className = "remark"+ct;
536         $("remark").l_remct = ct;
537         setTimeout(remark_step,500);
538     }
539     else
540         $("remark").className = "remark0";
541     
542     return;
543 }
544
545 function remark_on()
546 {
547     if ($("remark").l_remct == 0) {
548         $("remark").l_remct = 1;
549         setTimeout(remark_step,500);
550     }
551 }
552
553 function remark_off()
554 {
555     $("remark").l_remct = 0;
556     $("remark").className = "remark0";
557 }
558
559
560 function choose_seed(card)
561 {
562     var i;
563
564     $("chooseed").style.visibility = "visible";
565     for (i = 0 ; i < 4 ; i++) {
566         $("seed"+i).src = "img/"+i+""+card+".png";
567         seed=$("seed"+i);
568         eval("seed.onclick = function () { act_choose("+i+""+card+"); };");
569     }
570 }
571
572 function set_names(so,ea,ne,nw,we)
573 {
574 //    alert("EA: "+ea);
575     $("name").innerHTML = so; 
576     $("name").title = so; 
577     $("name_ea").innerHTML = ea;
578     $("name_ea").title = ea;
579     $("name_ne").innerHTML = ne;
580     $("name_ne").title = ne;
581     $("name_nw").innerHTML = nw;
582     $("name_nw").title = nw;
583     $("name_we").innerHTML = we;
584     $("name_we").title = we;
585
586     return;
587 }
588
589 var astat_suffix = new Array("","_ea","_ne","_nw","_we");
590
591 function show_astat(zer,uno,due,tre,qua)
592 {
593     var astat = new Array(zer,uno,due,tre,qua);
594
595     for (i = 0 ; i < PLAYERS_N ; i++) {
596         idx = (PLAYERS_N + i - table_pos) % PLAYERS_N;
597
598         if (astat[i] == -2) {
599             $("public"+astat_suffix[idx]).style.visibility = "hidden";
600         }
601         else if (astat[i] == -1) {
602             $("public"+astat_suffix[idx]).style.visibility = "visible";
603             $("pubacard"+astat_suffix[idx]).src = "img/astapasso.png";
604             $("pubapnt"+astat_suffix[idx]).innerHTML = "";
605             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
606         }
607         else if (astat[i] <= 10) {
608             $("public"+astat_suffix[idx]).style.visibility = "visible";
609             $("pubacard"+astat_suffix[idx]).src = "img/asta"+astat[i]+".png";
610             $("pubapnt"+astat_suffix[idx]).style.visibility = "hidden";
611         }
612         else if (astat[i] <= 120) {
613             $("public"+astat_suffix[idx]).style.visibility = "visible";
614             $("pubacard"+astat_suffix[idx]).src = "img/asta9.png";
615             $("pubapnt"+astat_suffix[idx]).style.visibility = "inherit"; // XXX VISIBLE
616             $("pubapnt"+astat_suffix[idx]).innerHTML = astat[i];
617         }
618     }
619 }
620
621 var fin = 0;
622
623 function table_init() {
624     var sux = new Array("", "_ea", "_ne", "_nw", "_we");
625
626     remark_off();
627
628     $("asta").style.visibility = "hidden";
629     $("caller").style.visibility = "hidden";
630     show_astat(-2,-2,-2,-2,-2);
631
632     for (i=0 ; i < 8 ; i++) {
633         Drag.init($("card" + i), card_mouseup_cb);
634         for (e = 0 ; e < PLAYERS_N ; e++)
635             $("card"+sux[e]+i).style.visibility = "hidden";
636     }
637     for (i=0 ; i < PLAYERS_N ; i++) {
638         $("takes"+sux[i]).style.visibility = "hidden";
639         }
640
641     for (i = 0 ; i < 8 ; i++) {
642         cards_pos[i] = i;
643         cards_ea_pos[i] = i;
644         cards_ne_pos[i] = i;
645         cards_nw_pos[i] = i;
646         cards_we_pos[i] = i;
647     }
648
649 }
650   
651
652
653 var chatt_lines = new Array();
654 var chatt_lines_n = 0;
655
656 /* PRO CHATT */
657 function chatt_sub(name,str)
658 {
659   // alert("ARRIVA NAME: "+ name + "  STR:"+str);
660   if (chatt_lines_n == 20) {
661     $("txt").innerHTML = "";
662     for (i = 0 ; i < 19 ; i++) {
663       chatt_lines[i] = chatt_lines[i+1];
664       $("txt").innerHTML += chatt_lines[i];
665     }
666     chatt_lines[i] = "<b>"+name+"</b> "+str+ "<br>";
667     $("txt").innerHTML += chatt_lines[i];
668   }
669   else {
670     chatt_lines[chatt_lines_n] = "<b>"+name+"</b> "+str+ "<br>";
671     $("txt").innerHTML += chatt_lines[chatt_lines_n];
672     chatt_lines_n++;
673   }
674   $("txt").innerHTML;
675   $("txt").scrollTop = 10000000;
676 }
677
678 /*
679  *  GESTIONE DEI COOKIES
680  */
681 function createCookie(name,value,hours,path) {
682         if (hours) {
683                 var date = new Date();
684                 date.setTime(date.getTime()+(hours*60*60*1000));
685                 var expires = "; expires="+date.toGMTString();
686         }
687         else var expires = "";
688         document.cookie = name+"="+value+expires+"; path="+path;
689 }
690
691 function readCookie(name) {
692         var nameEQ = name + "=";
693         var ca = document.cookie.split(';');
694         for(var i=0;i < ca.length;i++) {
695                 var c = ca[i];
696                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
697                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
698         }
699         return null;
700 }
701
702 function eraseCookie(name) {
703         createCookie(name,"",-1);
704 }
705
706 var onunload_times = 0;
707
708
709 function onunload_cb () {
710     var u = 0;
711     if (onunload_times == 0) {
712         var res = window.confirm("    Vuoi veramente abbandonare la briscola ?\n(clicca annulla o cancel se vuoi ricaricare la briscola)");
713         if (res == true) {
714             the_end = true; 
715             act_shutdown();
716             while (1) 
717                 u++;
718         }
719         else {
720             try {
721                 location = self.location;
722             } catch (e) {
723                 alert("Ripristino della briscola fallito, per non perdere la sessione ricaricare la pagina manualmente.");
724             }
725         }
726         onunload_times++;
727     }
728     
729     return(false);
730 }
731
732
733 function room_checkspace(emme,tables,inpe)
734 {
735     nome = "<b>";
736     for (i = 0 ; i < emme ; i++) 
737         nome += "m";
738     nome += "</b>";
739
740     alta = "";
741     for (i = 0 ; i < 5 ; i++) 
742         alta += nome+"<br>";
743
744     for (i = 0 ; i < tables ; i++) {
745         $("table"+i).innerHTML = alta;
746         $("table_act"+i).innerHTML = "<input type=\"button\" class=\"button\" name=\"xhenter"+i+"\"  value=\"Mi siedo.\" onclick=\"act_sitdown(1);\">";
747         }
748
749     stand = "<table class=\"table_standup\"><tbody><tr>";
750     for (i = 0 ; i < inpe ; i++) {
751         stand += "<td class=\"td_standup\">"+nome+"</td>";
752         if ((i+1) % 4 == 0) {
753             stand += "</tr><tr>";
754         }
755     }
756     stand += "</tr>";
757     $("standup").innerHTML = stand;
758
759     $("esco").innerHTML = "<input name=\"logout\" type=\"button\" value=\"Esco.\" onclick=\"window.onunload = null; act_logout();\" type=\"button\">";
760 }