/
status.go
executable file
·100 lines (86 loc) · 2.23 KB
/
status.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
package main
import (
"context"
"encoding/json"
"log"
"strconv"
"strings"
"time"
color "github.com/fatih/color"
"github.com/google/go-github/github"
"github.com/hashicorp/go-version"
"github.com/spf13/viper"
)
// ServerStatus struct
type ServerStatus struct {
ServerOnline bool
TES3MPVersion string
Build string
Players []string
CurrentPlayers int
MaxPlayers int
}
// UpdateStatusTimer run UpdateStatus loop
func UpdateStatusTimer() {
getServerMaxPlayers()
for {
time.Sleep(20 * time.Second)
_ = UpdateStatus()
}
}
func getServerMaxPlayers() {
MaxPlayers = 0
}
// UpdateStatus for keeping server stats synced
func UpdateStatus() (s []byte) {
CurrentStatus := ServerStatus{
ServerOnline: ServerRunning,
TES3MPVersion: TES3MPVersion,
Build: Build,
Players: Players,
CurrentPlayers: CurrentPlayers,
MaxPlayers: MaxPlayers,
}
var jsonData []byte
jsonData, err := json.Marshal(CurrentStatus)
if err != nil {
log.Println(err)
}
return jsonData
}
func getLatestGithubRelease() (isUpdate bool, latestVersion string) {
client := github.NewClient(nil)
releases, _, _ := client.Repositories.GetLatestRelease(context.Background(), "HotaruBlaze", "goTES3MP")
latestRelease := releases.GetTagName()
// Get the build number thats set on build
currentBuild, err := version.NewVersion(Build)
if err != nil {
log.Println(err)
}
// Get latest github release
latestBuild, err := version.NewVersion(latestRelease)
if err != nil {
log.Println(err)
}
if currentBuild.LessThan(latestBuild) {
return true, string("v" + latestBuild.String())
} else {
return false, "nil"
}
}
func getStatus(firstLaunch bool, showModules bool) {
if firstLaunch {
color.HiBlack(strings.Repeat("=", 32))
}
color.HiBlack("goTES3MP: " + Build)
color.HiBlack("Commit: " + GitCommit)
color.HiBlack("Github: " + "https://github.com/hotarublaze/goTES3MP" + "\n")
color.HiBlack("Interactive Console: " + strconv.FormatBool(viper.GetBool("enableInteractiveConsole")))
isUpdate, UpdateVersion := getLatestGithubRelease()
if isUpdate {
color.HiGreen("New build of goTES3MP is available: " + UpdateVersion + ", Current: " + Build)
}
if firstLaunch {
color.HiBlack(strings.Repeat("=", 32))
}
}