Skip to content

Commit

Permalink
Make message parsing more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
Rottenbeer committed Apr 5, 2020
1 parent 4edb075 commit e7ca5e7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 37 deletions.
76 changes: 42 additions & 34 deletions app-core/src/main/java/com/mercury/platform/core/ChatHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,45 @@ private void executeTradeMessage() {
result.contains("Bonjour") ||
result.contains("안녕") ||
result.contains("Зд"))) {
this.gameToFront();
MercuryStoreCore.blockHotkeySubject.onNext(true);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_A);

robot.keyPress(KeyEvent.VK_BACK_SPACE);
robot.keyRelease(KeyEvent.VK_BACK_SPACE);

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Timer timer = new Timer(300, action -> {
StringSelection selection = new StringSelection("");
clipboard.setContents(selection, null);
});
timer.setRepeats(false);
timer.start();

MercuryStoreCore.blockHotkeySubject.onNext(false);
if (this.gameToFront() == true) {
MercuryStoreCore.blockHotkeySubject.onNext(true);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_A);

robot.keyPress(KeyEvent.VK_BACK_SPACE);
robot.keyRelease(KeyEvent.VK_BACK_SPACE);

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Timer timer = new Timer(300, action -> {
StringSelection selection = new StringSelection("");
clipboard.setContents(selection, null);
});
timer.setRepeats(false);
timer.start();

MercuryStoreCore.blockHotkeySubject.onNext(false);
} else {
System.out.println("Failed to bring game to front!");
}

}
} catch (UnsupportedFlavorException | IOException e) {
} catch (UnsupportedFlavorException e) {
System.out.println("Unsupported Flavor Exception occured, logging it");
MercuryStoreCore.errorHandlerSubject.onNext(new MercuryError(e));
} catch (IOException e) {
System.out.println("IOException occured, logging it");
MercuryStoreCore.errorHandlerSubject.onNext(new MercuryError(e));
}
}
Expand Down Expand Up @@ -128,18 +136,18 @@ private void openChat(String whisper) {
MercuryStoreCore.blockHotkeySubject.onNext(false);
}

private void gameToFront() {
User32.INSTANCE.EnumWindows((hWnd, arg1) -> {
private boolean gameToFront() {
return !User32.INSTANCE.EnumWindows((hWnd, arg1) -> {
char[] className = new char[512];
User32.INSTANCE.GetClassName(hWnd, className, 512);
String wText = Native.toString(className);

if (wText.isEmpty()) {
return true;
}
//System.out.println(wText);
if (wText.equals("POEWindowClass")) {
User32.INSTANCE.SetForegroundWindow(hWnd);
return false;
return !User32.INSTANCE.SetForegroundWindow(hWnd);
}
return true;
}, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import com.mercury.platform.core.utils.interceptor.*;
import com.mercury.platform.shared.AsSubscriber;
import com.mercury.platform.shared.entity.message.MercuryError;
import com.mercury.platform.shared.store.MercuryStoreCore;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.RandomAccessFile;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
Expand Down Expand Up @@ -73,9 +77,19 @@ public void parse() {
.collect(Collectors.toList());

List<String> resultMessages = filteredMessages.stream().filter(message -> {
if (message == null) {
return false;
}
if ( message.contains("2019") || message.contains("2020" ) || message.contains("2021" )) { //todo
Date date = new Date(StringUtils.substring(message, 0, 20));
return date.after(lastMessageDate);
try {
DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date date = df.parse(StringUtils.substring(message, 0, 20).trim());
return date.after(lastMessageDate);
} catch (ParseException e) {
System.out.println("Error while parsing message, #datefix in message: " + message);
MercuryStoreCore.errorHandlerSubject.onNext(new MercuryError(e));
return false;
}
} else {
return false;
}
Expand All @@ -84,7 +98,13 @@ public void parse() {
this.interceptors.forEach(interceptor -> {
resultMessages.forEach(message -> {
if (interceptor.match(message)) {
this.lastMessageDate = new Date(StringUtils.substring(message, 0, 20));
try {
DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
this.lastMessageDate = df.parse(StringUtils.substring(message, 0, 20).trim());
} catch (ParseException e) {
System.out.println("Error while parsing message, #datefix in message: " + message);
MercuryStoreCore.errorHandlerSubject.onNext(new MercuryError(e));
}
}
});
});
Expand Down

0 comments on commit e7ca5e7

Please sign in to comment.