Fix: ELO with mixed ELO and getting rid of casual elo in favor of total elo
This commit is contained in:
parent
8db4870057
commit
ab5eaaf96b
2 changed files with 198 additions and 99 deletions
265
backend/hooks.go
265
backend/hooks.go
|
@ -34,13 +34,8 @@ func (app *application) SetupHooks() {
|
|||
// Check if any of these fields has been changed : Player1, Player2, Commandant1, Commandant2, Score1, Score2, Date, Official
|
||||
if(e.Record.GetString("player_1") != e.Record.Original().GetString("player_1") || e.Record.GetString("player_2") != e.Record.Original().GetString("player_2") || e.Record.GetString("commandant_1") != e.Record.Original().GetString("commandant_1") || e.Record.GetString("commandant_2") != e.Record.Original().GetString("commandant_2") || e.Record.GetString("score_1") != e.Record.Original().GetString("score_1") || e.Record.GetString("score_2") != e.Record.Original().GetString("score_2") || e.Record.GetString("date") != e.Record.Original().GetString("date") || e.Record.GetString("official") != e.Record.Original().GetString("official")) {
|
||||
UpdateRecordELO(e, default_ELO_value)
|
||||
UpdateLastELO(e, e.Record.GetFloat("elo_player_1"), e.Record.GetFloat("elo_player_2"), e.Record.GetFloat("elo_commandant_1"), e.Record.GetFloat("elo_commandant_2"))
|
||||
updateNextMatchs(e, DuelResult{
|
||||
Player1_NewELO: e.Record.GetFloat("elo_player_1"),
|
||||
Player2_NewELO: e.Record.GetFloat("elo_player_2"),
|
||||
Commander1_NewELO: e.Record.GetFloat("elo_commandant_1"),
|
||||
Commander2_NewELO: e.Record.GetFloat("elo_commandant_2"),
|
||||
})
|
||||
UpdateLastELO(e)
|
||||
updateNextMatchs(e)
|
||||
|
||||
}
|
||||
|
||||
|
@ -51,41 +46,35 @@ func (app *application) SetupHooks() {
|
|||
app.pb.OnRecordCreate("elo").BindFunc(func (e *core.RecordEvent) error {
|
||||
UpdateRecordELO(e, default_ELO_value)
|
||||
|
||||
UpdateLastELO(e, e.Record.GetFloat("elo_player_1"), e.Record.GetFloat("elo_player_2"), e.Record.GetFloat("elo_commandant_1"), e.Record.GetFloat("elo_commandant_2"))
|
||||
UpdateLastELO(e)
|
||||
|
||||
updateNextMatchs(e, DuelResult{
|
||||
Player1_NewELO: e.Record.GetFloat("elo_player_1"),
|
||||
Player2_NewELO: e.Record.GetFloat("elo_player_2"),
|
||||
Commander1_NewELO: e.Record.GetFloat("elo_commandant_1"),
|
||||
Commander2_NewELO: e.Record.GetFloat("elo_commandant_2"),
|
||||
})
|
||||
updateNextMatchs(e)
|
||||
|
||||
return e.Next()
|
||||
})
|
||||
}
|
||||
|
||||
func updateNextMatchs(e *core.RecordEvent, newData DuelResult) {
|
||||
func updateNextMatchs(e *core.RecordEvent) {
|
||||
official := e.Record.GetBool("official")
|
||||
needToRecalculateElo := false
|
||||
nextMatchsCount := int64(0)
|
||||
|
||||
nextMatchsCount, _ := e.App.CountRecords("elo", dbx.And(dbx.And(dbx.NewExp("date > {:date}", dbx.Params{"date": e.Record.GetString("date")}), dbx.NewExp("official = {:official}", dbx.Params{"official": official})),dbx.Or(dbx.NewExp("player_1 = {:id}", dbx.Params{"id": e.Record.GetString("player_1")}), dbx.NewExp("player_2 = {:id}", dbx.Params{"id": e.Record.GetString("player_1")}))))
|
||||
if nextMatchsCount > 0 {
|
||||
needToRecalculateElo = true
|
||||
filters := []dbx.Expression{
|
||||
// Player1
|
||||
dbx.And(dbx.NewExp("date > {:date}", dbx.Params{"date": e.Record.GetString("date")}),dbx.Or(dbx.NewExp("player_1 = {:id}", dbx.Params{"id": e.Record.GetString("player_1")}), dbx.NewExp("player_2 = {:id}", dbx.Params{"id": e.Record.GetString("player_1")}))),
|
||||
// Player2
|
||||
dbx.And(dbx.NewExp("date > {:date}", dbx.Params{"date": e.Record.GetString("date")}),dbx.Or(dbx.NewExp("player_1 = {:id}", dbx.Params{"id": e.Record.GetString("player_2")}), dbx.NewExp("player_2 = {:id}", dbx.Params{"id": e.Record.GetString("player_2")}))),
|
||||
// Commander1
|
||||
dbx.And(dbx.NewExp("date > {:date}", dbx.Params{"date": e.Record.GetString("date")}),dbx.NewExp("official = {:official}", dbx.Params{"official": official}),dbx.Or(dbx.NewExp("commandant_1 = {:id}", dbx.Params{"id": e.Record.GetString("commandant_1")}), dbx.NewExp("commandant_2 = {:id}", dbx.Params{"id": e.Record.GetString("commandant_1")}))),
|
||||
// Commander2
|
||||
dbx.And(dbx.NewExp("date > {:date}", dbx.Params{"date": e.Record.GetString("date")}),dbx.NewExp("official = {:official}", dbx.Params{"official": official}),dbx.Or(dbx.NewExp("commandant_1 = {:id}", dbx.Params{"id": e.Record.GetString("commandant_2")}), dbx.NewExp("commandant_2 = {:id}", dbx.Params{"id": e.Record.GetString("commandant_2")}))),
|
||||
}
|
||||
|
||||
nextMatchsCount, _ = e.App.CountRecords("elo", dbx.And(dbx.And(dbx.NewExp("date > {:date}", dbx.Params{"date": e.Record.GetString("date")}), dbx.NewExp("official = {:official}", dbx.Params{"official": official})),dbx.Or(dbx.NewExp("player_1 = {:id}", dbx.Params{"id": e.Record.GetString("player_2")}), dbx.NewExp("player_2 = {:id}", dbx.Params{"id": e.Record.GetString("player_2")}))))
|
||||
if nextMatchsCount > 0 {
|
||||
needToRecalculateElo = true
|
||||
}
|
||||
|
||||
nextMatchsCount, _ = e.App.CountRecords("elo", dbx.And(dbx.And(dbx.NewExp("date > {:date}", dbx.Params{"date": e.Record.GetString("date")}), dbx.NewExp("official = {:official}", dbx.Params{"official": official})),dbx.Or(dbx.NewExp("commandant_1 = {:id}", dbx.Params{"id": e.Record.GetString("commandant_1")}), dbx.NewExp("commandant_2 = {:id}", dbx.Params{"id": e.Record.GetString("commandant_1")}))))
|
||||
if nextMatchsCount > 0 {
|
||||
needToRecalculateElo = true
|
||||
}
|
||||
|
||||
nextMatchsCount, _ = e.App.CountRecords("elo", dbx.And(dbx.And(dbx.NewExp("date > {:date}", dbx.Params{"date": e.Record.GetString("date")}), dbx.NewExp("official = {:official}", dbx.Params{"official": official})),dbx.Or(dbx.NewExp("commandant_1 = {:id}", dbx.Params{"id": e.Record.GetString("commandant_2")}), dbx.NewExp("commandant_2 = {:id}", dbx.Params{"id": e.Record.GetString("commandant_2")}))))
|
||||
if nextMatchsCount > 0 {
|
||||
needToRecalculateElo = true
|
||||
for _, exp := range filters {
|
||||
nextMatchsCount, _ = e.App.CountRecords("elo", exp)
|
||||
if nextMatchsCount > 0 {
|
||||
needToRecalculateElo = true
|
||||
}
|
||||
}
|
||||
|
||||
if needToRecalculateElo {
|
||||
|
@ -94,99 +83,148 @@ func updateNextMatchs(e *core.RecordEvent, newData DuelResult) {
|
|||
|
||||
playerIdChanged := []string{ e.Record.GetString("player_1"), e.Record.GetString("player_2") }
|
||||
newPlayerData := map[string]float64{}
|
||||
newPlayerData[e.Record.GetString("player_1")] = newData.Player1_NewELO
|
||||
newPlayerData[e.Record.GetString("player_2")] = newData.Player2_NewELO
|
||||
newPlayerData[e.Record.GetString("player_1")] = e.Record.GetFloat("elo_player_1")
|
||||
newPlayerData[e.Record.GetString("player_2")] = e.Record.GetFloat("elo_player_2")
|
||||
newTournamentPlayerData := map[string]float64{}
|
||||
newTournamentPlayerData[e.Record.GetString("player_1")] = e.Record.GetFloat("elo_tournament_player_1")
|
||||
newTournamentPlayerData[e.Record.GetString("player_2")] = e.Record.GetFloat("elo_tournament_player_2")
|
||||
newCommanderData := map[string]float64{}
|
||||
newCommanderData[e.Record.GetString("commandant_1")] = newData.Commander1_NewELO
|
||||
newCommanderData[e.Record.GetString("commandant_2")] = newData.Commander2_NewELO
|
||||
newCommanderData[e.Record.GetString("commandant_1")] = e.Record.GetFloat("elo_commandant_1")
|
||||
newCommanderData[e.Record.GetString("commandant_2")] = e.Record.GetFloat("elo_commandant_2")
|
||||
newTournamentCommanderData := map[string]float64{}
|
||||
newTournamentCommanderData[e.Record.GetString("commandant_1")] = e.Record.GetFloat("elo_tournament_commandant_1")
|
||||
newTournamentCommanderData[e.Record.GetString("commandant_2")] = e.Record.GetFloat("elo_tournament_commandant_2")
|
||||
commanderIdChanged := []string{ e.Record.GetString("commandant_1"), e.Record.GetString("commandant_2") }
|
||||
|
||||
err := e.App.RecordQuery("elo").
|
||||
Where(dbx.NewExp("date > {:date}", dbx.Params{"date":e.Record.GetString("date")})).
|
||||
AndWhere(dbx.NewExp("official = {:official}",dbx.Params{"official": official})).
|
||||
OrderBy("date ASC").
|
||||
All(&eloMatchs)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
|
||||
for _, v := range eloMatchs {
|
||||
// Check if Match is concerned by the ELO recalculate
|
||||
if slices.Contains(playerIdChanged, v.Player1) || slices.Contains(playerIdChanged, v.Player2) {
|
||||
if slices.Contains(playerIdChanged, v.Player1) || slices.Contains(playerIdChanged, v.Player2) || slices.Contains(commanderIdChanged, v.Commander1) || slices.Contains(commanderIdChanged, v.Commander2) {
|
||||
record, _ := e.App.FindRecordById("elo", v.ID)
|
||||
|
||||
// If Player1 is not in the ID changed, now it is
|
||||
if !slices.Contains(playerIdChanged, v.Player1) {
|
||||
playerIdChanged = append(playerIdChanged, v.Player1)
|
||||
} else {
|
||||
record.Set("last_elo_player_1", newPlayerData[v.Player1])
|
||||
record.Set("last_tournament_elo_player_1", newTournamentPlayerData[v.Player1])
|
||||
}
|
||||
|
||||
|
||||
// If Player2 is not in the ID changed, now it is
|
||||
if !slices.Contains(playerIdChanged, v.Player2) {
|
||||
playerIdChanged = append(playerIdChanged, v.Player2)
|
||||
} else {
|
||||
record.Set("last_elo_player_2", newPlayerData[v.Player2])
|
||||
record.Set("last_tournament_elo_player_2", newTournamentPlayerData[v.Player2])
|
||||
}
|
||||
|
||||
tempEloData := CalculateELO(record.GetFloat("last_elo_player_1"), record.GetFloat("last_elo_player_2"), record.GetFloat("last_elo_commandant_1"), record.GetFloat("last_elo_commandant_2"), record.GetInt("score_1"), record.GetInt("score_2"))
|
||||
record.Set("elo_player_1", tempEloData.Player1_NewELO)
|
||||
record.Set("elo_player_2", tempEloData.Player2_NewELO)
|
||||
|
||||
newPlayerData[v.Player1] = tempEloData.Player1_NewELO
|
||||
newPlayerData[v.Player2] = tempEloData.Player2_NewELO
|
||||
|
||||
e.App.Save(record)
|
||||
}
|
||||
|
||||
if slices.Contains(commanderIdChanged, v.Commander1) || slices.Contains(commanderIdChanged, v.Commander2) {
|
||||
record, _ := e.App.FindRecordById("elo", v.ID)
|
||||
// If Player1 is not in the ID changed, now it is
|
||||
// If Commander1 is not in the ID changed, now it is
|
||||
if !slices.Contains(commanderIdChanged, v.Commander1) {
|
||||
commanderIdChanged = append(commanderIdChanged, v.Commander1)
|
||||
} else {
|
||||
record.Set("last_elo_commandant_1", newCommanderData[v.Commander1])
|
||||
record.Set("last_tournament_elo_commandant_1", newTournamentCommanderData[v.Commander1])
|
||||
}
|
||||
// If Player2 is not in the ID changed, now it is
|
||||
|
||||
// If Commander2 is not in the ID changed, now it is
|
||||
if !slices.Contains(commanderIdChanged, v.Commander2) {
|
||||
commanderIdChanged = append(commanderIdChanged, v.Commander2)
|
||||
} else {
|
||||
record.Set("last_elo_commandant_2", newCommanderData[v.Commander2])
|
||||
record.Set("last_tournament_elo_commandant_2", newTournamentCommanderData[v.Commander2])
|
||||
}
|
||||
|
||||
|
||||
tempEloData := CalculateELO(record.GetFloat("last_elo_player_1"), record.GetFloat("last_elo_player_2"), record.GetFloat("last_elo_commandant_1"), record.GetFloat("last_elo_commandant_2"), record.GetInt("score_1"), record.GetInt("score_2"))
|
||||
record.Set("elo_player_1", tempEloData.Player1_NewELO)
|
||||
record.Set("elo_player_2", tempEloData.Player2_NewELO)
|
||||
record.Set("elo_commandant_1", tempEloData.Commander1_NewELO)
|
||||
record.Set("elo_commandant_2", tempEloData.Commander2_NewELO)
|
||||
tempTournamentEloData := CalculateELO(record.GetFloat("last_tournament_elo_player_1"), record.GetFloat("last_tournament_elo_player_2"), record.GetFloat("last_tournament_elo_commandant_1"), record.GetFloat("last_tournament_elo_commandant_2"), record.GetInt("score_1"), record.GetInt("score_2"))
|
||||
record.Set("elo_tournament_player_1", tempTournamentEloData.Player1_NewELO)
|
||||
record.Set("elo_tournament_player_2", tempTournamentEloData.Player2_NewELO)
|
||||
record.Set("elo_tournament_commandant_1", tempTournamentEloData.Commander1_NewELO)
|
||||
record.Set("elo_tournament_commandant_2", tempTournamentEloData.Commander2_NewELO)
|
||||
|
||||
newPlayerData[v.Player1] = tempEloData.Player1_NewELO
|
||||
newPlayerData[v.Player2] = tempEloData.Player2_NewELO
|
||||
newTournamentPlayerData[v.Player1] = tempTournamentEloData.Player1_NewELO
|
||||
newTournamentPlayerData[v.Player2] = tempTournamentEloData.Player2_NewELO
|
||||
newCommanderData[v.Commander1] = tempEloData.Commander1_NewELO
|
||||
newCommanderData[v.Commander2] = tempEloData.Commander2_NewELO
|
||||
newTournamentCommanderData[v.Commander1] = tempTournamentEloData.Commander1_NewELO
|
||||
newTournamentCommanderData[v.Commander2] = tempTournamentEloData.Commander2_NewELO
|
||||
|
||||
e.App.Save(record)
|
||||
}
|
||||
}
|
||||
|
||||
UpdateLastELO(e, newPlayerData[e.Record.GetString("player_1")], newPlayerData[e.Record.GetString("player_2")], newCommanderData[e.Record.GetString("commandant_1")], newCommanderData[e.Record.GetString("commandant_2")])
|
||||
// Update Card and Player with the latest calculated ELO
|
||||
UpdateLastELO(e, newTournamentPlayerData[e.Record.GetString("player_1")], newTournamentPlayerData[e.Record.GetString("player_2")], newPlayerData[e.Record.GetString("player_1")], newPlayerData[e.Record.GetString("player_2")], newTournamentCommanderData[e.Record.GetString("commandant_1")], newTournamentCommanderData[e.Record.GetString("commandant_2")], newCommanderData[e.Record.GetString("commandant_1")], newCommanderData[e.Record.GetString("commandant_2")],)
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateLastELO(e *core.RecordEvent, player1_ELO float64, player2_ELO float64, commander1_ELO float64, commander2_ELO float64) {
|
||||
func UpdateLastELO(e *core.RecordEvent, args ...float64) {
|
||||
player1EloTournament := 0.0
|
||||
player2EloTournament := 0.0
|
||||
player1Elo := 0.0
|
||||
player2Elo := 0.0
|
||||
commander1EloTournament := 0.0
|
||||
commander2EloTournament := 0.0
|
||||
commander1Elo := 0.0
|
||||
commander2Elo := 0.0
|
||||
|
||||
// Check if args have been passed (It happens when updating match while inserting between two existing matchs)
|
||||
if len(args) == 8 {
|
||||
player1EloTournament = args[0]
|
||||
player2EloTournament = args[1]
|
||||
player1Elo = args[2]
|
||||
player2Elo = args[3]
|
||||
commander1EloTournament = args[4]
|
||||
commander2EloTournament = args[5]
|
||||
commander1Elo = args[6]
|
||||
commander2Elo = args[7]
|
||||
} else {
|
||||
player1EloTournament = e.Record.GetFloat("elo_tournament_player_1")
|
||||
player2EloTournament = e.Record.GetFloat("elo_tournament_player_2")
|
||||
player1Elo = e.Record.GetFloat("elo_player_1")
|
||||
player2Elo = e.Record.GetFloat("elo_player_2")
|
||||
commander1EloTournament = e.Record.GetFloat("elo_tournament_commandant_1")
|
||||
commander2EloTournament = e.Record.GetFloat("elo_tournament_commandant_2")
|
||||
commander1Elo = e.Record.GetFloat("elo_commandant_1")
|
||||
commander2Elo = e.Record.GetFloat("elo_commandant_2")
|
||||
}
|
||||
|
||||
recordPlayer1, _ := e.App.FindRecordById("elo_player", e.Record.GetString("player_1"))
|
||||
recordPlayer2, _ := e.App.FindRecordById("elo_player", e.Record.GetString("player_2"))
|
||||
|
||||
if e.Record.GetBool("official") {
|
||||
recordPlayer1.Set("elo_tournament", player1_ELO)
|
||||
recordPlayer2.Set("elo_tournament", player2_ELO)
|
||||
} else {
|
||||
recordPlayer1.Set("elo_casual", player1_ELO)
|
||||
recordPlayer2.Set("elo_casual", player2_ELO)
|
||||
recordPlayer1.Set("elo_tournament", player1EloTournament)
|
||||
recordPlayer2.Set("elo_tournament", player2EloTournament)
|
||||
}
|
||||
|
||||
// In any case set the elo_total
|
||||
recordPlayer1.Set("elo", player1Elo)
|
||||
recordPlayer2.Set("elo", player2Elo)
|
||||
|
||||
recordCommander1, _ := e.App.FindRecordById("carte", e.Record.GetString("commandant_1"))
|
||||
recordCommander2, _ := e.App.FindRecordById("carte", e.Record.GetString("commandant_2"))
|
||||
if e.Record.GetBool("official") {
|
||||
recordCommander1.Set("elo_tournament", commander1_ELO)
|
||||
recordCommander2.Set("elo_tournament", commander2_ELO)
|
||||
} else {
|
||||
recordCommander1.Set("elo_casual", commander1_ELO)
|
||||
recordCommander2.Set("elo_casual", commander2_ELO)
|
||||
recordCommander1.Set("elo_tournament", commander1EloTournament)
|
||||
recordCommander2.Set("elo_tournament", commander2EloTournament)
|
||||
}
|
||||
|
||||
recordCommander1.Set("elo", commander1Elo)
|
||||
recordCommander2.Set("elo", commander2Elo)
|
||||
|
||||
e.App.Save(recordPlayer1)
|
||||
e.App.Save(recordPlayer2)
|
||||
e.App.Save(recordCommander1)
|
||||
|
@ -206,20 +244,23 @@ func UpdateRecordELO(e *core.RecordEvent, default_ELO_value float64) {
|
|||
|
||||
// Base data in case it's the first match
|
||||
player1_ELO := default_ELO_value
|
||||
player1_ELOTournament := default_ELO_value
|
||||
player2_ELO := default_ELO_value
|
||||
player2_ELOTournament := default_ELO_value
|
||||
commander1_ELO := default_ELO_value
|
||||
commander1_ELOTournament := default_ELO_value
|
||||
commander2_ELO := default_ELO_value
|
||||
commander2_ELOTournament := default_ELO_value
|
||||
|
||||
// Update ELO With last match ELO (Tournament or Casual)
|
||||
updateEloPlayerWithLastMatch(e, player1Id, date, &player1_ELO, official)
|
||||
updateEloPlayerWithLastMatch(e, player2Id, date, &player2_ELO, official)
|
||||
updateEloPlayerWithLastMatch(e, player1Id, date, &player1_ELO, &player1_ELOTournament, official)
|
||||
updateEloPlayerWithLastMatch(e, player2Id, date, &player2_ELO, &player2_ELOTournament, official)
|
||||
|
||||
// Update ELO With last match ELO (Tournament or Casual)
|
||||
updateEloCommanderWithLastMatch(e, commander1Id, date, &commander1_ELO, official)
|
||||
updateEloCommanderWithLastMatch(e, commander2Id, date, &commander2_ELO, official)
|
||||
updateEloCommanderWithLastMatch(e, commander1Id, date, &commander1_ELO, &commander1_ELOTournament, official)
|
||||
updateEloCommanderWithLastMatch(e, commander2Id, date, &commander2_ELO, &commander2_ELOTournament, official)
|
||||
|
||||
newData := CalculateELO(player1_ELO, player2_ELO, commander1_ELO, commander2_ELO, score1, score2)
|
||||
|
||||
e.Record.Set("last_elo_player_1", player1_ELO)
|
||||
e.Record.Set("elo_player_1", newData.Player1_NewELO)
|
||||
e.Record.Set("last_elo_player_2", player2_ELO)
|
||||
|
@ -228,13 +269,45 @@ func UpdateRecordELO(e *core.RecordEvent, default_ELO_value float64) {
|
|||
e.Record.Set("elo_commandant_1", newData.Commander1_NewELO)
|
||||
e.Record.Set("last_elo_commandant_2", commander2_ELO)
|
||||
e.Record.Set("elo_commandant_2", newData.Commander2_NewELO)
|
||||
|
||||
if official {
|
||||
newDataTournament := CalculateELO(player1_ELOTournament, player2_ELOTournament, commander1_ELOTournament, commander2_ELOTournament, score1, score2)
|
||||
e.Record.Set("last_elo_tournament_player_1", player1_ELOTournament)
|
||||
e.Record.Set("elo_tournament_player_1", newDataTournament.Player1_NewELO)
|
||||
e.Record.Set("last_elo_tournament_player_2", player2_ELOTournament)
|
||||
e.Record.Set("elo_tournament_player_2", newDataTournament.Player2_NewELO)
|
||||
e.Record.Set("last_elo_tournament_commandant_1", commander1_ELOTournament)
|
||||
e.Record.Set("elo_tournament_commandant_1", newDataTournament.Commander1_NewELO)
|
||||
e.Record.Set("last_elo_tournament_commandant_2", commander2_ELOTournament)
|
||||
e.Record.Set("elo_tournament_commandant_2", newDataTournament.Commander2_NewELO)
|
||||
}
|
||||
}
|
||||
|
||||
func updateEloPlayerWithLastMatch(e *core.RecordEvent, playerId string, date string, elo *float64, official bool) {
|
||||
func updateEloPlayerWithLastMatch(e *core.RecordEvent, playerId string, date string, elo *float64, eloTournament *float64, official bool) {
|
||||
lastMatch := Elo{}
|
||||
err := e.App.RecordQuery("elo").
|
||||
lastMatchTournament := Elo{}
|
||||
|
||||
var err error
|
||||
|
||||
if official {
|
||||
err = e.App.RecordQuery("elo").
|
||||
Where(dbx.NewExp("date < {:date}", dbx.Params{"date": date})).
|
||||
AndWhere(dbx.NewExp("official = {:official}", dbx.Params{"official": official})).
|
||||
AndWhere(dbx.Or(dbx.NewExp("player_1 = {:id}", dbx.Params{"id": playerId}), dbx.NewExp("player_2 = {:id}", dbx.Params{"id": playerId}))).
|
||||
OrderBy("date DESC").
|
||||
One(&lastMatchTournament)
|
||||
if err == nil {
|
||||
// If a match exists before this one update ELO
|
||||
if lastMatchTournament.Player1 == playerId {
|
||||
*eloTournament = lastMatchTournament.EloTournamentPlayer1
|
||||
} else {
|
||||
*eloTournament = lastMatchTournament.EloTournamentPlayer2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = e.App.RecordQuery("elo").
|
||||
Where(dbx.NewExp("date < {:date}", dbx.Params{"date": date})).
|
||||
AndWhere(dbx.NewExp("official = {:official}", dbx.Params{"official": official})).
|
||||
AndWhere(dbx.Or(dbx.NewExp("player_1 = {:id}", dbx.Params{"id": playerId}), dbx.NewExp("player_2 = {:id}", dbx.Params{"id": playerId}))).
|
||||
OrderBy("date DESC").
|
||||
One(&lastMatch)
|
||||
|
@ -248,11 +321,32 @@ func updateEloPlayerWithLastMatch(e *core.RecordEvent, playerId string, date str
|
|||
}
|
||||
}
|
||||
|
||||
func updateEloCommanderWithLastMatch(e *core.RecordEvent, commanderId string, date string, elo *float64, official bool) {
|
||||
func updateEloCommanderWithLastMatch(e *core.RecordEvent, commanderId string, date string, elo *float64, eloTournament *float64, official bool) {
|
||||
lastMatch := Elo{}
|
||||
err := e.App.RecordQuery("elo").
|
||||
lastMatchTournament := Elo{}
|
||||
|
||||
var err error
|
||||
|
||||
if official {
|
||||
err = e.App.RecordQuery("elo").
|
||||
Where(dbx.NewExp("date < {:date}", dbx.Params{"date": date})).
|
||||
AndWhere(dbx.NewExp("official = {:official}", dbx.Params{"official": official})).
|
||||
AndWhere(dbx.Or(dbx.NewExp("commandant_1 = {:id}", dbx.Params{"id": commanderId}), dbx.NewExp("commandant_2 = {:id}", dbx.Params{"id": commanderId}))).
|
||||
OrderBy("date DESC").
|
||||
One(&lastMatchTournament)
|
||||
|
||||
if err == nil {
|
||||
// If a match exists before this one update ELO
|
||||
if lastMatchTournament.Commander1 == commanderId {
|
||||
*eloTournament = lastMatchTournament.EloTournamentCommander1
|
||||
} else {
|
||||
*eloTournament = lastMatchTournament.EloTournamentCommander2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = e.App.RecordQuery("elo").
|
||||
Where(dbx.NewExp("date < {:date}", dbx.Params{"date": date})).
|
||||
AndWhere(dbx.NewExp("official = {:official}", dbx.Params{"official": official})).
|
||||
AndWhere(dbx.Or(dbx.NewExp("commandant_1 = {:id}", dbx.Params{"id": commanderId}), dbx.NewExp("commandant_2 = {:id}", dbx.Params{"id": commanderId}))).
|
||||
OrderBy("date DESC").
|
||||
One(&lastMatch)
|
||||
|
@ -268,8 +362,9 @@ func updateEloCommanderWithLastMatch(e *core.RecordEvent, commanderId string, da
|
|||
}
|
||||
|
||||
func CalculateELO(player1_ELO float64, player2_ELO float64, commander1_ELO float64, commander2_ELO float64, score1 int, score2 int) DuelResult {
|
||||
K_PLAYER := 30.0
|
||||
K_COMMANDER := 20.0
|
||||
K_PLAYER := 20.0
|
||||
K_COMMANDER := 10.0
|
||||
ALPHA := 0.7
|
||||
|
||||
won_player1 := 0.0
|
||||
won_player2 := 0.0
|
||||
|
@ -283,16 +378,16 @@ func CalculateELO(player1_ELO float64, player2_ELO float64, commander1_ELO float
|
|||
won_player2 = 0.5
|
||||
}
|
||||
|
||||
eloModifier_player1 := won_player1 - 1.0 / (1 + math.Pow(10, (player2_ELO - player1_ELO) / 400.0))
|
||||
eloModifier_player2 := won_player2 - 1.0 / (1 + math.Pow(10, (player1_ELO - player2_ELO) / 400.0))
|
||||
mixed_ELO1 := ALPHA * commander1_ELO + (1 - ALPHA) * player1_ELO
|
||||
mixed_ELO2 := ALPHA * commander2_ELO + (1 - ALPHA) * player2_ELO
|
||||
|
||||
eloModifier_commander1 := won_player1 - 1.0 / (1 + math.Pow(10, (commander2_ELO - commander1_ELO) / 400.0))
|
||||
eloModifier_commander2 := won_player2 - 1.0 / (1 + math.Pow(10, (commander1_ELO - commander2_ELO) / 400.0))
|
||||
eloModifier1 := won_player1 - 1.0 / (1 + math.Pow(10, (mixed_ELO2 - mixed_ELO1) / 400.0))
|
||||
eloModifier2 := won_player2 - 1.0 / (1 + math.Pow(10, (mixed_ELO1 - mixed_ELO2) / 400.0))
|
||||
|
||||
return DuelResult{
|
||||
Player1_NewELO: player1_ELO + K_PLAYER * eloModifier_player1,
|
||||
Player2_NewELO: player2_ELO + K_PLAYER * eloModifier_player2,
|
||||
Commander1_NewELO: commander1_ELO + K_COMMANDER * eloModifier_commander1,
|
||||
Commander2_NewELO: commander2_ELO + K_COMMANDER * eloModifier_commander2,
|
||||
Player1_NewELO: player1_ELO + K_PLAYER * eloModifier1,
|
||||
Player2_NewELO: player2_ELO + K_PLAYER * eloModifier2,
|
||||
Commander1_NewELO: commander1_ELO + K_COMMANDER * eloModifier1,
|
||||
Commander2_NewELO: commander2_ELO + K_COMMANDER * eloModifier2,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,19 +57,23 @@ type Deck struct {
|
|||
}
|
||||
|
||||
type Elo struct {
|
||||
ID string `db:"id" json:"id"`
|
||||
Date string `db:"date" json:"date"`
|
||||
Player1 string `db:"player_1" json:"player_1"`
|
||||
Player2 string `db:"player_2" json:"player_2"`
|
||||
Commander1 string `db:"commandant_1" json:"commander_1"`
|
||||
Commander2 string `db:"commandant_2" json:"commander_2"`
|
||||
Score1 int `db:"score_1" json:"score_1"`
|
||||
Score2 int `db:"score_2" json:"score_2"`
|
||||
Official bool `db:"official" json:"official"`
|
||||
EloCommander1 float64 `db:"elo_commandant_1" json:"elo_commander_1"`
|
||||
EloCommander2 float64 `db:"elo_commandant_2" json:"elo_commander_2"`
|
||||
EloPlayer1 float64 `db:"elo_player_1" json:"elo_player_1"`
|
||||
EloPlayer2 float64 `db:"elo_player_2" json:"elo_player_2"`
|
||||
ID string `db:"id" json:"id"`
|
||||
Date string `db:"date" json:"date"`
|
||||
Player1 string `db:"player_1" json:"player_1"`
|
||||
Player2 string `db:"player_2" json:"player_2"`
|
||||
Commander1 string `db:"commandant_1" json:"commander_1"`
|
||||
Commander2 string `db:"commandant_2" json:"commander_2"`
|
||||
Score1 int `db:"score_1" json:"score_1"`
|
||||
Score2 int `db:"score_2" json:"score_2"`
|
||||
Official bool `db:"official" json:"official"`
|
||||
EloCommander1 float64 `db:"elo_commandant_1" json:"elo_commander_1"`
|
||||
EloCommander2 float64 `db:"elo_commandant_2" json:"elo_commander_2"`
|
||||
EloPlayer1 float64 `db:"elo_player_1" json:"elo_player_1"`
|
||||
EloPlayer2 float64 `db:"elo_player_2" json:"elo_player_2"`
|
||||
EloTournamentCommander1 float64 `db:"elo_tournament_commandant_1" json:"elo_tournament_commander_1"`
|
||||
EloTournamentCommander2 float64 `db:"elo_tournament_commandant_2" json:"elo_tournament_commander_2"`
|
||||
EloTournamentPlayer1 float64 `db:"elo_tournament_player_1" json:"elo_tournament_player_1"`
|
||||
EloTournamentPlayer2 float64 `db:"elo_tournament_player_2" json:"elo_tournament_player_2"`
|
||||
}
|
||||
|
||||
type EloPlayer struct {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue