-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateElo.go
163 lines (139 loc) · 5.33 KB
/
calculateElo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package rating
import (
"github.com/NTNU-sondrbaa-2019/CLOUD-PROJECT/internal/pkg/database"
"log"
"math"
"strconv"
"time"
)
func calculateElo(games []Game, teamMembers []TeamMember) []TeamMember {
newTeamMembers := teamMembers
var lichessIDS [] string
// Loops through all games and calculates elo
for i := 0; i < len(games); i++ {
tmp := getWhiteAndBlackFromGame(games[i], newTeamMembers)
white := tmp[0]
black := tmp[1]
group1, _ := database.SelectGroupByLeagueIDAndLeagueSeasonNameAndName(white.LeagueID, LICHESS_LEAGUE_SEASON_NAME, white.Username)
group2, _ := database.SelectGroupByLeagueIDAndLeagueSeasonNameAndName(black.LeagueID, LICHESS_LEAGUE_SEASON_NAME, black.Username)
//TODO get elo from own database
log.Println("Group1 id: ", group1.ID)
log.Println("Group2 id: ", group2.ID)
count1, err1 := database.SelectResultCountByGroupID(group1.ID)
count2, err2 := database.SelectResultCountByGroupID(group2.ID)
if err1 != nil {
log.Println("Error SelectResultCountByGroupID: ", err1)
}
if err1 != nil {
log.Println("Error SelectResultCountByGroupID: ", err2)
}
log.Println("count 1 then 2")
log.Println(*count1, *count2)
if err1 != nil {
log.Println(err1)
}
if err2 != nil {
log.Println(err2)
}
if count1 != nil && *count1 > 0 {
lastResultWhite, err := database.SelectResultLastByGroupId(group1.ID)
if err != nil {
log.Println(err)
}
if lastResultWhite != nil && err == nil {
log.Println("Elo of result white: ")
log.Println(lastResultWhite.ELOAfter)
log.Println("Result found, setting elo for this user to " + strconv.Itoa(lastResultWhite.ELOAfter))
white.Elo = float64(lastResultWhite.ELOAfter)
}
} else {
white.Elo = 1500
log.Println("No results for this user, elo set to 1500")
}
if count2 != nil && *count2 > 0{
lastResultBlack, err := database.SelectResultLastByGroupId(group2.ID)
if err != nil {
log.Println(err)
}
if lastResultBlack != nil && err == nil {
log.Println("Elo of resultblack: ")
log.Println(lastResultBlack.ELOAfter)
log.Println("Result found, setting elo for this user to " + strconv.Itoa(lastResultBlack.ELOAfter))
black.Elo = float64(lastResultBlack.ELOAfter)
}
} else {
black.Elo = 1500
log.Println("No results for this user, elo set to 1500")
}
// TODO GET ELO from last results corresponding to the teamMember/GROUP
var w float64
var b float64
// TODO START make this block a single function to calculate elo
K := 64.0
R1 := math.Pow(10, white.Elo/400)
R2 := math.Pow(10, black.Elo/400)
WhiteChance := R1 / (R1 + R2)
BlackChance := R2 / (R1 + R2)
var outcome1 string
var outcome2 string
if games[i].Winner == "white" {
w = white.Elo + (K * (1 - WhiteChance))
b = black.Elo + (K * (0 - BlackChance))
outcome1 = "win"
outcome2 = "loss"
} else if games[i].Winner == "black" {
w = white.Elo + (K * (0 - WhiteChance))
b = black.Elo + (K * (1 - BlackChance))
outcome1 = "loss"
outcome2 = "win"
} else if games[i].Status == "draw"{
w = white.Elo + (K * (0.5 - WhiteChance))
b = black.Elo + (K * (0.5 - BlackChance))
outcome1 = "draw"
outcome2 = "draw"
}
// TODO END
newTeamMembers = insertElo(w, b, white, black, newTeamMembers)
var result1 database.RESULT
var result2 database.RESULT
result1.GroupID = group1.ID // DATABASE GROUP ID WTF DUDE
result2.GroupID = group2.ID // DATABASE GROUP ID WTF DUDE
result1.ELOBefore = int(white.Elo)
result2.ELOBefore = int(black.Elo)
result1.ELOAfter = int(w)
result2.ELOAfter = int(b)
log.Println(result1.ELOBefore, result1.ELOAfter)
log.Println(result2.ELOBefore, result2.ELOAfter)
result1.Outcome = outcome1 // win lose draw
result2.Outcome = outcome2 // win lose draw
seconds := games[i].CreatedAt / 1000 // Seconds
nanoseconds := games[i].CreatedAt % 1000000 // Nanoseconds
result1.Played = time.Unix(int64(seconds), int64(nanoseconds)) // time, unix convert
result2.Played = result1.Played // time, unix convert
lichessIDS = append(lichessIDS, games[i].ID)
lichessIDS = append(lichessIDS, games[i].ID)
// TODO see if we get the result id from this function. Add result to RESULT_PLATFORM_ELO
log.Println("Insert before: " + strconv.Itoa(result1.ELOBefore) + " after: " + strconv.Itoa(result1.ELOAfter) + " user: " + strconv.Itoa(int(result1.GroupID)))
id, _ := database.InsertResult(result1)
var resultPlatformElo database.RESULT_PLATFORM_ELO
resultPlatformElo.PlatformEloID = LICHESS_PLATFORM_ID
if id != nil{
resultPlatformElo.ResultID = *id
}
resultPlatformElo.VerificationKey = games[i].ID
_, _ = database.InsertResultPlatformElo(resultPlatformElo)
// TODO see if we get the result id from this function. Add result to RESULT_PLATFORM_ELO
log.Println("Insert before: " + strconv.Itoa(result2.ELOBefore) + " after: " + strconv.Itoa(result2.ELOAfter) + " user: " + strconv.Itoa(int(result2.GroupID)))
id, _ = database.InsertResult(result2)
resultPlatformElo.PlatformEloID = LICHESS_PLATFORM_ID
if id != nil{
resultPlatformElo.ResultID = *id
}
resultPlatformElo.VerificationKey = games[i].ID
_, _ = database.InsertResultPlatformElo(resultPlatformElo)
}
for i := 0; i < len(newTeamMembers); i++ {
print("Elo for member " + newTeamMembers[i].Username + ":\t" + strconv.FormatFloat(newTeamMembers[i].Elo, 'f', -1, 64) + "\n")
}
return newTeamMembers
}