-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbank.go
250 lines (200 loc) · 5 KB
/
bank.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"errors"
"fmt"
)
/*
As the name implies, the logic (if you can call it that) here controls interactions with the server bank.
The bank has a balance on hold that can be used for issuing loans.
Loan logic is also included here
*/
// Bank struct
type Bank struct {
db *DBHandler
conf *Config
user *UserHandler
}
// BankRecord struct
type BankRecord struct {
ID string `storm:"id"`
Pin string
Balance int
LoansEnabled bool
}
// AccountRecord struct
type AccountRecord struct {
ID string `storm:"id"`
Pin string
UserID string `storm:"index"`
Balance int
ActiveLoan bool `storm:"index"`
}
// Init function
func (h *Bank) Init() {
if h.conf.BankConfig.Reset {
err := h.ResetBank()
if err != nil {
fmt.Println("Error resetting bank: " + err.Error())
return
}
return
}
_, err := h.GetMainBankAccount()
if err != nil {
fmt.Println(err.Error())
fmt.Println("!!!!!! If you are seeing this message, You need to run ~init from the bank prompt !!!!!!")
fmt.Println("!!!!!! If you are seeing this message, You need to run ~init from the bank prompt !!!!!!")
fmt.Println("!!!!!! If you are seeing this message, You need to run ~init from the bank prompt !!!!!!")
fmt.Println("!!!!!! If you are seeing this message, You need to run ~init from the bank prompt !!!!!!")
return
}
}
// CreateBank function
func (h *Bank) CreateBank() (err error) {
_, err = h.GetMainBankAccount()
if err == nil {
return errors.New("Main Bank Account already exists, it must be removed first! (See Readme for Bank Reset Information)")
}
// Loans are disabled for now!
uuid, err := GetUUID()
if err != nil {
return errors.New("Fatal Error generating UUID")
}
mainrecord := BankRecord{ID: uuid, Pin: h.conf.BankConfig.Pin,
Balance: h.conf.BankConfig.SeedWallet, LoansEnabled: false}
db := h.db.rawdb.From("Bank")
/* Let's see if this works without this delete first
err = db.DeleteStruct(&account)
if err != nil{
return err
}
*/
err = db.Save(&mainrecord)
if err != nil {
return err
}
return nil
}
// ResetBank function
func (h *Bank) ResetBank() (err error) {
account, err := h.GetMainBankAccount()
if err != nil {
return err
}
db := h.db.rawdb.From("Bank")
err = db.DeleteStruct(&account)
if err != nil {
return err
}
return h.CreateBank()
}
// SaveBank function
func (h *Bank) SaveBank() (err error) {
account, err := h.GetMainBankAccount()
if err != nil {
return err
}
db := h.db.rawdb.From("Bank")
/* Let's see if this works without this delete first
err = db.DeleteStruct(&account)
if err != nil{
return err
}
*/
err = db.Save(&account)
if err != nil {
return err
}
return nil
}
// BankInitialized function
func (h *Bank) BankInitialized() bool {
_, err := h.GetMainBankAccount()
if err != nil {
return false
}
return true
}
// GetMainBankAccount function
func (h *Bank) GetMainBankAccount() (account BankRecord, err error) {
db := h.db.rawdb.From("Bank")
err = db.One("Pin", h.conf.BankConfig.Pin, &account)
if err != nil {
return account, err
}
return account, nil
}
// GetAccountForUser function
func (h *Bank) GetAccountForUser(userid string) (account AccountRecord, err error) {
if !h.CheckUserAccount(userid) {
h.CreateUserAccount(userid)
}
bankdb := h.db.rawdb.From("Bank")
accountdb := bankdb.From("Accounts")
record := AccountRecord{}
err = accountdb.One("UserID", userid, &record)
if err != nil {
return record, err
}
return record, nil
}
// GetAccountByAccountID function
func (h *Bank) GetAccountByAccountID(accountid string) (account AccountRecord, err error) {
bankdb := h.db.rawdb.From("Bank")
accountdb := bankdb.From("Accounts")
err = accountdb.One("ID", accountid, &account)
if err != nil {
return account, err
}
return account, nil
}
// CheckUserAccount function
func (h *Bank) CheckUserAccount(userid string) bool {
bankdb := h.db.rawdb.From("Bank")
accountdb := bankdb.From("Accounts")
account := AccountRecord{}
err := accountdb.One("UserID", userid, &account)
if err != nil {
return false
}
return true
}
// CreateUserAccount function
func (h *Bank) CreateUserAccount(userid string) (err error) {
bankdb := h.db.rawdb.From("Bank")
accountdb := bankdb.From("Accounts")
account := AccountRecord{}
err = accountdb.One("UserID", userid, &account)
if err == nil {
return errors.New("User Account Already Exists")
}
account.ID, err = GetUUID()
if err != nil {
return err
}
account.Pin = ""
account.UserID = userid
account.Balance = h.conf.BankConfig.SeedUserAccountBalance
account.ActiveLoan = false
err = h.SaveUserAccount(account)
if err != nil {
return err
}
return nil
}
// SaveUserAccount function
func (h *Bank) SaveUserAccount(account AccountRecord) (err error) {
bankdb := h.db.rawdb.From("Bank")
accountdb := bankdb.From("Accounts")
if h.CheckUserAccount(account.UserID) {
err = accountdb.DeleteStruct(&account)
if err != nil {
return err
}
}
err = accountdb.Save(&account)
if err != nil {
return err
}
return nil
}