Skip to content

Commit

Permalink
Add reload games cache to admin dash
Browse files Browse the repository at this point in the history
  • Loading branch information
ALEEF02 committed Nov 11, 2022
1 parent 39b9ee0 commit 04ad967
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion MavenBack/src/main/java/ppp/db/controllers/CGames.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class CGames {
// Since our client runs on a single machine at once, it is viable to use a local cache to speed up API times.
private static Map<Integer, OGame> gamesCache = new ConcurrentHashMap<>();

public static void init() {
public static void init() { // Dump (if there's games already) and fill the cache
gamesCache = new ConcurrentHashMap<>();
List<OGame> games = getALLGames();
for (OGame game : games) {
gamesCache.put(game.id, game);
Expand Down
5 changes: 5 additions & 0 deletions MavenBack/src/main/java/ppp/staticServe/DashServe.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jakarta.servlet.http.HttpServletResponse;
import ppp.api.GetGames;
import ppp.auth.Authenticator;
import ppp.db.controllers.CGames;
import ppp.db.controllers.CUser;
import ppp.db.model.OUser;
import ppp.meta.GlickoTwo;
Expand Down Expand Up @@ -71,6 +72,10 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
GlickoTwo.run();
response.setStatus(200);
return;
} else if (parameters.containsKey("reloadGames")) {
CGames.init();
response.setStatus(200);
return;
}

response.setStatus(400);
Expand Down
22 changes: 22 additions & 0 deletions MavenBack/src/main/webapp/dash.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
}
}

function games() {
var postData = 'reloadGames=1';
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.open('POST', '/dash');
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.send(postData);
ajaxRequest.onreadystatechange = function(){
if (ajaxRequest.readyState == 4) {
if (ajaxRequest.status == 200) {
// Successfully accepted the game
console.log(JSON.parse(ajaxRequest.responseText));
} else {
console.warn(JSON.parse(ajaxRequest.responseText).error);
}
}
}
}

</script>
</head>
<body>
Expand All @@ -29,5 +47,9 @@ <h2>Run Glicko2</h2>
<p>
<button type="button" id="glickoButton" onclick="glicko()">Run Glicko2</button>
</p>
<h2>Reload cache</h2>
<p>
<button type="button" id="gamesButton" onclick="games()">Reload cache</button>
</p>
</body>
</html>

0 comments on commit 04ad967

Please sign in to comment.