add some User functions, move api tests from js to python

This commit is contained in:
arnaucube
2019-06-14 20:00:11 +02:00
parent 8f0d0fd5e0
commit be27edda88
13 changed files with 290 additions and 158 deletions

View File

@@ -2,9 +2,20 @@ package models
import "gopkg.in/mgo.v2/bson"
type Planet struct {
Id bson.ObjectId `json:"id", bson:"_id, omitempty"`
Size int64 // fields/slots
Name string
OwnerId bson.ObjectId
type BuildingsList struct {
MetalMine int64
CrystalMine int64
DeuteriumMine int64
EnergyMine int64
FusionReactor int64
RoboticsFactory int64
Shipyard int64
RessearchLab int64
}
type Planet struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
Size int64 // fields/slots
Name string
OwnerId bson.ObjectId
Buildings BuildingsList
}

View File

@@ -2,11 +2,8 @@ package models
import "gopkg.in/mgo.v2/bson"
const GALAXYSIZE = 50
const SOLARSYSTEMSIZE = 15
type SolarSystem struct {
Id bson.ObjectId `json:"id", bson:"_id, omitempty"`
Position int64 // position of the solar system in the galaxy, the maximum position is GALAXYSIZE-1
Planets []bson.ObjectId // array with ids of the planets, if empty is equal to ""
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
Position int64 // position of the solar system in the galaxy, the maximum position is GALAXYSIZE-1
Planets []string // array with ids of the planets, if empty is equal to ""
}

View File

@@ -1,21 +1,138 @@
package models
import "gopkg.in/mgo.v2/bson"
import (
"fmt"
"time"
type Resource struct {
Value int64
Max int64
"github.com/arnaucube/gogame/database"
"gopkg.in/mgo.v2/bson"
)
type Resources struct {
Metal int64
Crystal int64
Deuterium int64
Energy int64
}
// UserDb is the data in DB
type UserDb struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
Name string
Password string
Email string
LastUpdated time.Time
Resources Resources
Planets []bson.ObjectId
}
// User is the data in memory, after getting it from DB
type User struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
Name string
Password string
Email string
Resources struct {
Metal Resource
Crystal Resource
Deuterium Resource
Energy Resource
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
Name string
LastUpdated time.Time
db *database.Db
Resources Resources
}
func NewUser(db *database.Db, name, password, email string) (*User, error) {
newUser := UserDb{
Id: bson.NewObjectId(),
Name: name,
Password: password,
Email: email,
LastUpdated: time.Now(),
}
err := db.Users.Insert(newUser)
if err != nil {
return nil, err
}
user := UserDbToUser(db, newUser)
return user, nil
}
func UserDbToUser(db *database.Db, u UserDb) *User {
return &User{
Id: u.Id,
Name: u.Name,
LastUpdated: u.LastUpdated,
db: db,
Resources: u.Resources,
}
}
func (u *User) StoreInDb() error {
userDb := UserDb{
Id: u.Id,
Name: u.Name,
LastUpdated: u.LastUpdated,
Resources: u.Resources,
}
err := u.db.Users.Update(bson.M{"_id": u.Id}, userDb)
return err
}
func (u *User) GetFromDb() error {
var userDb UserDb
err := u.db.Users.Find(bson.M{"_id": u.Id}).One(&userDb)
if err != nil {
return err
}
u = UserDbToUser(u.db, userDb)
return nil
}
func (u *User) GetPlanets() ([]Planet, error) {
var planets []Planet
err := u.db.Users.Find(bson.M{"OwnerId": u.Id}).All(&planets)
if err != nil {
return planets, err
}
return planets, nil
}
// GetResources updates the values of resources and returns the value
// Resource types: metal, crystal, deuterium, energy
func (u *User) GetResources() (*User, error) {
// get current values
err := u.GetFromDb()
if err != nil {
return nil, err
}
// get u.LastUpdated
fmt.Println(u.LastUpdated)
// calculate Delta time = currentTime - u.LastUpdated
delta := time.Since(u.LastUpdated)
// get planets
planets, err := u.GetPlanets()
if err != nil {
return nil, err
}
// get Resource-Plant level in each planet
var metalGrowth, crystalGrowth, deuteriumGrowth, energyGrowth int64
for _, planet := range planets {
// calculate growth = ResourcePlant.Level for each planet
// TODO find correct formulas
metalGrowth = metalGrowth + (planet.Buildings.MetalMine * int64(delta))
crystalGrowth = crystalGrowth + (planet.Buildings.CrystalMine * int64(delta))
deuteriumGrowth = deuteriumGrowth + (planet.Buildings.DeuteriumMine * int64(delta))
energyGrowth = energyGrowth + (planet.Buildings.EnergyMine * int64(delta))
}
// calculate newAmount = oldAmount + (growth & DeltaTime)
u.Resources.Metal = u.Resources.Metal + metalGrowth
u.Resources.Crystal = u.Resources.Crystal + crystalGrowth
u.Resources.Deuterium = u.Resources.Deuterium + deuteriumGrowth
u.Resources.Energy = u.Resources.Energy + energyGrowth
// store new amount to user db
err = u.StoreInDb()
if err != nil {
return nil, err
}
// return user
return u, nil
}

17
models/user_test.go Normal file
View File

@@ -0,0 +1,17 @@
package models
import (
"testing"
"github.com/arnaucube/gogame/database"
"github.com/stretchr/testify/assert"
)
func TestCreateUser(t *testing.T) {
db, err := database.New("127.0.0.1:27017", "gogametests")
assert.Nil(t, err)
user, err := NewUser(db, "user00", "password00", "user00@email.com")
assert.Nil(t, err)
assert.Equal(t, user.Name, "user00")
}