Skip to content

Commit 77b9eb9

Browse files
committed
Correct WinRate calculation across DB implementations
1 parent d5e1792 commit 77b9eb9

3 files changed

Lines changed: 14 additions & 23 deletions

File tree

miner/src/main/java/fr/rakambda/channelpointsminer/miner/database/MariaDBDatabase.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected void resolveUserPredictions(double returnRatioForWin, @NonNull String
5555
SET
5656
`PredictionCnt`=`PredictionCnt`+1,
5757
`WinCnt`=`WinCnt`+?,
58-
`WinRate`=`WinCnt`/`PredictionCnt`,
58+
`WinRate`=(`WinCnt` + ?)/`PredictionCnt`,
5959
`ReturnOnInvestment`=`ReturnOnInvestment`+?
6060
WHERE `ID`=? AND `ChannelID`=?""")
6161
){
@@ -65,20 +65,17 @@ protected void resolveUserPredictions(double returnRatioForWin, @NonNull String
6565
try(var result = getOpenPredictionStmt.executeQuery()){
6666
while(result.next()){
6767
var userPrediction = Converters.convertUserPrediction(result);
68-
if(badge.equals(userPrediction.getBadge())){
69-
updatePredictionUserStmt.setInt(1, 1);
70-
updatePredictionUserStmt.setDouble(2, returnOnInvestment);
71-
}
72-
else{
73-
updatePredictionUserStmt.setInt(1, 0);
74-
updatePredictionUserStmt.setDouble(2, -1);
75-
}
76-
updatePredictionUserStmt.setInt(3, userPrediction.getUserId());
77-
updatePredictionUserStmt.setString(4, userPrediction.getChannelId());
68+
boolean isWinner = badge.equals(userPrediction.getBadge());
69+
70+
updatePredictionUserStmt.setInt(1, isWinner ? 1 : 0);
71+
updatePredictionUserStmt.setInt(2, isWinner ? 1 : 0);
72+
updatePredictionUserStmt.setDouble(3, isWinner ? returnOnInvestment : -1);
73+
updatePredictionUserStmt.setInt(4, userPrediction.getUserId());
74+
updatePredictionUserStmt.setString(5, userPrediction.getChannelId());
7875
updatePredictionUserStmt.addBatch();
7976
}
8077
updatePredictionUserStmt.executeBatch();
8178
}
8279
}
8380
}
84-
}
81+
}

miner/src/main/java/fr/rakambda/channelpointsminer/miner/database/PostgreSqlDatabase.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ protected void resolveUserPredictions(double returnRatioForWin, @NonNull String
5454
SET
5555
PredictionCnt = PredictionCnt + 1,
5656
WinCnt = WinCnt + ?,
57-
WinRate = CASE WHEN PredictionCnt + 1 > 0
58-
THEN (WinCnt + ?)::FLOAT / (PredictionCnt + 1)
59-
ELSE 0 END,
57+
WinRate = (WinCnt + ?)::FLOAT / (PredictionCnt + 1),
6058
ReturnOnInvestment = ReturnOnInvestment + ?
6159
WHERE ID = ? AND ChannelID = ?;
6260
""")) {

miner/src/main/java/fr/rakambda/channelpointsminer/miner/database/SQLiteDatabase.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,10 @@ WITH wi AS (SELECT ? AS n)
7171
try(var result = getOpenPredictionStmt.executeQuery()){
7272
while(result.next()){
7373
var userPrediction = Converters.convertUserPrediction(result);
74-
if(badge.equals(userPrediction.getBadge())){
75-
updatePredictionUserStmt.setInt(1, 1);
76-
updatePredictionUserStmt.setDouble(2, returnOnInvestment);
77-
}
78-
else{
79-
updatePredictionUserStmt.setInt(1, 0);
80-
updatePredictionUserStmt.setDouble(2, -1);
81-
}
74+
boolean isWinner = badge.equals(userPrediction.getBadge());
75+
76+
updatePredictionUserStmt.setInt(1, isWinner ? 1 : 0);
77+
updatePredictionUserStmt.setDouble(2, isWinner ? returnOnInvestment : -1);
8278
updatePredictionUserStmt.setInt(3, userPrediction.getUserId());
8379
updatePredictionUserStmt.setString(4, userPrediction.getChannelId());
8480
updatePredictionUserStmt.addBatch();

0 commit comments

Comments
 (0)