arnaucube
arnaucube/gogame-oldPublic Self-hosted repository
add points for buildings
arnaucube arnaucube@gmail.com
Wed, 19 Jun 2019 22:32:44 +0200
commit

6226d4779102137a290ed561d221765a6b6617f6

parent

992894c87bfdb565d90ff09b774afb07553c5896

4 files changed, 66 insertions(+), 9 deletions(-)

jump to
M README.md → README.md

@@ -20,6 +20,7 @@ - [ ] ships

- [x] mongodb connectors - [x] buildings engine - [x] resources engine +- [x] points - [ ] costs calculators - [x] mining growth from mines/buildings - [x] building costs
M endpoint/handlers.go → endpoint/handlers.go

@@ -34,7 +34,11 @@ }

func handleRegister(c *gin.Context) { var registerMsg RegisterMsg - c.BindJSON(&registerMsg) + err := c.BindJSON(&registerMsg) + if err != nil { + fail(c, err, "error on json") + return + } user, err := userservice.Register(registerMsg.Name, registerMsg.Password, registerMsg.Email) if err != nil { fail(c, err, "error on register")

@@ -52,7 +56,11 @@ }

func handleLogin(c *gin.Context) { var loginMsg LoginMsg - c.BindJSON(&loginMsg) + err := c.BindJSON(&loginMsg) + if err != nil { + fail(c, err, "error on json") + return + } token, user, err := userservice.Login(loginMsg.Email, loginMsg.Password) if err != nil { fail(c, err, "error on login")
M models/planet.go → models/planet.go

@@ -2,6 +2,7 @@ package models

import ( "errors" + "fmt" "strconv" "time"

@@ -50,10 +51,11 @@ }

func NewPlanet(db *database.Db, size int64, name string, ownerId bson.ObjectId) (*Planet, error) { newPlanet := Planet{ - Db: db, - Size: size, - Name: name, - OwnerId: ownerId, + Db: db, + LastUpdated: time.Now(), + Size: size, + Name: name, + OwnerId: ownerId, Resources: Resources{ Metal: 500, Crystal: 500,

@@ -73,12 +75,14 @@ err := db.Planets.Insert(newPlanet)

if err != nil { return nil, err } + fmt.Println(newPlanet.Resources) var planet *Planet err = db.Planets.Find(bson.M{"ownerid": newPlanet.OwnerId}).One(&planet) if err != nil { return nil, err } + fmt.Println(planet.Resources) return planet, nil }

@@ -132,7 +136,6 @@ if err != nil {

return nil, err } - // return user return &p.Resources, nil }

@@ -208,6 +211,21 @@ func (p *Planet) CheckCurrentBuild() (bool, error) {

if p.CurrentBuild.Title != "" { // the planet is building something, check if has ended if p.CurrentBuild.Ends.Unix() < time.Now().Unix() { + // add points for resources spend + resourcesNeeded, err := p.GetBuildingCost(p.CurrentBuild.Building) + if err != nil { + return true, err + } + + // add points for resources used (each 1000 units, 1 point + points := ResourcesToPoints(resourcesNeeded) + // 1000 point is the 1 point for the building + points += 1000 + err = AddPoints(p.Db, p.OwnerId, points) + if err != nil { + return true, err + } + // upgrade level of building in planet p.Buildings[p.CurrentBuild.Building] += 1

@@ -216,7 +234,7 @@ p.CurrentBuild.Title = ""

p.CurrentBuild.Building = "" // store in db - err := p.Db.Planets.Update(bson.M{"_id": p.Id}, p) + err = p.Db.Planets.Update(bson.M{"_id": p.Id}, p) if err != nil { return true, err }

@@ -229,6 +247,7 @@ }

return false, nil } + func (p *Planet) UpgradeBuilding(building string) error { busy, err := p.CheckCurrentBuild() if err != nil {

@@ -244,7 +263,7 @@ if err != nil {

return err } // get time cost of the build - timei64 := ConstructionTime(resourcesNeeded, p.Buildings[building]+1) + timei64 := ConstructionTime(resourcesNeeded, p.Buildings["roboticsfactory"]) endsTime := time.Now().Add(time.Second * time.Duration(timei64)) // if user have enough resources to upgrade the building, upgrade the building

@@ -252,6 +271,7 @@ err = p.SpendResources(resourcesNeeded)

if err != nil { return err } + // add current task to planet p.CurrentBuild.Building = building p.CurrentBuild.Title = building + " - Level " + strconv.Itoa(int(p.Buildings[building]))

@@ -262,3 +282,13 @@ // store planet in db

err = p.Db.Planets.Update(bson.M{"_id": p.Id}, p) return nil } + +func ResourcesToPoints(r Resources) int64 { + p := int64(0) + p = p + r.Metal + p = p + r.Crystal + p = p + r.Deuterium + p = p + r.Energy + fmt.Println("p", p) + return p +}
M models/user.go → models/user.go

@@ -15,6 +15,7 @@ Password string

Email string LastUpdated time.Time Planets []bson.ObjectId + Points int64 // real points are this value / 1000 } // User is the data in memory, after getting it from DB

@@ -24,6 +25,7 @@ Id bson.ObjectId `json:"id" bson:"_id,omitempty"`

Name string LastUpdated time.Time Planets []bson.ObjectId + Points int64 } func NewUser(db *database.Db, name, password, email string) (*User, error) {

@@ -33,6 +35,7 @@ Name: name,

Password: password, Email: email, LastUpdated: time.Now(), + Points: 0, } err := db.Users.Insert(newUser) if err != nil {

@@ -49,6 +52,7 @@ Name: u.Name,

LastUpdated: u.LastUpdated, db: db, Planets: u.Planets, + Points: u.Points, } }

@@ -79,3 +83,17 @@ return planets, err

} return planets, nil } + +func AddPoints(db *database.Db, userId bson.ObjectId, points int64) error { + var userDb UserDb + err := db.Users.Find(bson.M{"_id": userId}).One(&userDb) + if err != nil { + return err + } + newPoints := userDb.Points + points + + err = db.Users.Update(bson.M{"_id": userId}, bson.M{"$set": bson.M{ + "points": newPoints, + }}) + return err +}