Make HiscoreClient call the OSRS hiscore API directly

This commit is contained in:
Tomas Slusny
2018-09-14 21:31:50 +02:00
committed by Adam
parent db70df613e
commit 89b8bc52ca
9 changed files with 111 additions and 166 deletions

View File

@@ -53,8 +53,7 @@ public class HiscoreController
@RequestMapping("/{endpoint}")
public HiscoreResult lookup(@PathVariable HiscoreEndpoint endpoint, @RequestParam String username) throws ExecutionException
{
HiscoreResultBuilder resultBuilder = hiscoreService.lookupUsername(username, endpoint);
HiscoreResult result = resultBuilder.build();
HiscoreResult result = hiscoreService.lookupUsername(username, endpoint);
// Submit to xp tracker?
switch (endpoint)
@@ -75,10 +74,10 @@ public class HiscoreController
HiscoreSkill skill = HiscoreSkill.valueOf(skillName.toUpperCase());
// RS api only supports looking up all stats
HiscoreResultBuilder result = hiscoreService.lookupUsername(username, endpoint);
HiscoreResult result = hiscoreService.lookupUsername(username, endpoint);
// Find the skill to return
Skill requested = result.getSkill(skill.ordinal());
Skill requested = result.getSkill(skill);
SingleHiscoreSkillResult skillResult = new SingleHiscoreSkillResult();
skillResult.setPlayer(username);

View File

@@ -1,91 +0,0 @@
/*
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service.hiscore;
import java.util.ArrayList;
import java.util.List;
import net.runelite.http.api.hiscore.HiscoreResult;
import net.runelite.http.api.hiscore.Skill;
public class HiscoreResultBuilder
{
private String player;
private final List<Skill> skills = new ArrayList<>();
public void setPlayer(String player)
{
this.player = player;
}
public void setNextSkill(Skill skill)
{
skills.add(skill);
}
public Skill getSkill(int index)
{
return skills.get(index);
}
public HiscoreResult build()
{
HiscoreResult hiscoreResult = new HiscoreResult();
hiscoreResult.setPlayer(player);
hiscoreResult.setOverall(skills.get(0));
hiscoreResult.setAttack(skills.get(1));
hiscoreResult.setDefence(skills.get(2));
hiscoreResult.setStrength(skills.get(3));
hiscoreResult.setHitpoints(skills.get(4));
hiscoreResult.setRanged(skills.get(5));
hiscoreResult.setPrayer(skills.get(6));
hiscoreResult.setMagic(skills.get(7));
hiscoreResult.setCooking(skills.get(8));
hiscoreResult.setWoodcutting(skills.get(9));
hiscoreResult.setFletching(skills.get(10));
hiscoreResult.setFishing(skills.get(11));
hiscoreResult.setFiremaking(skills.get(12));
hiscoreResult.setCrafting(skills.get(13));
hiscoreResult.setSmithing(skills.get(14));
hiscoreResult.setMining(skills.get(15));
hiscoreResult.setHerblore(skills.get(16));
hiscoreResult.setAgility(skills.get(17));
hiscoreResult.setThieving(skills.get(18));
hiscoreResult.setSlayer(skills.get(19));
hiscoreResult.setFarming(skills.get(20));
hiscoreResult.setRunecraft(skills.get(21));
hiscoreResult.setHunter(skills.get(22));
hiscoreResult.setConstruction(skills.get(23));
hiscoreResult.setClueScrollEasy(skills.get(24));
hiscoreResult.setClueScrollMedium(skills.get(25));
hiscoreResult.setClueScrollAll(skills.get(26));
hiscoreResult.setBountyHunterRogue(skills.get(27));
hiscoreResult.setBountyHunterHunter(skills.get(28));
hiscoreResult.setClueScrollHard(skills.get(29));
hiscoreResult.setLastManStanding(skills.get(30));
hiscoreResult.setClueScrollElite(skills.get(31));
hiscoreResult.setClueScrollMaster(skills.get(32));
return hiscoreResult;
}
}

View File

@@ -24,6 +24,7 @@
*/
package net.runelite.http.service.hiscore;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
@@ -31,104 +32,38 @@ import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.hiscore.HiscoreClient;
import net.runelite.http.api.hiscore.HiscoreEndpoint;
import net.runelite.http.api.hiscore.HiscoreSkill;
import net.runelite.http.api.hiscore.Skill;
import net.runelite.http.service.util.exception.InternalServerErrorException;
import net.runelite.http.service.util.exception.NotFoundException;
import net.runelite.http.api.hiscore.HiscoreResult;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class HiscoreService
{
private final LoadingCache<HiscoreKey, HiscoreResultBuilder> hiscoreCache = CacheBuilder.newBuilder()
private final HiscoreClient hiscoreClient = new HiscoreClient();
private final LoadingCache<HiscoreKey, HiscoreResult> hiscoreCache = CacheBuilder.newBuilder()
.maximumSize(128)
.expireAfterWrite(1, TimeUnit.MINUTES)
.build(
new CacheLoader<HiscoreKey, HiscoreResultBuilder>()
new CacheLoader<HiscoreKey, HiscoreResult>()
{
@Override
public HiscoreResultBuilder load(HiscoreKey key) throws IOException
public HiscoreResult load(HiscoreKey key) throws IOException
{
return lookupUsername(key.getUsername(), key.getEndpoint().getHiscoreURL());
return hiscoreClient.lookup(key.getUsername(), key.getEndpoint());
}
});
public HiscoreResultBuilder lookupUsername(String username, HiscoreEndpoint endpoint) throws ExecutionException
@VisibleForTesting
HiscoreResult lookupUsername(String username, HttpUrl httpUrl) throws IOException
{
return hiscoreClient.lookup(username, httpUrl);
}
public HiscoreResult lookupUsername(String username, HiscoreEndpoint endpoint) throws ExecutionException
{
return hiscoreCache.get(new HiscoreKey(username, endpoint));
}
HiscoreResultBuilder lookupUsername(String username, HttpUrl hiscoreUrl) throws IOException
{
HttpUrl url = hiscoreUrl.newBuilder()
.addQueryParameter("player", username)
.build();
log.debug("Built URL {}", url);
Request okrequest = new Request.Builder()
.url(url)
.build();
String responseStr;
try (Response okresponse = RuneLiteAPI.CLIENT.newCall(okrequest).execute())
{
if (!okresponse.isSuccessful())
{
switch (HttpStatus.valueOf(okresponse.code()))
{
case NOT_FOUND:
throw new NotFoundException();
default:
throw new InternalServerErrorException("Error retrieving data from Jagex Hiscores: " + okresponse.message());
}
}
responseStr = okresponse.body().string();
}
CSVParser parser = CSVParser.parse(responseStr, CSVFormat.DEFAULT);
HiscoreResultBuilder hiscoreBuilder = new HiscoreResultBuilder();
hiscoreBuilder.setPlayer(username);
int count = 0;
for (CSVRecord record : parser.getRecords())
{
if (count++ >= HiscoreSkill.values().length)
{
log.warn("Jagex Hiscore API returned unexpected data");
break; // rest is other things?
}
// rank, level, experience
int rank = Integer.parseInt(record.get(0));
int level = Integer.parseInt(record.get(1));
// items that are not skills do not have an experience parameter
long experience = -1;
if (record.size() == 3)
{
experience = Long.parseLong(record.get(2));
}
Skill skill = new Skill(rank, level, experience);
hiscoreBuilder.setNextSkill(skill);
}
return hiscoreBuilder;
}
}

View File

@@ -30,7 +30,6 @@ import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.hiscore.HiscoreEndpoint;
import net.runelite.http.api.hiscore.HiscoreResult;
import net.runelite.http.api.xp.XpData;
import net.runelite.http.service.hiscore.HiscoreResultBuilder;
import net.runelite.http.service.hiscore.HiscoreService;
import net.runelite.http.service.xp.beans.PlayerEntity;
import net.runelite.http.service.xp.beans.XpEntity;
@@ -53,8 +52,7 @@ public class XpTrackerService
public void update(String username) throws ExecutionException
{
HiscoreResultBuilder hiscoreResultBuilder = hiscoreService.lookupUsername(username, HiscoreEndpoint.NORMAL);
HiscoreResult hiscoreResult = hiscoreResultBuilder.build();
HiscoreResult hiscoreResult = hiscoreService.lookupUsername(username, HiscoreEndpoint.NORMAL);
update(username, hiscoreResult);
}