!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.DCAgent={})}(this,function(t){"use strict";function e(){}function n(t){return"function"==typeof t}function r(t){return t&&"[object Object]"===zt.call(t)}function o(t){console.log("---- DCAgent log start ----\n"+t+"\n---- DCAgent log end ----")}function i(t){var e,n,r;return e=Date.now(),n="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx",r=n.replace(/[xy]/g,function(t){var n;return n=(e+16*Math.random())%16|0,e=Math.floor(e/16),"x"===t?n.toString(16):(7&n|8).toString(16)}),(t||"")+r.replace(/-/g,"").toUpperCase()}function c(t){var e,n;for(e in t)n=t[e],t[e]=n;var r=arguments.length>=2?[].slice.call(arguments,1):[];return r.forEach(function(r){var o;o=[];for(e in r)n=r[e],o.push(t[e]=n);return o}),t}function u(t){return Wt(function(){var e="0";t.setItem(e,e);var n=t.getItem(e)===e;return t.removeItem(e),n})}function a(t){for(var e=arguments.length<=1||void 0===arguments[1]?0:arguments[1],n="",r=0;e>r;r+=1)n+=t;return n}function s(t,e,n){return t?t&&t.length>=n?t:t+a(e,Math.ceil(n-t.length)/e.length):t}function f(t,e,n){return function(){Array.isArray(e)||(e=[e]),Array.isArray(n)||(n=[n]);var r,o;for(r=0;r=1e21&&(t=9527e16),Xt.parseInt(t,n)||e}function p(t){return Math.min(99e19,t)}function m(t){try{return t?JSON.stringify(t):null}catch(e){o("invalid json format")}return null}function b(t){try{return t?JSON.parse(t):null}catch(e){o("invalid json string")}return null}function j(t,e){var n=this;this.duration=e,this.status="running",this.timer=qt(function(){return n.run()},this.duration),this.run=function(){"cancelled"!==n.status&&(Bt(n.timer),Ct.attempt(t),n.timer=qt(function(){return n.run()},n.duration))},this.stop=function(){n.status="stopped",Bt(n.timer)},this.reset=function(t){"cancelled"!==n.status&&(n.stop(),t&&(n.duration=t),n.run())},this.cancel=function(){this.status="cancelled",Bt(this.timer)}}function T(t){Nt&&(Nt.stop(),qt(function(){Nt&&Nt.reset(t)},t),t&&(Ft.interval=t))}function I(){Nt&&(Nt.cancel(),Nt=null)}function v(){Nt&&Nt.stop()}function E(){Nt&&Nt.run()}function h(t,e){Nt=new j(t,e)}function O(){if(Yt)return Yt;if(ee+=1,!(ee>4)){var t={egret:"egret",layabox:"layabox",cocos:"cc.game",impact:"ig",phaser:"Phaser",pixi:"PIXI",create:"createjs",three:"THREE",gameMaker:"asset_get_type",playCanvas:"pc.fw",turbulenz:"TurbulenzEngine",quintus:"Quintus",melon:"me.game",lychee:"lychee",wade:"wade.addSceneObject",crafty:"Crafty",lime:"lime.Scene",enchant:"enchant",isogenic:"IgeEngine",gameclosure:"GC.Application",panda:"game.Scene",kiwi:"Kiwi",jaws:"jaws",sirius2d:"ss2d",collie:"collie",physics:"Physics",stage:"Stage.Anim",babylon:"BABYLON"};for(var e in t){var n=t[e];if(n.indexOf(".")>-1){var r=n.split("."),o=Xt[r[0]];if(o&&o[r[1]])return Yt=e,e}else if(Xt[n])return Yt=e,e}}}function y(){var t=Ft.loginTime||Ft.initTime;return{loginTime:t,onlineTime:Ct.parseInt(Date.now()/1e3)-t||1,extendMap:{from:Ft.from,engine:O()||"",app:Ft.app}}}function _(t,e){var n={headerInfo:le,onlineInfo:y(),errorInfoList:te.concat(),eventInfoList:pe.concat()};return t&&(n.paymentInfo=t),e&&(n.userInfo=e),n}function S(){pe.length=0,te.length=0}function A(){if($t&&n($t.createElement)){var t=$t.createElement("div");if(!t)return!1;if(n(t.querySelector)){t.innerHTML="";var e=t.querySelector("i");return!!e&&"I"===e.tagName}if(n(t.getElementsByTagName)){var r=t.getElementsByTagName("i");return!!r&&1===r.length}}return!1}function L(t){return le.appId+"."+t}function N(t,e){t=L(t),fn.setItem(t,e),an.set(t,e,3650)}function D(t){return t=L(t),fn.getItem(t)||an.get(t)}function C(t,e){fn.setItem(L(t),e)}function R(t){return fn.getItem(L(t))}function w(t){fn.removeItem(L(t))}function x(){Ut.setItem(xt.LOGOUT_TIME,Ct.parseInt(Date.now()/1e3)),(te.length||pe.length)&&Ut.setItem(xt.QUIT_SNAPSHOT,Ct.jsonStringify(_()))}function U(){var t=Ut.getItem(xt.QUIT_SNAPSHOT);return t&&Ct.jsonParse(t)}function M(t){me>=wt.MAX_ERROR_COUNT||(te.push(t),me+=1)}function P(t){pe.push(t)}function V(t,e){if(!t)return void Ct.tryThrow("Missing eventId");var n=function(t){return t.replace(/%/g,"_")};arguments.length>2&&(e=arguments[2]);var r={};if(Ct.isObject(e))for(var o in e)r[n(o)]="number"==typeof e[o]?e[o]:encodeURIComponent(e[o]);var i={eventId:n(t),eventMap:r};return Rt.addEvent(i),e&&e.immediate?(Dt.stop(),Dt.run(),!1):void 0}function G(){return le.uid||""}function H(t){var e=Xt.egret,n=new e.URLLoader,r=Date.now();n.addEventListener(e.Event.COMPLETE,function(e){var n=Date.now()-r,o=e.target,i="success"===o.data;Ct.attempt(i?t.success:t.error,o,[o,n,n>=t.timeout]),Ct.attempt(t.complete,o,[o,n])});var o=new e.URLRequest(t.url);o.method=t.method||e.URLRequestMethod.POST,o.data=Ct.jsonStringify(t.data),n.load(o)}function F(){return Xt.cc.loader.getXMLHttpRequest()}function k(t){var e=ln();sn.timeout&&(e.timeout=t.timeout),e.open(t.method||"POST",t.url,!0),sn.setContentType(e,"text/plain; charset=UTF-8");var n=Date.now();e.onreadystatechange=function(){if(4===this.readyState){var e=this.status>=200&&this.status<300,r=Date.now()-n;Ct.attempt(e?t.success:t.error,this,[this,r]),Ct.attempt(t.complete,this,[this,r]),this.onreadystatechange=null,this.ontimeout=null}},sn.timeout&&(e.ontimeout=function(){var e=Date.now()-n;Ct.attempt(t.error,this,[this,e,!0]),Ct.attempt(t.complete,this,[this,e]),this.onreadystatechange=null,this.ontimeout=null}),e.send(Ct.jsonStringify(t.data))}function X(){var t=!0,e=!0,n=ln();try{n.timeout=1}catch(r){t=!1}try{n.contentType="text/plain; charset=UTF-8"}catch(r){e=!1}var o=Mt.useXDR?function(t,n){e&&(t.contentType=n)}:function(t,e){t.setRequestHeader("Content-Type",e)};return{timeout:t,contentType:e,setContentType:o}}function q(e,n){var r=Date.now();if(!n){if(r-Tn1&&Dt.reset(1e3*i)}}}})}function B(t){if(!t)return!1;var e=t.onlineInfo.onlineTime;return 1>e||e>wt.MAX_ONLINE_TIME?(Ct.tryThrow("Illegal online time"),!1):!0}function K(){return Ft.inited?void 0:(Ct.tryThrow("DCAgent.init needed"),!1)}function Y(){return Ft.loginTime?void 0:(Ct.tryThrow("DCAgent.login needed"),!1)}function z(){return Ft.destroyed?(Ct.tryThrow("DCAgent is destroyed already"),!1):void 0}function Q(t){return t+"?__deuid="+le.uid+"&__deappid="+le.appId}function J(t){return t+"?type=h520&appId="+le.appId+"&uid="+le.uid+"&mac="+(le.mac||"")+"&imei="+(le.imei||"")+"&idfa="+(le.idfa||"")}function W(t,e,n){if(t||!Ct.hiddenProperty||!$t[Ct.hiddenProperty]){var r={url:Vt.appendOnline(Vt.API_PATH)},o=1;jn&&jn%Ft.oss===0&&(Rt.addEvent({eventId:xt.REQ_KEY,eventMap:{succ:jn-gn,fail:gn,total:jn}}),o+=1),r.data=Rt.collect(e,n);var i=r.data.eventInfoList[r.data.eventInfoList.length-o];if(i&&i.eventMap&&i.eventMap.immediate&&(t=!0),Pt.isParamsValid(r.data)){Rt.clear();var c=r.data.errorInfoList,u=r.data.eventInfoList;(u.length||c.length)&&(r.error=function(){c.forEach(function(t){Rt.addError(t)}),u.forEach(function(t){Rt.addEvent(t)})}),q(r,t)}}}function $(t){t||(t=wt.ASAP_TIMEOUT),Bt(mn),Dt.stop(),mn=qt(function(){Dt.run()},t)}function Z(t){if(!t)return void Ct.tryThrow("Missing accountID");if(le.accountId===t)return void(Ft.loginTime=Ft.loginTime||Ct.parseInt(Date.now()/1e3));Gt.setPollingDebounce(Ft.interval),W(!0),Ft.loginTime=Ct.parseInt(Date.now()/1e3);var e=xt.ACCOUNT_RELATED_SETTINGS+","+xt.ACCOUNT_ROLE_SETTINGS;e.split(",").forEach(function(t){return le[t]=""}),le.age=wt.DEFAULT_AGE,le.gender=wt.DEFAULT_GENDER,le.roleLevel=wt.DEFAULT_ROLE_LEVEL,le.accountId=t,W(!0)}function tt(t,e,n,r){var o=arguments;xt.ACCOUNT_ROLE_SETTINGS.split(",").forEach(function(t,e){return le[t]=o[e]||""}),le.roleLevel=Ct.parseInt(r)||1}function et(t,e,n,r){tt(t,e,n,r),V("DE_EVENT_CREATE_ROLE",{roleId:String(t),roleRace:String(e),roleClass:String(n)})}function nt(t){le.gender=2===t?2:1}function rt(t){le.gameServer=String(t)}function ot(t){t=Ct.parseInt(t),le.age=t>0&&128>t?t:0}function it(t){le.accountType=String(t)}function ct(t,e){return e=Ct.parseInt(e),0>e?(Ct.tryThrow("Argument error"),!1):void V(xt.EVT_TASK,{actionType:"taskUnfinish",taskId:String(t),elapsed:e})}function ut(t,e){return e=Ct.parseInt(e),0>e?(Ct.tryThrow("Argument error"),!1):void V(xt.EVT_TASK,{actionType:"taskFinish",taskId:String(t),elapsed:e})}function at(t){if(!t||!t.hasOwnProperty("amount"))return void Ct.tryThrow("Missing amount");var e={currencyAmount:Ct.max(parseFloat(t.amount,10)||0),currencyType:t.currencyType||"CNY",payType:String(t.payType||""),iapid:String(t.iapid||""),payTime:Ct.parseInt(Date.now()/1e3),extendMap:{orderId:String(t.orderId||"")}};return e.currencyAmount<=0?void Ct.tryThrow("amount must be greater than 0"):(W(!0,e),e)}function st(t,e){return e=Ct.parseInt(e),0>e?(Ct.tryThrow("Argument error"),!1):void V(xt.EVT_MISSION,{actionType:"guankaUnfinish",guankaId:String(t),duration:e})}function ft(t,e){return e=Ct.parseInt(e),0>e?(Ct.tryThrow("Argument error"),!1):void V(xt.EVT_MISSION,{actionType:"guankaFinish",guankaId:String(t),duration:e})}function gt(t,e,n){return t=Ct.parseInt(t),e=Ct.parseInt(e),n=Ct.parseInt(n),0>t||0>e||t>e||0>n?(Ct.tryThrow("Argument error"),!1):(le.roleLevel=e,void V(xt.EVT_LEVEL,{startLevel:t,endLevel:e,duration:n}))}function dt(t,e,n,r){return e=Ct.parseInt(e),0>e?(Ct.tryThrow("Argument error"),!1):void V(xt.EVT_ITEM,{actionType:"itemUse",itemId:String(t),itemNum:e,reason:String(r),missonID:String(n)})}function lt(t,e,n,r){return e=Ct.parseInt(e),0>e?(Ct.tryThrow("Argument error"),!1):void V(xt.EVT_ITEM,{actionType:"itemGet",itemId:String(t),itemNum:e,reason:String(r),missonID:String(n)})}function pt(t,e,n,r,o){return e=Ct.parseInt(e),r=Ct.parseInt(r),0>e||0>r?(Ct.tryThrow("Argument error"),!1):void V(xt.EVT_ITEM,{actionType:"itemBuy",itemId:String(t),itemNum:e,coinType:String(n),coinNum:r,missonID:String(o)})}function mt(t,e,n,r){return e=Ct.parseInt(e),t=Ct.parseInt(t),0>e||0>t?(Ct.tryThrow("Argument error"),!1):void V(xt.EVT_COIN,{actionType:"coinUse",coinType:String(n),balanceNum:e,coinNum:t,reason:String(r)})}function bt(t,e,n,r){return e=Ct.parseInt(e),t=Ct.parseInt(t),0>e||0>t||t>e?(Ct.tryThrow("Argument error"),!1):void V(xt.EVT_COIN,{actionType:"coinGet",coinType:String(n),balanceNum:e,coinNum:t,reason:String(r)})}function jt(){for(var t=["pagehide","beforeunload","unload"],e=0;e=3?decodeURIComponent(n[2]):null},set:function(t,e,n,r,o,i){var c;n&&(c=new Date,c.setTime(c.getTime()+864e5*n));var u=n?" expires="+c.toGMTString():"",a=" path="+(o||"/"),s=r?" domain="+r:"",f=i?" secure":"";$t.cookie=t+"="+encodeURIComponent(e)+u+a+s+f},remove:function(t,e,n){Ze.set(t,"",-1,e,n)}},tn=!!Xt.localStorage||Kt.isEgret||Kt.isCocos||Kt.isLayabox,en=A(),nn=en&&"cookie"in $t,rn=kt.location||{},on="https:"===rn.protocol?"https:":"http:",cn=!!Xt.XDomainRequest,un=Mt.hasCookie?Ze:{get:Ct.noop,set:Ct.noop},an=un;Le=Kt.isEgret?Xt.egret.localStorage:Kt.isCocos?Xt.cc.sys.localStorage:tn?Xt.localStorage:{getItem:e,setItem:e,removeItem:e};var sn,fn=Le,gn=0,dn=Mt.useXDR?function(){return new Xt.XDomainRequest}:function(){return new Xt.XMLHttpRequest},ln=Kt.isCocos?F:dn,pn=function(){return Xt.XMLHttpRequest||Kt.isCocos?(sn=X(),k):Kt.isEgret?H:(Ct.log("XMLHttpRequest not found"),Ct.noop)}();t.report;var mn,bn,jn=0,Tn=Date.now()-wt.ASAP_TIMEOUT,In=Mt.protocol+"//"+xt.HOST+xt.API_PATH,vn={login:Z,getUid:G,onEvent:V},En={onCoinGet:bt,onCoinUse:mt,onItemBuy:pt,onItemProduce:lt,onItemUse:dt,onLevelUp:gt,onMissionFinished:ft,onMissionUnfinished:st,onPayment:at,onTaskFinished:ut,onTaskUnfinished:ct,setAccountType:it,setAge:ot,setGameServer:rt,setGender:nt,setRoleInfo:tt,createRole:et},hn=[Pt.shouldNotBeDestoryed,Pt.shouldBeInited],On=[Pt.shouldNotBeDestoryed,Pt.shouldBeLoggedIn],yn=[function(){return Gt.setPollingDebounce()}];for(bn in vn)t[bn]=Ct.aspect(vn[bn],hn,"onEvent"===bn&&yn);for(bn in En)t[bn]=Ct.aspect(En[bn],On,"onPayment"!==bn&&yn);t.init=St,t.isReady=At,t.destroy=Lt;var _n=Xt.DCAgentObject;if(_n){var Sn=Xt[_n];if(Ct.isFunction(Sn)){var An=Sn.cache;An.length&&(An.forEach(function(e){Ct.attempt(t[e[0]],t,Ct.slice(e,1))}),An.length=0)}}var Ln={get isNew(){var t=Ft.loginTime||Ft.initTime;return Ft.createTime===t},get initTime(){return Ft.initTime},get createTime(){return Ft.createTime},get loginTime(){return Ft.loginTime},get lastLogoutTime(){return parseInt(Ut.getItem(xt.LOGOUT_TIME))},get reportCount(){return jn},get reportFailedCount(){return gn}};t.state=Ft,t.player=Ln});