Skip to content

Commit

Permalink
Bug fixes and improvements part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Exslims committed Aug 25, 2017
1 parent 80bc441 commit 83114a6
Show file tree
Hide file tree
Showing 53 changed files with 663 additions and 350 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public MessageFileHandler(String logFilePath) {
this.interceptors.add(new PlainMessageInterceptor());
this.interceptors.add(new PlayerJoinInterceptor());
this.interceptors.add(new PlayerLeftInterceptor());
this.interceptors.add(new PlayerInaccessibleInterceptor());

this.subscribe();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.mercury.platform.core.utils.interceptor;

import com.mercury.platform.core.utils.interceptor.filter.MessageFilter;
import com.mercury.platform.core.utils.interceptor.filter.MessageMatcher;

/**
* Created by Константин on 11.01.2017.
*/
public abstract class MessageInterceptor {
protected MessageFilter filter;
protected MessageMatcher filter;

public MessageInterceptor() {
filter = getFilter();
filter = match();
}

public boolean match(String message) {
Expand All @@ -22,5 +22,5 @@ public boolean match(String message) {

protected abstract void process(String message);

protected abstract MessageFilter getFilter();
protected abstract MessageMatcher match();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.mercury.platform.core.utils.interceptor;


import com.mercury.platform.core.utils.interceptor.filter.MessageFilter;
import com.mercury.platform.core.utils.interceptor.filter.MessageMatcher;
import com.mercury.platform.core.utils.interceptor.plain.*;
import com.mercury.platform.shared.entity.message.PlainMessageDescriptor;
import com.mercury.platform.shared.store.MercuryStoreCore;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PlainMessageInterceptor extends MessageInterceptor {
private List<LocalizationMatcher> clients = new ArrayList<>();
Expand All @@ -31,172 +29,18 @@ protected void process(String message) {
.filter(matcher -> matcher.isSuitableFor(message))
.findAny().orElse(null);
if (localizationMatcher != null) {
localizationMatcher.processMessage(message);
PlainMessageDescriptor plainMessage = localizationMatcher.getPlainMessage(message);
if (plainMessage != null) {
MercuryStoreCore.plainMessageSubject.onNext(plainMessage);
}
}
}

@Override
protected MessageFilter getFilter() {
protected MessageMatcher match() {
return message ->
this.clients.stream()
.filter(matcher -> matcher.isSuitableFor(message))
.findAny().orElse(null) != null;
}

private abstract class LocalizationMatcher {
public abstract boolean isSuitableFor(String message);

public abstract boolean isIncoming();

public abstract String trimString(String message);

public void processMessage(String message) {
Pattern pattern = Pattern.compile("^(\\<.+?\\>)?\\s?(.+?):(.+)$");
Matcher matcher = pattern.matcher(this.trimString(message));
if (matcher.find()) {
PlainMessageDescriptor descriptor = new PlainMessageDescriptor();
descriptor.setNickName(matcher.group(2));
descriptor.setMessage(matcher.group(3));
descriptor.setIncoming(this.isIncoming());
MercuryStoreCore.plainMessageSubject.onNext(descriptor);
}
}
}

private class EngIncLocalizationMatcher extends LocalizationMatcher {
@Override
public boolean isSuitableFor(String message) {
return message.contains("@From");
}

@Override
public boolean isIncoming() {
return true;
}

@Override
public String trimString(String message) {
return StringUtils.substringAfter(message, "@From ");
}
}

private class EngOutLocalizationMatcher extends LocalizationMatcher {
@Override
public boolean isSuitableFor(String message) {
return message.contains("@To");
}

@Override
public boolean isIncoming() {
return false;
}

@Override
public String trimString(String message) {
return StringUtils.substringAfter(message, "@To ");
}
}

private class RuIncLocalizationMatcher extends LocalizationMatcher {
@Override
public boolean isSuitableFor(String message) {
return message.contains("@От кого");
}

@Override
public boolean isIncoming() {
return true;
}

@Override
public String trimString(String message) {
return StringUtils.substringAfter(message, "@От кого ");
}
}

private class RuOutLocalizationMatcher extends LocalizationMatcher {
@Override
public boolean isSuitableFor(String message) {
return message.contains("@Кому");
}

@Override
public boolean isIncoming() {
return false;
}

@Override
public String trimString(String src) {
return StringUtils.substringAfter(src, "@Кому ");
}

}

private class ArabicInLocalizationMatcher extends LocalizationMatcher {
@Override
public boolean isSuitableFor(String message) {
return message.contains("@จาก");
}

@Override
public boolean isIncoming() {
return true;
}

@Override
public String trimString(String src) {
return StringUtils.substringAfter(src, "@จาก");
}
}

private class ArabicOutLocalizationMatcher extends LocalizationMatcher {
@Override
public boolean isSuitableFor(String message) {
return message.contains("@ถึง");
}

@Override
public boolean isIncoming() {
return false;
}

@Override
public String trimString(String src) {
return StringUtils.substringAfter(src, "@ถึง");
}
}

private class BZIncLocalizationMatcher extends LocalizationMatcher {
@Override
public boolean isSuitableFor(String message) {
return message.contains("@De");
}

@Override
public boolean isIncoming() {
return true;
}

@Override
public String trimString(String src) {
return StringUtils.substringAfter(src, "@De");
}
}

private class BZOutLocalizationMatcher extends LocalizationMatcher {
@Override
public boolean isSuitableFor(String message) {
return message.contains("@Para");
}

@Override
public boolean isIncoming() {
return false;
}

@Override
public String trimString(String src) {
return StringUtils.substringAfter(src, "@Para");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mercury.platform.core.utils.interceptor;

import com.mercury.platform.core.utils.interceptor.filter.MessageMatcher;
import com.mercury.platform.shared.entity.message.PlainMessageDescriptor;
import com.mercury.platform.shared.store.MercuryStoreCore;
import org.apache.commons.lang3.StringUtils;


public class PlayerInaccessibleInterceptor extends MessageInterceptor {
private PlainMessageDescriptor lastPlainMessage;

public PlayerInaccessibleInterceptor() {
MercuryStoreCore.plainMessageSubject.subscribe(message -> {
this.lastPlainMessage = message;
});
}

@Override
protected void process(String message) {
this.lastPlainMessage.setMessage(StringUtils.substringAfter(message, " : "));
MercuryStoreCore.plainMessageSubject.onNext(this.lastPlainMessage);
}

@Override
protected MessageMatcher match() {
return message -> message.contains("That character is not online.");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mercury.platform.core.utils.interceptor;

import com.mercury.platform.core.utils.interceptor.filter.MessageFilter;
import com.mercury.platform.core.utils.interceptor.filter.MessageMatcher;
import com.mercury.platform.shared.store.MercuryStoreCore;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -11,7 +11,7 @@ protected void process(String message) {
}

@Override
protected MessageFilter getFilter() {
protected MessageMatcher match() {
return message -> message.contains("has joined the area.");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mercury.platform.core.utils.interceptor;

import com.mercury.platform.core.utils.interceptor.filter.MessageFilter;
import com.mercury.platform.core.utils.interceptor.filter.MessageMatcher;
import com.mercury.platform.shared.store.MercuryStoreCore;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -12,7 +12,7 @@ protected void process(String message) {
}

@Override
protected MessageFilter getFilter() {
protected MessageMatcher match() {
return message -> message.contains("has left the area.");
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
package com.mercury.platform.core.utils.interceptor;

import com.mercury.platform.core.misc.SoundType;
import com.mercury.platform.core.utils.interceptor.filter.MessageFilter;
import com.mercury.platform.core.utils.interceptor.filter.MessageMatcher;
import com.mercury.platform.shared.MessageParser;
import com.mercury.platform.shared.config.Configuration;
import com.mercury.platform.shared.config.configration.PlainConfigurationService;
import com.mercury.platform.shared.config.descriptor.NotificationSettingsDescriptor;
import com.mercury.platform.shared.entity.message.NotificationDescriptor;
import com.mercury.platform.shared.store.MercuryStoreCore;
import net.jodah.expiringmap.ExpiringMap;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class TradeIncMessagesInterceptor extends MessageInterceptor {
private MessageParser messageParser = new MessageParser();
private PlainConfigurationService<NotificationSettingsDescriptor> config;
private List<LocalizationMatcher> clients = new ArrayList<>();
private Map<String, String> expiresMessages;

public TradeIncMessagesInterceptor() {
this.config = Configuration.get().notificationConfiguration();
this.clients.add(new EngIncLocalizationMatcher());
this.clients.add(new RuIncLocalizationMatcher());
this.clients.add(new ArabicInLocalizationMatcher());
this.clients.add(new BZIncLocalizationMatcher());
this.expiresMessages = ExpiringMap.builder()
.expiration(1, TimeUnit.HOURS)
.build();
MercuryStoreCore.expiredNotificationSubject.subscribe(notificationDescriptor -> {
this.expiresMessages.put(UUID.randomUUID().toString(), StringUtils.substringAfter(notificationDescriptor.getSourceString(), ":"));
});
}

@Override
Expand All @@ -50,7 +38,7 @@ protected void process(String message) {
}

@Override
protected MessageFilter getFilter() {
protected MessageMatcher match() {
return message ->
this.clients.stream()
.filter(matcher -> matcher.isSuitableFor(message))
Expand All @@ -73,10 +61,7 @@ public NotificationDescriptor getDescriptor(String message) {
public void processMessage(String message) {
NotificationDescriptor notificationDescriptor = this.getDescriptor(message);
if (notificationDescriptor != null) {
if (!expiresMessages.containsValue(StringUtils.substringAfter(notificationDescriptor.getSourceString(), ":"))) {
MercuryStoreCore.soundSubject.onNext(SoundType.MESSAGE);
MercuryStoreCore.newNotificationSubject.onNext(notificationDescriptor);
}
MercuryStoreCore.newNotificationSubject.onNext(notificationDescriptor);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mercury.platform.core.utils.interceptor;

import com.mercury.platform.core.utils.interceptor.filter.MessageFilter;
import com.mercury.platform.core.utils.interceptor.filter.MessageMatcher;
import com.mercury.platform.shared.MessageParser;
import com.mercury.platform.shared.config.Configuration;
import com.mercury.platform.shared.config.configration.PlainConfigurationService;
Expand Down Expand Up @@ -40,7 +40,7 @@ protected void process(String message) {
}

@Override
protected MessageFilter getFilter() {
protected MessageMatcher match() {
return message ->
this.clients.stream()
.filter(matcher -> matcher.isSuitableFor(message))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mercury.platform.core.utils.interceptor.filter;


public interface MessageFilter {
public interface MessageMatcher {
boolean isMatching(String message);
}
Loading

0 comments on commit 83114a6

Please sign in to comment.