This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
76 lines (57 loc) · 1.51 KB
/
game.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 (
"encoding/json"
"fmt"
"time"
)
var gameVersionsCache []string
//Set the initial game versions in the cache
func populateInitialGameVersions() error {
versions, err := getGameVersions()
if err != nil {
return err
}
for _, version := range versions {
gameVersionsCache = append(gameVersionsCache, version.ID)
}
fmt.Printf("Loaded %d initial game versions\n", len(gameVersionsCache))
return nil
}
func gameUpdateCheck(callback func(message string) error) error {
currentVersions, err := getGameVersions()
if err != nil {
return err
}
for _, version := range currentVersions {
if !contains(gameVersionsCache, version.ID) {
gameVersionsCache = append(gameVersionsCache, version.ID)
err = callback(gameVersionAsString(version))
if err != nil {
return err
}
}
}
return nil
}
func gameVersionAsString(version Version) string {
return fmt.Sprintf("A new %s version of minecraft was just released! : %s", version.Type, version.ID)
}
func getGameVersions() ([]Version, error) {
jsonStr, err := getJson("https://launchermeta.mojang.com/mc/game/version_manifest.json")
if err != nil {
return nil, err
}
var meta GameMeta
err = json.Unmarshal([]byte(jsonStr), &meta)
return meta.Versions, err
}
type GameMeta struct {
Versions []Version `json:"versions"`
}
type Version struct {
ID string `json:"id"`
Type string `json:"type"`
URL string `json:"url"`
Time time.Time `json:"time"`
ReleaseTime time.Time `json:"releaseTime"`
}