From bdfa19137b844cc1d6ab5dc237721ff730397241 Mon Sep 17 00:00:00 2001 From: MatVeiQaaa Date: Mon, 18 Dec 2023 14:30:46 -0800 Subject: [PATCH] logical operators for random select filters (#1) A feature to add logical operators to random select filters, particularly for keys with integer values. Implements "<", ">", "<=", ">=" and "&&" operations. --- .../player/beatoraja/select/BarRenderer.java | 113 ++++++++++++++---- 1 file changed, 88 insertions(+), 25 deletions(-) diff --git a/core/src/bms/player/beatoraja/select/BarRenderer.java b/core/src/bms/player/beatoraja/select/BarRenderer.java index 0b9c3836..2a8c54c8 100644 --- a/core/src/bms/player/beatoraja/select/BarRenderer.java +++ b/core/src/bms/player/beatoraja/select/BarRenderer.java @@ -407,6 +407,93 @@ public Map getFilter() { public void setFilter(Map filter) { this.filter = filter; } + + public Boolean filterSong(ScoreData scoreData) { + Set 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) { @@ -1019,31 +1106,7 @@ public boolean updateBar(Bar bar) { Set 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)