mean.cloud Eternity Tower common library

Common Eternity Tower-specific code

目前为 2018-01-09 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/37181/242218/meancloud%20Eternity%20Tower%20common%20library.js

document.ET_TryingToLoad = false;

if (!document.ET_TryingToLoad)
{
    document.ET_WantDebug = false;
    document.ET_WantFasterAbilityCDs = false;

    document.ET_InBattle = false;
    document.ET_FinishedLoading = false;
    document.ET_Initialized = false;
    document.ET_AbilitiesReady = false;
    document.ET_InitialAbilityCheck = true;
    document.ET_TimeLeftOnCD = 9999;
    document.ET_TimeLastFight = 0;

    document.ET_CombatID = undefined;
}

document.ET_ToastMessageSuccess = function(msg)
{
    toastr.success(msg);
};

document.ET_ToastMessageWarning = function(msg)
{
    toastr.warning(msg);
};

document.ET_EventSubscribe = function(sEventName, fnCallback)
{
    jQ(document).on("ET:" + sEventName.trim(), function() { fnCallback(); });
};

document.ET_MeteorCall = function(sMethod, oParam1, oParam2, sMsgSuccess, sMsgFailure)
{
    Package.meteor.Meteor.call("crafting.craftItem", sRecipeID, iBatchAmt, function(errResp)
    {
        if (errResp)
            document.ET_ToastMessageWarning(sMsgFailure);
        else
            document.ET_ToastMessageSuccess(sMsgSuccess);
    });
};

document.ET_FasterAbilityUpdates = function()
{
    try
    {
        if ((document.ET_WantFasterAbilityCDs) && (document.ET_FinishedLoading) && (!document.ET_InBattle) && (!document.ET_AbilitiesReady))
            Meteor.call("abilities.gameUpdate", function(e, t) { });
	}
    catch (err) { }

	setTimeout(document.ET_FasterAbilityUpdates, 2000);
};

document.ET_AbilityCDTrigger = function()
{
    try
    {
        bStillInCombat = document.ET_InBattle || ((document.time_val() - document.ET_TimeLastFight) < 3);

        if ((document.ET_FinishedLoading) && (!bStillInCombat))
        {
            iTotalCD = 0;
            iTotalCDTest = 0;
            iHighestCD = 0;

            document.ET_GetAbilities().forEach(function(oThisAbility, index, array)
            {
                if (oThisAbility.equipped)
                {
                    if (parseInt(oThisAbility.currentCooldown) > 0)
                    {
                        iTotalCD += parseInt(oThisAbility.currentCooldown);
                        if (iHighestCD < parseInt(oThisAbility.currentCooldown))
                            iHighestCD = parseInt(oThisAbility.currentCooldown);
                    }
                }

                iTotalCDTest += parseInt(oThisAbility.cooldown);
            });

            if ((iTotalCDTest > 0) && (iTotalCD === 0))
            {
                if (!document.ET_AbilitiesReady)
                {
                    if (!document.ET_InitialAbilityCheck)
                    {
                        if (document.ET_WantDebug) console.log("<-- triggering ET:abilitiesReady -->");
                        jQ(document).trigger("ET:abilitiesReady");
                    }
                }

                document.ET_AbilitiesReady = true;
                document.ET_TimeLeftOnCD = 0;
            }
            else
            {
                document.ET_AbilitiesReady = false;
                document.ET_TimeLeftOnCD = iHighestCD;
            }

            document.ET_InitialAbilityCheck = false;
        }
        else
        {
            document.ET_AbilitiesReady = false;
            document.ET_TimeLeftOnCD = 9999;
        }
    }
    catch (err) { }

    setTimeout(document.ET_AbilityCDTrigger, 500);
};

