Owyn 8 years ago
parent
commit
f1c3b414b4
3 changed files with 219 additions and 217 deletions
  1. 208 208
      index.js
  2. 6 6
      manifest.json
  3. 5 3
      module.json

+ 208 - 208
index.js

@@ -1,208 +1,208 @@
-const Command = require('command');
-
-module.exports = function boxOpener(dispatch){
-	const command = Command(dispatch)
-	
-	let	hooks = [],
-		enabled = false,
-		boxEvent = null,
-		gacha_detected = false,
-		isLooting = false,
-		location = null,
-		timer = null,
-		delay = 5000,
-		useDelay = false,
-		statOpened = 0,
-		statUsed = 0,
-		statStarted = null,
-		scanning = false;
-		boxId = 166901, // MWA box as default.
-		inventory = null;
-		
-	command.add('box', () => {
-		if(!enabled && !scanning)
-		{
-			scanning = true;
-			load();
-			command.message('Please normally open a box now and the script will continue opening it');
-		}
-		else
-		{
-			stop();
-		}
-	});
-		
-	command.add('boxdelay', (arg) => {
-		if(arg === "0")
-		{
-			useDelay = false;
-			delay = 5000;
-			command.message("Turning OFF minimum box opening delay, enjoy the speed");
-		}
-		else if(!isNaN(arg))
-		{
-			useDelay = true;
-			delay = parseInt(arg);
-			command.message("Minimum box opening delay is set to: " + (delay / 1000) + " sec");
-		}
-		else
-		{
-			command.message("Minimum box opening delay is set to: " + (useDelay ? (delay / 1000) + " sec" : "no delay" ));
-		}
-    });
-	
-	dispatch.hook('C_PLAYER_LOCATION', 4, event =>{location = event});
-	
-	function load()
-	{
-		hook('S_INVEN', 12, event =>{
-			if(!enabled) return
-			
-			isLooting = false; // S_INVEN comes only after all S_SYSTEM_MESSAGE_LOOT_ITEM
-
-			if(event.first) inventory = []
-			else if(!inventory) return
-
-			for(let item of event.items) inventory.push(item)
-
-			if(!event.more) 
-			{
-				let box = false
-				for(let item of inventory) 
-				{
-					if(item.slot < 40) continue 
-					if(item.id == boxId)
-					{
-						box = true
-					}
-				}
-				if(!box) 
-				{
-					command.message("You ran out of boxes, stopping");
-					stop();
-				}
-				inventory.splice(0,inventory.length)
-				inventory = [];
-				inventory = null
-			}
-		});
-		
-		hook('C_USE_ITEM', 3, event =>{
-			if(!scanning) return
-		
-			if(scanning){
-				boxEvent = event;
-				boxId = event.id;
-				command.message("Box set to: "+boxId+", proceeding to auto-open it with "  + (useDelay ? "a minimum " + (delay / 1000) + " sec delay" : "no delay" ));
-				scanning = false;
-				
-				let d = new Date();
-				statStarted = d.getTime();
-				enabled = true;
-				timer = setTimeout(openBox,delay);
-			}
-		});
-		
-		hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
-			if(!gacha_detected && !isLooting)
-			{
-				isLooting = true;
-				statOpened++;
-				if(!useDelay)
-				{
-					clearTimeout(timer);
-					openBox();
-				}
-			}
-		});
-		
-		hook('S_GACHA_END', 1, event => {
-				statOpened++;
-				if(!useDelay)
-				{
-					clearTimeout(timer);
-					openBox();
-				}
-		});
-		
-		hook('S_SYSTEM_MESSAGE', 1, event => {
-			const msg = dispatch.base.parseSystemMessage(event.message);
-			if(msg.id === 'SMT_ITEM_MIX_NEED_METERIAL' || msg.id === 'SMT_CANT_CONVERT_NOW')
-			{
-				command.message("Box can not be opened anymore, stopping");
-				stop();
-			}
-        });
-		
-		hook('S_GACHA_START', 1, event => {
-			gacha_detected = true;
-			dispatch.toServer('C_GACHA_TRY', 1,{
-				id:event.id
-			})
-        });
-			
-	}
-	
-	function openBox() 
-	{
-		boxEvent.loc = location.loc;
-		boxEvent.w = location.w;
-		dispatch.toServer('C_USE_ITEM', 3, boxEvent);
-		if(useDelay)
-		{
-			statUsed++;	// counter for used items other than boxes
-		}
-		timer = setTimeout(openBox,delay);
-	}
-	
-	function addZero(i) 
-	{
-		if (i < 10) {
-			i = "0" + i;
-		}
-		return i;
-	}
-
-	function stop() 
-	{
-		unload();
-		if(scanning)
-		{
-			scanning = false;
-			command.message('Scanning for a box is aborted');
-		}
-		else
-		{
-			clearTimeout(timer);
-			enabled = false;
-			gacha_detected = false;
-			if(useDelay && statOpened == 0)
-			{
-				statOpened = statUsed;
-			}
-			let d = new Date();
-			let t = d.getTime();
-			let timeElapsedMSec = t-statStarted;
-			d = new Date(1970, 0, 1); // Epoch
-			d.setMilliseconds(timeElapsedMSec);
-			let h = addZero(d.getHours());
-			let m = addZero(d.getMinutes());
-			let s = addZero(d.getSeconds());
-			command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + ". Per box: " + ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) + " sec");
-			statOpened = 0;
-			statUsed = 0;
-		}
-	}
-	
-	function unload() {
-		if(hooks.length) {
-			for(let h of hooks) dispatch.unhook(h)
-
-			hooks = []
-		}
-	}
-
-	function hook() {
-		hooks.push(dispatch.hook(...arguments))
-	}
-}
+const Command = require('command');
+
+module.exports = function boxOpener(dispatch){
+	const command = Command(dispatch)
+	
+	let	hooks = [],
+		enabled = false,
+		boxEvent = null,
+		gacha_detected = false,
+		isLooting = false,
+		location = null,
+		timer = null,
+		delay = 5500,
+		useDelay = false,
+		statOpened = 0,
+		statUsed = 0,
+		statStarted = null,
+		scanning = false;
+		boxId = 166901, // MWA box as default.
+		inventory = null;
+		
+	command.add('box', () => {
+		if(!enabled && !scanning)
+		{
+			scanning = true;
+			load();
+			command.message('Please normally open a box now and the script will continue opening it');
+		}
+		else
+		{
+			stop();
+		}
+	});
+		
+	command.add('boxdelay', (arg) => {
+		if(arg === "0")
+		{
+			useDelay = false;
+			delay = 5000;
+			command.message("Turning OFF minimum box opening delay, enjoy the speed");
+		}
+		else if(!isNaN(arg))
+		{
+			useDelay = true;
+			delay = parseInt(arg);
+			command.message("Minimum box opening delay is set to: " + (delay / 1000) + " sec");
+		}
+		else
+		{
+			command.message("Minimum box opening delay is set to: " + (useDelay ? (delay / 1000) + " sec" : "no delay" ));
+		}
+    });
+	
+	dispatch.hook('C_PLAYER_LOCATION', 4, event =>{location = event});
+	
+	function load()
+	{
+		hook('S_INVEN', 12, event =>{
+			if(!enabled) return
+			
+			isLooting = false; // S_INVEN comes only after all S_SYSTEM_MESSAGE_LOOT_ITEM
+
+			if(event.first) inventory = []
+			else if(!inventory) return
+
+			for(let item of event.items) inventory.push(item)
+
+			if(!event.more) 
+			{
+				let box = false
+				for(let item of inventory) 
+				{
+					if(item.slot < 40) continue 
+					if(item.id == boxId)
+					{
+						box = true
+					}
+				}
+				if(!box) 
+				{
+					command.message("You ran out of boxes, stopping");
+					stop();
+				}
+				inventory.splice(0,inventory.length)
+				inventory = [];
+				inventory = null
+			}
+		});
+		
+		hook('C_USE_ITEM', 3, event =>{
+			if(!scanning) return
+		
+			if(scanning){
+				boxEvent = event;
+				boxId = event.id;
+				command.message("Box set to: "+boxId+", proceeding to auto-open it with "  + (useDelay ? "a minimum " + (delay / 1000) + " sec delay" : "no delay" ));
+				scanning = false;
+				
+				let d = new Date();
+				statStarted = d.getTime();
+				enabled = true;
+				timer = setTimeout(openBox,delay);
+			}
+		});
+		
+		hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
+			if(!gacha_detected && !isLooting)
+			{
+				isLooting = true;
+				statOpened++;
+				if(!useDelay)
+				{
+					clearTimeout(timer);
+					openBox();
+				}
+			}
+		});
+		
+		hook('S_GACHA_END', 1, event => {
+				statOpened++;
+				if(!useDelay)
+				{
+					clearTimeout(timer);
+					openBox();
+				}
+		});
+		
+		hook('S_SYSTEM_MESSAGE', 1, event => {
+			const msg = dispatch.base.parseSystemMessage(event.message);
+			if(msg.id === 'SMT_ITEM_MIX_NEED_METERIAL' || msg.id === 'SMT_CANT_CONVERT_NOW')
+			{
+				command.message("Box can not be opened anymore, stopping");
+				stop();
+			}
+        });
+		
+		hook('S_GACHA_START', 1, event => {
+			gacha_detected = true;
+			dispatch.toServer('C_GACHA_TRY', 1,{
+				id:event.id
+			})
+        });
+			
+	}
+	
+	function openBox() 
+	{
+		boxEvent.loc = location.loc;
+		boxEvent.w = location.w;
+		dispatch.toServer('C_USE_ITEM', 3, boxEvent);
+		if(useDelay)
+		{
+			statUsed++;	// counter for used items other than boxes
+		}
+		timer = setTimeout(openBox,delay);
+	}
+	
+	function addZero(i) 
+	{
+		if (i < 10) {
+			i = "0" + i;
+		}
+		return i;
+	}
+
+	function stop() 
+	{
+		unload();
+		if(scanning)
+		{
+			scanning = false;
+			command.message('Scanning for a box is aborted');
+		}
+		else
+		{
+			clearTimeout(timer);
+			enabled = false;
+			gacha_detected = false;
+			if(useDelay && statOpened == 0)
+			{
+				statOpened = statUsed;
+			}
+			let d = new Date();
+			let t = d.getTime();
+			let timeElapsedMSec = t-statStarted;
+			d = new Date(1970, 0, 1); // Epoch
+			d.setMilliseconds(timeElapsedMSec);
+			let h = addZero(d.getHours());
+			let m = addZero(d.getMinutes());
+			let s = addZero(d.getSeconds());
+			command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + ". Per box: " + ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) + " sec");
+			statOpened = 0;
+			statUsed = 0;
+		}
+	}
+	
+	function unload() {
+		if(hooks.length) {
+			for(let h of hooks) dispatch.unhook(h)
+
+			hooks = []
+		}
+	}
+
+	function hook() {
+		hooks.push(dispatch.hook(...arguments))
+	}
+}

