-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
185 lines (151 loc) · 3.98 KB
/
types.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package MoneyBot
import (
"encoding/json"
"strings"
"github.com/shvimas/teleBot"
"os"
"strconv"
"bytes"
)
func LoadFrom(file *os.File, target interface{}) error {
return json.NewDecoder(file).Decode(target)
}
func serialize(obj interface{}) ([]byte, error) {
return json.MarshalIndent(obj, "", "\t")
}
const EncodingExtension = ".json"
// ------------------------------------------------------------------------------------
type user struct {
Name string
Username string
Id int
Container *container
History *history
}
func NewUser(uid int, name, username string) *user {
return &user{Name: name, Username: username, Id: uid, Container: NewContainer(uid), History: NewHistory(uid)}
}
func (user user) CheckFilled() bool {
return user.Id != 0 && user.Username != "" && user.Name != "" && user.Container != nil && user.History != nil
}
func (user user) String() string {
return teleBot.StructToString(user)
}
func (user user) Serialize() ([]byte, error) {
return serialize(user)
}
// ------------------------------------------------------------------------------------
const totalField = "total"
type container struct {
Id int
Amounts map[string]float64
}
func NewContainer(uid int) *container {
return &container{uid, make(map[string]float64)}
}
func (cont container) IsFilled() bool {
return cont.Id != 0 && cont.Amounts != nil
}
func (cont container) Size() int {
return len(cont.Amounts)
}
func (cont *container) Add(category string, amount float64) error {
// zero values!
cont.Amounts[strings.ToLower(category)] += amount
cont.Amounts[totalField] += amount
return nil
}
func (cont *container) Delete(category string) error {
cont.Amounts[totalField] -= cont.Amounts[category]
delete(cont.Amounts, strings.ToLower(category))
return nil
}
func (cont *container) Erase() error {
cont.Amounts = make(map[string]float64)
return nil
}
func (cont container) ToString() string {
buf := bytes.Buffer{}
for category, amount := range cont.Amounts {
if category != totalField {
add(&buf, category, amount)
}
}
if total, ok := cont.Amounts[totalField]; ok {
buf.WriteString("-------\n")
add(&buf, totalField, total)
}
if buf.Len() == 0 {
buf.WriteString("<empty>")
}
return buf.String()
}
func add(buf *bytes.Buffer, category string, amount float64) {
buf.WriteString(category)
buf.WriteString(": ")
buf.WriteString(strconv.FormatFloat(amount, 'f', 0, 64))
buf.WriteString("\n")
}
func (cont container) String() string {
return teleBot.StructToString(cont)
}
func (cont container) Serialize() ([]byte, error) {
return serialize(cont)
}
// ------------------------------------------------------------------------------------
type history struct {
Id int
Containers map[string]container
}
func NewHistory(uid int) *history {
return &history{uid, make(map[string]container)}
}
func (hist history) IsFilled() bool {
return hist.Id != 0 && hist.Containers != nil
}
func (hist history) Size() int {
return len(hist.Containers)
}
func (hist history) Get(name string) (container, bool) {
cont, ok := hist.Containers[name]
return cont, ok
}
func (hist *history) Add(container container, name string) string {
if name == "" {
counter := 1
name = strconv.Itoa(hist.Size() + counter)
for _, has := hist.Containers[name]; has; {
counter++
name = strconv.Itoa(hist.Size() + counter)
}
}
hist.Containers[name] = container
return name
}
func (hist *history) Delete(name string) bool {
_, ok := hist.Containers[name]
if ok {
delete(hist.Containers, name)
}
return ok
}
func (hist history) ToString() string {
buf := bytes.Buffer{}
for name, container := range hist.Containers {
buf.WriteString("### ")
buf.WriteString(name)
buf.WriteString(" ###\n")
buf.WriteString(container.ToString())
buf.WriteString("\n\n")
}
if buf.Len() == 0 {
buf.WriteString("<empty>")
}
return buf.String()
}
func (hist history) String() string {
return teleBot.StructToString(hist)
}
func (hist history) Serialize() ([]byte, error) {
return serialize(hist)
}