-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaderboard_stuff.go
76 lines (63 loc) · 1.51 KB
/
leaderboard_stuff.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
package main
import (
"bytes"
"fmt"
"log"
"net/http"
)
func CheckLDConnection() {
if UInfo.LD_URL == "" || UInfo.LD_Pass == "" {
LDConnection = "no leaderboard URL"
return
}
req, err := http.NewRequest("GET", UInfo.LD_URL+"/api/test", nil)
if err != nil {
fmt.Println(err)
LDConnection = "could not test the connection"
return
}
req.Header.Add("authorization", fmt.Sprintf("BEARER %v", UInfo.LD_Pass))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
LDConnection = "could not test the connection"
return
}
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
LDConnection = "ok"
return
case 401:
LDConnection = "invalid password"
return
default:
LDConnection = "not connected"
return
}
}
func SendScore(score int) {
if UInfo.LD_URL == "" || UInfo.LD_Pass == "" {
return
}
jsonBody := []byte(fmt.Sprintf(`{"name": %q, "score": %v, "version": %q}`, UInfo.Name, UInfo.Highscore, VERSION))
bodyReader := bytes.NewReader(jsonBody)
fmt.Println(string(jsonBody))
fmt.Println(UInfo.Name)
req, err := http.NewRequest(http.MethodPost, UInfo.LD_URL+"/api/scores", bodyReader)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("authorization", fmt.Sprintf("BEARER %v", UInfo.LD_Pass))
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERROR] -", err)
} else {
fmt.Println(resp.Status)
defer resp.Body.Close()
}
}