Browse Source

add get planet data

master
arnaucube 5 years ago
parent
commit
3bac070c04
5 changed files with 43 additions and 8 deletions
  1. +22
    -0
      endpoint/handlers.go
  2. +1
    -0
      endpoint/serve.go
  3. +7
    -7
      models/user.go
  4. +9
    -0
      services/gamesrv/gamesrv.go
  5. +4
    -1
      services/usersrv/usersrv.go

+ 22
- 0
endpoint/handlers.go

@ -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 {
PlanetId string
Building string

+ 1
- 0
endpoint/serve.go

@ -96,6 +96,7 @@ func newApiService() *gin.Engine {
api.GET("/", handleGetUser)
api.GET("/resources", handleGetResources)
api.GET("/planets", handleGetUserPlanets)
api.GET("/planets/:planetid", handleGetPlanet)
api.POST("/buildings", handlePostUpgradeBuilding)
}
return api

+ 7
- 7
models/user.go

@ -33,6 +33,7 @@ type User struct {
LastUpdated time.Time
db *database.Db
Resources Resources
Planets []bson.ObjectId
}
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,
db: db,
Resources: u.Resources,
Planets: u.Planets,
}
}
func (u *User) StoreInDb() error {
userDb := UserDb{
Id: u.Id,
Name: u.Name,
LastUpdated: time.Now(),
Resources: u.Resources,
}
err := u.db.Users.Update(bson.M{"_id": u.Id}, userDb)
err := u.db.Users.Update(bson.M{"_id": u.Id}, bson.M{"$set": bson.M{
"lastupdated": time.Now(),
"resources": u.Resources,
"planets": u.Planets,
}})
return err
}

+ 9
- 0
services/gamesrv/gamesrv.go

@ -92,6 +92,15 @@ func (srv Service) PutPlanetInSolarSystem(position int64, planet *models.Planet)
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) {
// get planet
var planet models.Planet

+ 4
- 1
services/usersrv/usersrv.go

@ -49,7 +49,10 @@ func (srv Service) Register(name, password, email string) (*models.User, error)
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
}

Loading…
Cancel
Save