Global scores response is completed, but it's *very* slow. Gonna construct it in memory on startup and update it as scores are saved instead.

This commit is contained in:
skogaby 2019-01-21 02:59:09 -06:00
parent 18194e273d
commit b60269f6e0
5 changed files with 9611 additions and 16 deletions

View File

@ -28,9 +28,9 @@ public class UserSongRecordDao extends AbstractHibernateDao<UserSongRecord> {
/**
* Finds a song record by its endtime and the user.
* @param endtime
* @param user
* @return
* @param endtime The endtime of the record
* @param user The user of the record
* @return A matching record, or null
*/
public UserSongRecord findByEndtimeAndUser(final LocalDateTime endtime, final UserProfile user) {
this.openCurrentSession();
@ -43,4 +43,24 @@ public class UserSongRecordDao extends AbstractHibernateDao<UserSongRecord> {
this.closeCurrentSession();
return result;
}
/**
* Finds the top score for a given song and difficulty
* @param mcode The mcode of the song to search for
* @param difficulty The difficulty to search for
* @return A matching record, or null
*/
public UserSongRecord findTopScoreForSongDifficulty(int mcode, int difficulty) {
this.openCurrentSession();
final Query<UserSongRecord> query = this.currentSession.createQuery(
"from UserSongRecord r where r.songId = :songId and r.noteType = :noteType order by r.score desc")
.setMaxResults(1);
query.setParameter("songId", mcode);
query.setParameter("noteType", difficulty);
final UserSongRecord result = query.uniqueResult();
this.closeCurrentSession();
return result;
}
}

View File

@ -10,6 +10,7 @@ import com.buttongames.butterfly.http.exception.UnsupportedRequestException;
import com.buttongames.butterfly.http.handlers.BaseRequestHandler;
import com.buttongames.butterfly.model.Card;
import com.buttongames.butterfly.model.ddr16.GhostData;
import com.buttongames.butterfly.model.ddr16.Song;
import com.buttongames.butterfly.model.ddr16.UserProfile;
import com.buttongames.butterfly.model.ddr16.UserSongRecord;
import com.buttongames.butterfly.model.ddr16.options.AppearanceOption;
@ -46,8 +47,10 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Random;
/**
@ -128,16 +131,14 @@ public class PlayerDataRequestHandler extends BaseRequestHandler {
*/
private static NodeList EVENTS_2018042300;
/**
* Static list of songs for the server.
*/
private static List<Song> SONGS_2018042300;
static {
try {
final Path path = Paths.get(ClassLoader.getSystemResource("static_responses/mdx_2018042300/events.xml").toURI());
byte[] respBody = Files.readAllBytes(path);
final Element doc = XmlUtils.byteArrayToXmlFile(respBody);
EVENTS_2018042300 = XmlUtils.nodesAtPath(doc, "/events/eventdata");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
loadEvents();
loadMusicDb();
}
public PlayerDataRequestHandler(final ButterflyUserDao userDao, final CardDao cardDao, final ProfileDao profileDao,
@ -149,6 +150,47 @@ public class PlayerDataRequestHandler extends BaseRequestHandler {
this.songRecordDao = songRecordDao;
}
/**
* Load the events data into memory.
*/
private static void loadEvents() {
try {
final Path path = Paths.get(ClassLoader.getSystemResource("static_responses/mdx_2018042300/events.xml").toURI());
final byte[] respBody = Files.readAllBytes(path);
final Element doc = XmlUtils.byteArrayToXmlFile(respBody);
EVENTS_2018042300 = XmlUtils.nodesAtPath(doc, "/events/eventdata");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
/**
* Load the music database into memory.
*/
private static void loadMusicDb() {
try {
final Path path = Paths.get(ClassLoader.getSystemResource("static_responses/mdx_2018042300/musicdb.xml").toURI());
final byte[] body = Files.readAllBytes(path);
final Element doc = XmlUtils.byteArrayToXmlFile(body);
final NodeList nodes = XmlUtils.nodesAtPath(doc, "/mdb/music");
SONGS_2018042300 = new ArrayList<>();
for (int i = 0; i < nodes.getLength(); i++) {
Element node = (Element) nodes.item(i);
SONGS_2018042300.add(new Song(XmlUtils.intAtChild(node, "mcode"),
XmlUtils.strAtChild(node, "basename"),
XmlUtils.strAtChild(node, "title"),
XmlUtils.strAtChild(node, "artist")));
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
/**
* Handles an incoming request for the <code>playerdata</code> module.
* @param requestBody The XML document of the incoming request.
@ -244,12 +286,33 @@ public class PlayerDataRequestHandler extends BaseRequestHandler {
* @return A response object for Spark
*/
private Object handleGlobalScoresRequest(final Request request, final Response response) {
// TODO: Implement this properly and load/save scores...
final KXmlBuilder respBuilder = KXmlBuilder.create("response")
KXmlBuilder respBuilder = KXmlBuilder.create("response")
.e("playerdata")
.s32("result", 0).up()
.e("data")
.s32("recordtype", 0);
.s32("recordtype", 0).up();
UserSongRecord record;
// we need to return the top score for every song/difficulty
for (Song song : SONGS_2018042300) {
for (int i = 0; i < 8; i++) {
record = this.songRecordDao.findTopScoreForSongDifficulty(song.getMcode(), i);
if (record != null) {
respBuilder = respBuilder.e("record")
.u32("mcode", song.getMcode()).up()
.u8("notetype", i).up()
.u8("rank", record.getRank()).up()
.u8("clearkind", record.getClearKind()).up()
.u8("flagdata", 0).up()
.str("name", record.getUser().getName()).up()
.s32("code", record.getUser().getDancerCode()).up()
.s32("score", record.getScore()).up()
.s32("ghostid", ((Long) record.getGhostData().getId()).intValue()).up(2);
}
}
}
return this.sendResponse(request, response, respBuilder);
}

View File

@ -0,0 +1,52 @@
package com.buttongames.butterfly.model.ddr16;
/**
* This doesn't get persisted to the database, but it represents a song in the musicdb.
* @author skogaby (skogabyskogaby@gmail.com)
*/
public class Song {
private int mcode;
private String baseName;
private String title;
private String artist;
public Song(final int mcode, final String baseName, final String title, final String artist) {
this.mcode = mcode;
this.baseName = baseName;
this.title = title;
this.artist = artist;
}
public int getMcode() {
return mcode;
}
public void setMcode(int mcode) {
this.mcode = mcode;
}
public String getBaseName() {
return baseName;
}
public void setBaseName(String baseName) {
this.baseName = baseName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
}

View File

@ -114,7 +114,7 @@ public class UserSongRecord implements Externalizable {
@Column(name = "note_type")
private int noteType;
/** The rank for the record (?) */
/** The grade for the record */
@Column(name = "rank")
private int rank;

File diff suppressed because it is too large Load Diff