loot tracker: batch loot submissions

This commit is contained in:
Adam
2019-08-18 12:26:33 -04:00
parent bd0badf7fc
commit c8ffa3994d
5 changed files with 98 additions and 27 deletions

View File

@@ -53,7 +53,7 @@ public class LootTrackerController
private AuthFilter auth;
@RequestMapping(method = RequestMethod.POST)
public void storeLootRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody LootRecord record) throws IOException
public void storeLootRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody Collection<LootRecord> records) throws IOException
{
SessionEntry e = auth.handle(request, response);
if (e == null)
@@ -62,7 +62,7 @@ public class LootTrackerController
return;
}
service.store(record, e.getUser());
service.store(records, e.getUser());
response.setStatus(HttpStatusCodes.STATUS_CODE_OK);
}

View File

@@ -64,7 +64,7 @@ public class LootTrackerService
// Queries for inserting kills
private static final String INSERT_KILL_QUERY = "INSERT INTO kills (accountId, type, eventId) VALUES (:accountId, :type, :eventId)";
private static final String INSERT_DROP_QUERY = "INSERT INTO drops (killId, itemId, itemQuantity) VALUES (LAST_INSERT_ID(), :itemId, :itemQuantity)";
private static final String INSERT_DROP_QUERY = "INSERT INTO drops (killId, itemId, itemQuantity) VALUES (:killId, :itemId, :itemQuantity)";
private static final String SELECT_LOOT_QUERY = "SELECT killId,time,type,eventId,itemId,itemQuantity FROM kills JOIN drops ON drops.killId = kills.id WHERE accountId = :accountId ORDER BY TIME DESC LIMIT :limit OFFSET :offset";
@@ -89,29 +89,49 @@ public class LootTrackerService
/**
* Store LootRecord
*
* @param record LootRecord to store
* @param records LootRecords to store
* @param accountId runelite account id to tie data too
*/
public void store(LootRecord record, int accountId)
public void store(Collection<LootRecord> records, int accountId)
{
try (Connection con = sql2o.beginTransaction())
{
// Kill Entry Query
con.createQuery(INSERT_KILL_QUERY, true)
.addParameter("accountId", accountId)
.addParameter("type", record.getType())
.addParameter("eventId", record.getEventId())
.executeUpdate();
Query killQuery = con.createQuery(INSERT_KILL_QUERY, true);
for (LootRecord record : records)
{
killQuery
.addParameter("accountId", accountId)
.addParameter("type", record.getType())
.addParameter("eventId", record.getEventId())
.addToBatch();
}
killQuery.executeBatch();
Object[] keys = con.getKeys();
if (keys.length != records.size())
{
throw new RuntimeException("Mismatch in keys vs records size");
}
Query insertDrop = con.createQuery(INSERT_DROP_QUERY);
// Append all queries for inserting drops
for (GameItem drop : record.getDrops())
int idx = 0;
for (LootRecord record : records)
{
insertDrop
.addParameter("itemId", drop.getId())
.addParameter("itemQuantity", drop.getQty())
.addToBatch();
for (GameItem drop : record.getDrops())
{
insertDrop
.addParameter("killId", keys[idx])
.addParameter("itemId", drop.getId())
.addParameter("itemQuantity", drop.getQty())
.addToBatch();
}
++idx;
}
insertDrop.executeBatch();

View File

@@ -83,10 +83,10 @@ public class LootTrackerControllerTest
lootRecord.setTime(Instant.now());
lootRecord.setDrops(Collections.singletonList(new GameItem(4151, 1)));
String data = RuneLiteAPI.GSON.toJson(lootRecord);
String data = RuneLiteAPI.GSON.toJson(Collections.singletonList(lootRecord));
mockMvc.perform(post("/loottracker").content(data).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
verify(lootTrackerService).store(eq(lootRecord), anyInt());
verify(lootTrackerService).store(eq(Collections.singletonList(lootRecord)), anyInt());
}
}