-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbug.go
105 lines (89 loc) · 2.06 KB
/
bug.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
package main
import (
"bytes"
"encoding/json"
"strings"
"time"
)
const (
MIN_REPORT = 4
MAX_REPORT = 4096
MAX_REPORTS = 100
)
var bugList bugListData
type bugListData struct {
Version int
Bugs []bugData
dirty bool
}
type reporterData struct {
Name string
UUID uuidData
Location LocData
}
type bugData struct {
Reporter reporterData
Created time.Time
Text string
Fixed bool `json:",omitempty"`
}
func cmdBug(player *characterData, input string) {
if player.Level >= LEVEL_ADMIN && strings.EqualFold(input, "read") {
for _, item := range bugList.Bugs {
player.send("(%v) %v", item.Created, item.Reporter.Name)
player.send("Message:"+NEWLINE+"%v", item.Text)
player.send("Fixed: %v", item.Fixed)
}
return
}
input = strings.TrimSpace(input)
inputLen := len(input)
if inputLen <= MIN_REPORT {
player.send("That is really short... Rejected.")
return
} else if inputLen > MAX_REPORT {
player.send("That seems a bit excessive... Rejected.")
}
if player.NumReports > MAX_REPORTS {
player.send("You already have %v reports recorded. Try our discord instead.")
return
}
player.NumReports++
target := reporterData{Name: player.Name, UUID: player.UUID, Location: player.Loc}
report := bugData{Reporter: target, Created: time.Now().UTC(), Text: input}
bugList.Bugs = append(bugList.Bugs, report)
bugList.dirty = true
player.send("Your report has been recorded!")
}
func loadBugs() {
data, err := readFile(DATA_DIR + BUGS_FILE)
if err != nil {
return
}
err = json.Unmarshal(data, &bugList)
if err != nil {
critLog("readBugs: Unable to unmarshal the data.")
return
}
}
func writeBugs() {
if !bugList.dirty {
return
} else {
bugList.dirty = false
}
outbuf := new(bytes.Buffer)
enc := json.NewEncoder(outbuf)
enc.SetIndent("", "\t")
bugList.Version = BUGS_VERSION
err := enc.Encode(&bugList)
if err != nil {
critLog("writeBugs: enc.Encode: %v", err.Error())
return
}
err = saveFile(DATA_DIR+BUGS_FILE, outbuf.Bytes())
if err != nil {
critLog("writeBugs: saveFile failed %v", err.Error())
return
}
}