/
init.go
116 lines (82 loc) · 2.1 KB
/
init.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
package gaming
import (
"encoding/json"
"io/ioutil"
"log"
"github.com/olebedev/config"
"github.com/tryanzu/core/deps"
"github.com/tryanzu/core/modules/exceptions"
"github.com/tryanzu/core/modules/feed"
"github.com/tryanzu/core/modules/user"
"gopkg.in/mgo.v2/bson"
)
func Boot(file string) *Module {
module := &Module{}
data, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
// Unmarshal file with gaming rules
if err := json.Unmarshal(data, &module.Rules); err != nil {
panic(err)
}
return module
}
func logFunc(message string) {
log.Println(message)
}
type Module struct {
User *user.Module `inject:""`
Feed *feed.FeedModule `inject:""`
Config *config.Config `inject:""`
Errors *exceptions.ExceptionsModule `inject:""`
Rules Rules
}
// Get user gaming struct
func (self *Module) Get(usr interface{}) *User {
module := self
switch usr.(type) {
case bson.ObjectId:
// Use user module reference to get the user and then create the user gaming instance
user_obj, err := self.User.Get(usr.(bson.ObjectId))
if err != nil {
panic(err)
}
user_gaming := &User{user: user_obj, di: module}
return user_gaming
case *user.One:
user_gaming := &User{user: usr.(*user.One), di: module}
return user_gaming
default:
panic("Unkown argument")
}
}
// Get post gaming struct
func (self *Module) Post(post interface{}) *Post {
module := self
switch post.(type) {
case bson.ObjectId:
// Use user module reference to get the user and then create the user gaming instance
post_object, err := self.Feed.Post(post.(bson.ObjectId))
if err != nil {
panic(err)
}
post_gaming := &Post{post: post_object, di: module}
return post_gaming
case *feed.Post:
post_gaming := &Post{post: post.(*feed.Post), di: module}
return post_gaming
default:
panic("Unkown argument")
}
}
// Get gamification model with badges
func (self *Module) GetRules() Rules {
database := deps.Container.Mgo()
rules := self.Rules
err := database.C("badges").Find(nil).All(&rules.Badges)
if err != nil {
panic(err)
}
return rules
}