Browse code

Corrección de errores, principalmente en la pérdida de 'words' al reiniciar una partida

NikaZhenya authored on 14/09/2018 15:22:46
Showing 4 changed files
... ...
@@ -1,4 +1,4 @@
1
-var type  = 'arcade',
1
+var type  = 'story',
2 2
     lang  = 'es',
3 3
     debug = false,
4 4
     timer
... ...
@@ -141,7 +141,10 @@ La función `foo.wipe()` solo tiene un parámetro necesario disponible:
141 141
 
142 142
 Varios datos del juego se guardan en [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).
143 143
 
144
-* `story || arcade`. `Object` que guarda el estado de la partida.
145
-* `story_bombs_bonus || arcade_bombs_bonus`. `Array` que guarda un mapa de las bombas o bonos ya activados.
146
-* `story_words || arcade_words`. `Object` que guarda las palabras encontradas, su cantidad y la cantidad de todas las palabras de una modalidad.
147
-* `story_highest_scores || arcade_highest_scores`. `Array` que guarda los mayores puntajes de cada modalidad.
144
+Se guardan según el idioma, con la coletilla `_lang` donde `lang` es igual
145
+al idioma en juego, como `_es`.
146
+
147
+* `story_es || arcade_es`. `Object` que guarda el estado de la partida.
148
+* `story_bombs_bonus_es || arcade_bombs_bonus_es`. `Array` que guarda un mapa de las bombas o bonos ya activados.
149
+* `story_words_es || arcade_words_es`. `Object` que guarda las palabras encontradas, su cantidad y la cantidad de todas las palabras de una modalidad.
150
+* `story_highest_scores_es || arcade_highest_scores_es`. `Array` que guarda los mayores puntajes de cada modalidad.
... ...
@@ -32,7 +32,7 @@ var foo = (function() {
32 32
     // Crea un conjunto a partir de la diferencia de dos conjuntos; viene de: https://stackoverflow.com/questions/18998767/remove-contents-of-an-array-from-another-array#18998911
33 33
     Array.prototype.diff = function (a) {
34 34
 
35
-        function computate (a, b) {
35
+        function compute (a, b) {
36 36
             return a.filter(function (v) {
37 37
                 return b.indexOf(v) == -1
38 38
             })
... ...
@@ -41,9 +41,9 @@ var foo = (function() {
41 41
         if (this.length == 0)
42 42
             return a
43 43
         else if (this.length > a.length)
44
-            return computate(this, a)
44
+            return compute(this, a)
45 45
         else
46
-            return computate(a, this)
46
+            return compute(a, this)
47 47
     }
48 48
 
49 49
     // Elimina duplicados; basado en: https://stackoverflow.com/questions/4833651/javascript-array-sort-and-unique#answer-7076202
... ...
@@ -295,7 +295,7 @@ var foo = (function() {
295 295
 
296 296
             // Analiza si ejecutar las funciones de la bomba o el bono
297 297
             function trigger_bomb_bonus (el, i) {
298
-                var bomb_bonus   = game_opt.type + '_bombs_bonus',
298
+                var bomb_bonus   = game_opt.type + '_bombs_bonus_' + game_opt.lang,
299 299
                     picked       = el.getAttribute('data-picked'),
300 300
                     fn           = picked == 0 ? el.getAttribute('data-on_bomb') : el.getAttribute('data-on_bonus'),
301 301
                     line_index   = get_index(el.parentElement.previousSibling, 0),
... ...
@@ -617,7 +617,7 @@ var foo = (function() {
617 617
 
618 618
     // Analiza todo lo relativo a las palabras
619 619
     function compute_words () {
620
-        var sav_words = localStorage.getItem(game_opt.type + '_words') != null ? JSON.parse(localStorage.getItem(game_opt.type + '_words')) : {total: 0, current: 0, words: []},
620
+        var sav_words = localStorage.getItem(game_opt.type + '_words_' + game_opt.lang) != null ? JSON.parse(localStorage.getItem(game_opt.type + '_words_' + game_opt.lang)) : {total: 0, current: 0, words: []},
621 621
             lis_words = game_opt.data.words,                         // Todas las palabras de la modalidad
622 622
             cur_words = game_opt.words.uniq(),                       // Palabras únicas encontradas
623 623
             new_words = sav_words.words.diff(cur_words),             // Palabras nuevas
... ...
@@ -635,7 +635,7 @@ var foo = (function() {
635 635
             sav_words.words   = fin_words.slice()
636 636
 
637 637
             // Guarda las palabras
638
-            localStorage.setItem(game_opt.type + '_words', JSON.stringify(sav_words))
638
+            localStorage.setItem(game_opt.type + '_words_' + game_opt.lang, JSON.stringify(sav_words))
639 639
 
640 640
             // Se eliminan las palabras
641 641
             game_opt.words = null
... ...
@@ -644,7 +644,7 @@ var foo = (function() {
644 644
 
645 645
     // Analiza si hay nuevos récords
646 646
     function highest_scores () {
647
-        var hig_score  = localStorage.getItem(game_opt.type + '_highest_scores'),
647
+        var hig_score  = localStorage.getItem(game_opt.type + '_highest_scores_' + game_opt.lang),
648 648
             new_score  = foo.data.score,
649 649
             new_words  = foo.data.words
650 650
 
... ...
@@ -673,7 +673,7 @@ var foo = (function() {
673 673
                 hig_score = hig_score.slice(0, game_opt.top)
674 674
 
675 675
             // Guarda los récords
676
-            localStorage.setItem(game_opt.type + '_highest_scores', JSON.stringify(hig_score))
676
+            localStorage.setItem(game_opt.type + '_highest_scores_' + game_opt.lang, JSON.stringify(hig_score))
677 677
         }
678 678
     }
679 679
 
... ...
@@ -831,7 +831,7 @@ var foo = (function() {
831 831
                         result = relabel(JSON.parse(this.responseText))
832 832
 
833 833
                         // Guardado local del juego
834
-                        localStorage.setItem(game_opt.type, result)
834
+                        localStorage.setItem(game_opt.type + '_' + game_opt.lang, result)
835 835
 
836 836
                         // Manda a construir el juego
837 837
                         build()
... ...
@@ -1092,7 +1092,7 @@ var foo = (function() {
1092 1092
 
1093 1093
             // Construye el juego
1094 1094
             function build_game (parent, block_size, lines_before) {
1095
-                game_opt.data = JSON.parse(localStorage[opt.type])
1095
+                game_opt.data = JSON.parse(localStorage[opt.type + '_' + game_opt.lang])
1096 1096
 
1097 1097
                 // Construye cada letra
1098 1098
                 function build_letter (div_parent, letter, index) {
... ...
@@ -1247,11 +1247,11 @@ var foo = (function() {
1247 1247
 
1248 1248
                             // Se fija variable
1249 1249
                             if (div.id.split('-')[1] == 'score') {
1250
-                                game_opt.score     = JSON.parse(localStorage[opt.type])[div.id.split('-')[1]]
1250
+                                game_opt.score     = JSON.parse(localStorage[opt.type + '_' + game_opt.lang])[div.id.split('-')[1]]
1251 1251
                                 foo.data.score = game_opt.score
1252 1252
                                 p.innerHTML    = game_opt.score
1253 1253
                             } else {
1254
-                                game_opt.time      = JSON.parse(localStorage[opt.type])[div.id.split('-')[1]]
1254
+                                game_opt.time      = JSON.parse(localStorage[opt.type + '_' + game_opt.lang])[div.id.split('-')[1]]
1255 1255
                                 foo.data.time  = game_opt.time
1256 1256
                                 p.innerHTML    = game_opt.time
1257 1257
                             }
... ...
@@ -1393,7 +1393,7 @@ var foo = (function() {
1393 1393
             foo.data.finished   = game_opt.finished
1394 1394
 
1395 1395
             // La primera vez obtiene la información del JSON
1396
-            if (localStorage.getItem(game_opt.type) == null) {
1396
+            if (localStorage.getItem(game_opt.type + '_' + game_opt.lang) == null) {
1397 1397
 
1398 1398
                 // No procede si el tipo es erróneo
1399 1399
                 if (opt.type != 'story' && opt.type != 'arcade')
... ...
@@ -1528,10 +1528,11 @@ var foo = (function() {
1528 1528
             // Obtiene los elementos generales del juego
1529 1529
             var foo_game  = document.getElementById('foo-game'),
1530 1530
                 json_game = {
1531
-                    type:         game_opt.type,
1532
-                    score:        parseInt(game_opt.score),
1533
-                    timer:        parseInt(game_opt.time),
1534
-                    content:      []
1531
+                    type   :    game_opt.type,
1532
+                    score  :    parseInt(game_opt.score),
1533
+                    timer  :    parseInt(game_opt.time),
1534
+                    words  :    game_opt.data.words,
1535
+                    content:    []
1535 1536
                 }
1536 1537
 
1537 1538
             // Iteración para obtener los elementos de cada línea
... ...
@@ -1601,7 +1602,7 @@ var foo = (function() {
1601 1602
             }
1602 1603
 
1603 1604
             // Guardado local del juego
1604
-            localStorage.setItem(game_opt.type, JSON.stringify(json_game))
1605
+            localStorage.setItem(game_opt.type + '_' + game_opt.lang, JSON.stringify(json_game))
1605 1606
 
1606 1607
             // Corre función cuando ya está listo
1607 1608
             exec_fn(opt.on_finish)
... ...
@@ -1639,13 +1640,13 @@ var foo = (function() {
1639 1640
                 }
1640 1641
 
1641 1642
                 // Elimina el juego y la lista de bombas y bonos activados
1642
-                localStorage.removeItem(o)
1643
-                localStorage.removeItem(o + '_bombs_bonus')
1643
+                localStorage.removeItem(o + '_' + game_opt.lang)
1644
+                localStorage.removeItem(o + '_bombs_bonus_' + game_opt.lang)
1644 1645
 
1645 1646
                 // Con esto se borra todo lo relacionado al juego
1646 1647
                 if (all) {
1647
-                    localStorage.removeItem(o + '_words')
1648
-                    localStorage.removeItem(o + '_highest_scores')
1648
+                    localStorage.removeItem(o + '_words_' + game_opt.lang)
1649
+                    localStorage.removeItem(o + '_highest_scores_' + game_opt.lang)
1649 1650
                 }
1650 1651
             }
1651 1652
 
... ...
@@ -1 +1 @@
1
-var foo=(function(){var lang_loading='Loading…',lang_load_error='Data not found.';var game_opt;var touch_old=null,touch_timer,game_timer,bar_timer;Array.prototype.sample=function(){return this[Math.floor(Math.random()*this.length)]};Array.prototype.capitalize=function(){var fin=[];for(i=0;i<this.length;i+=1){fin.push(this[i].charAt(0).toUpperCase()+this[i].slice(1))}return fin};Array.prototype.diff=function(a){function computate(a,b){return a.filter(function(v){return b.indexOf(v)==-1})}if(this.length==0){return a}else if(this.length>a.length){return computate(this,a)}else{return computate(a,this)}};Array.prototype.uniq=function(){var fin=this.sortHard();return fin.filter(function(el,i,a){return!i||el.toLowerCase()!=a[i-1].toLowerCase()})};Array.prototype.sortHard=function(){var tmp=[],fin=[];for(i=0;i<this.length;i+=1){tmp.push({n:this[i].transliterate(),r:this[i]})}tmp=tmp.slice(0).sort(function(a,b){return(a.n>b.n)?1:(a.n<b.n)?-1:0});for(j=0;j<tmp.length;j+=1){fin.push(tmp[j].r)}return fin};Array.prototype.shuffle=function(){var x=this.length,j,temp;if(x==0){return this}while(x-=1){j=Math.floor(Math.random()*(x+1));temp=this[x];this[x]=this[j];this[j]=temp}return this};String.prototype.transliterate=function(){var translate_re=/[ÁÉÍÓÚÜÑáéíóúüñ]/g,translate={'Á':'A','É':'E','Í':'I','Ó':'O','Ú':'U','Ü':'U','Ñ':'N','á':'a','é':'e','í':'i','ó':'o','ú':'u','ü':'u','ñ':'n'};return(this.replace(translate_re,function(match){return translate[match]}))};function scrollTo(element,to,duration){if(duration<=0){return}var difference=to-element.scrollTop,perTick=difference/duration*10;setTimeout(function(){element.scrollTop=element.scrollTop+perTick;if(element.scrollTop===to){return}scrollTo(element,to,duration-10)},10)}function resize_bar(increment=0){var div_pbar=document.getElementById('foo-bar'),total_width=document.body.clientWidth,current_width=div_pbar.clientWidth,total_lines=game_opt.data.content.length-(game_opt.size[1]-1),line_width=total_width/total_lines,new_width;function move(){if(current_width>=new_width){clearInterval(bar_timer);bar_timer=0}else{if(increment){current_width+=10}else{current_width++}div_pbar.style.width=current_width+'px'}}if(increment==0){var lines=document.getElementsByClassName('foo-line'),last_lt=lines[lines.length-1].getElementsByClassName('foo-letter')[0].getAttribute('data-status'),total=0;for(i=0;i<lines.length;i+=1){if(lines[i].getAttribute('data-visible')=='true'){total+=1}}if(total_lines==total-(game_opt.size[1]-1)&&(last_lt=='correct'||last_lt=='incorrect')){increment=(total-(game_opt.size[1]-1))}else{increment=(total-(game_opt.size[1]-1))-1}}new_width=current_width+(line_width*increment);if(typeof bar_timer==='undefined'||bar_timer==0){bar_timer=setInterval(move,1)}}function validate_array(array,min_length,min_quantity,label){if(array.length!=min_length){throw new Error(label+" array requires only "+min_length+" elements.");}for(i=0;i<array.length;i+=1){if(typeof array[i]!='number'||array[i]%1!==0||array[i]<min_quantity){throw new Error(label+" array's elements have to be integers equal or greater than "+min_quantity+".")}}}function exec_fn(e){if(typeof e==='string'&&typeof window[e]==='function'){window[e]()}else if(typeof e==='function'){e()}}function new_time(extra=0){var div_timer=document.getElementById('foo-timer-total'),qnt_timer=parseInt(div_timer.innerHTML);if(extra!=0){qnt_timer=qnt_timer+extra;}else{if(game_opt.type=='story'){qnt_timer=qnt_timer+1}else{qnt_timer=qnt_timer-1}}game_opt.time=qnt_timer;foo.data.time=game_opt.time;div_timer.innerHTML=qnt_timer;if(qnt_timer==10&&game_opt.type=='arcade'){exec_fn(game_opt.ar_on_ten_seconds);}if(qnt_timer<=0&&game_opt.type=='arcade'){lose();}exec_fn(game_opt.on_new_time)}function new_score(int){var div_score=document.getElementById('foo-score-total'),qnt_score=parseInt(div_score.innerHTML);game_opt.past_score=qnt_score;foo.data.past_score=game_opt.past_score;qnt_score=qnt_score+int;game_opt.score=qnt_score;foo.data.score=game_opt.score;div_score.innerHTML=qnt_score;if(qnt_score<0){lose();}exec_fn(game_opt.on_new_score)}function on_touchstart(e){if(!game_opt.win){var element=e.currentTarget,p=element.getElementsByTagName('p')[0],type=element.getAttribute('data-type'),status=element.getAttribute('data-status'),correct=element.getAttribute('data-correct'),picked=element.getAttribute('data-picked'),picked_i=element.getAttribute('data-picked_i'),letters=element.getAttribute('data-letters').split('¤'),chances=element.getAttribute('data-chances'),loops=element.getAttribute('data-loops'),on_first_change=element.getAttribute('data-on_first_change'),on_change=element.getAttribute('data-on_change'),on_loop=element.getAttribute('data-on_loop'),on_bomb=element.getAttribute('data-on_bomb'),on_bonus=element.getAttribute('data-on_bonus');game_opt.currentTarget=element;game_opt.letter=element;foo.data.letter=game_opt.letter;function draw_label(l){var d_add=document.getElementById('foo-adds'),p_add=document.createElement('p');if(document.getElementById('foo-token')){document.getElementById('foo-token').parentElement.removeChild(document.getElementById('foo-token'));}d_add.style.display='inherit';p_add.id='foo-token';p_add.style.cssText='width:'+d_add.clientHeight+'px;height:'+d_add.clientHeight+'px;font-size:'+((parseInt(game_opt.game_fontSize)*2)+game_opt.game_fontSize.replace(/\d+/,''))+';background:'+game_opt.lt_bkcolor_enable+';border-radius:'+(d_add.clientHeight/5)+'px;margin:0 auto;display:flex;align-items:center;justify-content:center;border:1px solid #ccc;-webkit-box-shadow: 0 0 25px 0 rgba(0,0,0,0.25);-moz-box-shadow: 0 0 25px 0 rgba(0,0,0,0.25);box-shadow: 0 0 25px 0 rgba(0,0,0,0.25);';p_add.innerHTML=l;d_add.appendChild(p_add)}function trigger_bomb_bonus(el,i){var bomb_bonus=game_opt.type+'_bombs_bonus',picked=el.getAttribute('data-picked'),fn=picked==0?el.getAttribute('data-on_bomb'):el.getAttribute('data-on_bonus'),line_index=get_index(el.parentElement.previousSibling,0),letter_index=get_index(el.previousSibling,0),position=line_index+'-'+letter_index+'-'+i,valid=true,final_pos=[];function get_index(e,i){while(e!=null){e=e.previousSibling;i+=1}return i}if(localStorage.getItem(bomb_bonus)==null){final_pos.push(position);}else{var bomb_bonus_array=JSON.parse(localStorage[bomb_bonus]);for(j=0;j<bomb_bonus_array.length;j+=1){if(bomb_bonus_array[j]==position){valid=false}}bomb_bonus_array.push(position);final_pos=bomb_bonus_array.slice()}if(valid){exec_fn(fn);if(picked==0){new_score(-3)}else{new_score(3);}localStorage.setItem(bomb_bonus,JSON.stringify(final_pos));return '<span style="font-size:'+game_opt.lt_chance_fontSize+';vertical-align:.25em;">'+(picked=='0'?'-':'+')+'</span>3'}else{return '<span style="color:#999"><span style="font-size:'+game_opt.lt_chance_fontSize+';vertical-align:.25em;">'+(picked=='0'?'-':'+')+'</span>3</span>'}}clearInterval(touch_timer);if(touch_old!=element&&touch_old!=null){compute_letter(touch_old);}if(chances>0&&status=='enable'){exec_fn(on_change);if(loops==0&&picked==letters[0]){exec_fn(on_first_change);}for(i=0;i<letters.length;i+=1){if(i==picked_i){if(i!=letters.length-1){picked_i=i+1}else{picked_i=0;loops=parseInt(loops)+1;element.setAttribute('data-loops',loops);exec_fn(on_loop)}picked=letters[picked_i];element.setAttribute('data-picked',picked);element.setAttribute('data-picked_i',picked_i);if(picked!=0&&picked!=1){label=picked}else{label=trigger_bomb_bonus(element,i+1);}draw_label(label);p.innerHTML=label;break}}}touch_old=element}}function on_touchend(e){var element=e.currentTarget;if(!game_opt.win){if(element==game_opt.currentTarget){touch_timer=setInterval(compute_letter,500,this)}else{touch_timer=setInterval(compute_letter,500,game_opt.currentTarget)}}}function compute_letter(e){var chances=e.getAttribute('data-chances'),status=e.getAttribute('data-status'),d_add=document.getElementById('foo-adds');clearInterval(touch_timer);touch_old=null;d_add.style.display='none';if(document.getElementById('foo-token')){document.getElementById('foo-token').parentElement.removeChild(document.getElementById('foo-token'));}if(chances>0&&status=='enable'){e.setAttribute('data-chances',chances-1);e.getElementsByTagName('p')[1].innerHTML=chances-1;if(chances-1==0){e.setAttribute('data-status','disable');e.style.background=game_opt.lt_bkcolor_disable;e.style.cssText+='-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;';e_new=e.cloneNode(true);e.parentNode.replaceChild(e_new,e);e=e_new}}compute_line(e.parentNode);foo.save()}function compute_line(e){var foo_game=document.getElementById('foo-game'),on_line_completed=e.getAttribute('data-on_line_completed'),on_line_correct=e.getAttribute('data-on_line_correct'),on_line_incorrect=e.getAttribute('data-on_line_incorrect'),childrens=e.childNodes,disable=true,correct=true,next=true,score=0,bar_resize=1,results=[],line_text='',line_text_correct='',line_completed=(childrens[0].getAttribute('data-status')!='correct')&&(childrens[0].getAttribute('data-status')!='incorrect');function change_color(childrens){for(k=0;k<childrens.length;k+=1){var child=childrens[k],child_type=child.getAttribute('data-type'),child_status=child.getAttribute('data-status'),child_correct=child.getAttribute('data-correct'),child_picked=child.getAttribute('data-picked');if(child_type=='1'){if(child_correct==child_picked){child.style.background=game_opt.lt_bkcolor_correct;child.setAttribute('data-status','correct')}else{child.style.background=game_opt.lt_bkcolor_incorrect;child.setAttribute('data-status','incorrect')}}}}game_opt.line=e;foo.data.line=game_opt.line;for(i=0;i<childrens.length;i+=1){var child=childrens[i],child_status=child.getAttribute('data-status'),child_correct=child.getAttribute('data-correct'),child_picked=child.getAttribute('data-picked'),child_chances=child.getAttribute('data-chances');if(child_status=='enable'){disable=false;results.push(child_correct==child_picked?true:false)}if(child_correct!=child_picked){correct=false;results.push(child_status=='enable'?false:true)}if(child_chances!=null){score+=parseInt(child_chances);}line_text+=(child_picked!=null?child_picked:' ');line_text_correct+=(child_correct!=null?child_correct:' ')}for(j=0;j<results.length;j+=1){if(results[j]==false){next=false}}if(disable||correct||next){if(line_completed){new_score(score);}change_color(childrens);game_opt.line_correct=line_text_correct.trim();foo.data.line_correct=game_opt.line_correct;if(correct){if(line_completed){line_text=line_text.trim().split(' ');game_opt.words=(foo.data.words.join('¤')+'¤'+line_text.join('¤')).replace(/^¤/,'').replace(/[^A-Za-z0-9ÁÉÍÓÚÜÑáéíóúüñ¤\-]/,'').replace('-¤','').split('¤').capitalize();foo.data.words=new Array;foo.data.words=game_opt.words.slice();if(game_opt.type=='arcade'){new_time(score);}foo.data.lines_correct++;exec_fn(on_line_correct)}}else{if(line_completed){foo.data.lines_incorrect++;exec_fn(on_line_incorrect)}}if(e.nextSibling!=null){if(e.nextSibling.childNodes.length==1){e.nextSibling.setAttribute('data-visible',true);e.nextSibling.style.display='initial';e.nextSibling.nextSibling.setAttribute('data-visible',true);e.nextSibling.nextSibling.style.display='initial';bar_resize=2}else{e.nextSibling.setAttribute('data-visible',true);e.nextSibling.style.display='initial'}}else if(line_completed){win();}if(line_completed){resize_bar(bar_resize);}scrollTo(foo_game,foo_game.scrollHeight,500);if(line_completed){exec_fn(on_line_completed)}}}function compute_words(){var sav_words=localStorage.getItem(game_opt.type+'_words')!=null?JSON.parse(localStorage.getItem(game_opt.type+'_words')):{total:0,current:0,words:[]},lis_words=game_opt.data.words,cur_words=game_opt.words.uniq(),new_words=sav_words.words.diff(cur_words),fin_words=sav_words.words.concat(new_words).sortHard();if(typeof lis_words!=='undefined'){game_opt.new_words=new_words.slice();foo.data.new_words=new Array;foo.data.new_words=game_opt.new_words.slice();sav_words.total=lis_words.length;sav_words.current=fin_words.length;sav_words.words=fin_words.slice();localStorage.setItem(game_opt.type+'_words',JSON.stringify(sav_words));game_opt.words=null}}function highest_scores(){var hig_score=localStorage.getItem(game_opt.type+'_highest_scores'),new_score=foo.data.score,new_words=foo.data.words;if(new_words.length>1){if(hig_score!=null){hig_score=JSON.parse(hig_score)}else{hig_score=[];}if(hig_score.length<10){exec_fn(game_opt.on_new_record)}else if(new_score>hig_score[hig_score.length-1]){exec_fn(game_opt.on_new_record);}hig_score.push(new_score);hig_score=hig_score.sort().reverse();if(hig_score.length>game_opt.top){hig_score=hig_score.slice(0,game_opt.top);}localStorage.setItem(game_opt.type+'_highest_scores',JSON.stringify(hig_score))}}function win(){if(foo.data.letter==null){foo.data.active=false;foo.data.finished=true;clearInterval(game_timer);}else{var additional_score=parseInt(foo.data.time/4);resize_bar(1);if(additional_score>100){additional_score=100;}if(game_opt.type=='story'){new_score(-Math.abs(additional_score));}else{new_score(additional_score);}highest_scores();game_opt.finished=true;foo.data.finished=game_opt.finished;foo.disable();if(typeof game_opt.words!=='undefined'&&game_opt.words!=null){compute_words();}exec_fn(game_opt.on_win)}game_opt.win=true}function lose(){foo.disable();exec_fn(game_opt.on_lose);foo.data.active=false;foo.data.finished=true}return{data:{type:null,active:null,finished:null,time:null,score:null,past_score:null,line:null,line_correct:null,lines_correct:null,lines_incorrect:null,letter:null,words:[],new_words:[]},create:function(opt){opt.type=opt.type||null;opt.url=opt.url||null;opt.lang=opt.lang||'es';opt.size=opt.size||[7,13];opt.margin=opt.margin||[2,1,1,1];opt.border=opt.border||[0,0];opt.score_position=opt.score_position||2;opt.timer_position=opt.timer_position||0;opt.top=opt.top||10;opt.score_label=opt.score_label||'Puntos';opt.timer_label=opt.timer_label||'Tiempo';opt.border_style=opt.border_style||'solid';opt.border_color=opt.border_color||'black';opt.game_fontSize=opt.game_fontSize||'1em';opt.game_fontFamily=opt.game_fontFamily||'Impact, Charcoal, sans-serif';opt.labels_fontSize=opt.labels_fontSize||'.75em';opt.labels_fontFamily=opt.labels_fontFamily||'Impact, Charcoal, sans-serif';opt.totals_fontSize=opt.totals_fontSize||'1.5em';opt.totals_fontFamily=opt.totals_fontFamily||'Impact, Charcoal, sans-serif';opt.lt_fontSize=opt.lt_fontSize||'1em';opt.lt_chance_fontSize=opt.lt_chance_fontSize||'.5em';opt.lt_bkcolor_correct=opt.lt_bkcolor_correct||'green';opt.lt_bkcolor_incorrect=opt.lt_bkcolor_incorrect||'red';opt.lt_bkcolor_enable=opt.lt_bkcolor_enable||'white';opt.lt_bkcolor_disable=opt.lt_bkcolor_disable||'#ddd';opt.bar_style=opt.bar_style||'3px solid #ccc';opt.on_init=opt.on_init||'';opt.on_finish=opt.on_finish||'';opt.on_win=opt.on_win||'';opt.on_lose=opt.on_lose||'';opt.on_error=opt.on_error||'';opt.on_new_time=opt.on_new_time||'';opt.on_new_score=opt.on_new_score||'';opt.on_new_record=opt.on_new_record||'';opt.ar_chances=opt.ar_chances||3;opt.ar_min_answers=opt.ar_min_answers||2;opt.ar_max_answers=opt.ar_max_answers||3;opt.ar_max_answers_total=opt.ar_max_answers_total||5;opt.ar_bomb_or_bonus=opt.ar_bomb_or_bonus||20;opt.ar_random_first_letter=opt.ar_random_first_letter||3;opt.ar_length=opt.ar_length||10;opt.ar_on_ten_seconds=opt.ar_on_ten_seconds||'';opt.ar_on_line_completed=opt.ar_on_line_completed||'';opt.ar_on_line_correct=opt.ar_on_line_correct||'';opt.ar_on_line_incorrect=opt.ar_on_line_incorrect||'';opt.ar_on_first_change=opt.ar_on_first_change||'';opt.ar_on_change=opt.ar_on_change||'';opt.ar_on_loop=opt.ar_on_loop||'';opt.ar_on_bomb=opt.ar_on_bomb||'';opt.ar_on_bonus=opt.ar_on_bonus||'';exec_fn(opt.on_init);function request(url){var xhr=new XMLHttpRequest();xhr.open('GET',url);xhr.responseType='text';xhr.send();xhr.onprogress=function(e){if(e.lengthComputable){console.log(parseInt((e.loaded/e.total)*100)+'%')}else{console.log(lang_loading)}};xhr.onreadystatechange=function(){if(this.readyState==4&&this.status==200){result=relabel(JSON.parse(this.responseText));localStorage.setItem(game_opt.type,result);build()}};xhr.onerror=function(e){exec_fn(game_opt.on_error);throw new Error(lang_load_error)}}function relabel(result){new_result={type:result.type,score:result.score,timer:result.timer,words:result.words,content:(game_opt.type=='story'?result.content:[])};function random_letters(index_total,index_actual,correct,ignore){var total=Math.floor(Math.random()*(game_opt.ar_max_answers-game_opt.ar_min_answers+1))+game_opt.ar_min_answers,final_pick=[],status='disable';function random_select(c){var vowels_es=['a','a','a','a','a','a','a','a','á','á','á','á','e','e','e','e','e','e','e','e','é','é','é','é','i','i','i','i','í','í','o','o','o','o','o','o','ó','ó','ó','u','u','u','ú','ü'];var consonants_es=['b','b','c','c','c','c','d','d','d','d','d','f','g','g','h','h','j','k','l','l','l','l','m','m','n','n','n','n','n','ñ','p','p','q','r','r','r','r','r','s','s','s','s','s','s','t','t','t','t','v','w','x','y','z'];var punctuation_es=['.','.','.','.','.',',',',',',',',',',',';',';',';',':',':',':','¡','¡','¡','¡','!','!','!','!','¿','¿','¿','¿','?','?','?','?','(','(','(',')',')',')','«','»','“','”','-','[',']'];var vowels_en=['a','a','a','a','a','a','a','a','a','e','e','e','e','e','e','e','e','e','e','e','e','i','i','i','i','i','i','i','i','i','o','o','o','o','o','o','o','o','u','u','u','u'];var consonants_en=['b','b','c','c','d','d','d','d','f','f','g','g','g','h','h','j','k','l','l','l','l','m','m','n','n','n','n','n','n','p','p','q','r','r','r','r','r','r','s','s','s','s','t','t','t','t','t','t','v','v','w','w','x','y','y','z'];var punctuation_en=['.','.','.','.','.',',',',',',',',',',',';',';',';',':',':',':','’','’','’','’','!','!','!','!','?','?','?','?','(','(','(',')',')',')','“','”','-','[',']'];vowels=eval('vowels_'+lang);consonants=eval('consonants_'+lang);punctuation=eval('punctuation_'+lang);if(final_pick.length==0){final_pick.push(c);}else{var accepted=true,candidate_type=Math.floor(Math.random()*(game_opt.ar_bomb_or_bonus-0+1))+0;if(candidate_type==0){candidate=0;}else if(candidate_type==1){candidate=1;}else{if(/\w/.test(c.transliterate())){if(/[aeiou]/.test(c.transliterate())){candidate=vowels.sample()}else{candidate=consonants.sample()}}else{candidate=punctuation.sample()}}for(k=0;k<final_pick.length;k+=1){if(candidate_type>1&&candidate.toLowerCase()==(''+final_pick[k]).toLowerCase()){accepted=false;break}}if(accepted){if(candidate_type>1&&/[A-Z]/.test(c.transliterate())){candidate=candidate.toUpperCase()}final_pick.push(candidate)}else{random_select(c)}}}function check_shuffle(array){if(typeof array[0]==='number'){return check_shuffle(array.shuffle())}else{return array}}while(total!=0){random_select(correct);total-=1}if(Math.floor(Math.random()*Math.floor(game_opt.ar_random_first_letter))!=0&&!ignore){final_pick=check_shuffle(final_pick.shuffle());status='enable'}reach=parseInt((index_actual*game_opt.ar_max_answers_total)/index_total);if(game_opt.ar_max_answers<reach&&reach<=game_opt.ar_max_answers_total){game_opt.ar_max_answers=reach;game_opt.ar_min_answers=game_opt.ar_min_answers+1;game_opt.ar_bomb_or_bonus=game_opt.ar_bomb_or_bonus-1;}return{status:status,letters:final_pick}}for(i=0;i<game_opt.size[1]-1;i+=1){if(game_opt.type!='story'){new_result.content.push({visible:true,on_line_completed:game_opt.ar_on_line_completed,on_line_correct:game_opt.ar_on_line_correct,on_line_incorrect:game_opt.ar_on_line_incorrect});}else{new_result.content.unshift({visible:true,on_line_completed:game_opt.ar_on_line_completed,on_line_correct:game_opt.ar_on_line_correct,on_line_incorrect:game_opt.ar_on_line_incorrect})}}if(game_opt.type!='story'){for(i=0;i<game_opt.ar_length;i+=1){var line=result.content.sample(),letters=line.split(''),new_line={visible:new_result.content.length==game_opt.size[1]-1?true:false,on_line_completed:game_opt.ar_on_line_completed,on_line_correct:game_opt.ar_on_line_correct,on_line_incorrect:game_opt.ar_on_line_incorrect,content:[]},content_tmp=[];for(j=0;j<letters.length;j+=1){letter=letters[j];if(letter==' '){content_tmp.push({type:0});}else{r_letters=random_letters(game_opt.ar_length-1,i,letter,(j==0?true:false));content_tmp.push({type:1,status:r_letters.status,correct:letter,picked:r_letters.letters[0],picked_i:0,letters:r_letters.letters,chances:game_opt.ar_chances,loops:0,on_first_change:game_opt.ar_on_first_change,on_change:game_opt.ar_on_change,on_loop:game_opt.ar_on_loop,on_bomb:game_opt.ar_on_bomb,on_bonus:game_opt.ar_on_bonus})}}new_line.content=content_tmp;new_result.content.push(new_line)}}return JSON.stringify(new_result)}function build_game(parent,block_size,lines_before){game_opt.data=JSON.parse(localStorage[opt.type]);function build_letter(div_parent,letter,index){var div=document.createElement('div');div.style.float='left';div.style.padding='0';if(letter.type==0){div.style.width=block_size+'px';div.style.height=block_size+'px';div.innerHTML='<p style="margin:0;padding:0;"></p>'}else{div.setAttribute('data-type',letter.type);div.setAttribute('data-status',letter.status);div.setAttribute('data-correct',letter.correct);div.setAttribute('data-picked',letter.picked);div.setAttribute('data-picked_i',letter.picked_i);div.setAttribute('data-letters',letter.letters.join('¤'));div.setAttribute('data-chances',letter.chances);div.setAttribute('data-loops',letter.loops);div.setAttribute('data-on_first_change',letter.on_first_change);div.setAttribute('data-on_change',letter.on_change);div.setAttribute('data-on_loop',letter.on_loop);div.setAttribute('data-on_bomb',letter.on_bomb);div.setAttribute('data-on_bonus',letter.on_bonus);div.classList.add('foo-letter');div.style.position='relative';div.style.width=(block_size-4)+'px';div.style.height=(block_size-4)+'px';div.style.margin='1px';div.style.cursor='pointer';div.style.border='1px solid #ccc';div.style.borderRadius=(block_size/5)+'px';if(letter.status=='correct'){l_correct=true;div.style.background=game_opt.lt_bkcolor_correct}else if(letter.status=='incorrect'){l_incorrect=true;div.style.background=game_opt.lt_bkcolor_incorrect}else if(letter.status=='enable'){div.style.background=game_opt.lt_bkcolor_enable}else if(letter.status=='disable'){div.style.background=game_opt.lt_bkcolor_disable;}if(letter.picked!=0&&letter.picked!=1){lt_label=letter.picked}else{lt_label='<span style="font-size:'+game_opt.lt_chance_fontSize+';vertical-align:.25em;">'+(letter.letters[0]=='0'?'-':'+')+'</span>3';}div.innerHTML='<p style="font-size:'+opt.lt_fontSize+';margin:'+(block_size/10)+'px 0 0 0;padding:0;text-align:center;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;">'+lt_label+'</p>';div.innerHTML+='<p style="font-size:'+game_opt.lt_chance_fontSize+';position:absolute;bottom:1px;right:5px;margin:0;padding:0;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;">'+letter.chances+'</p>';if('ontouchstart'in document.documentElement==true){div.addEventListener("touchstart",on_touchstart);div.addEventListener("touchend",on_touchend)}else{div.addEventListener("mousedown",on_touchstart);div.addEventListener("mouseup",on_touchend)}}div_parent.appendChild(div)}for(i=0;i<game_opt.data.content.length;i+=1){var line=game_opt.data.content[i],div=document.createElement('div');div.setAttribute('data-visible',line.visible);div.setAttribute('data-on_line_completed',line.on_line_completed);div.setAttribute('data-on_line_correct',line.on_line_correct);div.setAttribute('data-on_line_incorrect',line.on_line_incorrect);div.classList.add('foo-line');div.style.width=(parent.offsetWidth-100)+'px';div.style.height=block_size+'px';div.style.margin='0';div.style.padding='0';div.style.display=line.visible?'block':'none';parent.appendChild(div);if(typeof line.content!=='undefined'){var l_correct=false,l_incorrect=false;for(j=0;j<line.content.length;j+=1){build_letter(div,line.content[j],j);}if(l_correct&&!l_incorrect){foo.data.lines_correct++}if(l_incorrect){foo.data.lines_incorrect++;}}else{div.innerHTML='<div style="clear:both;width:'+block_size+'px;height:'+block_size+'px;margin:0;padding:0;"></div>'}}}function build(){function build_header(i){div=document.createElement('div');div.id='foo-head-'+i;div.style.float='left';div.style.width='33.3333333333%';div.style.height=(opt.margin[0]*block_size)+'px';div_head.appendChild(div)}function build_metric(div){function build_p(i){if(i==1){height=(div.clientHeight/8)*2}else if(i==2){height=(div.clientHeight/8)*4}else{height=(div.clientHeight/8)}p=document.createElement('p');p.style.display='flex';p.style.alignItems='center';p.style.justifyContent='center';p.style.width='100%';p.style.height=height+'px';p.style.margin='0';p.style.padding='0';p.style.textAlign='center';p.style.cssText+='-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;';if(i==1){p.id=div.id+'-label';p.style.fontSize=opt.labels_fontSize;p.style.fontFamily=opt.labels_fontFamily;p.innerHTML=eval('opt.'+div.id.split('-')[1]+'_label');}else if(i==2){p.id=div.id+'-total';p.style.fontSize=opt.totals_fontSize;p.style.fontFamily=opt.totals_fontFamily;if(div.id.split('-')[1]=='score'){game_opt.score=JSON.parse(localStorage[opt.type])[div.id.split('-')[1]];foo.data.score=game_opt.score;p.innerHTML=game_opt.score}else{game_opt.time=JSON.parse(localStorage[opt.type])[div.id.split('-')[1]];foo.data.time=game_opt.time;p.innerHTML=game_opt.time}}div.appendChild(p)}for(i=0;i<4;i+=1){build_p(i)}}validate_array(opt.size,2,3,'Size');validate_array(opt.margin,4,1,'Margin');game_timer=setInterval(new_time,1000);var div_main=document.createElement('div'),div_head=document.createElement('div'),div_adds=document.createElement('div'),div_scrl=document.createElement('div'),div_game=document.createElement('div'),div_pbar=document.createElement('div');var total_width=opt.size[0]+opt.margin[1]+opt.margin[3],total_height=opt.size[1]+opt.margin[0]+opt.margin[2];var block_width=parseInt(document.documentElement.clientWidth/total_width),block_height=parseInt(document.documentElement.clientHeight/total_height),block_size=block_width>block_height?block_height:block_width;document.body.style.width='100vw';document.body.style.height='100vh';document.body.style.display='flex';document.body.style.alignItems='center';document.body.style.justifyContent='center';document.body.style.margin='0';document.body.style.padding='0';div_main.id='foo-main';div_main.style.width=(total_width*block_size)+'px';div_main.style.height=(total_height*block_size)+'px';div_main.style.overflow='hidden';document.body.appendChild(div_main);div_head.id='foo-head';div_head.style.width=(opt.size[0]*block_size)+'px';div_head.style.height=(opt.margin[0]*block_size)+'px';div_head.style.margin='0 '+(opt.margin[1]*block_size)+'px 0 '+(opt.margin[3]*block_size)+'px ';div_main.appendChild(div_head);for(i=0;i<3;i+=1){build_header(i);}if(opt.score_position==opt.timer_position){throw new Error("Score position cannot be the same that timer position.");}var div_score=document.getElementById('foo-head-'+opt.score_position),div_timer=document.getElementById('foo-head-'+opt.timer_position);div_score.id='foo-score';div_timer.id='foo-timer';build_metric(div_score);build_metric(div_timer);div_adds.id='foo-adds';div_adds.style.position='absolute';div_adds.style.display='none';div_adds.style.width=(opt.size[0]*block_size)+'px';div_adds.style.height=(2*block_size)+'px';div_adds.style.top=(div_head.clientHeight+block_size)+'px';div_adds.style.left='50%';div_adds.style.marginRight='-50%';div_adds.style.transform='translate(-50%, 0)';div_adds.style.textAlign='center';div_adds.style.fontFamily=opt.game_fontFamily;div_main.appendChild(div_adds);div_scrl.id='foo-scrl';div_scrl.style.width=(opt.size[0]*block_size)+'px';div_scrl.style.height=(opt.size[1]*block_size+(opt.border[0]+opt.border[1]+4))+'px';div_scrl.style.marginLeft=(block_size)+'px';div_scrl.style.padding='0';div_scrl.style.overflow='hidden';div_main.appendChild(div_scrl);div_game.id='foo-game';div_game.style.width=(opt.size[0]*block_size)+'px';div_game.style.height=(opt.size[1]*block_size)+'px';div_game.style.margin=(opt.border[0]+2)+'px 0 0 0';div_game.style.padding='1px 100px 1px 0';div_game.style.overflowY='scroll';div_game.style.fontSize=game_opt.game_fontSize;div_game.style.fontFamily=opt.game_fontFamily;div_game.style.borderTop=opt.border[0]+'px '+opt.border_style+' '+opt.border_color;div_game.style.borderBottom=opt.border[1]+'px '+opt.border_style+' '+opt.border_color;div_scrl.appendChild(div_game);build_game(div_game,block_size,opt.size[1]-1);div_pbar.id='foo-bar';div_pbar.style.width=0;div_pbar.style.height=0;div_pbar.style.position='absolute';div_pbar.style.bottom=0;div_pbar.style.left=0;div_pbar.style.borderTop=opt.bar_style;div_main.appendChild(div_pbar);resize_bar();div_game.scrollTop=div_game.scrollHeight}game_opt=opt;game_opt.active=true;game_opt.finished=false;foo.data.type=game_opt.type;foo.data.active=game_opt.active;foo.data.finished=game_opt.finished;if(localStorage.getItem(game_opt.type)==null){if(opt.type!='story'&&opt.type!='arcade'){throw new Error("'type' option can only be 'story' or 'arcade'");}if(opt.url==null){throw new Error("'url' option is mandatory.")}else{request(opt.url);}}else{build();}if(foo.data.score<0||foo.data.time<0){setTimeout(lose,300);}if(typeof game_opt.data!=='undefined'&&typeof game_opt.data.content!=='undefined'&&game_opt.data.content!=null){if(game_opt.data.content[game_opt.data.content.length-1].content[0].status=='correct'||game_opt.data.content[game_opt.data.content.length-1].content[0].status=='incorrect'){setTimeout(win,300)}}exec_fn(opt.on_finish)},destroy:function(opt={}){opt.on_init=opt.on_init||'';opt.on_finish=opt.on_finish||'';exec_fn(opt.on_init);var foo_els=document.body.childNodes;for(i=0;i<foo_els.length;i+=1){if(/^foo-/.test(foo_els[i].id)){foo_els[i].parentElement.removeChild(foo_els[i])}}document.body.style.width='';document.body.style.height='';document.body.style.display='';document.body.style.alignItems='';document.body.style.justifyContent='';document.body.style.margin='';document.body.style.padding='';game_opt.active=false;foo.data.active=game_opt.active;clearInterval(game_timer);exec_fn(opt.on_finish)},disable:function(opt={}){opt.on_init=opt.on_init||'';opt.on_finish=opt.on_finish||'';exec_fn(opt.on_init);if(foo.data.active&&document.getElementById('foo-main')!=null){var foo_disable=document.createElement('div');foo_disable.id='foo-disable';foo_disable.style.position='absolute';foo_disable.style.zIndex=1;foo_disable.style.top=0;foo_disable.style.left=0;foo_disable.style.width='100vw';foo_disable.style.height='100vh';foo_disable.style.background='rgba(0,0,0,0.5)';document.getElementById('foo-main').appendChild(foo_disable);game_opt.active=false;foo.data.active=game_opt.active;clearInterval(game_timer)}exec_fn(opt.on_finish)},enable:function(opt={}){opt.on_init=opt.on_init||'';opt.on_finish=opt.on_finish||'';exec_fn(opt.on_init);if(!foo.data.active){var foo_disable=document.getElementById('foo-disable');if(foo_disable!=null){foo_disable.parentElement.removeChild(foo_disable);}game_opt.active=true;foo.data.active=game_opt.active;game_timer=setInterval(new_time,1000)}exec_fn(opt.on_finish)},save:function(opt={}){opt.on_init=opt.on_init||'';opt.on_finish=opt.on_finish||'';exec_fn(opt.on_init);var foo_game=document.getElementById('foo-game'),json_game={type:game_opt.type,score:parseInt(game_opt.score),timer:parseInt(game_opt.time),content:[]};for(i=0;i<foo_game.childNodes.length;i+=1){var line=foo_game.childNodes[i],l_visible=line.getAttribute('data-visible'),l_on_line_completed=line.getAttribute('data-on_line_completed'),l_on_line_correct=line.getAttribute('data-on_line_correct'),l_on_line_incorrect=line.getAttribute('data-on_line_incorrect'),json_line={visible:(l_visible=='true'),on_line_completed:l_on_line_completed,on_line_correct:l_on_line_correct,on_line_incorrect:l_on_line_incorrect};if(line.childNodes[0].getAttribute('data-type')!=null){json_line.content=[];for(j=0;j<line.childNodes.length;j+=1){var letter=line.childNodes[j],lt_type=letter.getAttribute('data-type'),lt_status=letter.getAttribute('data-status'),lt_correct=letter.getAttribute('data-correct'),lt_picked=letter.getAttribute('data-picked'),lt_picked_i=letter.getAttribute('data-picked_i'),lt_letters=letter.getAttribute('data-letters'),lt_chances=letter.getAttribute('data-chances'),lt_loops=letter.getAttribute('data-loops'),lt_on_first_change=letter.getAttribute('data-on_first_change'),lt_on_change=letter.getAttribute('data-on_change'),lt_on_loop=letter.getAttribute('data-on_loop'),lt_on_bomb=letter.getAttribute('data-on_bomb'),lt_on_bonus=letter.getAttribute('data-on_bonus'),json_letter={};if(parseInt(lt_type)==1){json_letter={type:parseInt(lt_type),status:lt_status,correct:lt_correct,picked:lt_picked,picked_i:parseInt(lt_picked_i),letters:lt_letters.split('¤'),chances:parseInt(lt_chances),loops:parseInt(lt_loops),on_first_change:lt_on_first_change,on_change:lt_on_change,on_loop:lt_on_loop,on_bomb:lt_on_bomb,on_bonus:lt_on_bonus}}else{json_letter={type:0}}json_line.content.push(json_letter)}}json_game.content.push(json_line)}localStorage.setItem(game_opt.type,JSON.stringify(json_game));exec_fn(opt.on_finish)},wipe:function(opt=''){function wipe_data(){foo.data={type:null,active:null,finished:null,time:null,score:null,past_score:null,line:null,line_correct:null,lines_correct:null,lines_incorrect:null,letter:null,words:[],new_words:[]}}function wipe_me(o,all=false){if(document.getElementById('foo-main')){foo.destroy();wipe_data();game_opt.data=null}localStorage.removeItem(o);localStorage.removeItem(o+'_bombs_bonus');if(all){localStorage.removeItem(o+'_words');localStorage.removeItem(o+'_highest_scores')}}if(opt==''){throw new Error("Function 'wipe' requires 'story' or 'arcade' as parameter.");}if(opt=='all'||opt=='both'){if(opt=='all'){wipe_me('story',true);wipe_me('arcade',true)}else{wipe_me('story');wipe_me('arcade')}}else if(opt=='story'||opt=='arcade'){wipe_me(opt);}else if(opt=='data'){wipe_data()}}}})();
1
+var foo=(function(){var lang_loading='Loading…',lang_load_error='Data not found.';var game_opt;var touch_old=null,touch_timer,game_timer,bar_timer;Array.prototype.sample=function(){return this[Math.floor(Math.random()*this.length)]};Array.prototype.capitalize=function(){var fin=[];for(i=0;i<this.length;i+=1){fin.push(this[i].charAt(0).toUpperCase()+this[i].slice(1))}return fin};Array.prototype.diff=function(a){function compute(a,b){return a.filter(function(v){return b.indexOf(v)==-1})}if(this.length==0){return a}else if(this.length>a.length){return compute(this,a)}else{return compute(a,this)}};Array.prototype.uniq=function(){var fin=this.sortHard();return fin.filter(function(el,i,a){return!i||el.toLowerCase()!=a[i-1].toLowerCase()})};Array.prototype.sortHard=function(){var tmp=[],fin=[];for(i=0;i<this.length;i+=1){tmp.push({n:this[i].transliterate(),r:this[i]})}tmp=tmp.slice(0).sort(function(a,b){return(a.n>b.n)?1:(a.n<b.n)?-1:0});for(j=0;j<tmp.length;j+=1){fin.push(tmp[j].r)}return fin};Array.prototype.shuffle=function(){var x=this.length,j,temp;if(x==0){return this}while(x-=1){j=Math.floor(Math.random()*(x+1));temp=this[x];this[x]=this[j];this[j]=temp}return this};String.prototype.transliterate=function(){var translate_re=/[ÁÉÍÓÚÜÑáéíóúüñ]/g,translate={'Á':'A','É':'E','Í':'I','Ó':'O','Ú':'U','Ü':'U','Ñ':'N','á':'a','é':'e','í':'i','ó':'o','ú':'u','ü':'u','ñ':'n'};return(this.replace(translate_re,function(match){return translate[match]}))};function scrollTo(element,to,duration){if(duration<=0){return}var difference=to-element.scrollTop,perTick=difference/duration*10;setTimeout(function(){element.scrollTop=element.scrollTop+perTick;if(element.scrollTop===to){return}scrollTo(element,to,duration-10)},10)}function resize_bar(increment=0){var div_pbar=document.getElementById('foo-bar'),total_width=document.body.clientWidth,current_width=div_pbar.clientWidth,total_lines=game_opt.data.content.length-(game_opt.size[1]-1),line_width=total_width/total_lines,new_width;function move(){if(current_width>=new_width){clearInterval(bar_timer);bar_timer=0}else{if(increment){current_width+=10}else{current_width++}div_pbar.style.width=current_width+'px'}}if(increment==0){var lines=document.getElementsByClassName('foo-line'),last_lt=lines[lines.length-1].getElementsByClassName('foo-letter')[0].getAttribute('data-status'),total=0;for(i=0;i<lines.length;i+=1){if(lines[i].getAttribute('data-visible')=='true'){total+=1}}if(total_lines==total-(game_opt.size[1]-1)&&(last_lt=='correct'||last_lt=='incorrect')){increment=(total-(game_opt.size[1]-1))}else{increment=(total-(game_opt.size[1]-1))-1}}new_width=current_width+(line_width*increment);if(typeof bar_timer==='undefined'||bar_timer==0){bar_timer=setInterval(move,1)}}function validate_array(array,min_length,min_quantity,label){if(array.length!=min_length){throw new Error(label+" array requires only "+min_length+" elements.");}for(i=0;i<array.length;i+=1){if(typeof array[i]!='number'||array[i]%1!==0||array[i]<min_quantity){throw new Error(label+" array's elements have to be integers equal or greater than "+min_quantity+".")}}}function exec_fn(e){if(typeof e==='string'&&typeof window[e]==='function'){window[e]()}else if(typeof e==='function'){e()}}function new_time(extra=0){var div_timer=document.getElementById('foo-timer-total'),qnt_timer=parseInt(div_timer.innerHTML);if(extra!=0){qnt_timer=qnt_timer+extra;}else{if(game_opt.type=='story'){qnt_timer=qnt_timer+1}else{qnt_timer=qnt_timer-1}}game_opt.time=qnt_timer;foo.data.time=game_opt.time;div_timer.innerHTML=qnt_timer;if(qnt_timer==10&&game_opt.type=='arcade'){exec_fn(game_opt.ar_on_ten_seconds);}if(qnt_timer<=0&&game_opt.type=='arcade'){lose();}exec_fn(game_opt.on_new_time)}function new_score(int){var div_score=document.getElementById('foo-score-total'),qnt_score=parseInt(div_score.innerHTML);game_opt.past_score=qnt_score;foo.data.past_score=game_opt.past_score;qnt_score=qnt_score+int;game_opt.score=qnt_score;foo.data.score=game_opt.score;div_score.innerHTML=qnt_score;if(qnt_score<0){lose();}exec_fn(game_opt.on_new_score)}function on_touchstart(e){if(!game_opt.win){var element=e.currentTarget,p=element.getElementsByTagName('p')[0],type=element.getAttribute('data-type'),status=element.getAttribute('data-status'),correct=element.getAttribute('data-correct'),picked=element.getAttribute('data-picked'),picked_i=element.getAttribute('data-picked_i'),letters=element.getAttribute('data-letters').split('¤'),chances=element.getAttribute('data-chances'),loops=element.getAttribute('data-loops'),on_first_change=element.getAttribute('data-on_first_change'),on_change=element.getAttribute('data-on_change'),on_loop=element.getAttribute('data-on_loop'),on_bomb=element.getAttribute('data-on_bomb'),on_bonus=element.getAttribute('data-on_bonus');game_opt.currentTarget=element;game_opt.letter=element;foo.data.letter=game_opt.letter;function draw_label(l){var d_add=document.getElementById('foo-adds'),p_add=document.createElement('p');if(document.getElementById('foo-token')){document.getElementById('foo-token').parentElement.removeChild(document.getElementById('foo-token'));}d_add.style.display='inherit';p_add.id='foo-token';p_add.style.cssText='width:'+d_add.clientHeight+'px;height:'+d_add.clientHeight+'px;font-size:'+((parseInt(game_opt.game_fontSize)*2)+game_opt.game_fontSize.replace(/\d+/,''))+';background:'+game_opt.lt_bkcolor_enable+';border-radius:'+(d_add.clientHeight/5)+'px;margin:0 auto;display:flex;align-items:center;justify-content:center;border:1px solid #ccc;-webkit-box-shadow: 0 0 25px 0 rgba(0,0,0,0.25);-moz-box-shadow: 0 0 25px 0 rgba(0,0,0,0.25);box-shadow: 0 0 25px 0 rgba(0,0,0,0.25);';p_add.innerHTML=l;d_add.appendChild(p_add)}function trigger_bomb_bonus(el,i){var bomb_bonus=game_opt.type+'_bombs_bonus_'+game_opt.lang,picked=el.getAttribute('data-picked'),fn=picked==0?el.getAttribute('data-on_bomb'):el.getAttribute('data-on_bonus'),line_index=get_index(el.parentElement.previousSibling,0),letter_index=get_index(el.previousSibling,0),position=line_index+'-'+letter_index+'-'+i,valid=true,final_pos=[];function get_index(e,i){while(e!=null){e=e.previousSibling;i+=1}return i}if(localStorage.getItem(bomb_bonus)==null){final_pos.push(position);}else{var bomb_bonus_array=JSON.parse(localStorage[bomb_bonus]);for(j=0;j<bomb_bonus_array.length;j+=1){if(bomb_bonus_array[j]==position){valid=false}}bomb_bonus_array.push(position);final_pos=bomb_bonus_array.slice()}if(valid){exec_fn(fn);if(picked==0){new_score(-3)}else{new_score(3);}localStorage.setItem(bomb_bonus,JSON.stringify(final_pos));return '<span style="font-size:'+game_opt.lt_chance_fontSize+';vertical-align:.25em;">'+(picked=='0'?'-':'+')+'</span>3'}else{return '<span style="color:#999"><span style="font-size:'+game_opt.lt_chance_fontSize+';vertical-align:.25em;">'+(picked=='0'?'-':'+')+'</span>3</span>'}}clearInterval(touch_timer);if(touch_old!=element&&touch_old!=null){compute_letter(touch_old);}if(chances>0&&status=='enable'){exec_fn(on_change);if(loops==0&&picked==letters[0]){exec_fn(on_first_change);}for(i=0;i<letters.length;i+=1){if(i==picked_i){if(i!=letters.length-1){picked_i=i+1}else{picked_i=0;loops=parseInt(loops)+1;element.setAttribute('data-loops',loops);exec_fn(on_loop)}picked=letters[picked_i];element.setAttribute('data-picked',picked);element.setAttribute('data-picked_i',picked_i);if(picked!=0&&picked!=1){label=picked}else{label=trigger_bomb_bonus(element,i+1);}draw_label(label);p.innerHTML=label;break}}}touch_old=element}}function on_touchend(e){var element=e.currentTarget;if(!game_opt.win){if(element==game_opt.currentTarget){touch_timer=setInterval(compute_letter,500,this)}else{touch_timer=setInterval(compute_letter,500,game_opt.currentTarget)}}}function compute_letter(e){var chances=e.getAttribute('data-chances'),status=e.getAttribute('data-status'),d_add=document.getElementById('foo-adds');clearInterval(touch_timer);touch_old=null;d_add.style.display='none';if(document.getElementById('foo-token')){document.getElementById('foo-token').parentElement.removeChild(document.getElementById('foo-token'));}if(chances>0&&status=='enable'){e.setAttribute('data-chances',chances-1);e.getElementsByTagName('p')[1].innerHTML=chances-1;if(chances-1==0){e.setAttribute('data-status','disable');e.style.background=game_opt.lt_bkcolor_disable;e.style.cssText+='-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;';e_new=e.cloneNode(true);e.parentNode.replaceChild(e_new,e);e=e_new}}compute_line(e.parentNode);foo.save()}function compute_line(e){var foo_game=document.getElementById('foo-game'),on_line_completed=e.getAttribute('data-on_line_completed'),on_line_correct=e.getAttribute('data-on_line_correct'),on_line_incorrect=e.getAttribute('data-on_line_incorrect'),childrens=e.childNodes,disable=true,correct=true,next=true,score=0,bar_resize=1,results=[],line_text='',line_text_correct='',line_completed=(childrens[0].getAttribute('data-status')!='correct')&&(childrens[0].getAttribute('data-status')!='incorrect');function change_color(childrens){for(k=0;k<childrens.length;k+=1){var child=childrens[k],child_type=child.getAttribute('data-type'),child_status=child.getAttribute('data-status'),child_correct=child.getAttribute('data-correct'),child_picked=child.getAttribute('data-picked');if(child_type=='1'){if(child_correct==child_picked){child.style.background=game_opt.lt_bkcolor_correct;child.setAttribute('data-status','correct')}else{child.style.background=game_opt.lt_bkcolor_incorrect;child.setAttribute('data-status','incorrect')}}}}game_opt.line=e;foo.data.line=game_opt.line;for(i=0;i<childrens.length;i+=1){var child=childrens[i],child_status=child.getAttribute('data-status'),child_correct=child.getAttribute('data-correct'),child_picked=child.getAttribute('data-picked'),child_chances=child.getAttribute('data-chances');if(child_status=='enable'){disable=false;results.push(child_correct==child_picked?true:false)}if(child_correct!=child_picked){correct=false;results.push(child_status=='enable'?false:true)}if(child_chances!=null){score+=parseInt(child_chances);}line_text+=(child_picked!=null?child_picked:' ');line_text_correct+=(child_correct!=null?child_correct:' ')}for(j=0;j<results.length;j+=1){if(results[j]==false){next=false}}if(disable||correct||next){if(line_completed){new_score(score);}change_color(childrens);game_opt.line_correct=line_text_correct.trim();foo.data.line_correct=game_opt.line_correct;if(correct){if(line_completed){line_text=line_text.trim().split(' ');game_opt.words=(foo.data.words.join('¤')+'¤'+line_text.join('¤')).replace(/^¤/,'').replace(/[^A-Za-z0-9ÁÉÍÓÚÜÑáéíóúüñ¤\-]/,'').replace('-¤','').split('¤').capitalize();foo.data.words=new Array;foo.data.words=game_opt.words.slice();if(game_opt.type=='arcade'){new_time(score);}foo.data.lines_correct++;exec_fn(on_line_correct)}}else{if(line_completed){foo.data.lines_incorrect++;exec_fn(on_line_incorrect)}}if(e.nextSibling!=null){if(e.nextSibling.childNodes.length==1){e.nextSibling.setAttribute('data-visible',true);e.nextSibling.style.display='initial';e.nextSibling.nextSibling.setAttribute('data-visible',true);e.nextSibling.nextSibling.style.display='initial';bar_resize=2}else{e.nextSibling.setAttribute('data-visible',true);e.nextSibling.style.display='initial'}}else if(line_completed){win();}if(line_completed){resize_bar(bar_resize);}scrollTo(foo_game,foo_game.scrollHeight,500);if(line_completed){exec_fn(on_line_completed)}}}function compute_words(){var sav_words=localStorage.getItem(game_opt.type+'_words_'+game_opt.lang)!=null?JSON.parse(localStorage.getItem(game_opt.type+'_words_'+game_opt.lang)):{total:0,current:0,words:[]},lis_words=game_opt.data.words,cur_words=game_opt.words.uniq(),new_words=sav_words.words.diff(cur_words),fin_words=sav_words.words.concat(new_words).sortHard();if(typeof lis_words!=='undefined'){game_opt.new_words=new_words.slice();foo.data.new_words=new Array;foo.data.new_words=game_opt.new_words.slice();sav_words.total=lis_words.length;sav_words.current=fin_words.length;sav_words.words=fin_words.slice();localStorage.setItem(game_opt.type+'_words_'+game_opt.lang,JSON.stringify(sav_words));game_opt.words=null}}function highest_scores(){var hig_score=localStorage.getItem(game_opt.type+'_highest_scores_'+game_opt.lang),new_score=foo.data.score,new_words=foo.data.words;if(new_words.length>1){if(hig_score!=null){hig_score=JSON.parse(hig_score)}else{hig_score=[];}if(hig_score.length<10){exec_fn(game_opt.on_new_record)}else if(new_score>hig_score[hig_score.length-1]){exec_fn(game_opt.on_new_record);}hig_score.push(new_score);hig_score=hig_score.sort().reverse();if(hig_score.length>game_opt.top){hig_score=hig_score.slice(0,game_opt.top);}localStorage.setItem(game_opt.type+'_highest_scores_'+game_opt.lang,JSON.stringify(hig_score))}}function win(){if(foo.data.letter==null){foo.data.active=false;foo.data.finished=true;clearInterval(game_timer);}else{var additional_score=parseInt(foo.data.time/4);resize_bar(1);if(additional_score>100){additional_score=100;}if(game_opt.type=='story'){new_score(-Math.abs(additional_score));}else{new_score(additional_score);}highest_scores();game_opt.finished=true;foo.data.finished=game_opt.finished;foo.disable();if(typeof game_opt.words!=='undefined'&&game_opt.words!=null){compute_words();}exec_fn(game_opt.on_win)}game_opt.win=true}function lose(){foo.disable();exec_fn(game_opt.on_lose);foo.data.active=false;foo.data.finished=true}return{data:{type:null,active:null,finished:null,time:null,score:null,past_score:null,line:null,line_correct:null,lines_correct:null,lines_incorrect:null,letter:null,words:[],new_words:[]},create:function(opt){opt.type=opt.type||null;opt.url=opt.url||null;opt.lang=opt.lang||'es';opt.size=opt.size||[7,13];opt.margin=opt.margin||[2,1,1,1];opt.border=opt.border||[0,0];opt.score_position=opt.score_position||2;opt.timer_position=opt.timer_position||0;opt.top=opt.top||10;opt.score_label=opt.score_label||'Puntos';opt.timer_label=opt.timer_label||'Tiempo';opt.border_style=opt.border_style||'solid';opt.border_color=opt.border_color||'black';opt.game_fontSize=opt.game_fontSize||'1em';opt.game_fontFamily=opt.game_fontFamily||'Impact, Charcoal, sans-serif';opt.labels_fontSize=opt.labels_fontSize||'.75em';opt.labels_fontFamily=opt.labels_fontFamily||'Impact, Charcoal, sans-serif';opt.totals_fontSize=opt.totals_fontSize||'1.5em';opt.totals_fontFamily=opt.totals_fontFamily||'Impact, Charcoal, sans-serif';opt.lt_fontSize=opt.lt_fontSize||'1em';opt.lt_chance_fontSize=opt.lt_chance_fontSize||'.5em';opt.lt_bkcolor_correct=opt.lt_bkcolor_correct||'green';opt.lt_bkcolor_incorrect=opt.lt_bkcolor_incorrect||'red';opt.lt_bkcolor_enable=opt.lt_bkcolor_enable||'white';opt.lt_bkcolor_disable=opt.lt_bkcolor_disable||'#ddd';opt.bar_style=opt.bar_style||'3px solid #ccc';opt.on_init=opt.on_init||'';opt.on_finish=opt.on_finish||'';opt.on_win=opt.on_win||'';opt.on_lose=opt.on_lose||'';opt.on_error=opt.on_error||'';opt.on_new_time=opt.on_new_time||'';opt.on_new_score=opt.on_new_score||'';opt.on_new_record=opt.on_new_record||'';opt.ar_chances=opt.ar_chances||3;opt.ar_min_answers=opt.ar_min_answers||2;opt.ar_max_answers=opt.ar_max_answers||3;opt.ar_max_answers_total=opt.ar_max_answers_total||5;opt.ar_bomb_or_bonus=opt.ar_bomb_or_bonus||20;opt.ar_random_first_letter=opt.ar_random_first_letter||3;opt.ar_length=opt.ar_length||10;opt.ar_on_ten_seconds=opt.ar_on_ten_seconds||'';opt.ar_on_line_completed=opt.ar_on_line_completed||'';opt.ar_on_line_correct=opt.ar_on_line_correct||'';opt.ar_on_line_incorrect=opt.ar_on_line_incorrect||'';opt.ar_on_first_change=opt.ar_on_first_change||'';opt.ar_on_change=opt.ar_on_change||'';opt.ar_on_loop=opt.ar_on_loop||'';opt.ar_on_bomb=opt.ar_on_bomb||'';opt.ar_on_bonus=opt.ar_on_bonus||'';exec_fn(opt.on_init);function request(url){var xhr=new XMLHttpRequest();xhr.open('GET',url);xhr.responseType='text';xhr.send();xhr.onprogress=function(e){if(e.lengthComputable){console.log(parseInt((e.loaded/e.total)*100)+'%')}else{console.log(lang_loading)}};xhr.onreadystatechange=function(){if(this.readyState==4&&this.status==200){result=relabel(JSON.parse(this.responseText));localStorage.setItem(game_opt.type+'_'+game_opt.lang,result);build()}};xhr.onerror=function(e){exec_fn(game_opt.on_error);throw new Error(lang_load_error)}}function relabel(result){new_result={type:result.type,score:result.score,timer:result.timer,words:result.words,content:(game_opt.type=='story'?result.content:[])};function random_letters(index_total,index_actual,correct,ignore){var total=Math.floor(Math.random()*(game_opt.ar_max_answers-game_opt.ar_min_answers+1))+game_opt.ar_min_answers,final_pick=[],status='disable';function random_select(c){var vowels_es=['a','a','a','a','a','a','a','a','á','á','á','á','e','e','e','e','e','e','e','e','é','é','é','é','i','i','i','i','í','í','o','o','o','o','o','o','ó','ó','ó','u','u','u','ú','ü'];var consonants_es=['b','b','c','c','c','c','d','d','d','d','d','f','g','g','h','h','j','k','l','l','l','l','m','m','n','n','n','n','n','ñ','p','p','q','r','r','r','r','r','s','s','s','s','s','s','t','t','t','t','v','w','x','y','z'];var punctuation_es=['.','.','.','.','.',',',',',',',',',',',';',';',';',':',':',':','¡','¡','¡','¡','!','!','!','!','¿','¿','¿','¿','?','?','?','?','(','(','(',')',')',')','«','»','“','”','-','[',']'];var vowels_en=['a','a','a','a','a','a','a','a','a','e','e','e','e','e','e','e','e','e','e','e','e','i','i','i','i','i','i','i','i','i','o','o','o','o','o','o','o','o','u','u','u','u'];var consonants_en=['b','b','c','c','d','d','d','d','f','f','g','g','g','h','h','j','k','l','l','l','l','m','m','n','n','n','n','n','n','p','p','q','r','r','r','r','r','r','s','s','s','s','t','t','t','t','t','t','v','v','w','w','x','y','y','z'];var punctuation_en=['.','.','.','.','.',',',',',',',',',',',';',';',';',':',':',':','’','’','’','’','!','!','!','!','?','?','?','?','(','(','(',')',')',')','“','”','-','[',']'];vowels=eval('vowels_'+lang);consonants=eval('consonants_'+lang);punctuation=eval('punctuation_'+lang);if(final_pick.length==0){final_pick.push(c);}else{var accepted=true,candidate_type=Math.floor(Math.random()*(game_opt.ar_bomb_or_bonus-0+1))+0;if(candidate_type==0){candidate=0;}else if(candidate_type==1){candidate=1;}else{if(/\w/.test(c.transliterate())){if(/[aeiou]/.test(c.transliterate())){candidate=vowels.sample()}else{candidate=consonants.sample()}}else{candidate=punctuation.sample()}}for(k=0;k<final_pick.length;k+=1){if(candidate_type>1&&candidate.toLowerCase()==(''+final_pick[k]).toLowerCase()){accepted=false;break}}if(accepted){if(candidate_type>1&&/[A-Z]/.test(c.transliterate())){candidate=candidate.toUpperCase()}final_pick.push(candidate)}else{random_select(c)}}}function check_shuffle(array){if(typeof array[0]==='number'){return check_shuffle(array.shuffle())}else{return array}}while(total!=0){random_select(correct);total-=1}if(Math.floor(Math.random()*Math.floor(game_opt.ar_random_first_letter))!=0&&!ignore){final_pick=check_shuffle(final_pick.shuffle());status='enable'}reach=parseInt((index_actual*game_opt.ar_max_answers_total)/index_total);if(game_opt.ar_max_answers<reach&&reach<=game_opt.ar_max_answers_total){game_opt.ar_max_answers=reach;game_opt.ar_min_answers=game_opt.ar_min_answers+1;game_opt.ar_bomb_or_bonus=game_opt.ar_bomb_or_bonus-1;}return{status:status,letters:final_pick}}for(i=0;i<game_opt.size[1]-1;i+=1){if(game_opt.type!='story'){new_result.content.push({visible:true,on_line_completed:game_opt.ar_on_line_completed,on_line_correct:game_opt.ar_on_line_correct,on_line_incorrect:game_opt.ar_on_line_incorrect});}else{new_result.content.unshift({visible:true,on_line_completed:game_opt.ar_on_line_completed,on_line_correct:game_opt.ar_on_line_correct,on_line_incorrect:game_opt.ar_on_line_incorrect})}}if(game_opt.type!='story'){for(i=0;i<game_opt.ar_length;i+=1){var line=result.content.sample(),letters=line.split(''),new_line={visible:new_result.content.length==game_opt.size[1]-1?true:false,on_line_completed:game_opt.ar_on_line_completed,on_line_correct:game_opt.ar_on_line_correct,on_line_incorrect:game_opt.ar_on_line_incorrect,content:[]},content_tmp=[];for(j=0;j<letters.length;j+=1){letter=letters[j];if(letter==' '){content_tmp.push({type:0});}else{r_letters=random_letters(game_opt.ar_length-1,i,letter,(j==0?true:false));content_tmp.push({type:1,status:r_letters.status,correct:letter,picked:r_letters.letters[0],picked_i:0,letters:r_letters.letters,chances:game_opt.ar_chances,loops:0,on_first_change:game_opt.ar_on_first_change,on_change:game_opt.ar_on_change,on_loop:game_opt.ar_on_loop,on_bomb:game_opt.ar_on_bomb,on_bonus:game_opt.ar_on_bonus})}}new_line.content=content_tmp;new_result.content.push(new_line)}}return JSON.stringify(new_result)}function build_game(parent,block_size,lines_before){game_opt.data=JSON.parse(localStorage[opt.type+'_'+game_opt.lang]);function build_letter(div_parent,letter,index){var div=document.createElement('div');div.style.float='left';div.style.padding='0';if(letter.type==0){div.style.width=block_size+'px';div.style.height=block_size+'px';div.innerHTML='<p style="margin:0;padding:0;"></p>'}else{div.setAttribute('data-type',letter.type);div.setAttribute('data-status',letter.status);div.setAttribute('data-correct',letter.correct);div.setAttribute('data-picked',letter.picked);div.setAttribute('data-picked_i',letter.picked_i);div.setAttribute('data-letters',letter.letters.join('¤'));div.setAttribute('data-chances',letter.chances);div.setAttribute('data-loops',letter.loops);div.setAttribute('data-on_first_change',letter.on_first_change);div.setAttribute('data-on_change',letter.on_change);div.setAttribute('data-on_loop',letter.on_loop);div.setAttribute('data-on_bomb',letter.on_bomb);div.setAttribute('data-on_bonus',letter.on_bonus);div.classList.add('foo-letter');div.style.position='relative';div.style.width=(block_size-4)+'px';div.style.height=(block_size-4)+'px';div.style.margin='1px';div.style.cursor='pointer';div.style.border='1px solid #ccc';div.style.borderRadius=(block_size/5)+'px';if(letter.status=='correct'){l_correct=true;div.style.background=game_opt.lt_bkcolor_correct}else if(letter.status=='incorrect'){l_incorrect=true;div.style.background=game_opt.lt_bkcolor_incorrect}else if(letter.status=='enable'){div.style.background=game_opt.lt_bkcolor_enable}else if(letter.status=='disable'){div.style.background=game_opt.lt_bkcolor_disable;}if(letter.picked!=0&&letter.picked!=1){lt_label=letter.picked}else{lt_label='<span style="font-size:'+game_opt.lt_chance_fontSize+';vertical-align:.25em;">'+(letter.letters[0]=='0'?'-':'+')+'</span>3';}div.innerHTML='<p style="font-size:'+opt.lt_fontSize+';margin:'+(block_size/10)+'px 0 0 0;padding:0;text-align:center;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;">'+lt_label+'</p>';div.innerHTML+='<p style="font-size:'+game_opt.lt_chance_fontSize+';position:absolute;bottom:1px;right:5px;margin:0;padding:0;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;">'+letter.chances+'</p>';if('ontouchstart'in document.documentElement==true){div.addEventListener("touchstart",on_touchstart);div.addEventListener("touchend",on_touchend)}else{div.addEventListener("mousedown",on_touchstart);div.addEventListener("mouseup",on_touchend)}}div_parent.appendChild(div)}for(i=0;i<game_opt.data.content.length;i+=1){var line=game_opt.data.content[i],div=document.createElement('div');div.setAttribute('data-visible',line.visible);div.setAttribute('data-on_line_completed',line.on_line_completed);div.setAttribute('data-on_line_correct',line.on_line_correct);div.setAttribute('data-on_line_incorrect',line.on_line_incorrect);div.classList.add('foo-line');div.style.width=(parent.offsetWidth-100)+'px';div.style.height=block_size+'px';div.style.margin='0';div.style.padding='0';div.style.display=line.visible?'block':'none';parent.appendChild(div);if(typeof line.content!=='undefined'){var l_correct=false,l_incorrect=false;for(j=0;j<line.content.length;j+=1){build_letter(div,line.content[j],j);}if(l_correct&&!l_incorrect){foo.data.lines_correct++}if(l_incorrect){foo.data.lines_incorrect++;}}else{div.innerHTML='<div style="clear:both;width:'+block_size+'px;height:'+block_size+'px;margin:0;padding:0;"></div>'}}}function build(){function build_header(i){div=document.createElement('div');div.id='foo-head-'+i;div.style.float='left';div.style.width='33.3333333333%';div.style.height=(opt.margin[0]*block_size)+'px';div_head.appendChild(div)}function build_metric(div){function build_p(i){if(i==1){height=(div.clientHeight/8)*2}else if(i==2){height=(div.clientHeight/8)*4}else{height=(div.clientHeight/8)}p=document.createElement('p');p.style.display='flex';p.style.alignItems='center';p.style.justifyContent='center';p.style.width='100%';p.style.height=height+'px';p.style.margin='0';p.style.padding='0';p.style.textAlign='center';p.style.cssText+='-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;';if(i==1){p.id=div.id+'-label';p.style.fontSize=opt.labels_fontSize;p.style.fontFamily=opt.labels_fontFamily;p.innerHTML=eval('opt.'+div.id.split('-')[1]+'_label');}else if(i==2){p.id=div.id+'-total';p.style.fontSize=opt.totals_fontSize;p.style.fontFamily=opt.totals_fontFamily;if(div.id.split('-')[1]=='score'){game_opt.score=JSON.parse(localStorage[opt.type+'_'+game_opt.lang])[div.id.split('-')[1]];foo.data.score=game_opt.score;p.innerHTML=game_opt.score}else{game_opt.time=JSON.parse(localStorage[opt.type+'_'+game_opt.lang])[div.id.split('-')[1]];foo.data.time=game_opt.time;p.innerHTML=game_opt.time}}div.appendChild(p)}for(i=0;i<4;i+=1){build_p(i)}}validate_array(opt.size,2,3,'Size');validate_array(opt.margin,4,1,'Margin');game_timer=setInterval(new_time,1000);var div_main=document.createElement('div'),div_head=document.createElement('div'),div_adds=document.createElement('div'),div_scrl=document.createElement('div'),div_game=document.createElement('div'),div_pbar=document.createElement('div');var total_width=opt.size[0]+opt.margin[1]+opt.margin[3],total_height=opt.size[1]+opt.margin[0]+opt.margin[2];var block_width=parseInt(document.documentElement.clientWidth/total_width),block_height=parseInt(document.documentElement.clientHeight/total_height),block_size=block_width>block_height?block_height:block_width;document.body.style.width='100vw';document.body.style.height='100vh';document.body.style.display='flex';document.body.style.alignItems='center';document.body.style.justifyContent='center';document.body.style.margin='0';document.body.style.padding='0';div_main.id='foo-main';div_main.style.width=(total_width*block_size)+'px';div_main.style.height=(total_height*block_size)+'px';div_main.style.overflow='hidden';document.body.appendChild(div_main);div_head.id='foo-head';div_head.style.width=(opt.size[0]*block_size)+'px';div_head.style.height=(opt.margin[0]*block_size)+'px';div_head.style.margin='0 '+(opt.margin[1]*block_size)+'px 0 '+(opt.margin[3]*block_size)+'px ';div_main.appendChild(div_head);for(i=0;i<3;i+=1){build_header(i);}if(opt.score_position==opt.timer_position){throw new Error("Score position cannot be the same that timer position.");}var div_score=document.getElementById('foo-head-'+opt.score_position),div_timer=document.getElementById('foo-head-'+opt.timer_position);div_score.id='foo-score';div_timer.id='foo-timer';build_metric(div_score);build_metric(div_timer);div_adds.id='foo-adds';div_adds.style.position='absolute';div_adds.style.display='none';div_adds.style.width=(opt.size[0]*block_size)+'px';div_adds.style.height=(2*block_size)+'px';div_adds.style.top=(div_head.clientHeight+block_size)+'px';div_adds.style.left='50%';div_adds.style.marginRight='-50%';div_adds.style.transform='translate(-50%, 0)';div_adds.style.textAlign='center';div_adds.style.fontFamily=opt.game_fontFamily;div_main.appendChild(div_adds);div_scrl.id='foo-scrl';div_scrl.style.width=(opt.size[0]*block_size)+'px';div_scrl.style.height=(opt.size[1]*block_size+(opt.border[0]+opt.border[1]+4))+'px';div_scrl.style.marginLeft=(block_size)+'px';div_scrl.style.padding='0';div_scrl.style.overflow='hidden';div_main.appendChild(div_scrl);div_game.id='foo-game';div_game.style.width=(opt.size[0]*block_size)+'px';div_game.style.height=(opt.size[1]*block_size)+'px';div_game.style.margin=(opt.border[0]+2)+'px 0 0 0';div_game.style.padding='1px 100px 1px 0';div_game.style.overflowY='scroll';div_game.style.fontSize=game_opt.game_fontSize;div_game.style.fontFamily=opt.game_fontFamily;div_game.style.borderTop=opt.border[0]+'px '+opt.border_style+' '+opt.border_color;div_game.style.borderBottom=opt.border[1]+'px '+opt.border_style+' '+opt.border_color;div_scrl.appendChild(div_game);build_game(div_game,block_size,opt.size[1]-1);div_pbar.id='foo-bar';div_pbar.style.width=0;div_pbar.style.height=0;div_pbar.style.position='absolute';div_pbar.style.bottom=0;div_pbar.style.left=0;div_pbar.style.borderTop=opt.bar_style;div_main.appendChild(div_pbar);resize_bar();div_game.scrollTop=div_game.scrollHeight}game_opt=opt;game_opt.active=true;game_opt.finished=false;foo.data.type=game_opt.type;foo.data.active=game_opt.active;foo.data.finished=game_opt.finished;if(localStorage.getItem(game_opt.type+'_'+game_opt.lang)==null){if(opt.type!='story'&&opt.type!='arcade'){throw new Error("'type' option can only be 'story' or 'arcade'");}if(opt.url==null){throw new Error("'url' option is mandatory.")}else{request(opt.url);}}else{build();}if(foo.data.score<0||foo.data.time<0){setTimeout(lose,300);}if(typeof game_opt.data!=='undefined'&&typeof game_opt.data.content!=='undefined'&&game_opt.data.content!=null){if(game_opt.data.content[game_opt.data.content.length-1].content[0].status=='correct'||game_opt.data.content[game_opt.data.content.length-1].content[0].status=='incorrect'){setTimeout(win,300)}}exec_fn(opt.on_finish)},destroy:function(opt={}){opt.on_init=opt.on_init||'';opt.on_finish=opt.on_finish||'';exec_fn(opt.on_init);var foo_els=document.body.childNodes;for(i=0;i<foo_els.length;i+=1){if(/^foo-/.test(foo_els[i].id)){foo_els[i].parentElement.removeChild(foo_els[i])}}document.body.style.width='';document.body.style.height='';document.body.style.display='';document.body.style.alignItems='';document.body.style.justifyContent='';document.body.style.margin='';document.body.style.padding='';game_opt.active=false;foo.data.active=game_opt.active;clearInterval(game_timer);exec_fn(opt.on_finish)},disable:function(opt={}){opt.on_init=opt.on_init||'';opt.on_finish=opt.on_finish||'';exec_fn(opt.on_init);if(foo.data.active&&document.getElementById('foo-main')!=null){var foo_disable=document.createElement('div');foo_disable.id='foo-disable';foo_disable.style.position='absolute';foo_disable.style.zIndex=1;foo_disable.style.top=0;foo_disable.style.left=0;foo_disable.style.width='100vw';foo_disable.style.height='100vh';foo_disable.style.background='rgba(0,0,0,0.5)';document.getElementById('foo-main').appendChild(foo_disable);game_opt.active=false;foo.data.active=game_opt.active;clearInterval(game_timer)}exec_fn(opt.on_finish)},enable:function(opt={}){opt.on_init=opt.on_init||'';opt.on_finish=opt.on_finish||'';exec_fn(opt.on_init);if(!foo.data.active){var foo_disable=document.getElementById('foo-disable');if(foo_disable!=null){foo_disable.parentElement.removeChild(foo_disable);}game_opt.active=true;foo.data.active=game_opt.active;game_timer=setInterval(new_time,1000)}exec_fn(opt.on_finish)},save:function(opt={}){opt.on_init=opt.on_init||'';opt.on_finish=opt.on_finish||'';exec_fn(opt.on_init);var foo_game=document.getElementById('foo-game'),json_game={type:game_opt.type,score:parseInt(game_opt.score),timer:parseInt(game_opt.time),words:game_opt.data.words,content:[]};for(i=0;i<foo_game.childNodes.length;i+=1){var line=foo_game.childNodes[i],l_visible=line.getAttribute('data-visible'),l_on_line_completed=line.getAttribute('data-on_line_completed'),l_on_line_correct=line.getAttribute('data-on_line_correct'),l_on_line_incorrect=line.getAttribute('data-on_line_incorrect'),json_line={visible:(l_visible=='true'),on_line_completed:l_on_line_completed,on_line_correct:l_on_line_correct,on_line_incorrect:l_on_line_incorrect};if(line.childNodes[0].getAttribute('data-type')!=null){json_line.content=[];for(j=0;j<line.childNodes.length;j+=1){var letter=line.childNodes[j],lt_type=letter.getAttribute('data-type'),lt_status=letter.getAttribute('data-status'),lt_correct=letter.getAttribute('data-correct'),lt_picked=letter.getAttribute('data-picked'),lt_picked_i=letter.getAttribute('data-picked_i'),lt_letters=letter.getAttribute('data-letters'),lt_chances=letter.getAttribute('data-chances'),lt_loops=letter.getAttribute('data-loops'),lt_on_first_change=letter.getAttribute('data-on_first_change'),lt_on_change=letter.getAttribute('data-on_change'),lt_on_loop=letter.getAttribute('data-on_loop'),lt_on_bomb=letter.getAttribute('data-on_bomb'),lt_on_bonus=letter.getAttribute('data-on_bonus'),json_letter={};if(parseInt(lt_type)==1){json_letter={type:parseInt(lt_type),status:lt_status,correct:lt_correct,picked:lt_picked,picked_i:parseInt(lt_picked_i),letters:lt_letters.split('¤'),chances:parseInt(lt_chances),loops:parseInt(lt_loops),on_first_change:lt_on_first_change,on_change:lt_on_change,on_loop:lt_on_loop,on_bomb:lt_on_bomb,on_bonus:lt_on_bonus}}else{json_letter={type:0}}json_line.content.push(json_letter)}}json_game.content.push(json_line)}localStorage.setItem(game_opt.type+'_'+game_opt.lang,JSON.stringify(json_game));exec_fn(opt.on_finish)},wipe:function(opt=''){function wipe_data(){foo.data={type:null,active:null,finished:null,time:null,score:null,past_score:null,line:null,line_correct:null,lines_correct:null,lines_incorrect:null,letter:null,words:[],new_words:[]}}function wipe_me(o,all=false){if(document.getElementById('foo-main')){foo.destroy();wipe_data();game_opt.data=null}localStorage.removeItem(o+'_'+game_opt.lang);localStorage.removeItem(o+'_bombs_bonus_'+game_opt.lang);if(all){localStorage.removeItem(o+'_words_'+game_opt.lang);localStorage.removeItem(o+'_highest_scores_'+game_opt.lang)}}if(opt==''){throw new Error("Function 'wipe' requires 'story' or 'arcade' as parameter.");}if(opt=='all'||opt=='both'){if(opt=='all'){wipe_me('story',true);wipe_me('arcade',true)}else{wipe_me('story');wipe_me('arcade')}}else if(opt=='story'||opt=='arcade'){wipe_me(opt);}else if(opt=='data'){wipe_data()}}}})();