+ 6 - 6
manifest.json

@@ -1,17 +1,17 @@
 {
 	"files": {
-		"index.js": "675f07f248ff6a251f0817ea39b410970f1f915ead4946f887c3ebd1c6ed7ea3",
-		"module.json": "1cd5f735938765192c569a381cca2d3d7df8b43808dcbd8c3362667f7f1051bb",
-		"README.md": "4a82b827288a9d7f34cc1de1ff869ca1371d9a0144b960cf33f5501a56ff73b4"
+		"README.md": "4a82b827288a9d7f34cc1de1ff869ca1371d9a0144b960cf33f5501a56ff73b4",
+		"index.js": "2aec73dff4a44c46a918df4291c14231254a98b5bb4d8ec5f4c7ebadb7f328c4",
+		"module.json": "57f41ca13f08b91143fcdba101eff1a3e1f3b8d55dd5c536b822df6244531e8f"
 	},
 	"defs": {
+		"C_GACHA_TRY": 1,
 		"C_PLAYER_LOCATION": 4,
 		"C_USE_ITEM": 3,
-		"S_GACHA_START": 1,
-		"C_GACHA_TRY": 1,
 		"S_GACHA_END": 1,
+		"S_GACHA_START": 1,
 		"S_INVEN": 12,
 		"S_SYSTEM_MESSAGE": 1,
 		"S_SYSTEM_MESSAGE_LOOT_ITEM": 1
 	}
-}
+}

+ 5 - 3
module.json

@@ -1,4 +1,6 @@
 {
-  "servers": ["https://raw.githubusercontent.com/Owyn/generic-box-opener-item-user/master/"],
-  "supportUrl": "https://github.com/Owyn/generic-box-opener-item-user/issues"
-}
+	"disableAutoUpdate": false,
+	"options": { "niceName": "" },
+	"servers": ["https://raw.githubusercontent.com/Owyn/generic-box-opener-item-user/master/"],
+	"supportUrl": "https://github.com/Owyn/generic-box-opener-item-user/issues"
+}