Browse Source

add points for buildings

master
arnaucube 5 years ago
parent
commit
6226d47791
4 changed files with 66 additions and 9 deletions
  1. +1
    -0
      README.md
  2. +10
    -2
      endpoint/handlers.go
  3. +37
    -7
      models/planet.go
  4. +18
    -0
      models/user.go

+ 1
- 0
README.md

@ -20,6 +20,7 @@ The frontend is in https://github.com/arnaucube/gogame-frontend
- [x] mongodb connectors
- [x] buildings engine
- [x] resources engine
- [x] points
- [ ] costs calculators
- [x] mining growth from mines/buildings
- [x] building costs

+ 10
- 2
endpoint/handlers.go

@ -34,7 +34,11 @@ type RegisterMsg struct {
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 @@ type LoginMsg struct {
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")

+ 37
- 7
models/planet.go

@ -2,6 +2,7 @@ package models
import (
"errors"
"fmt"
"strconv"
"time"
@ -50,10 +51,11 @@ type Planet struct {
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 @@ func NewPlanet(db *database.Db, size int64, name string, ownerId bson.ObjectId)
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 @@ func (p *Planet) GetResources() (*Resources, error) {
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 @@ func (p *Planet) CheckCurrentBuild() (bool, error) {
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 @@ func (p *Planet) CheckCurrentBuild() (bool, error) {
return false, nil
}
func (p *Planet) UpgradeBuilding(building string) error {
busy, err := p.CheckCurrentBuild()
if err != nil {
@ -244,7 +263,7 @@ func (p *Planet) UpgradeBuilding(building string) error {
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 @@ func (p *Planet) UpgradeBuilding(building string) error {
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 @@ func (p *Planet) UpgradeBuilding(building string) error {
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
}

+ 18
- 0
models/user.go

@ -15,6 +15,7 @@ type UserDb struct {
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 @@ type User struct {
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 @@ func NewUser(db *database.Db, name, password, email string) (*User, error) {
Password: password,
Email: email,
LastUpdated: time.Now(),
Points: 0,
}
err := db.Users.Insert(newUser)
if err != nil {
@ -49,6 +52,7 @@ func UserDbToUser(db *database.Db, u UserDb) *User {
LastUpdated: u.LastUpdated,
db: db,
Planets: u.Planets,
Points: u.Points,
}
}
@ -79,3 +83,17 @@ func (u *User) GetPlanets() ([]Planet, error) {
}
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
}

Loading…
Cancel
Save