/
StatusBytes.go
148 lines (123 loc) · 2.38 KB
/
StatusBytes.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
package protocol
import (
"encoding/json"
"strings"
)
type StatusBit int
type StatusByte byte
const (
Protected StatusBit = 1 << iota
Dedicated
AllowOldClients
Started
Dynamix
WON
Reserved1
Reserved2
)
var statusBitString = map[StatusBit]string{
Protected: "Protected",
Dedicated: "Dedicated",
AllowOldClients: "AllowOldClients",
Started: "Started",
Dynamix: "Dynamix",
WON: "WON",
Reserved1: "Reserved1",
Reserved2: "Reserved2",
}
type StatusByteStruct struct {
Protected bool
Dedicated bool
AllowOldClients bool
Started bool
Dynamix bool
WON bool
Reserved1 bool
Reserved2 bool
}
func (s StatusByteStruct) MarshalBinary() (output byte) {
// TODO: there's gotta be a better way to do this.
if s.Protected {
output |= byte(Protected)
}
if s.Dedicated {
output |= byte(Dedicated)
}
if s.AllowOldClients {
output |= byte(AllowOldClients)
}
if s.Started {
output |= byte(Started)
}
if s.Dynamix {
output |= byte(Dynamix)
}
if s.WON {
output |= byte(WON)
}
if s.Reserved1 {
output |= byte(Reserved1)
}
if s.Reserved2 {
output |= byte(Reserved2)
}
return
}
func (s StatusByte) MarshalJSON() ([]byte, error) {
return json.Marshal(s.Struct())
}
func (s StatusByte) Struct() (output StatusByteStruct) {
statusArr := make(map[StatusBit]bool)
for i := 0; i < 8; i++ {
bit := StatusBit(1 << i)
if int(s)&int(bit) != 0 {
statusArr[bit] = true
} else {
statusArr[bit] = false
}
}
for k, v := range statusArr {
switch k {
case Protected:
output.Protected = v
case Dedicated:
output.Dedicated = v
case AllowOldClients:
output.AllowOldClients = v
case Started:
output.Started = v
case Dynamix:
output.Dynamix = v
case WON:
output.WON = v
case Reserved1:
output.Reserved1 = v
case Reserved2:
output.Reserved2 = v
}
}
return
}
func (s StatusByte) StringSlice() (statusArr []string) {
for i := 0; i < 8; i++ {
bit := StatusBit(1 << i)
if int(s)&int(bit) != 0 {
statusArr = append(statusArr, statusBitString[StatusBit(i)])
}
}
return
}
func (s StatusByte) String() string {
var (
out strings.Builder
statusArr = s.StringSlice()
)
for k, v := range statusArr {
if k == len(statusArr)-1 {
out.WriteString(v)
break
}
out.WriteString(v + " | ")
}
return out.String()
}