This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
174 lines (147 loc) · 4.58 KB
/
api.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
package switchcraftgo
import (
"strconv"
"time"
)
type PlayerCountsResponse struct {
Total int `json:"total"`
Active int `json:"active"`
}
// Fetches the current player counts from the API.
// If successful, it returns the response, otherwise it returns the error.
func GetPlayerCounts() (*PlayerCountsResponse, error) {
var counts PlayerCountsResponse
err := makeGetJsonRequest("https://api.sc3.io/v3/players", &counts)
if err != nil {
return nil, err
}
return &counts, nil
}
type TpsResponse struct {
AverageTps float32 `json:"tps"`
AverageMillisecondPerTick float32 `json:"avgMsPerTick"`
MillisecondsLastTick float32 `json:"lastMsPerTick"`
}
// Fetches information regarding ticks per seconds.
//
// If successful, it returns a variety of statistics:
// - The average ticks per second, averaged over the last 100 ticks
// - The time per tick, in milliseconds, averaged over the last 100 ticks
// - The time per tick, in milliseconds, for the last tick
//
// If the request failed, it will return nil, and the the error.
func GetTps() (*TpsResponse, error) {
var tps TpsResponse
err := makeGetJsonRequest("https://api.sc3.io/v3/tps", &tps)
if err != nil {
return nil, err
}
return &tps, nil
}
type PlayTimeLeaderboard struct {
UpdatedAt *time.Time
Entries []struct {
Uuid string `json:"uuid"`
Username string `json:"name"`
Seconds int `json:"time"`
}
}
// Fetches the leaderboard of who has been the most active player.
// Players are able to opt-out of this leaderboard.
// Sorted in descending order.
func GetPlaytimeLeaderboard() (*PlayTimeLeaderboard, error) {
var leaderboardData struct {
UpdatedAt string `json:"lastUpdated"`
Entries []struct {
Uuid string `json:"uuid"`
Username string `json:"name"`
Seconds int `json:"time"`
} `json:"entries"`
}
err := makeGetJsonRequest("https://api.sc3.io/v3/activetime", &leaderboardData)
if err != nil {
return nil, err
}
timestamp, err := parseScTimestamp(leaderboardData.UpdatedAt)
if err != nil {
return nil, err
}
return &PlayTimeLeaderboard{
UpdatedAt: ×tamp,
Entries: leaderboardData.Entries,
}, nil
}
type SupporterGoal struct {
SupporterUrl string `json:"supporterUrl"`
Current float64 `json:"current"`
Goal float64 `json:"goal"`
GoalMet bool `json:"goalMet"`
}
// Fetches information about the supporter goal
//
// Assuming success, the following information is returned:
// - SupporterUrl is a string containing the URL to support SwitchCraft
// - Current is a float of the current donated money this month, in dollars
// - Goal is a float of the supporter goal, in dollars
// - GoalMet reports whether the goal has been met
//
// In the case that something fails, the first return value is nil and the second is the error
func GetSupporterGoal() (*SupporterGoal, error) {
var supporterData struct {
SupporterUrl string `json:"supporterUrl"`
Current string `json:"current"`
Goal string `json:"goal"`
GoalMet bool `json:"goalMet"`
}
err := makeGetJsonRequest("https://api.sc3.io/v3/supporter", &supporterData)
if err != nil {
return nil, err
}
current, err := strconv.ParseFloat(supporterData.Current, 64)
if err != nil {
return nil, err
}
goal, err := strconv.ParseFloat(supporterData.Goal, 64)
if err != nil {
return nil, err
}
return &SupporterGoal{
SupporterUrl: supporterData.SupporterUrl,
Current: current,
Goal: goal,
GoalMet: supporterData.GoalMet,
}, nil
}
type DeathsLeaderboard []struct {
Uuid string `json:"uuid"`
Name string `json:"name"`
Count int `json:"count"`
}
// Fetches the leaderboard of most deaths.
//
// Returns an array of players along with the death count.
// The array is sorted in descending order by most deaths.
func GetDeathsLeaderboard() (DeathsLeaderboard, error) {
var leaderboard DeathsLeaderboard
err := makeGetJsonRequest("https://api.sc3.io/v3/deaths", &leaderboard)
if err != nil {
return nil, err
}
return leaderboard, nil
}
// Fetches the current ComputerCraft proxies
//
// Returns an array of IP ranges in CIDR notation that HTTP requests
// originate from when sent from ComputerCraft.
// This information can be used to block any traffic not originating from inside SwitchCraft.
// For an IPv4 address, a CIDR range of /32 means that the IP address will match exactly.
func GetProxyRanges() ([]string, error) {
var proxyInfo struct {
Proxies []string `json:"httpProxies"`
}
err := makeGetJsonRequest("https://api.sc3.io/v3/proxies", &proxyInfo)
if err != nil {
return nil, err
}
return proxyInfo.Proxies, nil
}