mirror of
https://github.com/arnaucube/gogame.git
synced 2026-02-06 19:16:40 +01:00
add get planet data
This commit is contained in:
@@ -121,6 +121,28 @@ func handleGetUserPlanets(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleGetPlanet(c *gin.Context) {
|
||||||
|
claims := jwt.ExtractClaims(c)
|
||||||
|
userid := bson.ObjectIdHex(claims[constants.JWTIdKey].(string))
|
||||||
|
planetid := c.Param("planetid")
|
||||||
|
|
||||||
|
user, err := userservice.GetUserById(userid)
|
||||||
|
if err != nil {
|
||||||
|
fail(c, err, "error on getting user")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
planet, err := gameservice.GetBuildings(user, bson.ObjectIdHex(planetid))
|
||||||
|
if err != nil {
|
||||||
|
fail(c, err, "error upgrading building")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"planet": planet,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type BuildMsg struct {
|
type BuildMsg struct {
|
||||||
PlanetId string
|
PlanetId string
|
||||||
Building string
|
Building string
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ func newApiService() *gin.Engine {
|
|||||||
api.GET("/", handleGetUser)
|
api.GET("/", handleGetUser)
|
||||||
api.GET("/resources", handleGetResources)
|
api.GET("/resources", handleGetResources)
|
||||||
api.GET("/planets", handleGetUserPlanets)
|
api.GET("/planets", handleGetUserPlanets)
|
||||||
|
api.GET("/planets/:planetid", handleGetPlanet)
|
||||||
api.POST("/buildings", handlePostUpgradeBuilding)
|
api.POST("/buildings", handlePostUpgradeBuilding)
|
||||||
}
|
}
|
||||||
return api
|
return api
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ type User struct {
|
|||||||
LastUpdated time.Time
|
LastUpdated time.Time
|
||||||
db *database.Db
|
db *database.Db
|
||||||
Resources Resources
|
Resources Resources
|
||||||
|
Planets []bson.ObjectId
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUser(db *database.Db, name, password, email string) (*User, error) {
|
func NewUser(db *database.Db, name, password, email string) (*User, error) {
|
||||||
@@ -64,17 +65,16 @@ func UserDbToUser(db *database.Db, u UserDb) *User {
|
|||||||
LastUpdated: u.LastUpdated,
|
LastUpdated: u.LastUpdated,
|
||||||
db: db,
|
db: db,
|
||||||
Resources: u.Resources,
|
Resources: u.Resources,
|
||||||
|
Planets: u.Planets,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) StoreInDb() error {
|
func (u *User) StoreInDb() error {
|
||||||
userDb := UserDb{
|
err := u.db.Users.Update(bson.M{"_id": u.Id}, bson.M{"$set": bson.M{
|
||||||
Id: u.Id,
|
"lastupdated": time.Now(),
|
||||||
Name: u.Name,
|
"resources": u.Resources,
|
||||||
LastUpdated: time.Now(),
|
"planets": u.Planets,
|
||||||
Resources: u.Resources,
|
}})
|
||||||
}
|
|
||||||
err := u.db.Users.Update(bson.M{"_id": u.Id}, userDb)
|
|
||||||
return err
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,6 +92,15 @@ func (srv Service) PutPlanetInSolarSystem(position int64, planet *models.Planet)
|
|||||||
return &solarSystem, err
|
return &solarSystem, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (srv Service) GetBuildings(user *models.User, planetid bson.ObjectId) (*models.Planet, error) {
|
||||||
|
var planet models.Planet
|
||||||
|
err := srv.db.Planets.Find(bson.M{"_id": planetid, "ownerid": user.Id}).One(&planet)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &planet, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (srv Service) UpgradeBuilding(user *models.User, planetid bson.ObjectId, building string) (*models.Planet, error) {
|
func (srv Service) UpgradeBuilding(user *models.User, planetid bson.ObjectId, building string) (*models.Planet, error) {
|
||||||
// get planet
|
// get planet
|
||||||
var planet models.Planet
|
var planet models.Planet
|
||||||
|
|||||||
@@ -49,7 +49,10 @@ func (srv Service) Register(name, password, email string) (*models.User, error)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = srv.gamesrv.CreatePlanet(user.Id)
|
_, planet, err := srv.gamesrv.CreatePlanet(user.Id)
|
||||||
|
|
||||||
|
user.Planets = append(user.Planets, planet.Id)
|
||||||
|
user.StoreInDb()
|
||||||
|
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user