Greasy Fork is available in English.

mean.cloud Eternity Tower common library

Common Eternity Tower-specific code

Versione datata 09/01/2018. Vedi la nuova versione l'ultima versione.

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greasyfork.org/scripts/37181/242181/meancloud%20Eternity%20Tower%20common%20library.js

// @require       https://greasyfork.org/scripts/37180/code/meancloud%20common%20library.js
// @require       https://greasyfork.org/scripts/37181/code/meancloud%20Eternity%20Tower%20common%20library.js

document.ET_WantDebug = false;
document.ET_WantFasterAbilityCDs = false;

document.ET_InBattle = false;
document.ET_FinishedLoading = 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("body").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("body").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()
{
    Meteor.connection._stream.on('message', function(sMeteorRawData)
    {
        try
        {
            if (document.ET_CombatID === undefined)
            {
                oDataTemp = Package.meteor.global.Accounts.connection._stores.combat._getCollection()._collection._docs._map;
                document.ET_CombatID = oDataTemp[Object.keys(oDataTemp)[0]]._id;
            }
        }
        catch (err) { }
        
        try
        {
            /*
            if ((!document.ET_FinishedLoading) && (sMeteorRawData.startsWith("{\"msg\":\"updated\",\"methods\":[\"")))
            {
                if (sMeteorRawData === "{\"msg\":\"updated\",\"methods\":[\"1\"]}")
                    return;
                
                document.ET_FinishedLoading = true;
                if (document.ET_WantDebug) console.log("<-- triggering ET:loaded -->");
                jQ("body").trigger("ET:loaded");
            }
            */
            
            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")
                    {
                        document.ET_InBattle = true;
                        
                        if (document.ET_WantDebug) console.log("<-- triggering ET:combatStart -->");
                        jQ("body").trigger("ET:combatStart");
                    }
                    else if (oMeteorData.msg === "removed")
                    {
                        document.ET_InBattle = false;
                        
                        if (document.ET_WantDebug) console.log("<-- triggering ET:combatEnd -->");
                        jQ("body").trigger("ET:combatEnd");
                    }
                }

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

                            if (!document.ET_WonLast)
                                document.ET_HP = 0;

                            if (oMeteorData.fields.win)              
                            {
                                if (document.ET_WantDebug) console.log("<-- triggering ET:combatWon -->");
                                jQ("body").trigger("ET:combatWon");
                            }
                            else
                            {
                                if (document.ET_WantDebug) console.log("<-- triggering ET:combatLost -->");
                                jQ("body").trigger("ET:combatLost");
                            }
                        }
                    }
                }
            }

            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("body").trigger("ET:combat:buffDemon");
                                                            }
                                                        }
                                                    }
                                                }
                                                catch (err) { }
                                            });
                                            
                                            return true; // break out of forEach()
                                        }
                                    }
                                    catch (err) { }
                                });
                            }
                        }
                    }
                }
            }
            catch (err) { }
        }
        catch (err) { }

		try
		{
            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()
{
    document.ET_InitMeteorTriggers();   
    document.ET_Setup_remaining();
};

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)
        {
            document.ET_FinishedLoading = true;
            if (document.ET_WantDebug) console.log("<-- triggering ET:loaded -->");
            jQ("body").trigger("ET:loaded");
        }

    }
    catch (err)
    {
        // any errors and we retry setup
        setTimeout(document.ET_Setup_remaining, 500);
    }
};

document.ET_Setup();