gogame-old/models/user.go
models/user.goRaw
1package models
2
3import (
4 "fmt"
5 "time"
6
7 "github.com/arnaucube/gogame/database"
8 "gopkg.in/mgo.v2/bson"
9)
10
11type Resources struct {
12 Metal int64
13 Crystal int64
14 Deuterium int64
15 Energy int64
16}
17
18// UserDb is the data in DB
19type UserDb struct {
20 Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
21 Name string
22 Password string
23 Email string
24 LastUpdated time.Time
25 Resources Resources
26 Planets []bson.ObjectId
27}
28
29// User is the data in memory, after getting it from DB
30type User struct {
31 Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
32 Name string
33 LastUpdated time.Time
34 db *database.Db
35 Resources Resources
36}
37
38func NewUser(db *database.Db, name, password, email string) (*User, error) {
39 newUser := UserDb{
40 Id: bson.NewObjectId(),
41 Name: name,
42 Password: password,
43 Email: email,
44 LastUpdated: time.Now(),
45 }
46 err := db.Users.Insert(newUser)
47 if err != nil {
48 return nil, err
49 }
50 user := UserDbToUser(db, newUser)
51 return user, nil
52}
53
54func UserDbToUser(db *database.Db, u UserDb) *User {
55 return &User{
56 Id: u.Id,
57 Name: u.Name,
58 LastUpdated: u.LastUpdated,
59 db: db,
60 Resources: u.Resources,
61 }
62}
63
64func (u *User) StoreInDb() error {
65 userDb := UserDb{
66 Id: u.Id,
67 Name: u.Name,
68 LastUpdated: time.Now(),
69 Resources: u.Resources,
70 }
71 err := u.db.Users.Update(bson.M{"_id": u.Id}, userDb)
72 return err
73
74}
75
76func (u *User) GetFromDb() error {
77 var userDb UserDb
78 err := u.db.Users.Find(bson.M{"_id": u.Id}).One(&userDb)
79 if err != nil {
80 return err
81 }
82 u = UserDbToUser(u.db, userDb)
83 return nil
84}
85
86func (u *User) GetPlanets() ([]Planet, error) {
87 var planets []Planet
88 err := u.db.Planets.Find(bson.M{"ownerid": u.Id}).All(&planets)
89 if err != nil {
90 return planets, err
91 }
92 return planets, nil
93}
94
95// GetResources updates the values of resources and returns the value, also updates the user.Resources
96// Resource types: metal, crystal, deuterium, energy
97func (u *User) GetResources() (*Resources, error) {
98 // get current values
99 err := u.GetFromDb()
100 if err != nil {
101 return nil, err
102 }
103 // get u.LastUpdated
104 fmt.Println(u.LastUpdated)
105 // calculate Delta time = currentTime - u.LastUpdated
106 delta := time.Since(u.LastUpdated)
107
108 // get planets
109 planets, err := u.GetPlanets()
110 if err != nil {
111 return nil, err
112 }
113
114 // get Resource-Plant level in each planet
115 // and calculate growth = ResourcePlant.Level for each planet
116 var metalGrowth, crystalGrowth, deuteriumGrowth, energyGrowth int64
117 for _, planet := range planets {
118 fmt.Println("planet", planet)
119 // TODO find correct formulas
120 metalGrowth = metalGrowth + ((1 + planet.Buildings.MetalMine) * int64(delta))
121 crystalGrowth = crystalGrowth + ((1 * planet.Buildings.CrystalMine) * int64(delta))
122 deuteriumGrowth = deuteriumGrowth + ((1 * planet.Buildings.DeuteriumMine) * int64(delta))
123 energyGrowth = energyGrowth + ((1 * planet.Buildings.EnergyMine) * int64(delta))
124 }
125 // calculate newAmount = oldAmount + growth
126 u.Resources.Metal = u.Resources.Metal + metalGrowth
127 u.Resources.Crystal = u.Resources.Crystal + crystalGrowth
128 u.Resources.Deuterium = u.Resources.Deuterium + deuteriumGrowth
129 u.Resources.Energy = u.Resources.Energy + energyGrowth
130
131 // store new amount to user db
132 err = u.StoreInDb()
133 if err != nil {
134 return nil, err
135 }
136
137 // return user
138 return &u.Resources, nil
139}