-
Notifications
You must be signed in to change notification settings - Fork 5
/
iccup.go
178 lines (146 loc) · 4.26 KB
/
iccup.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"log"
"math"
// "fmt"
)
type Ladderer interface {
CalculateNewPoints(winnerPoints, loserPoints int64, winnerDivision, loserDivision *Division) (winnerNew, loserNew int64)
InitDivisions()
GetDifference(div1, div2 *Division) int64
getInitialPlacementMatches() int64
// GetDivision(float64) (division *Division, position int64)
}
type Iccup struct {
}
var (
// If difference > maxDiff, use loseX[0] or loseX[8]
maxDiff = int64(4);
losePoints = [5][9]int64{
[9]int64{0, 13, 25, 37, 50, 63, 75, 100, 150}, // E
[9]int64{0, 13, 25, 37, 50, 63, 75, 100, 150}, // D
[9]int64{10, 19, 37, 56, 75, 93, 112, 131, 150}, // C
[9]int64{10, 25, 50, 75, 100, 125, 150, 175, 200}, // B
[9]int64{20, 50, 80, 110, 140, 170, 200, 230, 260}, // A
}
base = int64(100) // From diff = 0
win_step = int64(25)
win_min = float64(10)
initialPlacementMatches = int64(0);
)
func (iccup *Iccup) InitDivisions() {
log.Println("Initializing divisions")
divisionCount,err := dbMap.SelectInt("SELECT COUNT(*) FROM divisions WHERE system='iccup'");
divisions = make(Divisions, 0, divisionCount)
divs, err := dbMap.Select(Division{}, "SELECT * FROM divisions WHERE system='iccup' ORDER BY promotion_threshold")
if err != nil {
panic(err)
}
if len(divs) > 0 {
for x := range divs {
div := divs[x].(*Division);
div.Id = int64(x); // Assign useful Id
divisions = append(divisions, div)
}
} else {
i := int64(0)
for {
if i > divisionCount {
break
}
var rating float64
if i == 0 {
rating = 0
} else {
rating = divisionFirstRating + (float64(i-1) * divisionIncrements)
}
divisions = append(divisions, &Division{
PromotionThreshold: rating,
DemotionThreshold: rating - 1,
Name: divisionNames[i],
Id: 0,
LadderGroup: i,
})
i++
}
for _, x := range divisions {
err = dbMap.Insert(x)
if err != nil {
panic(err)
}
}
}
return
}
func (iccup *Iccup) CalculateNewPoints(winnerPoints, loserPoints int64, winnerDivision, loserDivision *Division) (winnerNew, loserNew int64) {
// We should make sure that we're using the right divisions and not the ones provided, which might not be updated correctly
winnerDivision,_ = divisions.GetDivision(winnerPoints);
loserDivision,_ = divisions.GetDivision(loserPoints);
difference := iccup.GetDifference(winnerDivision, loserDivision) // from E=0 to A+=12
// group_diff := winnerDivision.LadderGroup - loserDivision.LadderGroup // For new ICCUP system
if(difference > maxDiff){
difference = maxDiff; // Last element
}else if(difference < -maxDiff){
difference = -maxDiff; // First element
}
winnerNew = winnerPoints + int64(math.Max(win_min, float64(base+difference*win_step)))
if(winnerNew > int64(divisions[len(divisions)-1].PromotionThreshold)){
winnerNew = int64(divisions[len(divisions)-1].PromotionThreshold)
}
// fmt.Println("winnerpoints", math.Max(win_min, float64(base+difference*win_step)))
// fmt.Println("difference", difference, "maxDiff", maxDiff)
// Losing
// losingDiffIndex := difference
// if(losingDiffIndex > maxDiff){
// losingDiffIndex = maxDiff; // Last element
// }else if(losingDiffIndex < -maxDiff){
// losingDiffIndex = -maxDiff; // First element
// }
// losingDiffIndex := difference + maxDiff;
// fmt.Println("LadderGroup", loserDivision.LadderGroup)
// fmt.Println("losingDiffIndex",losingDiffIndex)
// fmt.Println("points", losePoints[loserDivision.LadderGroup][losingDiffIndex])
loserNew = loserPoints - losePoints[loserDivision.LadderGroup][difference + maxDiff]
if(loserNew < 0){
loserNew = 0;
}
return
}
func (iccup *Iccup) GetDifference(div1, div2 *Division) int64 {
if div1 == nil || div2 == nil {
return 0
}
var (
p1, p2 int64
i = int64(len(divisions))
)
for {
i--
if divisions[i] == div1 {
p1 = i
}
if divisions[i] == div2 {
p2 = i
}
if i == 0 {
break
}
}
return p2 - p1
}
func (d *Iccup) getInitialPlacementMatches() int64 {
return initialPlacementMatches;
}
// func (d Divisions) GetDivision(points float64) (division *Division, position int64) {
// i := int64(len(d))
// for {
// i--
// if points >= d[i].PromotionThreshold {
// return d[i], i
// }
// if i == 0 {
// break
// }
// }
// return nil, 0
// }