Browse Source

Add timeout to prevent disconnects + fix auto-trash

Pinkie Pie 9 years ago
parent
commit
b6099ddafe
2 changed files with 146 additions and 158 deletions
  1. 11 11
      config.json
  2. 135 147
      index.js

+ 11 - 11
config.json

@@ -1,13 +1,13 @@
 {
-    "modes" : {
-        "auto" : true,
-        "easy" : false,
-        "trash" : true
-    },
+	"modes" : {
+		"auto" : true,
+		"easy" : false,
+		"trash" : false
+	},
 
-    "motes" : [8000, 8001, 8002, 8005, 8007, 8008, 8009, 8010, 8011, 8012, 8013, 8014, 8015, 8016, 8017, 8018, 8019, 8020, 8021, 8022, 8023, 8025],
-    "blacklist" : [169890, 169886, 169887, 169888, 169889, 169891, 160, 7214],
-    "trash" : [160, 8007, 94201, 94202, 7214, 200997, 200998, 6452, 6453, 6454, 6522, 6523, 6524, 6525, 97392, 6468, 6463, 201018, 96100, 21311, 21312, 21313, 21314, 21315, 21316, 354, 650, 97013, 97012, 20904],
-    "crystals" : [8108, 8122, 8136, 8150, 8164, 8249, 8321, 8375, 8587, 8441, 8501, 8513, 8525, 8537, 8573, 8109, 8123, 8137, 8151, 8165, 8250, 8322, 8376, 8388, 8422, 8502, 8514, 8526, 8538, 8574, 8110, 8124, 8138, 8152, 8166, 8251, 8323, 8377, 8389, 8443, 8503, 8515, 8527, 8539, 8575, 8111, 8125, 8139, 8153, 8167, 8252, 8324, 8378, 8390, 8444, 8504, 8516, 8528, 8540, 8576, 8112, 8126, 8140, 8154, 8168, 8253, 8325, 8379, 8391, 8445, 8505, 8517, 8529, 8541, 8577, 8241, 8427, 8101, 8129, 8900, 8901, 8906, 8908], 
-    "strongboxes" : [169886, 169887, 169888, 169889, 169890, 169891] 
-}
+	"blacklist" : [169890, 169886, 169887, 169888, 169889, 169891, 160, 7214],
+	"motes" : [8000, 8001, 8002, 8005, 8007, 8008, 8009, 8010, 8011, 8012, 8013, 8014, 8015, 8016, 8017, 8018, 8019, 8020, 8021, 8022, 8023, 8025],
+	"trash" : [160, 8007, 94201, 94202, 7214, 200997, 200998, 6452, 6453, 6454, 6522, 6523, 6524, 6525, 97392, 6468, 6463, 201018, 96100, 21311, 21312, 21313, 21314, 21315, 21316, 354, 650, 97013, 97012, 20904],
+	"crystals" : [8108, 8122, 8136, 8150, 8164, 8249, 8321, 8375, 8587, 8441, 8501, 8513, 8525, 8537, 8573, 8109, 8123, 8137, 8151, 8165, 8250, 8322, 8376, 8388, 8422, 8502, 8514, 8526, 8538, 8574, 8110, 8124, 8138, 8152, 8166, 8251, 8323, 8377, 8389, 8443, 8503, 8515, 8527, 8539, 8575, 8111, 8125, 8139, 8153, 8167, 8252, 8324, 8378, 8390, 8444, 8504, 8516, 8528, 8540, 8576, 8112, 8126, 8140, 8154, 8168, 8253, 8325, 8379, 8391, 8445, 8505, 8517, 8529, 8541, 8577, 8241, 8427, 8101, 8129, 8900, 8901, 8906, 8908], 
+	"strongboxes" : [169886, 169887, 169888, 169889, 169890, 169891] 
+}

+ 135 - 147
index.js

@@ -1,151 +1,139 @@
-const config = require('./config.json');
+const Command = require('command'),
+	config = require('./config.json'),
+	blacklist = config.blacklist.concat(config.motes),
+	trash = config.trash.concat(config.crystals, config.strongboxes)
 
 module.exports = function Loot(dispatch) {
-
-    let auto = config.modes.auto || false,
-        autotrash = config.modes.trash || false,
-        enabled = config.modes.easy || true,
-        lootInterval = auto ? setInterval(tryLootAll, 250) : null,
-        location;
-
-    let blacklist = config.blacklist.concat(config.motes);
-    let trash = config.trash.concat(config.crystals.concat(config.strongboxes));
-
-    let cid = null;
-    let loot = {};
-    let inventory = null;
-
-    let commands = {
-        auto: {
-            alias: ['auto', 'autoloot', 'toggle'],
-            run: function() {
-                auto = !auto;
-                message(`Autoloot mode toggled: ${auto}`);
-                if(auto){
-					lootInterval = setInterval(tryLootAll, 250);
+	const command = Command(dispatch)
+
+	let auto = config.modes.auto || false,
+		autotrash = config.modes.trash || false,
+		enabled = config.modes.easy || true
+
+	let cid = null,
+		playerId = -1,
+		location = null,
+		inventory = null,
+		loot = {},
+		lootTimeout = null
+
+	let commands = {
+		auto: {
+			alias: ['auto', 'autoloot', 'toggle'],
+			run: function() {
+				auto = !auto
+				command.message(`Autoloot mode toggled: ${auto}`)
+				if(auto && !lootTimeout) tryLoot()
+				else {
+					clearTimeout(lootTimeout)
+					lootTimeout = null
 				}
-				else
-					clearInterval(lootInterval);
-            }
-        },
-        enable: {
-            alias: ['enable', 'on'],
-            run: function() {
-                enabled = true;
-                message('Easy looting is enabled.');
-            }
-        },
-        disable: {
-            alias: ['disable', 'off'],
-            run: function() {
-                enabled = false;
-                message('Easy looting is disabled.');
-            }
-        },
-        autotrash: {
-            alias: ['autotrash', 'trash'],
-            run: function() {
-                autotrash = !autotrash;
-
-                message('Autotrash toggled: ' + (autotrash ? 'on' : 'off'));
-                garbageCollect();
-            }
-        }
-    }
-
-    dispatch.hook('S_LOGIN', 1, event => { ({cid} = event) })
-
-    dispatch.hook('C_CHAT', 1, (event) => {
-        if(!event.message.includes('!loot'))
-            return;
-
-        let command = event.message.replace(/<\/?[^<>]*>/gi, '').split(' ');
-
-        if(command.length > 1) {
-            for(let cmd in commands) {
-                if(commands[cmd].alias.indexOf(command[1].toString()) > -1)
-                    commands[cmd].run();
-            }
-        }
-
-        return false;
-    });
-
-    dispatch.hook('S_LOAD_TOPO', 1, (event) => {
-        loot = {};
-    });
-
-    dispatch.hook('C_PLAYER_LOCATION', 1, (event) => {
-        location = event;
-    });
-
-    dispatch.hook('S_SPAWN_DROPITEM', 1, (event) => {
-        if(!(blacklist.indexOf(event.item) > -1)) loot[event.id.toString()] = event;
-    }); 
-
-    dispatch.hook('C_TRY_LOOT_DROPITEM', 1, (event) => {
-        if(enabled) tryLootAll();      
-    });
-    
-    dispatch.hook('S_DESPAWN_DROPITEM', 1, (event) => {
-        if(event.id.toString() in loot) delete loot[event.id.toString()];    
-    });
-
-    dispatch.hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
-        if(event.message === '@41') return false;  // Block "That isn't yours." system message.
-        if(trash.includes(event.item)) {
-            garbageCollect();
-        }
-    });
-
-    function garbageCollect(){
-            dispatch.hook('S_INVEN', 5, event => {
-                if(autotrash) {
-                    if(event.first) inventory = []
-                    else if(!inventory) return
-
-                    for(let item of event.items) inventory.push(item)
-
-                    if(!event.more) {
-                        for(let item of inventory) {
-                            if(item.slot < 40) continue // First 40 slots are reserved for equipment, etc.
-                            else if(trash.includes(item.item)) deleteItem(item.slot, item.amount)
-                        }
-                        inventory = null
-                    }
-                }
-            })                
-    }
-
-
-    function deleteItem(slot, amount) {
-        dispatch.toServer('C_DEL_ITEM', 1, {
-            cid: cid,
-            slot: slot - 40,
-            amount
-        })
-    }
-
-    function tryLootAll() {
-        for(let item in loot) {
-            if(location)
-                if(Math.abs(loot[item].x - location.x1) < 120 && Math.abs(loot[item].y - location.y1) < 120)
-                    dispatch.toServer('C_TRY_LOOT_DROPITEM', 1, {
-                        id: loot[item].id
-                    });
-        }
-    }
-
-    function message(msg) {
-        dispatch.toClient('S_CHAT', 1, {
-            channel: 24,
-            authorID: 0,
-            unk1: 0,
-            gm: 0,
-            unk2: 0,
-            authorName: '',
-            message: ' (Autoloot) ' + msg
-        });
-    }
-
+			}
+		},
+		enable: {
+			alias: ['enable', 'on'],
+			run: function() {
+				enabled = true
+				command.message('Easy looting is enabled.')
+			}
+		},
+		disable: {
+			alias: ['disable', 'off'],
+			run: function() {
+				enabled = false
+				command.message('Easy looting is disabled.')
+			}
+		},
+		autotrash: {
+			alias: ['autotrash', 'trash'],
+			run: function() {
+				autotrash = !autotrash
+
+				command.message('Autotrash toggled: ' + (autotrash ? 'on' : 'off'))
+				garbageCollect()
+			}
+		}
+	}
+
+	dispatch.hook('S_LOGIN', 1, event => { ({cid, playerId} = event) })
+
+	command.add('loot', c => {
+		if(c)
+			for(let cmd in commands)
+				if(commands[cmd].alias.includes(c))
+					commands[cmd].run()
+	})
+
+	dispatch.hook('S_LOAD_TOPO', 1, event => {
+		location = event
+		loot = {}
+	})
+
+	dispatch.hook('C_PLAYER_LOCATION', 2, event => { location = event })
+	dispatch.hook('S_RETURN_TO_LOBBY', 'raw', () => { loot = {} })
+
+	dispatch.hook('S_SPAWN_DROPITEM', 1, event => {
+		if(event.owners.some(owner => owner.id === playerId) && !blacklist.includes(event.item)) {
+			loot[event.id.toString()] = Object.assign(event, {priority: 0})
+
+			if(auto && !lootTimeout) tryLoot()
+		}
+	})
+
+	dispatch.hook('C_TRY_LOOT_DROPITEM', 1, event => {
+		if(enabled && !lootTimeout) lootTimeout = setTimeout(tryLoot, config.lootInterval)
+	})
+
+	dispatch.hook('S_DESPAWN_DROPITEM', 1, event => {
+		delete loot[event.id.toString()]
+	})
+
+	/*dispatch.hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
+		if(event.message === '@41') return false // Block "That isn't yours." system message.
+	})*/
+
+	dispatch.hook('S_INVEN', 10, event => {
+		if(autotrash) {
+			inventory = inventory ? inventory.concat(event.items) : event.items
+
+			if(!event.more) {
+				for(let item of inventory)
+					if(item.slot < 40) continue // First 40 slots are reserved for equipment, etc.
+					else if(trash.includes(item.dbid)) deleteItem(item.slot, item.amount)
+
+				inventory = null
+			}
+		}
+	})
+
+	function deleteItem(slot, amount) {
+		dispatch.toServer('C_DEL_ITEM', 1, {
+			cid: cid,
+			slot: slot - 40,
+			amount
+		})
+	}
+
+	function tryLoot() {
+		clearTimeout(lootTimeout)
+		lootTimeout = null
+
+		let lootItems = Object.values(loot).sort((a, b) => a.priority - b.priority)
+
+		if(!lootItems.length) return
+
+		for(let item of lootItems)
+			if(dist3D(location, item) < config.lootRadius) {
+				dispatch.toServer('C_TRY_LOOT_DROPITEM', 1, { id: item.id })
+				item.priority++
+				lootTimeout = setTimeout(tryLoot, config.lootInterval)
+				return
+			}
+
+		if(auto) setTimeout(tryLoot, config.lootScanInterval)
+	}
 }
+
+function dist3D(loc1, loc2) {
+	return Math.sqrt(Math.pow(loc2.x - loc1.x, 2) + Math.pow(loc2.y - loc1.y, 2) + Math.pow(loc2.z - loc1.z, 2))
+}