Skip to content

Instantly share code, notes, and snippets.

@NotArchon
Created May 10, 2023 19:45
Show Gist options
  • Save NotArchon/1a108a19c173784b3796b0ac8e65b53d to your computer and use it in GitHub Desktop.
Save NotArchon/1a108a19c173784b3796b0ac8e65b53d to your computer and use it in GitHub Desktop.
package com.neox.web.model.hiscores;
import com.google.gson.annotations.Expose;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import com.neox.web.Neox;
import com.neox.web.model.Session;
import com.neox.web.model.WrappedPage;
import com.neox.web.model.user.NeoxUser;
import com.neox.web.pojo.AdvLogDoc;
import com.neox.web.pojo.HiscoresDoc;
import com.neox.web.pojo.UserDoc;
import io.archon.misc.GsonInstance;
import io.archon.webserver.network.HttpAsyncRequest;
import io.archon.webserver.other.PageMapper;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@PageMapper({"/hiscores/*", "/highscores/*", "/scores/*"})
public class HiscoresPersonal extends WrappedPage {
@Expose private String name;
@Expose private String advLogJson;
@Expose private String gameMode, xpMode, accBuild;
@Expose private String hiscoresJson;
public HiscoresPersonal() {
super("ftl/hiscores/hiscores_personal.ftl");
}
@Override
public void load(HttpAsyncRequest req, Session session, NeoxUser sessionUser) {
String[] split = req.getUri().split("scores/");
if(split.length > 1) {
String name = URLDecoder.decode(split[1], StandardCharsets.UTF_8);
if(name != null)
loadUser(req, name);
}
}
private void loadUser(HttpAsyncRequest req, String queryName) {
NeoxUser user = Neox.getNode().getUserManager().findByDisplayName(queryName);
if(user == null)
return;
UserDoc doc = user.getDoc();
if(doc == null)
return;
name = doc.displayName();
AdvLogDoc advLog = Neox.getNode().getMongoDatabase().getCollection("adv_log", AdvLogDoc.class)
.find(Filters.eq("user_id", doc.id())).limit(1).first();
advLogJson = advLog == null ? null : advLog.logJson();
MongoCollection<HiscoresDoc> hiscores = Neox.getNode().getMongoDatabase()
.getCollection("hiscores", HiscoresDoc.class);
HiscoresDoc overallMin = hiscores.find(Filters.and(
Filters.eq("user_id", doc.id()),
Filters.eq("key", "skill-overall")
)).projection(Projections.fields(
Projections.include("game_mode", "xp_mode", "build_key"),
Projections.excludeId()
)).limit(1).first();
if(overallMin != null) {
gameMode = overallMin.gameMode();
xpMode = overallMin.xpMode();
accBuild = overallMin.buildKey();
Map<String, HiscoresDoc> map = new HashMap<>();
hiscores.find(Filters.and(
Filters.eq("user_id", doc.id()),
Filters.eq("game_mode", gameMode),
Filters.eq("xp_mode", xpMode)
)).projection(Projections.fields(
Projections.include("key", "primary", "secondary"),
Projections.excludeId()
)).forEach(d -> map.put(d.key(), d));
hiscoresJson = GsonInstance.normal().toJson(map);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment