Add qp to chat service

This commit is contained in:
Adam
2019-01-05 10:15:36 -05:00
parent 5192a28cec
commit b65018ae84
4 changed files with 88 additions and 6 deletions

View File

@@ -33,7 +33,7 @@ import okhttp3.Response;
public class ChatClient
{
public boolean submit(String username, String boss, int kc) throws IOException
public boolean submitKc(String username, String boss, int kc) throws IOException
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("chat")
@@ -54,7 +54,7 @@ public class ChatClient
}
}
public int get(String username, String boss) throws IOException
public int getKc(String username, String boss) throws IOException
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("chat")
@@ -76,4 +76,46 @@ public class ChatClient
return Integer.parseInt(response.body().string());
}
}
public boolean submitQp(String username, int qp) throws IOException
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("chat")
.addPathSegment("qp")
.addQueryParameter("name", username)
.addQueryParameter("qp", Integer.toString(qp))
.build();
Request request = new Request.Builder()
.post(RequestBody.create(null, new byte[0]))
.url(url)
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
{
return response.isSuccessful();
}
}
public int getQp(String username) throws IOException
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("chat")
.addPathSegment("qp")
.addQueryParameter("name", username)
.build();
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
{
if (!response.isSuccessful())
{
throw new IOException("Unable to look up quest points!");
}
return Integer.parseInt(response.body().string());
}
}
}

View File

@@ -48,7 +48,7 @@ public class ChatController
private ChatService chatService;
@PostMapping("/kc")
public void submit(@RequestParam String name, @RequestParam String boss, @RequestParam int kc)
public void submitKc(@RequestParam String name, @RequestParam String boss, @RequestParam int kc)
{
if (kc <= 0)
{
@@ -60,7 +60,7 @@ public class ChatController
}
@GetMapping("/kc")
public int get(@RequestParam String name, @RequestParam String boss)
public int getKc(@RequestParam String name, @RequestParam String boss)
{
Integer kc = killCountCache.getIfPresent(new KillCountKey(name, boss));
if (kc == null)
@@ -78,4 +78,26 @@ public class ChatController
}
return kc;
}
@PostMapping("/qp")
public void submitQp(@RequestParam String name, @RequestParam int qp)
{
if (qp < 0)
{
return;
}
chatService.setQp(name, qp);
}
@GetMapping("/qp")
public int getKc(@RequestParam String name)
{
Integer kc = chatService.getQp(name);
if (kc == null)
{
throw new NotFoundException();
}
return kc;
}
}

View File

@@ -60,4 +60,22 @@ public class ChatService
jedis.setex("kc." + name + "." + boss, (int) EXPIRE.getSeconds(), Integer.toString(kc));
}
}
public Integer getQp(String name)
{
String value;
try (Jedis jedis = jedisPool.getResource())
{
value = jedis.get("qp." + name);
}
return value == null ? null : Integer.parseInt(value);
}
public void setQp(String name, int qp)
{
try (Jedis jedis = jedisPool.getResource())
{
jedis.setex("qp." + name, (int) EXPIRE.getSeconds(), Integer.toString(qp));
}
}
}

View File

@@ -285,7 +285,7 @@ public class ChatCommandsPlugin extends Plugin
{
try
{
chatClient.submit(playerName, boss, kc);
chatClient.submitKc(playerName, boss, kc);
}
catch (Exception ex)
{
@@ -325,7 +325,7 @@ public class ChatCommandsPlugin extends Plugin
final int kc;
try
{
kc = chatClient.get(player, search);
kc = chatClient.getKc(player, search);
}
catch (IOException ex)
{