Owyn лет назад: 6
Родитель
Сommit
55970a1367
2 измененных файлов с 69 добавлено и 50 удалено
  1. 62 46
      index.js
  2. 7 4
      manifest.json

+ 62 - 46
index.js

@@ -6,6 +6,7 @@ module.exports = function boxOpener(dispatch){
 		enabled = false,
 		boxEvent = null,
 		gacha_detected = false,
+		gacha_contract = 0,
 		isLooting = false,
 		location = null,
 		timer = null,
@@ -14,8 +15,8 @@ module.exports = function boxOpener(dispatch){
 		statOpened = 0,
 		statUsed = 0,
 		statStarted = null,
-		scanning = false,
-		inventory = null;
+		scanning = false/*,
+		inventory = null*/;
 		
 	command.add('box', () => {
 		if(!enabled && !scanning)
@@ -94,6 +95,7 @@ module.exports = function boxOpener(dispatch){
 		});*/
 		
 		hook('C_USE_ITEM', 3, event =>{
+			if(gacha_detected) return false; // Prevent spaming right click on gacha-type box while its already opening.
 			if(!scanning) return
 		
 			if(scanning){
@@ -105,76 +107,90 @@ module.exports = function boxOpener(dispatch){
 				let d = new Date();
 				statStarted = d.getTime();
 				enabled = true;
-				timer = setTimeout(openBox,delay);
-				return true; // for consistency of sent data
+				timer = dispatch.setTimeout(openBox,delay);
+				//return true; // for consistency of sent data
 			}
 		});
 		
-		hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
+		hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 'raw', () => {
 			if(!gacha_detected && !isLooting && boxEvent)
 			{
 				isLooting = true;
-				statOpened++;
 				if(!useDelay)
 				{
-					clearTimeout(timer);
+					dispatch.clearTimeout(timer);
 					openBox();
 				}
 			}
 		});
 		
-		hook('S_GACHA_END', dispatch.majorPatchVersion >= 99 ? 3 : 1, event => {
-			if(boxEvent)
+		hook('S_GACHA_END', 'raw', () => {
+			if(boxEvent && gacha_detected)
 			{
-				statOpened++;
-				if(!useDelay)
-				{
-					clearTimeout(timer);
-					openBox();
-				}
+				dispatch.clearTimeout(timer);
+				if(useDelay) timer = dispatch.setTimeout(openBox,delay);
+				else process.nextTick(openBox);
 			}
 		});
 		
 		hook('S_SYSTEM_MESSAGE', 1, event => {
-			const msg = dispatch.parseSystemMessage(event.message);
-			if(msg.id === 'SMT_ITEM_MIX_NEED_METERIAL' || msg.id === 'SMT_CANT_CONVERT_NOW')
+			const msg = dispatch.parseSystemMessage(event.message).id;
+			if(['SMT_ITEM_MIX_NEED_METERIAL', 'SMT_CANT_CONVERT_NOW', 'SMT_GACHA_NO_MORE_ITEM_SHORT', 'SMT_NOTI_LEFT_LIMITED_GACHA_ITEM', 'SMT_GACHA_CANCEL', 'SMT_COMMON_NO_MORE_ITEM_TO_USE'].includes(msg))
 			{
 				command.message("Box can not be opened anymore, stopping");
 				stop();
 			}
-        });
+		});
 		
-		hook('S_GACHA_START', 1, event => {
-			gacha_detected = true;
-			if(dispatch.majorPatchVersion >= 99)
-			{
-				dispatch.toServer('C_GACHA_TRY', 2,{
-					id:event.id,
-					amount: 1
-				});
-			}
-			else
-			{
-				dispatch.toServer('C_GACHA_TRY', 1,{
-					id:event.id
-				});
-			}
-        });
-			
+		hook('S_REQUEST_CONTRACT', 1, event => {
+			if(event.type !== 53) return;
+			dispatch.hookOnce('S_GACHA_START', 'raw', () => {
+				gacha_detected = true;
+				gacha_contract = event.id;
+				openBox();
+				return false;
+			})
+			return false;
+		});
+
+		hook('S_CANCEL_CONTRACT', 1, event => {
+			if (!gacha_detected || event.type !== 53) return;
+			gacha_contract = 0;
+			stop();
+		});
 	}
 	
 	function openBox() 
 	{
 		if(dispatch.game.inventory.getTotalAmount(boxEvent.id) > 0)
 		{
-			boxEvent.loc = location.loc;
-			boxEvent.w = location.w;
-			dispatch.toServer('C_USE_ITEM', 3, boxEvent);
-			if(useDelay)
+			if(gacha_detected)
 			{
-				statUsed++;	// counter for used items other than boxes
+				if(dispatch.majorPatchVersion >= 99)
+				{
+					dispatch.toServer('C_GACHA_TRY', 2,{
+						id:gacha_contract,
+						amount: 1
+					});
+				}
+				else
+				{
+					dispatch.toServer('C_GACHA_TRY', 1,{
+						id:gacha_contract
+					});
+				}
 			}
-			timer = setTimeout(openBox,delay);
+			else {
+				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 = dispatch.setTimeout(openBox,delay);
+			}
+			statOpened++;
 		}
 		else
 		{
@@ -201,14 +217,14 @@ module.exports = function boxOpener(dispatch){
 		}
 		else
 		{
-			clearTimeout(timer);
+			if(gacha_detected && gacha_contract) dispatch.toServer('C_GACHA_CANCEL', 1, { id: gacha_contract });
+			if(useDelay && statOpened === 0) statOpened = statUsed;
+			if(!gacha_detected) statOpened += 1; // Add the box we used at start if not gacha.
+			dispatch.clearTimeout(timer);
 			enabled = false;
 			gacha_detected = false;
+			gacha_contract = 0;
 			boxEvent = null;
-			if(useDelay && statOpened == 0)
-			{
-				statOpened = statUsed;
-			}
 			let d = new Date();
 			let t = d.getTime();
 			let timeElapsedMSec = t-statStarted;

+ 7 - 4
manifest.json

@@ -1,16 +1,19 @@
 {
 	"files": {
 		"README.md": "8b1704eff4c6f1989542d77d59666e28bfda029392946c5688d88af94cced028",
-		"index.js": "bf0aefef5aa8dfc98b24efb1c7c008df8b1ca84f3418ddd901f96ef342f1f0c2",
+		"index.js": "52da9eb99a6fddee31105920663c7bbd1101d493f285af2f9684ed7a984e2214",
 		"module.json": "c40095006cfede1be501b2c0204dd2d778cd963dc0cb26e33dc3c8a839123fab"
 	},
 	"defs": {
+		"C_GACHA_CANCEL": 1,
 		"C_GACHA_TRY": [1,2],
 		"C_PLAYER_LOCATION": 5,
 		"C_USE_ITEM": 3,
-		"S_GACHA_END": [1,3],
-		"S_GACHA_START": 1,
+		"S_CANCEL_CONTRACT": 1,
+		"S_GACHA_END": "raw",
+		"S_GACHA_START": "raw",
+		"S_REQUEST_CONTRACT": 1,
 		"S_SYSTEM_MESSAGE": 1,
-		"S_SYSTEM_MESSAGE_LOOT_ITEM": 1
+		"S_SYSTEM_MESSAGE_LOOT_ITEM": "raw"
 	}
 }