Skip to content

Commit

Permalink
Add graph to Users page. Add colors to volitility
Browse files Browse the repository at this point in the history
Add GickoHistory, publicly avalible
  • Loading branch information
ALEEF02 committed Nov 17, 2022
1 parent 0d1a514 commit 7d2941b
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 22 deletions.
10 changes: 6 additions & 4 deletions MavenBack/src/main/java/ppp/api/GetUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* user [exc]:
* name: Return the user by the name
* cached: Specify whether to use the cached users
* withHistory: Specify to also get the user's Glicko histories.
* POST:
*/

Expand All @@ -43,7 +44,9 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
Authenticator auth = new Authenticator();
boolean loggedIn = auth.login(request) == LoginEnum.Status.SUCCESS;
int limit = 10;
try {
boolean withHistory = parameters.containsKey("withHistory");

try {

if (parameters.containsKey("ranks")) {

Expand Down Expand Up @@ -100,7 +103,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
if (parameters.containsKey("withRank")) {
user.getRank();
}
response.getWriter().print(user.toPublicJSON());
response.getWriter().print(user.toPublicJSON(false, withHistory));
return;

} else {
Expand All @@ -116,11 +119,10 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
response.getWriter().print(user.toPublicJSON());
return;
}

response.setContentType("application/json;");
response.getWriter().print("[");
for (OUser user:users) {
response.getWriter().print(user.toPublicJSON(true));
response.getWriter().print(user.toPublicJSON(true, withHistory));
if (users.indexOf(user) != users.size() - 1) {
response.getWriter().print(",");
}
Expand Down
11 changes: 11 additions & 0 deletions MavenBack/src/main/java/ppp/db/model/OGlicko.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ppp.db.model;

import ppp.db.AbstractModel;
import ppp.db.controllers.CGames;
import ppp.meta.GlickoTwo;
import ppp.meta.StatusEnum;

import java.sql.Timestamp;
import java.util.Date;
Expand All @@ -21,4 +23,13 @@ public double getMu() {
public double getPhi() {
return rd / GlickoTwo.GLICKO2_CONV;
}

public String toPublicJSON() {
return "{\"id\":\"" + id +
"\",\"elo\":" + rating +
",\"rd\":" + rd +
",\"vol\":" + volatility +
",\"date\":\"" + date +
"\"}";
}
}
55 changes: 38 additions & 17 deletions MavenBack/src/main/java/ppp/db/model/OUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import ppp.db.AbstractModel;
import ppp.db.controllers.CGames;
import ppp.db.controllers.CGlicko;
import ppp.db.controllers.CUser;
import ppp.meta.GlickoTwo;
import ppp.meta.StatusEnum;

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

/**
* A User object
Expand Down Expand Up @@ -39,7 +41,7 @@ public double getPhi() {
* @return {@code String} JSON with insensitive info
*/
public String toPublicJSON() {
return toPublicJSON(false);
return toPublicJSON(false, false);
}

/**
Expand All @@ -48,31 +50,50 @@ public String toPublicJSON() {
* @param numGamesPlayedInCycle if true, get the number of games they've played in the cycle
* @return {@code String} JSON with insensitive info
*/
public String toPublicJSON(boolean numGamesPlayedInCycle) {
if (numGamesPlayedInCycle) {
return "{\"id\":\"" + id +
"\",\"username\":\"" + username +
"\",\"elo\":\"" + rating +
"\",\"rd\":\"" + rd +
"\",\"vol\":\"" + volatility +
"\",\"signUpDate\":\"" + signUpDate +
"\",\"lastSignIn\":\"" + lastSignIn +
"\",\"banned\":\"" + banned +
"\",\"rank\":\"" + rank +
"\",\"gamesPlayedInCycle\":\"" + CGames.getNumOfGamesForUser(id, StatusEnum.Status.ACCEPTED, true) +
"\"}";
public String toPublicJSON(boolean numGamesPlayedInCycle, boolean withHistories) {

if (!numGamesPlayedInCycle && !withHistories) {
return "{\"id\":\"" + id +
"\",\"username\":\"" + username +
"\",\"elo\":\"" + rating +
"\",\"rd\":\"" + rd +
"\",\"vol\":\"" + volatility +
"\",\"signUpDate\":\"" + signUpDate +
"\",\"lastSignIn\":\"" + lastSignIn +
"\",\"banned\":\"" + banned +
"\",\"rank\":\"" + rank +
"\"}";
}

return "{\"id\":\"" + id +
String rt = "{\"id\":\"" + id +
"\",\"username\":\"" + username +
"\",\"elo\":\"" + rating +
"\",\"rd\":\"" + rd +
"\",\"vol\":\"" + volatility +
"\",\"signUpDate\":\"" + signUpDate +
"\",\"lastSignIn\":\"" + lastSignIn +
"\",\"banned\":\"" + banned +
"\",\"rank\":\"" + rank +
"\"}";
"\",\"rank\":\"" + rank;

if (withHistories) {
List<OGlicko> glickos = CGlicko.findByUserId(id);
String glickoHist = "[";
for (OGlicko glicko:glickos) {
glickoHist += glicko.toPublicJSON();
if (glickos.indexOf(glicko) != glickos.size() - 1) {
glickoHist += ",";
}
}
glickoHist += "]";
rt += "\",\"glickoHist\":" + glickoHist;
}

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

rt += "}";
return rt;

}

Expand Down
130 changes: 129 additions & 1 deletion MavenBack/src/main/webapp/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<link rel="preconnect" href="https://www.google-analytics.com">
-->
<link rel="preconnect" href="https://fonts.gstatic.com">
<link rel="preconnect" href="https://www.gstatic.com">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<link rel="apple-touch-startup-image" href="apple-touch-icon.png">

Expand All @@ -32,6 +33,8 @@

<title>User</title>

<script src="https://www.gstatic.com/charts/loader.js"></script>

<script>
var id = 0;
var hasRunGames = false;
Expand All @@ -55,6 +58,10 @@
document.getElementById("uname").innerHTML = userInfo.username;
document.getElementById("rank").innerHTML = userInfo.rank;
document.title = userInfo.username;
google.charts.load("current", {
packages: ["corechart"]
});
google.charts.setOnLoadCallback(drawElo);
getGames();

}
Expand All @@ -63,7 +70,7 @@
}
}
}
userReq.open('GET', '/api/users?withRank=1&user=' + window.location.href.substring(window.location.href.indexOf('/user/') + 6));
userReq.open('GET', '/api/users?withRank=1&withHistory=1&user=' + window.location.href.substring(window.location.href.indexOf('/user/') + 6));
userReq.send();
});