document.ET_InitMeteorTriggers = function()
{
    if ((Package.meteor.Meteor === undefined) || (Package.meteor.Meteor.connection === undefined) || (Package.meteor.Meteor.connection._stream === undefined))
    {
        setTimeout(document.ET_InitMeteorTriggers, 100);
        return;
    }

    Package.meteor.Meteor.connection._stream.on('message', function(sMeteorRawData)
    {
        if (document.ET_CombatID === undefined)
        {
            try
            {
                oDataTemp = Package.meteor.global.Accounts.connection._stores.combat._getCollection()._collection._docs._map;
                document.ET_CombatID = oDataTemp[Object.keys(oDataTemp)[0]]._id;
            }
            catch (err) { }
        }

        try
        {
            oMeteorData = JSON.parse(sMeteorRawData);

            /////////////////////////////////////////////////////////////////////////////////////////////////////////
            //
            //  BACKUP TO RETRIEVE USER AND COMBAT IDS
            //
            if (oMeteorData.collection === "users")
                if ((document.ET_UserID === undefined) || (document.ET_UserID.length !== 17))
                    document.ET_UserID = oMeteorData.id;

            if (oMeteorData.collection === "combat")
                if ((document.ET_CombatID === undefined) || (document.ET_CombatID.length !== 17))
                    if (oMeteorData.fields.owner === document.ET_UserID)
                        document.ET_CombatID = oMeteorData.id;
            //
            /////////////////////////////////////////////////////////////////////////////////////////////////////////

            if (document.ET_FinishedLoading)
            {
                if (oMeteorData.collection === "battlesList")
                {
                    document.ET_IsDemon = false;
                    document.ET_AbilitiesReady = false;

                    if ((oMeteorData.msg === "added") || (oMeteorData.msg === "removed"))
                    {
                        document.ET_InBattle = (oMeteorData.msg === "added");
                        if (document.ET_WantDebug) console.log("<-- triggering ET:combat" + (((oMeteorData.msg === "added")) ? ("Start") : ("End")) + " -->");
                        jQ(document).trigger("ET:combat" + (((oMeteorData.msg === "added")) ? ("Start") : ("End")));
                    }
                }

                if ((oMeteorData.collection === "battles") && (oMeteorData.msg === "added"))
                {
                    if (oMeteorData.fields.finished)
                    {
                        document.ET_WonLast = oMeteorData.fields.win;
                        document.ET_TimeLastFight = document.time_val();

                        if (!oMeteorData.fields.win)
                            document.ET_HP = 0;

                        if (document.ET_WantDebug) console.log("<-- triggering ET:combat" + ((oMeteorData.fields.win) ? ("Won") : ("Lost")) + " -->");
                        jQ(document).trigger("ET:combat" + ((oMeteorData.fields.win) ? ("Won") : ("Lost")));
                    }
                }
            }

            try
            {
                if (document.ET_FinishedLoading)
                {
                    if (oMeteorData.id)
                    {
                        if (oMeteorData.id.startsWith("battles-"))
                        {
                            if (oMeteorData.msg !== "removed")
                            {
                                battleID = oMeteorData.id;
                                battleData = JSON.parse(oMeteorData.fields.value);

                                jQ.makeArray(battleData.units).forEach(function(currentPlayer, index, array)
                                {
                                    try
                                    {
                                        if (currentPlayer.name == document.ET_UserName)
                                        {
                                            jQ.makeArray(currentPlayer.buffs).forEach(function(currentBuff, index2, array2)
                                             {
                                                try
                                                {
                                                    if (currentBuff.id === "demons_heart")
                                                    {
                                                        if (currentBuff.data.active)
                                                        {
                                                            if (!document.ET_IsDemon)
                                                            {
                                                                document.ET_IsDemon = true;

                                                                if (document.ET_WantDebug) console.log("<-- triggering ET:combat:buffDemon -->");
                                                                jQ(document).trigger("ET:combat:buffDemon");
                                                            }
                                                        }
                                                    }
                                                }
                                                catch (err) { }
                                            });

                                            return true; // break out of forEach()
                                        }
                                    }
                                    catch (err) { }
                                });
                            }
                        }
                    }
                }
            }
            catch (err) { }
        }
        catch (err) { }

		try
		{
            //todo: use life data from Meteor vs captured meteor response data
            oMeteorData = JSON.parse(sMeteorRawData);

			if (oMeteorData.collection === "combat")
			{
                if ((oMeteorData.fields.owner === document.ET_UserID) || (oMeteorData.id === document.ET_CombatID))
				{
					document.ET_HP  = oMeteorData.fields.stats.health;
					document.ET_NRG = oMeteorData.fields.stats.energy;
				}
			}
        }
        catch (err) { }
    });
};

document.ET_GetAbilities = function()
{
    return Object.keys(document.ET_AbilityManager._collection._docs._map).map(key => document.ET_AbilityManager._collection._docs._map[key])[0].learntAbilities;
};

