Skip to content

Commit

Permalink
Add changes to GlickoHist, Add more stats
Browse files Browse the repository at this point in the history
Sleep service thread for 10 minutes at a time
  • Loading branch information
ALEEF02 committed Nov 30, 2022
1 parent f3a0f19 commit fd5be8f
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 13 deletions.
17 changes: 16 additions & 1 deletion MavenBack/src/main/java/ppp/db/controllers/CGames.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,16 @@ public static int getNumOfGamesUntilRating() {
public static int getNumOfGamesForUser(int userId, StatusEnum.Status status) {
return getNumOfGamesForUser(userId, status, false);
}


/**
* Get the number of games that a user has played
*
* @param userId the id of the user
* @param status the status of the games
* @param userCache whether to user the cache or not
*
* @returns int - the # of games
*/
public static int getNumOfGamesForUser(int userId, StatusEnum.Status status, boolean useCache) {
int games = 0;
if (!useCache) {
Expand Down Expand Up @@ -223,6 +232,12 @@ public static List<OGame> getLatestGamesByStatus(StatusEnum.Status status) {
return getLatestGamesByStatus(status, 20);
}

/**
* Get the latest games that fit to a specified status
* @param status
* @param limit
* @return The list of Game Objects
*/
public static List<OGame> getLatestGamesByStatus(StatusEnum.Status status, int limit) {
if (limit < 1 || limit > 200) limit = 20;
if (status == StatusEnum.Status.ANY) return getLatestGames(limit);
Expand Down
7 changes: 4 additions & 3 deletions MavenBack/src/main/java/ppp/db/controllers/CGlicko.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,16 @@ private static OGlicko fillRecord(ResultSet rs) throws SQLException {
s.rating = rs.getDouble("rating");
s.rd = rs.getDouble("rd");
s.volatility = rs.getDouble("volatility");
s.ratingCycle = rs.getInt("rating_cycle");
return s;
}

public static void insert(OGlicko record) {
try {
record.id = WebDb.get().insert(
"INSERT INTO glicko(userId, date, rating, rd, volatility) " +
"VALUES (?,?,?,?,?)",
record.userId, record.date, record.rating, record.rd, record.volatility);
"INSERT INTO glicko(userId, date, rating, rd, volatility, rating_cycle) " +
"VALUES (?,?,?,?,?,?)",
record.userId, record.date, record.rating, record.rd, record.volatility, record.ratingCycle);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
11 changes: 10 additions & 1 deletion MavenBack/src/main/java/ppp/db/model/OGlicko.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

public class OGlicko extends AbstractModel {
public int id = 0;
Expand All @@ -15,6 +16,13 @@ public class OGlicko extends AbstractModel {
public double rating = GlickoTwo.BASE_RATING; // TODO: Update this to double
public double rd = GlickoTwo.BASE_RD; // TODO: Update this to double
public double volatility = GlickoTwo.BASE_VOLATILITY; // TODO: Update this to double
public int ratingCycle = 0;

public void checkRatingCycle() {
if (ratingCycle != 0) return;
List<OGame> lastCalculatedGame = CGames.getLatestGamesByStatus(StatusEnum.Status.CALCULATED, 1);
ratingCycle = lastCalculatedGame.isEmpty() ? 1 : lastCalculatedGame.get(0).ratingCycle;
}

public double getMu() {
return (rating - GlickoTwo.BASE_RATING) / GlickoTwo.GLICKO2_CONV;
Expand All @@ -26,7 +34,8 @@ public double getPhi() {

public String toPublicJSON() {
return "{\"id\":\"" + id +
"\",\"elo\":" + rating +
"\",\"ratingCycle\":" + ratingCycle +
",\"elo\":" + rating +
",\"rd\":" + rd +
",\"vol\":" + volatility +
",\"date\":\"" + date +
Expand Down
6 changes: 3 additions & 3 deletions MavenBack/src/main/java/ppp/db/model/OUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public String toPublicJSON(boolean numGamesPlayedInCycle, boolean withHistories)
"\",\"signUpDate\":\"" + signUpDate +
"\",\"lastSignIn\":\"" + lastSignIn +
"\",\"banned\":\"" + banned +
"\",\"rank\":\"" + rank;
"\",\"rank\":\"" + rank + "\"";

if (withHistories) {
List<OGlicko> glickos = CGlicko.findByUserId(id);
Expand All @@ -85,11 +85,11 @@ public String toPublicJSON(boolean numGamesPlayedInCycle, boolean withHistories)
}
}
glickoHist += "]";
rt += "\",\"glickoHist\":" + glickoHist;
rt += ",\"glickoHist\":" + glickoHist;
}

if (numGamesPlayedInCycle) {
rt += "\",\"gamesPlayedInCycle\":\"" + CGames.getNumOfGamesForUser(id, StatusEnum.Status.ACCEPTED, true) + "\"";
rt += ",\"gamesPlayedInCycle\":\"" + CGames.getNumOfGamesForUser(id, StatusEnum.Status.ACCEPTED, true) + "\"";
}

rt += "}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void run() {
for (AbstractService instance : instances) {
instance.start();
}
sleep(30_000L); // Run every 30 seconds
sleep(600_000L); // Run every 10 minutes
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Loading

0 comments on commit fd5be8f

Please sign in to comment.