Expand Down Expand Up @@ -165,6 +172,126 @@
gamesReq.open('GET', '/api/games?status=FILED&user=' + id);
gamesReq.send();
}

function drawElo() {
var container = document.getElementById('eloDiv');
var chart = new google.visualization.LineChart(container);
var dataTable = new google.visualization.DataTable();
dataTable.addColumn('date', 'Date');
dataTable.addColumn('number', "Rating");
dataTable.addColumn({type:'number', id:'Uppper Error', role:'interval'});
dataTable.addColumn({type:'number', id:'Upp Error', role:'interval'});
dataTable.addColumn({type:'number', id:'Low Error', role:'interval'});
dataTable.addColumn({type:'number', id:'Lower Error', role:'interval'});
dataTable.addColumn('number', "Volatility");
var glickos = userInfo.glickoHist;
for (var i=0; i < glickos.length; i++) {
var glicko = glickos[i];
var toAdd = [
[
new Date(glicko.date),
parseFloat(glicko.elo.toFixed(2)),
parseFloat((glicko.elo + (glicko.rd * 2)).toFixed(1)),
parseFloat((glicko.elo + glicko.rd).toFixed(1)),
parseFloat((glicko.elo - glicko.rd).toFixed(1)),
parseFloat((glicko.elo - (glicko.rd * 2)).toFixed(1)),
parseFloat(glicko.vol.toFixed(6))
]
];

dataTable.addRows(toAdd);
}

var options = {
title: 'Rating over Time',
seriesType: 'line',
titleTextStyle: {
color: '#FFFFEA',
fontName: 'Source Sans Pro'
},
hAxis: {
title: 'Date',
baselineColor: '#FFFFEA',
textStyle: {
color: '#FFFFEA',
fontName: 'Source Sans Pro',
italic: true
},
titleTextStyle: {
color: '#FFFFEA',
fontName: 'Source Sans Pro',
italic: true
},
gridlines: {
color: '#FFFFEA'
}
},

vAxes: [
{
title: 'Rating',
baselineColor: '#FFFFEA',
textStyle: {
color: '#FFFFEA',
fontName: 'Source Sans Pro',
italic: true
},
titleTextStyle: {
color: '#FFFFEA',
fontName: 'Source Sans Pro',
italic: true
},
gridlines: {
color: '#7799AA'
}
},
{
title: 'Volatility',
baselineColor: '#FFFFEA',
textStyle: {
color: '#FFFFEA',
fontName: 'Source Sans Pro',
italic: true
},
titleTextStyle: {
color: '#FFFFEA',
fontName: 'Source Sans Pro',
italic: true
},
gridlines: {
color: '#77AA77'
}
}
],
series: {
0: {targetAxisIndex:0},
1: {targetAxisIndex:1}
},
legend: {
textStyle: {
color: '#FFFFEA',
fontName: 'Source Sans Pro'
},
position: 'top',
alignment: 'end'
},
width: '100%',
height: '100%',
backgroundColor: '#121113',
intervals: {
'style':'area',
'fillOpacity': 0.4
},
colors: ['#99F5FF', '#80FF80']
};

var formatterVol = new google.visualization.NumberFormat({pattern: '.######'});
formatterVol.format(dataTable, 6);

// use a DataView to hide the category column from the Timeline
var view = new google.visualization.DataView(dataTable);
chart.draw(view, options);
}
</script>
</head>
<body>
Expand All @@ -188,6 +315,7 @@ <h2>Join Date:</h2>
<p id="jd"></p>
<h2>Last Login:</h2>
<p id="ld"></p>
<div id="eloDiv" style="height: 600px; width: 100%; min-width: 350px;"></div>
</div>

<div class = "tableContainer">
Expand Down

0 comments on commit 7d2941b

Please sign in to comment.