jocly
Version:
Board games 3D/2D UI and AI web library
1 lines • 16.1 kB
JavaScript
"use strict";function _slicedToArray(e,t){return _arrayWithHoles(e)||_iterableToArrayLimit(e,t)||_unsupportedIterableToArray(e,t)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(e,t):void 0}}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function _iterableToArrayLimit(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,a,s=[],m=!0,u=!1;try{if(i=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;m=!1}else for(;!(m=(n=i.call(r)).done)&&(s.push(n.value),s.length!==t);m=!0);}catch(e){u=!0,o=e}finally{try{if(!m&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(u)throw o}}return s}}function _arrayWithHoles(e){if(Array.isArray(e))return e}!function(){function e(e,t,r,n,o){var i=function(){this.g={}};Object.assign(i.prototype,r.Game.prototype,n.model.Game);var a=function(){};Object.assign(a.prototype,r.Board.prototype,n.model.Board),i.prototype.mBoardClass=a;var s=function(e){this.Init(e)};Object.assign(s.prototype,r.Move.prototype,n.model.Move),i.prototype.mMoveClass=s,i.prototype.config=o.config,"browser"==u&&(i.prototype.config.baseURL=SystemJS.getConfig().baseURL,i.prototype.config.view.fullPath=SystemJS.getConfig().baseURL+"games/"+t.module),i.prototype.module=t.module,i.prototype.name=e;var m={game:o.config.model.gameOptions,view:o.config.view};c[e]={gameClass:i,initArgs:m};var h=new i;return h.Init(m),h}function t(e,t){var i=c[e];if(i){return new Promise(function(e,t){var r=new i.gameClass;r.Init(i.initArgs),e(r)})}return"worker"==u?n(e,t):"node"==u?r(e,t):o(e,t)}function r(t,r){Date.now();return r=r||{},new Promise(function(r,n){var o=require("./jocly-allgames.js").games,i=o[t];if(!i)return n(new Error("Game "+t+" not found"));var a=require("./jocly.game.js"),s=require("./games/"+i.module+"/"+t+"-model.js"),m=require("./games/"+i.module+"/"+t+"-config.js");r(e(t,i,a,s,m))})}function n(t,r){Date.now();return r=r||{},new Promise(function(r,n){importScripts("jocly-allgames.js");var o=exports.games,i=o[t];if(!i)return n(new Error("Game "+t+" not found"));var a=exports,s=exports={};importScripts("jocly.game.js");var m=exports={};importScripts("games/"+i.module+"/"+t+"-model.js");var u=exports={};importScripts("games/"+i.module+"/"+t+"-config.js"),exports=a,r(e(t,i,s,m,u))})}function o(t,r){return r=r||{},new Promise(function(r,n){exports.listGames().then(function(o){var i=o[t];if(!i)return n(new Error("Game "+t+" not found"));var a=[SystemJS.import("jocly.game.js"),SystemJS.import("games/"+i.module+"/"+t+"-model.js"),SystemJS.import("games/"+i.module+"/"+t+"-config.js")];Promise.all(a).then(function(n){var o=_slicedToArray(n,3),a=o[0],s=o[1],m=o[2],u=e(t,i,a,s,m);r(u)},function(e){n(e)})},n)})}function i(e,t){this.gameName=e,this.game=t,this.iframe=null,this.id=++p,f[this.id]=this}function a(e){var t=e.data,r=f[t.joclyEmbeddedGameId];if(r){var n=t.replyId;if(n){var o=l[n];if(o)if(delete l[n],"error"==t.message.type){var i=new Error(t.message.error.message,t.message.error.fileName,t.message.error.lineNumber);o(i)}else o(null,t.message)}else"machine-progress"==t.message.type&&r.machineProgressListener&&r.machineProgressListener(t.message.progress)}}function s(e,t,r){var n={message:t};if(r){var o=++g;n.replyId=o,l[o]=r}try{e.iframe.contentWindow.postMessage(n,"*")}catch(e){console.error("Message",t,"could not be posted:",e)}}function m(e,t,r){return r=Array.from(r||[]),new Promise(function(n,o){s(e,{type:"method",id:e.id,methodName:t,args:r},function(e,t){e?o(e):n.apply(null,t&&t.args||[])})})}var u="browser";"undefined"==typeof WorkerGlobalScope&&"undefined"==typeof SystemJS?u="node":"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope&&(u="worker");var c={};exports.listGames=function(){return new Promise(function(e,t){"browser"==u||"worker"==u?SystemJS.import("jocly-allgames.js").then(function(t){var r={},n=SystemJS.getConfig().baseURL;for(var o in t.games){var i=Object.assign({},t.games[o]);i.thumbnail=n+"games/"+i.module+"/"+i.thumbnail,r[o]=i}e(r)},function(e){console.warn("Could not load Jocly games",e),t(e)}):e(require("./jocly-allgames.js").games)})},exports.createMatch=function(e){return new Promise(function(r,n){t(e).then(function(t){var n=new i(e,t);r(n)},function(e){n(e)})})},exports.getGameConfig=function(e){return new Promise(function(t,r){exports.listGames().then(function(n){var o=n[e];if(!o)return r(new Error("Game "+e+" not found"));"undefined"!=typeof SystemJS?SystemJS.import("games/"+o.module+"/"+e+"-config.js").then(function(e){e.config.view.fullPath=SystemJS.getConfig().baseURL+"games/"+o.module,t(e.config)}).catch(r):t(require("./games/"+o.module+"/"+e+"-config.js").config)},r)})};var h=!1,f={},g=0,l={},p=0;i.prototype.attachElement=function(e,t){if("node"==u)return Promise.reject(new Error("attachElement(): not supported in node.js"));if(!this.game||this.iframe)return Promise.reject(new Error("attachElement(): match is already attached"));t=t||{};var r=this;return new Promise(function(n,o){h||(h=!0,window.addEventListener("message",a)),r.element=e;var i=document.createElement("iframe"),m={name:"jocly-embedded-"+r.id,frameborder:0,src:SystemJS.getConfig().baseURL+"jocly.embed.html",width:"100%",height:"100%"};Object.keys(m).forEach(function(e){i.setAttribute(e,m[e])}),Object.assign(i.style,{position:"absolute",top:0,right:0,bottom:0,left:0,whiteSpace:"normal"});var u=document.createElement("div");Object.assign(u.style,{position:"relative",whiteSpace:"nowrap",width:"100%",height:"100%"}),u.appendChild(i),e.appendChild(u),r.iframe=i,i.onload=function(){s(r,{type:"init",id:r.id,gameName:r.gameName,playedMoves:r.game.mPlayedMoves,options:t},function(e,t){e?o(e):(r.game=null,n())})}})},i.prototype.detachElement=function(){if("node"==u)return Promise.reject(new Error("detachElement(): not supported in node.js"));if(this.game||!this.iframe)return Promise.reject(new Error("detachElement(): match is not attached"));var e=this;return new Promise(function(r,n){Promise.all([t(e.gameName),m(e,"getPlayedMoves")]).then(function(t){var r=_slicedToArray(t,2),n=r[0],o=r[1];return e.game=n,n.Load({playedMoves:o}),m(e,"destroy")}).then(function(){for(;e.element.firstChild;)e.element.removeChild(e.element.firstChild);delete e.element,delete e.iframe,r()}).catch(n)})},i.prototype.getTurn=function(){if(this.game){var e=this;return new Promise(function(t,r){t(e.game.GetWho())})}return m(this,"getTurn",arguments)},i.prototype.getMoveString=function(e,t){if(this.game){var r=this;return new Promise(function(n,o){n(Array.isArray(e)?e.map(function(e){return r.game.CreateMove(e).ToString(t)}):r.game.CreateMove(e).ToString(t))})}return m(this,"getMoveString",arguments)},i.prototype.pickMove=function(e){if(this.game){var t=this;return new Promise(function(r,n){t.game.mBoard.mMoves&&0!=t.game.mBoard.mMoves.length||t.game.mBoard.GenerateMoves(t.game),r(t.game.GetBestMatchingMove(e,t.game.mBoard.mMoves))})}return m(this,"pickMove",arguments)},i.prototype.playMove=function(e){if("node"==u||!this.iframe&&!this.area)return this.applyMove(e);if(this.game){var t=this;return new Promise(function(r,n){t.game.PlayMove(e).then(function(){t.game.InvertWho();var e=t.game.GetFinished();t.game.DisplayBoard(),r({finished:!!e,winner:e})}).catch(function(e){n(e)})})}return m(this,"playMove",arguments)},i.prototype.applyMove=function(e){if(this.game){var t=this;return new Promise(function(r,n){t.game.ApplyMove(e),t.game.InvertWho();var o=t.game.GetFinished();t.area&&t.game.DisplayBoard(),r({finished:!!o,winner:o})})}return m(this,"applyMove",arguments)},i.prototype.destroy=function(e){var t=this;if(this.game){return new Promise(function(e,r){t.area&&t.game.GameDestroyView(),e()})}return this.detachElement().then(function(){delete f[t.id]})},i.prototype.getPlayedMoves=function(){if(this.game){var e=this;return new Promise(function(t,r){t(Array.from(e.game.mPlayedMoves))})}return m(this,"getPlayedMoves")},i.prototype.userTurn=function(){if("node"==u)return Promise.reject(new Error("userTurn(): not supported in node.js"));if(!this.area&&!this.iframe)return Promise.reject(new Error("userTurn(): match is not attached to DOM element"));if(this.game){var e,t=function(){r.game.HumanMove=e,delete r.userTurnReject},r=this;return new Promise(function(t,n){var o;o=r.userTurnReject?r.abortUserTurn():new Promise(function(e){e()}),o.then(function(){},function(){}).then(function(){function o(e){function o(){r.game.InvertWho();var n=r.game.GetFinished();r.game.DisplayBoard(),t({move:e,finished:!!n,winner:n})}!1===r.game.config.view.animateSelfMoves?(r.game.ApplyMove(e),o()):r.game.PlayMove(e).then(o,n)}r.userTurnReject=n,e=r.game.HumanMove,r.game.HumanMove=o,r.game.HumanTurn()})}).then(function(e){return t(),e}).catch(function(e){throw t(),e})}return m(this,"userTurn")},i.prototype.abortUserTurn=function(e){if("node"==u)return Promise.reject(new Error("abortUserTurn(): not supported in node.js"));if(!this.area&&!this.iframe)return Promise.reject(new Error("abortUserTurn(): match is not attached to DOM element"));if(this.game){var t=this;if(!this.userTurnReject)return e?Promise.reject(new Error("abortUserTurn(): not in user input mode")):Promise.resolve();return new Promise(function(e,r){t.game.HumanTurnEnd();var n=t.userTurnReject;delete t.userTurnReject,n(new Error("User input aborted")),e()})}return m(this,"abortUserTurn",arguments)},i.prototype.machineSearch=function(e){var t=this;if(this.game){var r=function(){t.game.MachineMove=n,t.game.MachineProgress=o,delete t.machineSearchReject};e=Object.assign({level:t.game.config.model.levels[0],threaded:!0},e);var n,o;return new Promise(function(r,i){var a;a=t.machineSearchReject?t.abortMachineSearch():new Promise(function(e){e()}),a.then(function(){},function(){}).then(function(){function a(e){var n=e.move;delete e.move;var o=t.game.GetFinished();r({move:n,finished:!!o,winner:o})}function s(e){t.area&&window.parent.postMessage({joclyEmbeddedGameId:t.id,message:{type:"machine-progress",progress:e}},"*")}t.machineSearchReject=i,n=t.game.MachineMove,t.game.MachineMove=a,o=t.game.MachineProgress,t.game.MachineProgress=s,t.game.StartMachine(e)})}).then(function(e){return r(),e}).catch(function(e){throw r(),e})}var i=function(){delete t.machineProgressListener};return e=e||{},t.machineProgressListener=e.progress||function(){},delete e.progress,m(this,"machineSearch",[e]).then(function(e){return i(),e}).catch(function(e){throw i(),e})},i.prototype.abortMachineSearch=function(e){if(this.game){var t=this;if(!this.machineSearchReject)return e?Promise.reject(new Error("abortMachineSearch(): machine not searching")):Promise.resolve();return new Promise(function(e,r){t.game.StopThreadedMachine();var n=t.machineSearchReject;delete t.machineSearchReject,n(new Error("Machine search aborted")),e()})}return m(this,"abortMachineSearch")},i.prototype.setViewOptions=function(e){if("node"==u)return Promise.reject(new Error("setViewOptions(): not supported in node.js"));if(!this.area&&!this.iframe)return Promise.reject(new Error("setViewOptions(): match is not attached to DOM element"));if(this.game){var t=this;return new Promise(function(r,n){t.game.GameDestroyView();var o={mSkin:"skin",mNotation:"notation",mSounds:"sounds",mShowMoves:"showMoves",mAutoComplete:"autoComplete",mAnaglyph:"anaglyph"};for(var i in o)void 0!==e[o[i]]&&(t.game[i]=e[o[i]]);e.viewAs&&t.game.mViewOptions.switchable&&(t.game.mViewAs=e.viewAs),t.game.GameInitView(),t.game.DisplayBoard(),r()})}return m(this,"setViewOptions",arguments)},i.prototype.getViewOptions=function(){if(this.game){var e=this;return new Promise(function(t,r){var n={skin:e.game.mSkin,sounds:e.game.mSounds,anaglyph:e.game.mAnaglyph};e.game.mViewOptions.useNotation&&(n.notation=!!e.game.mNotation),e.game.mViewOptions.useShowMoves&&(n.showMoves=!!e.game.mShowMoves),e.game.mViewOptions.useAutoComplete&&(n.autoComplete=!!e.game.mAutoComplete),e.game.mViewOptions.switchable&&(n.viewAs=e.game.mViewAs),t(n)})}return m(this,"getViewOptions")},i.prototype.getFinished=function(){if(this.game){var e=this;return new Promise(function(t,r){var n=e.game.GetFinished();t({finished:!!n,winner:n})})}return m(this,"getFinished")},i.prototype.rollback=function(e){if(this.game){var t=this;return new Promise(function(r,n){e<0&&(e=t.game.mPlayedMoves.length+e),e=Math.max(0,Math.min(e,t.game.mPlayedMoves.length));var o=t.game.mPlayedMoves;t.game.BackTo(e,o),t.area&&t.game.DisplayBoard(),r()})}return m(this,"rollback",arguments)},i.prototype.otherPlayer=function(e){return new Promise(function(t,r){1==e?t(-1):-1==e?t(1):r(new Error("otherPlayer: invalid input"))})},i.prototype.save=function(){if(this.game){var e=this;return new Promise(function(t,r){t({playedMoves:Array.from(e.game.mPlayedMoves),initialBoard:e.game.mInitialString,game:e.gameName})})}return m(this,"save")},i.prototype.load=function(e){if(this.game){var t=this;return new Promise(function(r,n){if(e.game&&e.game!=t.gameName)return n(new Error("Trying to load "+e.game+" to "+t.gameName+" match"));t.game.Load(e),t.area&&t.game.DisplayBoard(),r()})}return m(this,"load",arguments)},i.prototype.getConfig=function(){if(this.game){var e=this;return new Promise(function(t,r){t(e.game.config)})}return m(this,"getConfig")},i.prototype.viewAs=function(e){if("node"==u)return Promise.reject(new Error("viewAs(): not supported in node.js"));if(!this.area&&!this.iframe)return Promise.reject(new Error("viewAs(): match is not attached to DOM element"));if(console.warn("Match.viewAs() is obsolete, use Match.setViewOptions() instead"),this.game){var t=this;return new Promise(function(r,n){t.game.GameDestroyView(),t.game.mViewAs=e,t.game.GameInitView(),t.game.DisplayBoard(),r()})}return m(this,"viewAs",arguments)},i.prototype.viewControl=function(e,t){if("node"==u)return Promise.reject(new Error("viewAs(): not supported in node.js"));if(!this.area&&!this.iframe)return Promise.reject(new Error("viewAs(): match is not attached to DOM element"));if(this.game){return this.game.ViewControl(e,t)}return m(this,"viewControl",arguments)},i.prototype.getPossibleMoves=function(){var e=this;if(this.game){var e=this;return new Promise(function(t,r){e.game.mBoard.mMoves&&0!=e.game.mBoard.mMoves.length||e.game.mBoard.GenerateMoves(e.game),t(e.game.mBoard.mMoves)})}return m(this,"getPossibleMoves",arguments)},i.prototype.getBoardState=function(e){var t=this;if(this.game){var t=this;return new Promise(function(r,n){r(t.game.mBoard.ExportBoardState(t.game,e))})}return m(this,"getBoardState",arguments)},i.prototype.getInitialBoardState=function(e){var t=this;if(this.game){var t=this;return new Promise(function(r,n){r(t.game.ExportInitialBoardState(e))})}return m(this,"getInitialBoardState",arguments)},i.prototype.resetView=function(e){if("node"==u)return Promise.reject(new Error("resetView(): not supported in node.js"));if(!this.area&&!this.iframe)return Promise.reject(new Error("resetView(): match is not attached to DOM element"));if(this.game){var t=this;return t.viewControl("stopAnimations").then(function(r){(r||e)&&t.game.DisplayBoard()})}return m(this,"resetView")},i.prototype.getAvailableSkins=function(){if("node"==u)return Promise.reject(new Error("resetView(): not supported in node.js"));if(this.game){var e=function(){try{return!!window.WebGLRenderingContext&&!!document.createElement("canvas").getContext("experimental-webgl")}catch(e){return!1}}();return Promise.resolve(this.game.mViewOptions.skins.filter(function(t){return e||!t["3d"]}))}return m(this,"getAvailableSkins")},exports._createInternalGame=t,exports.PLAYER_A=1,exports.PLAYER_B=-1,exports.DRAW=2}();