mirror of
https://github.com/arnaucube/gogame.git
synced 2026-02-06 19:16:40 +01:00
add UpgradeBuilding functionallity
This commit is contained in:
@@ -35,7 +35,7 @@ func start(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
apiService := endpoint.Serve(config.C, db, userservice)
|
apiService := endpoint.Serve(config.C, db, userservice, gameservice)
|
||||||
apiService.Run(config.C.Server.ServiceApi)
|
apiService.Run(config.C.Server.ServiceApi)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package constants
|
package constants
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/arnaucube/gogame/models"
|
||||||
|
)
|
||||||
|
|
||||||
// game constants
|
// game constants
|
||||||
|
|
||||||
const GALAXYSIZE = 50
|
const GALAXYSIZE = 50
|
||||||
@@ -31,3 +35,28 @@ var EnergyMineLevels = []int64{
|
|||||||
5,
|
5,
|
||||||
10,
|
10,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BuildingsNeededResources hold
|
||||||
|
// map with all the buildings, that each one is a map with the levels of the buildings with the needed ressources
|
||||||
|
var BuildingsNeededResources = map[string]map[int64]models.Resources{
|
||||||
|
"metalplant": map[int64]models.Resources{
|
||||||
|
1: models.Resources{
|
||||||
|
Metal: 50,
|
||||||
|
Crystal: 50,
|
||||||
|
Deuterium: 50,
|
||||||
|
Energy: 50,
|
||||||
|
},
|
||||||
|
2: models.Resources{
|
||||||
|
Metal: 70,
|
||||||
|
Crystal: 70,
|
||||||
|
Deuterium: 70,
|
||||||
|
Energy: 70,
|
||||||
|
},
|
||||||
|
3: models.Resources{
|
||||||
|
Metal: 90,
|
||||||
|
Crystal: 90,
|
||||||
|
Deuterium: 90,
|
||||||
|
Energy: 90,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package endpoint
|
package endpoint
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"gopkg.in/mgo.v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func fail(c *gin.Context, err error, msg string) {
|
func fail(c *gin.Context, err error, msg string) {
|
||||||
@@ -64,14 +67,14 @@ func handleLogin(c *gin.Context) {
|
|||||||
func handleGetResources(c *gin.Context) {
|
func handleGetResources(c *gin.Context) {
|
||||||
userid := c.Param("userid")
|
userid := c.Param("userid")
|
||||||
|
|
||||||
user, err := userservice.GetUserById(userid)
|
user, err := userservice.GetUserById(bson.ObjectIdHex(userid))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(c, err, "error on getting user")
|
fail(c, err, "error on getting user")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resources, err := user.GetResources()
|
resources, err := user.GetResources()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(c, err, "error on getting user")
|
fail(c, err, "error on getting user resources")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,3 +83,49 @@ func handleGetResources(c *gin.Context) {
|
|||||||
"resources": resources,
|
"resources": resources,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleGetUserPlanets(c *gin.Context) {
|
||||||
|
userid := c.Param("userid")
|
||||||
|
|
||||||
|
planets, err := userservice.GetUserPlanetsById(bson.ObjectIdHex(userid))
|
||||||
|
if err != nil {
|
||||||
|
fail(c, err, "error on getting user planets")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"planets": planets,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type BuildMsg struct {
|
||||||
|
PlanetId string
|
||||||
|
Building string
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlePostUpgradeBuilding(c *gin.Context) {
|
||||||
|
userid := c.Param("userid")
|
||||||
|
var buildMsg BuildMsg
|
||||||
|
err := c.BindJSON(&buildMsg)
|
||||||
|
if err != nil {
|
||||||
|
fail(c, err, "error parsing json")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(buildMsg)
|
||||||
|
|
||||||
|
user, err := userservice.GetUserById(bson.ObjectIdHex(userid))
|
||||||
|
if err != nil {
|
||||||
|
fail(c, err, "error on getting user")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
planet, err := gameservice.UpgradeBuilding(user, bson.ObjectIdHex(buildMsg.PlanetId), buildMsg.Building)
|
||||||
|
if err != nil {
|
||||||
|
fail(c, err, "error upgrading building")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"planet": planet,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package endpoint
|
|||||||
import (
|
import (
|
||||||
"github.com/arnaucube/gogame/config"
|
"github.com/arnaucube/gogame/config"
|
||||||
"github.com/arnaucube/gogame/database"
|
"github.com/arnaucube/gogame/database"
|
||||||
|
"github.com/arnaucube/gogame/services/gamesrv"
|
||||||
"github.com/arnaucube/gogame/services/usersrv"
|
"github.com/arnaucube/gogame/services/usersrv"
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -11,6 +12,7 @@ import (
|
|||||||
var serverConfig config.Config
|
var serverConfig config.Config
|
||||||
var db *database.Db
|
var db *database.Db
|
||||||
var userservice *usersrv.Service
|
var userservice *usersrv.Service
|
||||||
|
var gameservice *gamesrv.Service
|
||||||
|
|
||||||
func newApiService() *gin.Engine {
|
func newApiService() *gin.Engine {
|
||||||
api := gin.Default()
|
api := gin.Default()
|
||||||
@@ -22,12 +24,15 @@ func newApiService() *gin.Engine {
|
|||||||
|
|
||||||
// TODO add jwt checker
|
// TODO add jwt checker
|
||||||
api.GET("/resources/:userid", handleGetResources)
|
api.GET("/resources/:userid", handleGetResources)
|
||||||
|
api.GET("/planets/:userid", handleGetUserPlanets)
|
||||||
|
api.POST("/buildings/:userid", handlePostUpgradeBuilding)
|
||||||
return api
|
return api
|
||||||
}
|
}
|
||||||
|
|
||||||
func Serve(cnfg config.Config, _db *database.Db, _userservice *usersrv.Service) *gin.Engine {
|
func Serve(cnfg config.Config, _db *database.Db, _userservice *usersrv.Service, _gameservice *gamesrv.Service) *gin.Engine {
|
||||||
serverConfig = cnfg
|
serverConfig = cnfg
|
||||||
db = _db
|
db = _db
|
||||||
userservice = _userservice
|
userservice = _userservice
|
||||||
|
gameservice = _gameservice
|
||||||
return newApiService()
|
return newApiService()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ package models
|
|||||||
|
|
||||||
import "gopkg.in/mgo.v2/bson"
|
import "gopkg.in/mgo.v2/bson"
|
||||||
|
|
||||||
type BuildingsList struct {
|
/*
|
||||||
|
BuildingsList
|
||||||
MetalMine int64
|
MetalMine int64
|
||||||
CrystalMine int64
|
CrystalMine int64
|
||||||
DeuteriumMine int64
|
DeuteriumMine int64
|
||||||
@@ -11,11 +12,11 @@ type BuildingsList struct {
|
|||||||
RoboticsFactory int64
|
RoboticsFactory int64
|
||||||
Shipyard int64
|
Shipyard int64
|
||||||
RessearchLab int64
|
RessearchLab int64
|
||||||
}
|
*/
|
||||||
type Planet struct {
|
type Planet struct {
|
||||||
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
|
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
|
||||||
Size int64 // fields/slots
|
Size int64 // fields/slots
|
||||||
Name string
|
Name string
|
||||||
OwnerId bson.ObjectId
|
OwnerId bson.ObjectId
|
||||||
Buildings BuildingsList
|
Buildings map[string]int64
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -42,6 +43,12 @@ func NewUser(db *database.Db, name, password, email string) (*User, error) {
|
|||||||
Password: password,
|
Password: password,
|
||||||
Email: email,
|
Email: email,
|
||||||
LastUpdated: time.Now(),
|
LastUpdated: time.Now(),
|
||||||
|
Resources: Resources{
|
||||||
|
Metal: 500,
|
||||||
|
Crystal: 500,
|
||||||
|
Deuterium: 500,
|
||||||
|
Energy: 500,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
err := db.Users.Insert(newUser)
|
err := db.Users.Insert(newUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -117,10 +124,10 @@ func (u *User) GetResources() (*Resources, error) {
|
|||||||
for _, planet := range planets {
|
for _, planet := range planets {
|
||||||
fmt.Println("planet", planet)
|
fmt.Println("planet", planet)
|
||||||
// TODO find correct formulas
|
// TODO find correct formulas
|
||||||
metalGrowth = metalGrowth + ((1 + planet.Buildings.MetalMine) * int64(delta))
|
metalGrowth = metalGrowth + ((1 + planet.Buildings["metalmine"]) * int64(delta))
|
||||||
crystalGrowth = crystalGrowth + ((1 * planet.Buildings.CrystalMine) * int64(delta))
|
crystalGrowth = crystalGrowth + ((1 + planet.Buildings["crystalmine"]) * int64(delta))
|
||||||
deuteriumGrowth = deuteriumGrowth + ((1 * planet.Buildings.DeuteriumMine) * int64(delta))
|
deuteriumGrowth = deuteriumGrowth + ((1 + planet.Buildings["deuteriummine"]) * int64(delta))
|
||||||
energyGrowth = energyGrowth + ((1 * planet.Buildings.EnergyMine) * int64(delta))
|
energyGrowth = energyGrowth + ((1 + planet.Buildings["energymine"]) * int64(delta))
|
||||||
}
|
}
|
||||||
// calculate newAmount = oldAmount + growth
|
// calculate newAmount = oldAmount + growth
|
||||||
u.Resources.Metal = u.Resources.Metal + metalGrowth
|
u.Resources.Metal = u.Resources.Metal + metalGrowth
|
||||||
@@ -137,3 +144,34 @@ func (u *User) GetResources() (*Resources, error) {
|
|||||||
// return user
|
// return user
|
||||||
return &u.Resources, nil
|
return &u.Resources, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SpendResources checks if user has enough resources, then substracts the resources, and updates the amounts in the database
|
||||||
|
func (u *User) SpendResources(r Resources) error {
|
||||||
|
err := u.GetFromDb()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if u.Resources.Metal < r.Metal {
|
||||||
|
return errors.New("not enough metal resources")
|
||||||
|
}
|
||||||
|
if u.Resources.Crystal < r.Crystal {
|
||||||
|
return errors.New("not enough crystal resources")
|
||||||
|
}
|
||||||
|
if u.Resources.Deuterium < r.Deuterium {
|
||||||
|
return errors.New("not enough deuterium resources")
|
||||||
|
}
|
||||||
|
if u.Resources.Energy < r.Energy {
|
||||||
|
return errors.New("not enough energy resources")
|
||||||
|
}
|
||||||
|
|
||||||
|
u.Resources.Metal = u.Resources.Metal - r.Metal
|
||||||
|
u.Resources.Crystal = u.Resources.Crystal - r.Crystal
|
||||||
|
u.Resources.Deuterium = u.Resources.Deuterium - r.Deuterium
|
||||||
|
u.Resources.Energy = u.Resources.Energy - r.Energy
|
||||||
|
|
||||||
|
err = u.StoreInDb()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,13 +29,14 @@ func (srv Service) CreatePlanet(userId bson.ObjectId) (*models.SolarSystem, *mod
|
|||||||
Size: size,
|
Size: size,
|
||||||
Name: name,
|
Name: name,
|
||||||
OwnerId: userId,
|
OwnerId: userId,
|
||||||
Buildings: models.BuildingsList{
|
|
||||||
MetalMine: 1,
|
|
||||||
CrystalMine: 1,
|
|
||||||
DeuteriumMine: 1,
|
|
||||||
EnergyMine: 1,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
// in case that wants to start with resources plants
|
||||||
|
// newPlanet.Buildings = make(map[string]int64)
|
||||||
|
// newPlanet.Buildings["metalplant"] = 1
|
||||||
|
// newPlanet.Buildings["crystalplant"] = 1
|
||||||
|
// newPlanet.Buildings["deuteriumplant"] = 1
|
||||||
|
// newPlanet.Buildings["energyplant"] = 1
|
||||||
|
|
||||||
err := srv.db.Planets.Insert(newPlanet)
|
err := srv.db.Planets.Insert(newPlanet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@@ -90,3 +91,26 @@ func (srv Service) PutPlanetInSolarSystem(position int64, planet *models.Planet)
|
|||||||
|
|
||||||
return &solarSystem, err
|
return &solarSystem, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (srv Service) UpgradeBuilding(user *models.User, planetid bson.ObjectId, building string) (*models.Planet, error) {
|
||||||
|
// get planet
|
||||||
|
var planet models.Planet
|
||||||
|
err := srv.db.Planets.Find(bson.M{"_id": planetid}).One(&planet)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// get current building level, and get the needed resources for next level
|
||||||
|
resourcesNeeded := constants.BuildingsNeededResources[building][planet.Buildings[building]+1]
|
||||||
|
|
||||||
|
// if user have enough resources to upgrade the building, upgrade the building
|
||||||
|
err = user.SpendResources(resourcesNeeded)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// upgrade level of building in planet
|
||||||
|
planet.Buildings[building] += 1
|
||||||
|
// store planet in db
|
||||||
|
err = srv.db.Planets.Update(bson.M{"_id": planet.Id}, planet)
|
||||||
|
return &planet, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -82,15 +82,24 @@ func (srv Service) Login(email, password string) (*string, *models.User, error)
|
|||||||
return &tokenString, user, err
|
return &tokenString, user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv Service) GetUserById(userid string) (*models.User, error) {
|
func (srv Service) GetUserById(userid bson.ObjectId) (*models.User, error) {
|
||||||
var userDb models.UserDb
|
var userDb models.UserDb
|
||||||
err := srv.db.Users.Find(bson.M{"_id": bson.ObjectIdHex(userid)}).One(&userDb)
|
err := srv.db.Users.Find(bson.M{"_id": userid}).One(&userDb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return models.UserDbToUser(srv.db, userDb), nil
|
return models.UserDbToUser(srv.db, userDb), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (srv Service) GetUserPlanetsById(userid bson.ObjectId) ([]*models.Planet, error) {
|
||||||
|
var planets []*models.Planet
|
||||||
|
err := srv.db.Planets.Find(bson.M{"ownerid": userid}).All(&planets)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return planets, err
|
||||||
|
}
|
||||||
|
|
||||||
// func (srv Service) GetUser(id bson.ObjectId) (*models.User, error) {
|
// func (srv Service) GetUser(id bson.ObjectId) (*models.User, error) {
|
||||||
// // update user stats
|
// // update user stats
|
||||||
// user := getUserFromDB
|
// user := getUserFromDB
|
||||||
|
|||||||
32
test/test.py
32
test/test.py
@@ -41,11 +41,41 @@ t.rStatus("get /resources", r)
|
|||||||
jsonR = r.json()
|
jsonR = r.json()
|
||||||
print(jsonR)
|
print(jsonR)
|
||||||
|
|
||||||
time.sleep(4)
|
time.sleep(1)
|
||||||
r = requests.get(URL + "/resources/"+ userid)
|
r = requests.get(URL + "/resources/"+ userid)
|
||||||
t.rStatus("get /resources", r)
|
t.rStatus("get /resources", r)
|
||||||
jsonR = r.json()
|
jsonR = r.json()
|
||||||
print(jsonR)
|
print(jsonR)
|
||||||
|
|
||||||
|
r = requests.get(URL + "/resources/"+ userid)
|
||||||
|
t.rStatus("get /resources", r)
|
||||||
|
jsonR = r.json()
|
||||||
|
print(jsonR)
|
||||||
|
|
||||||
|
r = requests.get(URL + "/planets/"+userid)
|
||||||
|
t.rStatus("post /planets/:userid", r)
|
||||||
|
jsonR = r.json()
|
||||||
|
print(jsonR)
|
||||||
|
print(jsonR["planets"][0])
|
||||||
|
planetid = jsonR["planets"][0]["id"]
|
||||||
|
|
||||||
|
d = {
|
||||||
|
"planetid": planetid,
|
||||||
|
"building": "metalplant",
|
||||||
|
}
|
||||||
|
r = requests.post(URL + "/buildings/"+userid, json=d)
|
||||||
|
t.rStatus("post /building/:userid", r)
|
||||||
|
jsonR = r.json()
|
||||||
|
print(jsonR)
|
||||||
|
|
||||||
|
d = {
|
||||||
|
"planetid": planetid,
|
||||||
|
"building": "ressearchlab",
|
||||||
|
}
|
||||||
|
r = requests.post(URL + "/buildings/"+userid, json=d)
|
||||||
|
t.rStatus("post /building/:userid", r)
|
||||||
|
jsonR = r.json()
|
||||||
|
print(jsonR)
|
||||||
|
|
||||||
t.printScores()
|
t.printScores()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user