diff --git a/src/io/github/sammers/pla/http/Http.java b/src/io/github/sammers/pla/http/Http.java index 21d79925..be453dd0 100644 --- a/src/io/github/sammers/pla/http/Http.java +++ b/src/io/github/sammers/pla/http/Http.java @@ -70,23 +70,25 @@ public void start() { List search = ladder.search(searchQ); int remaining = 20 - search.size(); if (remaining > 0) { - List> topRealms = realmStats.top20Realms().subList(0, remaining); - List additionalResults = topRealms.stream().map(realm -> - new SearchResult(String.format("%s-%s", searchQ, realm.getValue0()), realm.getValue1(), "null") - ).toList(); - search.addAll(additionalResults); + String[] split = searchQ.trim().split("-"); + if (split.length == 1) { + List> top20 = realmStats.top20Realms(); + List> topRealms = top20.subList(0, Math.min(remaining, top20.size())); + List additionalResults = topRealms.stream().map(realm -> + new SearchResult(String.format("%s-%s", searchQ, realm.getValue0()), realm.getValue1(), "null") + ).toList(); + search.addAll(additionalResults); + } else if (split.length > 1 && !split[0].isEmpty()) { + List> top20 = realmStats.realmsStartingWithTop20(split[1]); + List> topRealms = top20.subList(0, Math.min(remaining, top20.size())); + List additionalResults = topRealms.stream() + .map(realm -> new SearchResult(String.format("%s-%s", split[0], realm.getValue0()), realm.getValue1(), "null") + ).toList(); + search.addAll(additionalResults); + } } List list = search.stream().map(SearchResult::toJson).map(j -> j.put("source", "pvpqnet")).toList(); - if (list.isEmpty()) { - checkPvPFrAPI.searchChars(searchQ).subscribe(searchResults -> { - ctx.response().end(new JsonArray(searchResults.stream().map(ExtCharacterSearcher.CheckPvPSearchResult::toJson).map(j -> j.put("source", "checkpvp.fr")).toList()).encode()); - }, err -> { - log.error("Failed to search chars", err); - ctx.response().end(new JsonArray().encode()); - }); - } else { - ctx.response().end(new JsonArray(list).encode()); - } + ctx.response().end(new JsonArray(list).encode()); } }); }); diff --git a/src/io/github/sammers/pla/logic/RealmStats.java b/src/io/github/sammers/pla/logic/RealmStats.java index 71dda129..76339b14 100644 --- a/src/io/github/sammers/pla/logic/RealmStats.java +++ b/src/io/github/sammers/pla/logic/RealmStats.java @@ -24,4 +24,14 @@ public List> top20Realms() { return top20.subList(0, Math.min(20, top20.size())); } + public List> realmsStartingWithTop20(String prefix) { + List> realms = new ArrayList<>(); + for (Pair realm : realmStats.keySet()) { + if (realm.getValue0().toLowerCase().startsWith(prefix.toLowerCase())) { + realms.add(realm); + } + } + return realms; + } + }