Skip to content

Commit

Permalink
logical operators for random select filters (#1)
Browse files Browse the repository at this point in the history
A feature to add logical operators to random select filters, particularly for keys with integer values. Implements "<", ">", "<=", ">=" and "&&" operations.
  • Loading branch information
MatVeiQaaa authored Dec 18, 2023
1 parent ac568c2 commit bdfa191
Showing 1 changed file with 88 additions and 25 deletions.
113 changes: 88 additions & 25 deletions core/src/bms/player/beatoraja/select/BarRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,93 @@ public Map<String, Object> getFilter() {
public void setFilter(Map<String, Object> filter) {
this.filter = filter;
}

public Boolean filterSong(ScoreData scoreData) {
Set<String> filterKey = this.getFilter().keySet();
for (String key : filterKey) {
String getterMethodName = "get" + key.substring(0, 1).toUpperCase()
+ key.substring(1);
try {
if (this.getFilter().get(key) instanceof Integer) { // Fork for integer values of the filter key.
Integer value = (Integer)this.getFilter().get(key);
if (scoreData == null) {
if (0 != value) {
return false;
}
} else {
Method getterMethod = ScoreData.class.getMethod(getterMethodName);
Object propertyValue = getterMethod.invoke(scoreData);
if (!propertyValue.equals(value)) {
return false;
}
}
return true;
}
Object valueArr[] = ((String)this.getFilter().get(key)).split("&&");
for (Object value : valueArr) // Fork for string values of the filter key.
{
value = ((String)value).replaceAll("\\s",""); // Clean from whitespaces.
if (scoreData == null) {
String stringValue = (String) value;
if (!stringValue.isEmpty() && !stringValue.substring(0, 1).equals("<")) {
return false; // Because lack of value would be less than anything.
}
} else {
Method getterMethod = ScoreData.class.getMethod(getterMethodName);
Object propertyValue = getterMethod.invoke(scoreData);
if (propertyValue instanceof Integer) {
String valueString = (String)value;
Integer propertyValueInt = (Integer)propertyValue;
Integer filterValueInt;
// Checking the position for the integer bit of the key value.
if (valueString.substring(1,2).equals("=")){ // If the operation is either >= or <=.
filterValueInt = Integer.parseInt(valueString.substring(2));
}
// If the operation is > or <.
else filterValueInt = Integer.parseInt(valueString.substring(1));

if (valueString.substring(0,1).equals(">")) // Fork for > and >= operations.
{
if (valueString.substring(1,2).equals("="))
{
if (!(propertyValueInt >= filterValueInt))
{
return false;
}
}
else if (!(propertyValueInt > filterValueInt))
{
return false;
}
}
if (valueString.substring(0,1).equals("<")) // Fork for < and <= operations.
{
if (valueString.substring(1,2).equals("="))
{
if (!(propertyValueInt <= filterValueInt))
{
return false;
}
}
else if (!(propertyValueInt < filterValueInt))
{
return false;
}
}
}
else if (!propertyValue.equals(value)) {
return false;
}
}
}
} catch (Throwable e) {
e.printStackTrace();
return false;
}
}
return true;
}

}

synchronized public void setAppendDirectoryBar(String key, Bar bar) {
Expand Down Expand Up @@ -1019,31 +1106,7 @@ public boolean updateBar(Bar bar) {
Set<String> filterKey = randomFolder.getFilter().keySet();
randomTargets = Stream.of(randomTargets).filter(r -> {
ScoreData scoreData = select.getScoreDataCache().readScoreData(r, config.getLnmode());
for (String key : filterKey) {
String getterMethodName = "get" + key.substring(0, 1).toUpperCase()
+ key.substring(1);
try {
Object value = randomFolder.getFilter().get(key);
if (scoreData == null) {
if (value instanceof String && !"".equals((String) value)) {
return false;
}
if (value instanceof Integer && 0 != (Integer) value) {
return false;
}
} else {
Method getterMethod = ScoreData.class.getMethod(getterMethodName);
Object propertyValue = getterMethod.invoke(scoreData);
if (!propertyValue.equals(value)) {
return false;
}
}
} catch (Throwable e) {
e.printStackTrace();
return false;
}
}
return true;
return randomFolder.filterSong(scoreData);
}).toArray(SongData[]::new);
}
if ((randomFolder.filter != null && randomTargets.length >= 1)
Expand Down

0 comments on commit bdfa191

Please sign in to comment.