magcore-app-2048
Version:
2048 is one of the world’s most popular puzzles. The rules are simple: you need to shuffle all the bars to one side of the game field. Your goal is to reach 2048.
2 lines • 24.2 kB
JavaScript
!function(t){var e={};function i(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,i),o.l=!0,o.exports}i.m=t,i.c=e,i.d=function(t,e,n){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)i.d(n,o,function(e){return t[e]}.bind(null,o));return n},i.n=function(t){var e=t&&t.__esModule?function(){return t["default"]}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=7)}([function(t,e,i){"use strict";var n=i(2),o=i(10).parse,s=new n;s.query=o(document.location.search.substring(1)),s.config=i(11),s.activePage=null,s.route=function(t,e){var i,n=s.activePage;return!(!t||t.active)&&(t.name,t.id,t.name,t.id,(i=s.activePage)&&i.active&&(i.$node.classList.remove("active"),i.active=!1,s.activePage=null,i.name,i.id,i.name,i.id,i.events["hide"]&&i.emit("hide")),function(t,e){!t||t.active||(t.$node.classList.add("active"),t.active=!0,s.activePage=t,t.name,t.id,t.name,t.id,t.events["show"]&&t.emit("show",{data:e}))}(t,e),this.events["route"]&&this.emit("route",{from:n,to:t}),!0)},t.exports=s},function(t,e,i){"use strict";var n=i(0),o=i(2),s=0;function a(t){var e,i=this;if(t=t||{},this.visible=!0,this.focusable=!0,this.$node=null,this.$body=null,this.parent=null,this.children=[],this.propagate=!!t.propagate,o.call(this),this.$node=t.$node||document.createElement("div"),this.$body=t.$body||this.$node,this.$node.className=this.name+" "+(t.className||""),this.id=t.id||this.$node.id||"cid"+s++,t.parent&&t.parent.add(this),!1===t.visible&&this.hide(),!1===t.focusable&&(this.focusable=!1),this.defaultEvents)for(e in t.events=t.events||{},this.defaultEvents)t.events[e]=t.events[e]||this.defaultEvents[e];t.events&&Object.keys(t.events).forEach(function(e){i.addListener(e,t.events[e])}),t.children&&this.add.apply(this,t.children),this.$node.addEventListener("click",function(t){i.focus(),i.events["click"]&&i.emit("click",t),t.stopPropagation()}),this.name,this.id,this.name,this.id}a.prototype=Object.create(o.prototype),a.prototype.constructor=a,a.prototype.defaultEvents=null,a.prototype.add=function(t){var e;for(e=0;e<arguments.length;e++)t=arguments[e],this.children.push(t),t.parent=this,t.$node&&null===t.$node.parentNode&&this.$body.appendChild(t.$node),t.name,t.id,this.name,this.id,this.name,this.id,t.name,t.id,this.events["add"]&&this.emit("add",{item:t})},a.prototype.remove=function(){this.parent&&(n.activePage.activeComponent===this&&(this.blur(),this.parent.focus()),this.parent.children.splice(this.parent.children.indexOf(this),1)),this.children.forEach(function(t){t.remove()}),this.$node.parentNode.removeChild(this.$node),this.events["remove"]&&this.emit("remove"),this.events={},this.name,this.id,this.name,this.id},a.prototype.focus=function(t){var e=n.activePage,i=e.activeComponent;return!(!this.focusable||this===i)&&(i&&i.blur(),e.activeComponent=i=this,i.$node.classList.add("focus"),this.name,this.id,this.name,this.id,i.events["focus"]&&i.emit("focus",t),!0)},a.prototype.blur=function(){var t=n.activePage,e=t.activeComponent;return this.$node.classList.remove("focus"),this===e?(t.activeComponent=null,this.name,this.id,this.name,this.id,this.events["blur"]&&this.emit("blur"),!0):(this.name,this.id,this.name,this.id,!1)},a.prototype.show=function(t,e){return!this.visible&&(this.$node.classList.remove("hidden"),this.visible=!0,this.name,this.id,this.name,this.id,this.events["show"]&&this.emit("show",t),"function"==typeof e&&setTimeout(e),!0)},a.prototype.hide=function(t){return!!this.visible&&(this.$node.classList.add("hidden"),this.visible=!1,this.name,this.id,this.name,this.id,this.events["hide"]&&this.emit("hide"),"function"==typeof t&&setTimeout(t),!0)},t.exports=a},function(t,e,i){"use strict";function n(){this.events={}}n.prototype={addListener:function(t,e){this.events[t]=this.events[t]||[],this.events[t].push(e)},once:function(t,e){var i=this;this.events[t]=this.events[t]||[],this.events[t].push(function n(){i.removeListener(t,n),e.apply(i,arguments)})},addListeners:function(t){var e;for(e in t)t.hasOwnProperty(e)&&this.addListener(e,t[e])},removeListener:function(t,e){this.events[t]&&(this.events[t]=this.events[t].filter(function(t){return t!==e}),0===this.events[t].length&&(this.events[t]=void 0))},emit:function(t){var e,i=this.events[t];if(i)for(e=0;e<i.length;e++)i[e].apply(this,Array.prototype.slice.call(arguments,1))}},n.prototype.constructor=n,t.exports=n},function(t,e,i){"use strict";var n=i(0);t.exports=function(t){var e=document.createElement("link");e.rel="stylesheet",e.href="css/"+t+"."+n.metrics.height+".css",document.head.appendChild(e)}},function(t,e,i){"use strict";t.exports=i(19),t.exports.prototype.name="stb-component-page"},function(t,e,i){"use strict";t.exports={backspace:8,tab:9,enter:13,escape:27,space:32,pageUp:33,pageDown:34,end:35,home:36,left:37,up:38,right:39,down:40,insert:45,del:46}},function(t,e,i){"use strict";function n(t,e){this.x=t.x,this.y=t.y,this.value=e||2,this.previousPosition=null,this.mergedFrom=null}n.prototype.savePosition=function(){this.previousPosition={x:this.x,y:this.y}},n.prototype.updatePosition=function(t){this.x=t.x,this.y=t.y},n.prototype.serialize=function(){return{position:{x:this.x,y:this.y},value:this.value}},t.exports=n},function(t,e,i){"use strict";var n=i(8);i(17),n.once("load",function(){n.pages={init:i(18),main:i(20)},setTimeout(function(){n.ready(),n.route(n.pages.main)},300)})},function(t,e,i){"use strict";var n=i(9);t.exports=n},function(t,e,i){"use strict";var n=i(0),o=i(12);window.core=window.parent.getCoreInstance(window,n),i(13),i(14),i(3)("sdk"),i(16),i(3)("app"),n.platform="mag",n.ready=function(){window.core.call("app:ready")},n.exit=function(){n.events["exit"]&&n.emit("exit"),core.call("exit")},o.load=function(t){document.body.setAttribute("platform",n.platform),core.ready?n.events["load"]&&n.emit("load",{}):core.once("load",function(){n.events[t.type]&&n.emit(t.type,t)})},o.contextmenu=function(t){t.preventDefault()},Object.keys(o).forEach(function(t){window.addEventListener(t,o[t])}),t.exports=n},function(t,e,i){"use strict";t.exports={parse:function(t){var e={};return t.split("&").forEach(function(t){2===(t=t.split("=")).length&&(e[t[0]]=decodeURIComponent(t[1]))}),e},stringify:function(t){var e=[];return Object.keys(t).forEach(function(i){e.push(i+"="+encodeURIComponent(t[i]))}),e.join("&")}}},function(t,e,i){"use strict";t.exports={}},function(t,e,i){"use strict";var n=i(0);t.exports={DOMContentLoaded:function(t){n.events["dom"]&&n.emit("dom",t)},load:function(t){n.events[t.type]&&n.emit(t.type,t)},unload:function(t){n.events[t.type]&&n.emit(t.type,t)},error:function(t){},keydown:function(t){var e,i=n.activePage,o={code:t.keyCode,stop:!1};t.ctrlKey&&(o.code+="c"),t.altKey&&(o.code+="a"),t.shiftKey&&(o.code+="s"),(e=i.activeComponent)&&e!==i&&(e.events[t.type]&&e.emit(t.type,o,t),!o.stop&&e.propagate&&e.parent&&e.parent.events[t.type]&&e.parent.emit(t.type,o,t)),o.stop||(i.events[t.type]&&i.emit(t.type,o,t),t.stop||n.events[t.type]&&n.emit(t.type,o,t))},keypress:function(t){var e=n.activePage;e.activeComponent&&e.activeComponent!==e&&e.activeComponent.events[t.type]&&e.activeComponent.emit(t.type,t)},mousewheel:function(t){var e=n.activePage;e.activeComponent&&e.activeComponent!==e&&e.activeComponent.events[t.type]&&e.activeComponent.emit(t.type,t),t.stop||e.events[t.type]&&e.emit(t.type,t)}}},function(t,e,i){"use strict";if(!document.documentElement.classList){var n=Array.prototype,o=n.indexOf,s=n.slice,a=n.push,r=n.splice,c=n.join;window.DOMTokenList=function(t){if(this._element=t,t.className!==this._classCache){if(this._classCache=t.className,!this._classCache)return;var e,i=this._classCache.replace(/^\s+|\s+$/g,"").split(/\s+/);for(e=0;e<i.length;e++)a.call(this,i[e])}},window.DOMTokenList.prototype={add:function(t){this.contains(t)||(a.call(this,t),this._element.className=s.call(this,0).join(" "))},contains:function(t){return-1!==o.call(this,t)},item:function(t){return this[t]||null},remove:function(t){var e=o.call(this,t);-1!==e&&(r.call(this,e,1),this._element.className=s.call(this,0).join(" "))},toString:function(){return c.call(this," ")},toggle:function(t){return this.contains(t)?this.remove(t):this.add(t),this.contains(t)}},Object.defineProperty(Element.prototype,"classList",{get:function(){return new window.DOMTokenList(this)}})}},function(t,e,i){"use strict";var n=i(0),o=i(15);n.metrics=o[n.query.screenHeight]||o[screen.height]||o[720],n.metrics.availHeight=n.metrics.height-(n.metrics.availTop+n.metrics.availBottom),n.metrics.availWidth=n.metrics.width-(n.metrics.availLeft+n.metrics.availRight)},function(t,e,i){"use strict";t.exports={480:{height:480,width:720,availTop:24,availBottom:24,availRight:32,availLeft:48},576:{height:576,width:720,availTop:24,availBottom:24,availRight:26,availLeft:54},720:{height:720,width:1280,availTop:30,availBottom:30,availRight:40,availLeft:40},1080:{height:1080,width:1920,availTop:45,availBottom:45,availRight:60,availLeft:60}}},function(t,e,i){"use strict";var n,o=i(0);(n=document.createElement("link")).rel="stylesheet",n.href=window.core.theme.path+o.metrics.height+".css",document.head.appendChild(n),t.exports=n},function(t,e,i){"use strict";!function(){var t,e=0,i=["webkit","moz"];for(t=0;t<i.length&&!window.requestAnimationFrame;++t)window.requestAnimationFrame=window[i[t]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[i[t]+"CancelAnimationFrame"]||window[i[t]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(t){var i,n=(new Date).getTime(),o=Math.max(0,16-(n-e));return i=window.setTimeout(function(){t(n+o)},o),e=n+o,i}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(t){clearTimeout(t)})}()},function(t,e,i){"use strict";var n=new(i(4))({$node:window.pageInit});t.exports=n},function(t,e,i){"use strict";var n=i(1);function o(t){t=t||{},this.active=!1,this.activeComponent=null,n.call(this,t),this.active=this.$node.classList.contains("active"),null===this.$node.parentNode&&document.body.appendChild(this.$node),this.page=this}o.prototype=Object.create(n.prototype),o.prototype.constructor=o,o.prototype.name="spa-component-page",t.exports=o},function(t,e,i){"use strict";var n,o=new(i(4))({$node:document.getElementById("pageMain")}),s=i(21),a=i(22),r=i(23),c=i(25),l=i(27),u=i(29),h=i(30),p=new a({$node:document.getElementById("game"),focusable:!0}),d=new c({visible:!1,className:"gameModal",events:{show:function(){this.children[1].focus()},hide:function(){p.focus()}},children:[new a({className:"modalTitle"}),new r({value:"Restart",events:{keydown:function(t){switch(r.prototype.defaultEvents.keydown.call(this,t),t.code){case s.right:d.children[2].focus();break;case s.exit:case s.back:d.hide(),n.restart()}},click:function(){d.hide(),n.restart()}}}),new r({value:"exit",events:{keydown:function(t){switch(r.prototype.defaultEvents.keydown.call(this,t),t.code){case s.left:d.children[1].focus();break;case s.exit:case s.back:d.hide(),n.restart()}},click:function(){core.call("exit")}}})]}),m=new c({visible:!1,className:"gameModal",events:{show:function(){m.children[1].focus()},hide:function(){p.focus()}},children:[new a({className:"modalTitle"}),new r({value:"Continue",events:{keydown:function(t){switch(r.prototype.defaultEvents.keydown.call(this,t),t.code){case s.right:m.children[2].focus();break;case s.exit:case s.back:m.hide(),n._keepPlaying()}},click:function(){m.hide(),n._keepPlaying()}}}),new r({value:"Restart",events:{keydown:function(t){switch(r.prototype.defaultEvents.keydown.call(this,t),t.code){case s.left:m.children[1].focus();break;case s.exit:case s.back:m.hide(),n._keepPlaying()}},click:function(){m.hide(),n.restart()}}})]}),f={$left:document.getElementById("leftKey"),$right:document.getElementById("rightKey"),$top:document.getElementById("topKey"),$bot:document.getElementById("botKey"),last:null,timer:0};o.add(d),o.add(m),d.children[0].$node.innerText="Game over! :(",m.children[0].$node.innerText="You win :)",o.addListener("show",function(){window.game=n=new l(4,u,h),p.focus(),n.actuator.addListeners({lose:function(){d.show()},win:function(){m.show()}})}),p.addListener("keydown",function(t){switch(t.code){case s.down:case s.up:case s.left:case s.right:n.move(t.code),function(t){f.last&&f.last.classList.remove("click");switch(clearTimeout(f.timer),t){case s.left:f.last=f.$left;break;case s.right:f.last=f.$right;break;case s.up:f.last=f.$top;break;case s.down:f.last=f.$bot}f.last.classList.add("click"),f.timer=setTimeout(function(){f.last.classList.remove("click")},100)}(t.code);break;case s.f2:n.restart();break;case s.back:core.notify({title:"Do you want to exit?",icon:"alert",type:"",buttons:[{value:"Exit",click:function(){core.call("exit")}},{value:"Cancel",click:function(){}}],timeout:5e3})}}),o.addListener("unload",function(){n.storageManager._saveSate(n.serialize())}),t.exports=o},function(t,e,i){"use strict";var n=i(5);n.back=n.backspace,n.channelNext=n.tab,n.channelPrev=n.tab+"s",n.ok=n.enter,n.exit=n.escape,n.volumeUp=107,n.volumeDown=109,n.f1="112c",n.f2="113c",n.f3="114c",n.f4="115c",n.refresh="116c",n.frame="117c",n.phone="119c",n.set="120c",n.tv="121c",n.menu="122c",n.app="123c",n.rewind="66a",n.forward="70a",n.audio="71a",n.standby="74a",n.keyboard="76a",n.usbMounted="80a",n.usbUnmounted="81a",n.playPause="82a",n.play=-1,n.pause=-1,n.stop="83a",n.power="85a",n.record="87a",n.info="89a",n.mute="192a",n.digit0=48,n.digit1=49,n.digit2=50,n.digit3=51,n.digit4=52,n.digit5=53,n.digit6=54,n.digit7=55,n.digit8=56,n.digit9=57,t.exports=n},function(t,e,i){"use strict";var n=i(1);function o(t){(t=t||{}).focusable=t.focusable||!1,n.call(this,t)}o.prototype=Object.create(n.prototype),o.prototype.constructor=o,o.prototype.name="spa-component-panel",t.exports=o},function(t,e,i){"use strict";t.exports=i(24),t.exports.prototype.name="stb-component-button"},function(t,e,i){"use strict";var n=i(1),o=i(5);function s(t){t=t||{},n.call(this,t),t.icon&&(this.$icon=this.$body.appendChild(document.createElement("div")),this.$icon.className="icon "+t.icon),this.$text=this.$body.appendChild(document.createElement("div")),this.$text.classList.add("text"),t.value&&(this.$text.innerText=t.value)}s.prototype=Object.create(n.prototype),s.prototype.constructor=s,s.prototype.name="spa-component-button",s.prototype.clickDuration=200,s.prototype.defaultEvents={click:function(){var t=this;this.$node.classList.add("click"),setTimeout(function(){t.$node.classList.remove("click")},this.clickDuration)},keydown:function(t){t.code===o.enter&&this.events["click"]&&this.emit("click",{event:t})}},t.exports=s},function(t,e,i){"use strict";t.exports=i(26),t.exports.prototype.name="stb-component-modal"},function(t,e,i){"use strict";var n=i(1);function o(t){(t=t||{}).$body=document.createElement("div"),t.$body.className="body",n.call(this,t),this.$node.appendChild(document.createElement("div").appendChild(this.$body).parentNode)}o.prototype=Object.create(n.prototype),o.prototype.constructor=o,o.prototype.name="spa-component-modal",t.exports=o},function(t,e,i){"use strict";var n=i(28),o=i(6);function s(t,e,i){this.size=t,this.storageManager=new i,this.actuator=new e,this.startTiles=2,this.setup()}s.prototype.restart=function(){this.storageManager.clearGameState(),this.actuator.continueGame(),this.setup()},s.prototype._keepPlaying=function(){this.keepPlaying=!0,this.actuator.continueGame()},s.prototype.isGameTerminated=function(){return this.over||this.won&&!this.keepPlaying},s.prototype.setup=function(){var t=this.storageManager.getGameState();t?(this.grid=new n(t.grid.size,t.grid.cells),this.score=t.score,this.over=t.over,this.won=t.won,this.keepPlaying=t.keepPlaying):(this.grid=new n(this.size),this.score=0,this.over=!1,this.won=!1,this.keepPlaying=!1,this.addStartTiles()),this.actuate()},s.prototype.addStartTiles=function(){var t;for(t=0;t<this.startTiles;t++)this.addRandomTile()},s.prototype.addRandomTile=function(){var t,e;this.grid.cellsAvailable()&&(t=Math.random()<.9?2:4,e=new o(this.grid.randomAvailableCell(),t),this.grid.insertTile(e))},s.prototype.actuate=function(){this.storageManager.getBestScore()<this.score&&this.storageManager.setBestScore(this.score),this.over?this.storageManager.clearGameState():this.storageManager.setGameState(this.serialize()),this.actuator.actuate(this.grid,{score:this.score,over:this.over,won:this.won,bestScore:this.storageManager.getBestScore(),terminated:this.isGameTerminated()})},s.prototype.serialize=function(){return{grid:this.grid.serialize(),score:this.score,over:this.over,won:this.won,keepPlaying:this.keepPlaying}},s.prototype.prepareTiles=function(){this.grid.eachCell(function(t,e,i){i&&(i.mergedFrom=null,i.savePosition())})},s.prototype.moveTile=function(t,e){this.grid.cells[t.x][t.y]=null,this.grid.cells[e.x][e.y]=t,t.updatePosition(e)},s.prototype.move=function(t){var e,i,n,s,a,r=this;t={38:0,39:1,40:2,37:3}[t],this.isGameTerminated()||(n=this.getVector(t),s=this.buildTraversals(n),a=!1,this.prepareTiles(),s.x.forEach(function(t){s.y.forEach(function(s){var c,l,u;e={x:t,y:s},(i=r.grid.cellContent(e))&&(l=r.findFarthestPosition(e,n),(u=r.grid.cellContent(l.next))&&u.value===i.value&&!u.mergedFrom?((c=new o(l.next,2*i.value)).mergedFrom=[i,u],r.grid.insertTile(c),r.grid.removeTile(i),i.updatePosition(l.next),r.score+=c.value,2048===c.value&&(r.won=!0)):r.moveTile(i,l.farthest),r.positionsEqual(e,i)||(a=!0))})}),a&&(this.addRandomTile(),this.movesAvailable()||(this.over=!0),this.actuate()))},s.prototype.getVector=function(t){return{0:{x:0,y:-1},1:{x:1,y:0},2:{x:0,y:1},3:{x:-1,y:0}}[t]},s.prototype.buildTraversals=function(t){var e,i={x:[],y:[]};for(e=0;e<this.size;e++)i.x.push(e),i.y.push(e);return 1===t.x&&(i.x=i.x.reverse()),1===t.y&&(i.y=i.y.reverse()),i},s.prototype.findFarthestPosition=function(t,e){var i;do{t={x:(i=t).x+e.x,y:i.y+e.y}}while(this.grid.withinBounds(t)&&this.grid.cellAvailable(t));return{farthest:i,next:t}},s.prototype.movesAvailable=function(){return this.grid.cellsAvailable()||this.tileMatchesAvailable()},s.prototype.tileMatchesAvailable=function(){var t,e,i,n,o,s,a;for(e=0;e<this.size;e++)for(i=0;i<this.size;i++)if(t=this.grid.cellContent({x:e,y:i}))for(s=0;s<4;s++)if(n={x:e+(a=this.getVector(s)).x,y:i+a.y},(o=this.grid.cellContent(n))&&o.value===t.value)return!0;return!1},s.prototype.positionsEqual=function(t,e){return t.x===e.x&&t.y===e.y},t.exports=s},function(t,e,i){"use strict";var n=i(6);function o(t,e){this.size=t,this.cells=e?this.fromState(e):this.empty()}o.prototype.empty=function(){for(var t=[],e=0;e<this.size;e++)for(var i=t[e]=[],n=0;n<this.size;n++)i.push(null);return t},o.prototype.fromState=function(t){for(var e=[],i=0;i<this.size;i++)for(var o=e[i]=[],s=0;s<this.size;s++){var a=t[i][s];o.push(a?new n(a.position,a.value):null)}return e},o.prototype.randomAvailableCell=function(){var t=this.availableCells();if(t.length)return t[Math.floor(Math.random()*t.length)]},o.prototype.availableCells=function(){var t=[];return this.eachCell(function(e,i,n){n||t.push({x:e,y:i})}),t},o.prototype.eachCell=function(t){for(var e=0;e<this.size;e++)for(var i=0;i<this.size;i++)t(e,i,this.cells[e][i])},o.prototype.cellsAvailable=function(){return!!this.availableCells().length},o.prototype.cellAvailable=function(t){return!this.cellOccupied(t)},o.prototype.cellOccupied=function(t){return!!this.cellContent(t)},o.prototype.cellContent=function(t){return this.withinBounds(t)?this.cells[t.x][t.y]:null},o.prototype.insertTile=function(t){this.cells[t.x][t.y]=t},o.prototype.removeTile=function(t){this.cells[t.x][t.y]=null},o.prototype.withinBounds=function(t){return t.x>=0&&t.x<this.size&&t.y>=0&&t.y<this.size},o.prototype.serialize=function(){for(var t=[],e=0;e<this.size;e++)for(var i=t[e]=[],n=0;n<this.size;n++)i.push(this.cells[e][n]?this.cells[e][n].serialize():null);return{size:this.size,cells:t}},t.exports=o},function(t,e,i){"use strict";var n=i(2);function o(){this.tileContainer=document.getElementById("tile-container"),this.scoreContainer=document.getElementById("score-container"),this.bestContainer=document.getElementById("best-container"),this.score=0,n.call(this)}o.prototype=Object.create(n.prototype),o.prototype.constructor=o,o.prototype.actuate=function(t,e){var i=this;window.requestAnimationFrame(function(){i.clearContainer(i.tileContainer),t.cells.forEach(function(t){t.forEach(function(t){t&&i.addTile(t)})}),i.updateScore(e.score),i.updateBestScore(e.bestScore),e.terminated&&(e.over?i.emit("lose"):e.won&&i.emit("win"))})},o.prototype.continueGame=function(){this.clearMessage()},o.prototype.clearContainer=function(t){for(;t.firstChild;)t.removeChild(t.firstChild)},o.prototype.addTile=function(t){var e=this,i=document.createElement("div"),n=document.createElement("div"),o=t.previousPosition||{x:t.x,y:t.y},s=this.positionClass(o),a=["tile","tile-"+t.value,s];t.value>2048&&a.push("tile-super"),this.applyClasses(i,a),n.classList.add("tile-inner"),n.textContent=t.value,t.previousPosition?window.requestAnimationFrame(function(){a[2]=e.positionClass({x:t.x,y:t.y}),e.applyClasses(i,a)}):t.mergedFrom?(a.push("tile-merged"),this.applyClasses(i,a),t.mergedFrom.forEach(function(t){e.addTile(t)})):(a.push("tile-new"),this.applyClasses(i,a)),i.appendChild(n),this.tileContainer.appendChild(i)},o.prototype.applyClasses=function(t,e){t.setAttribute("class",e.join(" "))},o.prototype.normalizePosition=function(t){return{x:t.x+1,y:t.y+1}},o.prototype.positionClass=function(t){return"tile-position-"+(t=this.normalizePosition(t)).x+"-"+t.y},o.prototype.updateScore=function(t){this.clearContainer(this.scoreContainer),this.score=t,this.scoreContainer.textContent=this.score},o.prototype.updateBestScore=function(t){this.bestContainer.textContent=t},o.prototype.message=function(t){var e=t?"game-won":"game-over",i=t?"You win!":"Game over!";this.messageContainer.classList.add(e),this.messageContainer.getElementsByTagName("p")[0].textContent=i},o.prototype.clearMessage=function(){},t.exports=o},function(t,e,i){"use strict";function n(){this.bestScoreKey="bestScore",this.gameStateKey="gameState",this.real=this.localStorageSupported(),this.storage=this.real||window.fakeStorage}window.fakeStorage={_data:{},setItem:function(t,e){return this._data[t]=String(e)},getItem:function(t){return this._data.hasOwnProperty(t)?this._data[t]:void 0},removeItem:function(t){return delete this._data[t]},clear:function(){return this._data={}}},n.prototype.localStorageSupported=function(){var t=window.top.stbStorage||window.localStorage||window.top.localStorage;try{return t.setItem("test","1"),t.removeItem("test"),t}catch(t){return!1}},n.prototype.getBestScore=function(){return this.storage.getItem(this.bestScoreKey)||0},n.prototype.setBestScore=function(t){this.storage.setItem(this.bestScoreKey,t)},n.prototype.getGameState=function(){if(this.real){var t=this.storage.getItem(this.gameStateKey);return t?JSON.parse(t):null}return this._getGameState()},n.prototype.setGameState=function(t){this.storage.setItem(this.gameStateKey,JSON.stringify(t))},n.prototype.clearGameState=function(){this.real?this.storage.removeItem(this.gameStateKey):this._clearGameState()},n.prototype._saveSate=function(t){window.top.gSTB.SaveUserData("2048.json",JSON.stringify({game:t,best:this.storage.getItem(this.bestScoreKey)}))},n.prototype._clearGameState=function(){window.top.gSTB.SaveUserData("2048.json",JSON.stringify({best:this.storage.getItem(this.bestScoreKey)}))},n.prototype._getGameState=function(){var t=window.top.gSTB.LoadUserData("2048.json");try{(t=JSON.parse(t))&&t.best&&this.setBestScore(t.best),t=t&&t.game?t.game:null}catch(e){t=null}return t},t.exports=n}]);
//# sourceMappingURL=main.js.map