arnaucube
arnaucube/gogame-oldPublic Self-hosted repository
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: u.LastUpdated,
 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.Users.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
 96// Resource types: metal, crystal, deuterium, energy
 97func (u *User) GetResources() (*User, 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	var metalGrowth, crystalGrowth, deuteriumGrowth, energyGrowth int64
116	for _, planet := range planets {
117		// calculate growth = ResourcePlant.Level for each planet
118		// TODO find correct formulas
119		metalGrowth = metalGrowth + (planet.Buildings.MetalMine * int64(delta))
120		crystalGrowth = crystalGrowth + (planet.Buildings.CrystalMine * int64(delta))
121		deuteriumGrowth = deuteriumGrowth + (planet.Buildings.DeuteriumMine * int64(delta))
122		energyGrowth = energyGrowth + (planet.Buildings.EnergyMine * int64(delta))
123	}
124	// calculate newAmount = oldAmount + (growth & DeltaTime)
125	u.Resources.Metal = u.Resources.Metal + metalGrowth
126	u.Resources.Crystal = u.Resources.Crystal + crystalGrowth
127	u.Resources.Deuterium = u.Resources.Deuterium + deuteriumGrowth
128	u.Resources.Energy = u.Resources.Energy + energyGrowth
129
130	// store new amount to user db
131	err = u.StoreInDb()
132	if err != nil {
133		return nil, err
134	}
135
136	// return user
137	return u, nil
138}