-
Notifications
You must be signed in to change notification settings - Fork 2
/
actions.go
131 lines (108 loc) · 2.69 KB
/
actions.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
package main
import (
"sync"
)
// ActionsDB struct
type ActionsDB struct {
db *DBHandler
querylocker sync.RWMutex
}
// Action struct
type Action struct {
ID string `json:"id"`
Name string
Type string
/*
There are six types of actions:
1)Standard
2)Move
3)Full-round
4)Swift
5)Immediate
6)Free
*/
// Actions may have an attribute requirement associated with them
// However equipment, spells, items, etc may all give attribute check bonuses too
Strength int
Dexterity int
Constitution int
Intelligence int
Wisdom int
Charisma int
Description string // A brief description about the action
SkillRequirement string // There may be a skill associated with an action, not all actions will have these
Timeout int // Time in seconds before using this action again
}
// SaveActionToDB function
func (h *ActionsDB) SaveActionToDB(action Action) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Actions")
err = db.Save(&action)
return err
}
// RemoveActionFromDB function
func (h *ActionsDB) RemoveActionFromDB(action Action) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Actions")
err = db.DeleteStruct(&action)
return err
}
// RemoveActionByID function
func (h *ActionsDB) RemoveActionByID(actionID string) (err error) {
action, err := h.GetActionByID(actionID)
if err != nil {
return err
}
err = h.RemoveActionFromDB(action)
if err != nil {
return err
}
return nil
}
// GetActionByID function
func (h *ActionsDB) GetActionByID(actionID string) (action Action, err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Actions")
err = db.One("ID", actionID, &action)
if err != nil {
return action, err
}
return action, nil
}
// GetActionByName function
func (h *ActionsDB) GetActionByName(actionName string) (action Action, err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Actions")
err = db.One("Name", actionName, &action)
if err != nil {
return action, err
}
return action, nil
}
// ValidateActionByID function
func (h *ActionsDB) ValidateActionByID(actionID string) (validated bool) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
var action Action
db := h.db.rawdb.From("Actions")
err := db.One("ID", actionID, &action)
if err != nil {
return false
}
return true
}
// GetAllActions function
func (h *ActionsDB) GetAllActions() (actionlist []Action, err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Actions")
err = db.All(&actionlist)
if err != nil {
return actionlist, err
}
return actionlist, nil
}