http-service: add examine service

This commit is contained in:
Adam
2017-07-08 12:37:39 -04:00
parent c690d11840
commit 24a6291145
5 changed files with 286 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ import net.runelite.http.api.RuneliteAPI;
import net.runelite.http.service.account.AccountService;
import net.runelite.http.service.account.AuthFilter;
import net.runelite.http.service.config.ConfigService;
import net.runelite.http.service.examine.ExamineService;
import net.runelite.http.service.hiscore.HiscoreService;
import net.runelite.http.service.item.ItemService;
import net.runelite.http.service.updatecheck.UpdateCheckService;
@@ -37,6 +38,8 @@ import net.runelite.http.service.worlds.WorldsService;
import net.runelite.http.service.xtea.XteaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.Request;
import spark.Response;
import spark.servlet.SparkApplication;
import static spark.Spark.*;
@@ -55,6 +58,9 @@ public class Service implements SparkApplication
@Inject
private ConfigService config;
@Inject
private ExamineService examine;
@Inject
private HiscoreService hiscores;
@@ -83,6 +89,7 @@ public class Service implements SparkApplication
accounts.init();
config.init();
item.init();
examine.init();
get("/version", (request, response) -> RuneliteAPI.getVersion());
get("/update-check", updateCheck::check, transformer);
@@ -119,6 +126,16 @@ public class Service implements SparkApplication
get("/:id/price", item::getPrice, transformer);
get("/:id/price/:time", item::getPrice, transformer);
});
path("/examine", () ->
{
get("/npc/:id", examine::getNpc);
get("/object/:id", examine::getObject);
get("/item/:id", examine::getItem);
post("/npc/:id", examine::submitNpc);
post("/object/:id", examine::submitObject);
post("/item/:id", examine::submitItem);
});
exception(Exception.class, this::handleException);
}

View File

@@ -37,6 +37,7 @@ import javax.sql.DataSource;
import net.runelite.http.service.account.AccountService;
import net.runelite.http.service.account.AuthFilter;
import net.runelite.http.service.config.ConfigService;
import net.runelite.http.service.examine.ExamineService;
import net.runelite.http.service.hiscore.HiscoreService;
import net.runelite.http.service.item.ItemService;
import net.runelite.http.service.updatecheck.UpdateCheckService;
@@ -121,6 +122,7 @@ public class ServiceModule extends AbstractModule
bind(AccountService.class);
bind(ConfigService.class);
bind(ExamineService.class);
bind(HiscoreService.class);
bind(ItemService.class);
bind(UpdateCheckService.class);

View File

@@ -0,0 +1,86 @@
/*
* 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.examine;
import java.time.Instant;
public class ExamineEntry
{
private ExamineType type;
private int id;
private Instant time;
private int count;
private String text;
public ExamineType getType()
{
return type;
}
public void setType(ExamineType type)
{
this.type = type;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public Instant getTime()
{
return time;
}
public void setTime(Instant time)
{
this.time = time;
}
public int getCount()
{
return count;
}
public void setCount(int count)
{
this.count = count;
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}
}

View File

@@ -0,0 +1,149 @@
/*
* 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.examine;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.sql.Timestamp;
import java.time.Instant;
import static net.runelite.http.service.examine.ExamineType.ITEM;
import static net.runelite.http.service.examine.ExamineType.NPC;
import static net.runelite.http.service.examine.ExamineType.OBJECT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sql2o.Connection;
import org.sql2o.Sql2o;
import spark.Request;
import spark.Response;
public class ExamineService
{
private static final Logger logger = LoggerFactory.getLogger(ExamineService.class);
private static final String CREATE_EXAMINE = "CREATE TABLE IF NOT EXISTS `examine` (\n"
+ " `type` enum('OBJECT','NPC','ID','') NOT NULL,\n"
+ " `id` int(11) NOT NULL,\n"
+ " `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n"
+ " `count` int(11) NOT NULL,\n"
+ " `text` tinytext NOT NULL,\n"
+ " UNIQUE KEY `type` (`type`,`id`,`text`(64))\n"
+ ") ENGINE=InnoDB";
private final Sql2o sql2o;
@Inject
public ExamineService(@Named("Runelite SQL2O") Sql2o sql2o)
{
this.sql2o = sql2o;
}
public void init()
{
try (Connection con = sql2o.open())
{
con.createQuery(CREATE_EXAMINE)
.executeUpdate();
}
}
public String getNpc(Request request, Response response)
{
int id = Integer.parseInt(request.params("id"));
return get(NPC, id);
}
public String getObject(Request request, Response response)
{
int id = Integer.parseInt(request.params("id"));
return get(OBJECT, id);
}
public String getItem(Request request, Response response)
{
int id = Integer.parseInt(request.params("id"));
return get(ITEM, id);
}
public Object submitNpc(Request request, Response response)
{
int id = Integer.parseInt(request.params("id"));
String examine = request.body();
insert(NPC, id, examine);
return "";
}
public Object submitObject(Request request, Response response)
{
int id = Integer.parseInt(request.params("id"));
String examine = request.body();
insert(OBJECT, id, examine);
return "";
}
public Object submitItem(Request request, Response response)
{
int id = Integer.parseInt(request.params("id"));
String examine = request.body();
insert(ITEM, id, examine);
return "";
}
private String get(ExamineType type, int id)
{
try (Connection con = sql2o.open())
{
ExamineEntry entry = con.createQuery("select text from examine where type = :type and id = :id "
+ "order by count desc limit 1")
.addParameter("type", type.toString())
.addParameter("id", id)
.executeAndFetchFirst(ExamineEntry.class);
if (entry != null)
{
return entry.getText();
}
}
return null;
}
private void insert(ExamineType type, int id, String examine)
{
try (Connection con = sql2o.open())
{
con.createQuery("insert into examine (type, id, time, count, text) values "
+ "(:type, :id, :time, :count, :text) on duplicate key update count = count + 1")
.addParameter("type", type.toString())
.addParameter("id", id)
.addParameter("time", Timestamp.from(Instant.now()))
.addParameter("count", 1)
.addParameter("text", examine)
.executeUpdate();
}
}
}

View File

@@ -0,0 +1,32 @@
/*
* 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.examine;
public enum ExamineType
{
OBJECT,
NPC,
ITEM;
}