aboutsummaryrefslogtreecommitdiff
path: root/rankings/filter.go
blob: f90321f914dc1761c827b477204de417ba7b34e4 (plain) (blame)
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
package main

import (
	"log"
	"math"
	"sort"
)

func filterRankings(spRankings, mpRankings, overallRankings *[]*Player, players map[string]*Player) {
	for k, p := range players {
		if p.SpIterations == 51 {
			*spRankings = append(*spRankings, p)
		}
		if p.MpIterations == 48 {
			*mpRankings = append(*mpRankings, p)
		}
		if p.SpIterations == 51 && p.MpIterations == 48 {
			p.OverallScoreCount = p.SpScoreCount + p.MpScoreCount
			*overallRankings = append(*overallRankings, p)
		}
		if p.SpIterations < 51 && p.MpIterations < 48 {
			delete(players, k)
		}
	}

	log.Println("getting player summaries for", len(players), "players")

	for _, chunk := range chunkMap(players, 100) {
		fetchPlayerInfo(chunk)
	}

	log.Println("sorting the ranks")
	sort.Slice(*spRankings, func(i, j int) bool {
		a := (*spRankings)[i]
		b := (*spRankings)[j]
		if a.SpScoreCount == b.SpScoreCount {
			return a.SteamID < b.SteamID
		}
		return a.SpScoreCount < b.SpScoreCount
	})

	rank := 1
	offset := 0

	for idx := 0; idx < len(*spRankings); idx++ {
		if idx == 0 {
			(*spRankings)[idx].SpRank = rank
			continue
		}
		if (*spRankings)[idx-1].SpScoreCount != (*spRankings)[idx].SpScoreCount {
			rank = rank + offset + 1
			offset = 0
		} else {
			offset++
		}
		(*spRankings)[idx].SpRank = rank
	}

	sort.Slice(*mpRankings, func(i, j int) bool {
		a := (*mpRankings)[i]
		b := (*mpRankings)[j]
		if a.MpScoreCount == b.MpScoreCount {
			return a.SteamID < b.SteamID
		}
		return a.MpScoreCount < b.MpScoreCount
	})

	rank = 1
	offset = 0

	for idx := 0; idx < len(*mpRankings); idx++ {
		if idx == 0 {
			(*mpRankings)[idx].MpRank = rank
			continue
		}
		if (*mpRankings)[idx-1].MpScoreCount != (*mpRankings)[idx].MpScoreCount {
			rank = rank + offset + 1
			offset = 0
		} else {
			offset++
		}
		(*mpRankings)[idx].MpRank = rank
	}

	sort.Slice(*overallRankings, func(i, j int) bool {
		a := (*overallRankings)[i]
		b := (*overallRankings)[j]
		if a.OverallScoreCount == b.OverallScoreCount {
			return a.SteamID < b.SteamID
		}
		return a.OverallScoreCount < b.OverallScoreCount
	})

	rank = 1
	offset = 0

	for idx := 0; idx < len(*overallRankings); idx++ {
		if idx == 0 {
			(*overallRankings)[idx].OverallRank = rank
			continue
		}
		if (*overallRankings)[idx-1].OverallScoreCount != (*overallRankings)[idx].OverallScoreCount {
			rank = rank + offset + 1
			offset = 0
		} else {
			offset++
		}
		(*overallRankings)[idx].OverallRank = rank
	}
}

func chunkMap[T any](m map[string]*T, chunkSize int) [][]*T {
	chunks := make([][]*T, 0, int(math.Ceil(float64(len(m))/float64(chunkSize))))
	chunk := make([]*T, 0, chunkSize)

	count := 0
	for _, player := range m {
		chunk = append(chunk, player)
		count++

		if count == chunkSize {
			chunks = append(chunks, chunk)
			chunk = make([]*T, 0, chunkSize)
			count = 0
		}
	}

	if len(chunk) > 0 {
		chunks = append(chunks, chunk)
	}

	return chunks
}