mota-js/libs/utils.min.js
2018-05-27 23:17:10 +08:00

1 line
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function utils(){}utils.prototype.init=function(){};utils.prototype.replaceText=function(a){return a.replace(/\${([^}]+)}/g,function(c,b){return core.calValue(b)})};utils.prototype.calValue=function(value){if(typeof value=="number"){return value}if(value instanceof Function){return value()}value=value.replace(/status:([\w\d_]+)/g,"core.getStatus('$1')");value=value.replace(/item:([\w\d_]+)/g,"core.itemCount('$1')");value=value.replace(/flag:([\w\d_]+)/g,"core.getFlag('$1', 0)");return eval(value)};utils.prototype.splitLines=function(a,g,f,c){if(core.isset(c)){core.setFont(a,c)}var b=[];var e=0;for(var d=0;d<g.length;d++){if(g.charAt(d)=="\n"){b.push(g.substring(e,d));e=d+1}else{if(g.charAt(d)=="\\"&&g.charAt(d+1)=="n"){b.push(g.substring(e,d));e=d+2}else{var h=g.substring(e,d+1);var j=core.canvas[a].measureText(h).width;if(j>f){b.push(g.substring(e,d));e=d}}}}b.push(g.substring(e));return b};utils.prototype.unshift=function(c,d){if(!(c instanceof Array)||!core.isset(d)){return}if(d instanceof Array){core.clone(d).reverse().forEach(function(a){c.unshift(a)})}else{c.unshift(d)}return c};utils.prototype.setLocalStorage=function(b,c){try{localStorage.setItem(core.firstData.name+"_"+b,JSON.stringify(c));return true}catch(a){console.log(a);return false}};utils.prototype.getLocalStorage=function(b,a){var c=localStorage.getItem(core.firstData.name+"_"+b);if(core.isset(c)){return JSON.parse(c)}return a};utils.prototype.removeLocalStorage=function(a){localStorage.removeItem(core.firstData.name+"_"+a)};utils.prototype.clone=function(b){if(!core.isset(b)){return b}if(b instanceof Date){var a=new Date();a.setTime(b.getTime());return a}if(b instanceof Array){var a=[];for(var c in b){a[c]=core.clone(b[c])}return a}if(b instanceof Function){return b}if(b instanceof Object){var a={};for(var c in b){if(b.hasOwnProperty(c)){a[c]=core.clone(b[c])}}return a}return b};utils.prototype.cropImage=function(e,g){g=g||32;var b=document.createElement("canvas");var c=b.getContext("2d");b.width=g;b.height=g;var a=[];for(var d=0;d<e.height;d+=g){c.drawImage(e,0,d,g,g,0,0,g,g);var f=new Image();f.src=b.toDataURL("image/png");a.push(f);c.clearRect(0,0,g,g)}return a};utils.prototype.formatDate=function(a){if(!core.isset(a)){return""}return a.getFullYear()+"-"+core.setTwoDigits(a.getMonth()+1)+"-"+core.setTwoDigits(a.getDate())+" "+core.setTwoDigits(a.getHours())+":"+core.setTwoDigits(a.getMinutes())+":"+core.setTwoDigits(a.getSeconds())};utils.prototype.formatDate2=function(a){if(!core.isset(a)){return""}return a.getFullYear()+core.setTwoDigits(a.getMonth()+1)+core.setTwoDigits(a.getDate())+core.setTwoDigits(a.getHours())+core.setTwoDigits(a.getMinutes())+core.setTwoDigits(a.getSeconds())};utils.prototype.setTwoDigits=function(a){return parseInt(a)<10?"0"+a:a};utils.prototype.formatBigNumber=function(g){g=Math.floor(parseFloat(g));if(!core.isset(g)){return"???"}var b=g<0?"-":"";g=Math.abs(g);if(g<=999999){return b+g}var a=[{val:1e+20,c:"g"},{val:1e+16,c:"j"},{val:1000000000000,c:"z"},{val:100000000,c:"e"},{val:10000,c:"w"},];for(var d=0;d<a.length;d++){var e=a[d];if(g>=10*e.val){var f=g/e.val;return b+f.toFixed(Math.max(0,Math.floor(4-Math.log10(f+1))))+e.c}}return b+g};utils.prototype.arrayToRGB=function(a){var d=parseInt(a[0])||0,c=parseInt(a[1])||0,b=parseInt(a[2])||0;if(d<0){d=0}if(b<0){b=0}if(c<0){c=0}if(d>255){d=255}if(b>255){b=255}if(c>255){c=255}return"#"+((1<<24)+(d<<16)+(c<<8)+b).toString(16).slice(1)};utils.prototype.encodeRoute=function(d){var a="";var c="",b=0;d.forEach(function(e){if(e=="up"||e=="down"||e=="left"||e=="right"){if(e!=c&&b>0){a+=c.substring(0,1).toUpperCase();if(b>1){a+=b}b=0}c=e;b++}else{if(b>0){a+=c.substring(0,1).toUpperCase();if(b>1){a+=b}b=0}if(e.indexOf("item:")==0){a+="I"+e.substring(5)+":"}else{if(e.indexOf("fly:")==0){a+="F"+e.substring(4)+":"}else{if(e.indexOf("choices:")==0){a+="C"+e.substring(8)}else{if(e.indexOf("shop:")==0){a+="S"+e.substring(5)}else{if(e=="turn"){a+="T"}else{if(e=="getNext"){a+="G"}else{if(e.indexOf("input:")==0){a+="P"+e.substring(6)}else{if(e=="no"){a+="N"}else{if(e.indexOf("move:")==0){a+="M"+e.substring(5)}else{if(e.indexOf("key:")==0){a+="K"+e.substring(4)}else{if(e.indexOf("random:")==0){a+="X"+e.substring(7)}}}}}}}}}}}}});if(b>0){a+=c.substring(0,1).toUpperCase();if(b>1){a+=b}}return a};utils.prototype.decodeRoute=function(j){if(!core.isset(j)){return j}var a=[],g=0;var d=function(c){var i="";while(g<j.length&&!isNaN(j.charAt(g))){i+=j.charAt(g++)}if(i.length==0){i="1"}return core.isset(c)?i:parseInt(i)};var e=function(){var c="";while(g<j.length&&/\w/.test(j.charAt(g))){c+=j.charAt(g++)}g++;return c};while(g<j.length){var b=j.charAt(g++);var h=(b=="I"||b=="F"||b=="S")?e():d();switch(b){case"U":for(var f=0;f<h;f++){a.push("up")}break;case"D":for(var f=0;f<h;f++){a.push("down")}break;case"L":for(var f=0;f<h;f++){a.push("left")}break;case"R":for(var f=0;f<h;f++){a.push("right")}break;case"I":a.push("item:"+h);break;case"F":a.push("fly:"+h);break;case"C":a.push("choices:"+h);break;case"S":a.push("shop:"+h+":"+d(true));break;case"T":a.push("turn");break;case"G":a.push("getNext");break;case"P":a.push("input:"+h);break;case"N":a.push("no");break;case"M":++g;a.push("move:"+h+":"+d());break;case"K":a.push("key:"+h);break;case"X":a.push("random:"+h);break}}return a};utils.prototype.isset=function(a){if(a==undefined||a==null||(typeof a=="number"&&isNaN(a))){return false}return true};utils.prototype.subarray=function(c,d){if(!core.isset(c)||!core.isset(d)||!(c instanceof Array)||!(d instanceof Array)||c.length<d.length){return null}var e=core.clone(c),f=core.clone(d);while(f.length>0){if(e.shift()!=f.shift()){return null}}return e};utils.prototype.__init_seed=function(){var a=new Date().getTime()%34834795+3534;a=this.__next_rand(a);a=this.__next_rand(a);a=this.__next_rand(a);core.setFlag("seed",a);core.setFlag("rand",a)};utils.prototype.__next_rand=function(a){a=(a%127773)*16807-~~(a/127773)*2836;a+=a<0?2147483647:0;return a};utils.prototype.rand=function(b){var c=core.getFlag("rand");c=this.__next_rand(c);core.setFlag("rand",c);var a=c/2147483647;if(core.isset(b)&&b>0){return Math.floor(a*b)}return a};utils.prototype.rand2=function(b){b=b||2147483648;var c;if(core.status.replay.replaying){var a=core.status.replay.toReplay.shift();if(a.indexOf("random:")==0){c=parseInt(a.substring(7))}else{core.stopReplay();core.drawTip("录像文件出错");return}}else{c=Math.floor(Math.random()*b)}core.status.route.push("random:"+c);return c};utils.prototype.readFile=function(c,a,b){core.platform.successCallback=c;core.platform.errorCallback=a;if(core.isset(window.jsinterface)){window.jsinterface.readFile();return}if(!core.platform.isOnline){alert("离线状态下不支持文件读取!");if(core.isset(a)){a()}return}if(core.platform.fileReader==null){alert("当前浏览器不支持FileReader");if(core.isset(a)){a()}return}if(core.platform.fileInput==null){core.platform.fileInput=document.createElement("input");core.platform.fileInput.style.opacity=0;core.platform.fileInput.type="file";core.platform.fileInput.onchange=function(){var d=core.platform.fileInput.files;if(d.length==0){if(core.isset(core.platform.errorCallback)){core.platform.errorCallback()}return}if(!b){core.platform.fileReader.readAsText(core.platform.fileInput.files[0])}else{core.platform.fileReader.readAsDataURL(core.platform.fileInput.files[0])}core.platform.fileInput.value=""}}core.platform.fileInput.click()};utils.prototype.readFileContent=function(a){var c=null;if(a.slice(0,4)==="data"){if(core.isset(core.platform.successCallback)){core.platform.successCallback(a)}return}try{c=JSON.parse(a);if(core.isset(c)){if(core.isset(core.platform.successCallback)){core.platform.successCallback(c)}return}}catch(b){console.log(b)}alert("不是有效的JSON文件");if(core.isset(core.platform.errorCallback)){core.platform.errorCallback()}};utils.prototype.download=function(d,b){if(core.isset(window.jsinterface)){window.jsinterface.download(d,b);return}if(!core.platform.isOnline){alert("离线状态下不支持下载操作!");return}if(core.platform.isIOS){alert("iOS平台下不支持下载操作");return}if(!core.platform.isPC){if(!core.platform.isChrome||core.platform.isQQ||core.platform.isWeChat){if(core.copy(b)){alert("移动端只有Chrome浏览器支持直接下载文件\n所有应下载内容已经复制到您的剪切板请自行创建空白文件并粘贴。")}else{alert("该平台或浏览器暂不支持下载操作!")}return}}if(core.platform.isSafari){alert("你当前使用的是Safari浏览器不支持直接下载文件。\n即将打开一个新窗口为应下载内容请自行全选复制然后创建空白文件并粘贴。");var a=new Blob([b],{type:"text/plain;charset=utf-8"});var e=window.URL.createObjectURL(a);var f=window.open(e,"_blank");window.URL.revokeObjectURL(e);return}var a=new Blob([b],{type:"text/plain;charset=utf-8"});if(window.navigator.msSaveOrOpenBlob){window.navigator.msSaveBlob(a,d)}else{var e=window.URL.createObjectURL(a);var c=window.document.createElement("a");c.href=e;c.download=d;document.body.appendChild(c);c.click();document.body.removeChild(c);window.URL.revokeObjectURL(e)}};utils.prototype.copy=function(a){if(core.isset(window.jsinterface)){window.jsinterface.copy(filename,content);return true}if(!core.platform.supportCopy){return false}var d=document.createElement("textarea");d.style.position="fixed";d.style.top=0;d.style.left=0;d.style.width="2em";d.style.height="2em";d.style.padding=0;d.style.border="none";d.style.outline="none";d.style.boxShadow="none";d.style.background="transparent";d.value=a;document.body.appendChild(d);d.select();var c=false;try{c=document.execCommand("copy")}catch(b){c=false}document.body.removeChild(d);return c};utils.prototype.show=function(b,e,a){if(!core.isset(e)){b.style.display="block";return}b.style.display="block";if(main.mode!="play"){b.style.opacity=1;if(core.isset(a)){a()}return}b.style.opacity=0;var c=0;var d=window.setInterval(function(){c+=0.03;b.style.opacity=c;if(c>1){clearInterval(d);if(core.isset(a)){a()}}},e)};utils.prototype.hide=function(c,e,a){if(!core.isset(e)){c.style.display="none";return}if(main.mode!="play"){c.style.display="none";if(core.isset(a)){a()}return}var d=1;var b=window.setInterval(function(){d-=0.03;c.style.opacity=d;if(d<0){c.style.display="none";clearInterval(b);if(core.isset(a)){a()}}},e)};utils.prototype.http=function(f,g,b,e,a,c,d){var h=new XMLHttpRequest();h.open(f,g,true);if(core.isset(c)){h.overrideMimeType(c)}if(core.isset(d)){h.responseType=d}h.onload=function(i){if(h.status==200){if(core.isset(e)){e(h.response)}}else{if(core.isset(a)){a("HTTP "+h.status)}}};h.onabort=function(){if(core.isset(a)){a("Abort")}};h.ontimeout=function(){if(core.isset(a)){a("Timeout")}};h.onerror=function(){if(core.isset(a)){a("Error on Connection")}};if(core.isset(b)){h.send(b)}else{h.send()}};