document.ET_GetAdventures = function()
{
    return Object.keys(document.ET_AdventureManager._collection._docs._map).map(key => document.ET_AdventureManager._collection._docs._map[key])[0].adventures;
};

document.ET_GetChats = function()
{
    return Object.keys(document.ET_ChatManager._collection._docs._map).map(key => document.ET_ChatManager._collection._docs._map[key]);
};

document.ET_GetItems = function()
{
    return Object.keys(document.ET_ItemManager._collection._docs._map).map(key => document.ET_ItemManager._collection._docs._map[key]);
};

// need a better way to check if the game has loaded basic data, but this is fine for now
document.ET_Setup = function()
{
    if ((!document.ET_TryingToLoad) && (!document.ET_FinishedLoading))
    {
        document.ET_TryingToLoad = true;
        
        if (window.jQ === undefined)
            window.jQ = jQuery.noConflict(true);
        
        document.ET_Setup_Initializer();
    }
};

document.ET_Setup_Initializer = function()
{
    if ((Package === undefined) || (Package.meteor === undefined) || (Package.meteor.Meteor === undefined) || (Package.meteor.Meteor.connection === undefined) || (Package.meteor.Meteor.connection._stream === undefined))
    {
        setTimeout(document.ET_Setup_Initializer, 10);
        return;
    }

    if (!document.ET_Initialized)
    {
        document.ET_Initialized = true;
        document.ET_Setup_SendDelayedInitializer();
        document.ET_InitMeteorTriggers();
        document.ET_Setup_remaining();
    }    
};

document.ET_Setup_SendDelayedInitializer = function()
{
    try
    {
        jQ(document).trigger("ET:initialized");
        console.log("<-- triggering ET:initialized -->");
    }
    catch (err)
    {
        setTimeout(document.ET_Setup_SendDelayedInitializer, 100);
    }
};

document.ET_Setup_remaining = function()
{
    try
    {
        document.ET_UserID = Package.meteor.global.Accounts.connection._userId;
        document.ET_UserName = Package.meteor.global.Accounts.connection._stores.users._getCollection()._collection._docs._map[Package.meteor.global.Accounts.connection._userId].username;
        try
        {
            oDataTemp = Package.meteor.global.Accounts.connection._stores.combat._getCollection()._collection._docs._map;
            document.ET_CombatID = oDataTemp[Object.keys(oDataTemp)[0]]._id;
        }
        catch (err) { }

        document.ET_AbilityManager = Package.meteor.global.Accounts.connection._stores.abilities._getCollection();
        document.ET_AdventureManager = Package.meteor.global.Accounts.connection._stores.adventures._getCollection();
        document.ET_ChatManager = Package.meteor.global.Accounts.connection._stores.simpleChats._getCollection();
        document.ET_ItemManager = Package.meteor.global.Accounts.connection._stores.items._getCollection();

        if (document.ET_GetAbilities().length < 0) throw "Not loaded yet: no abilities";
        if (document.ET_GetItems().length < 0) throw "Not loaded yet: no items";
        if (document.ET_GetChats().length < 0) throw "Not loaded yet: no chats";

        // if the above is all good, then this should be no problem:

        document.ET_AbilityCDTrigger();     // set up ability CD trigger
        document.ET_FasterAbilityUpdates(); // set up faster ability updates (do not disable, this is controlled via configurable setting)

        // trigger finished-loading event
        if (!document.ET_FinishedLoading)
        {
            if (document.ET_WantDebug) console.log("<-- triggering ET:loaded -->");
            jQ(document).trigger("ET:loaded");
            document.ET_FinishedLoading = true;
        }
    }
    catch (err)
    {
        // any errors and we retry setup
        setTimeout(document.ET_Setup_remaining, 500);
    }
};

if (!document.ET_TryingToLoad)
    document.ET_Setup();

document.ET_Loaded = function(fnCallback)
{
    if (!document.ET_FinishedLoading)
        document.ET_EventSubscribe("loaded", fnCallback);
    else
        fnCallback();
};

document.ET_Ready = function(fnCallback)
{
    if (!document.ET_Initialized)
        document.ET_EventSubscribe("initialized", fnCallback);
    else
        fnCallback();
};