add CreatePlanet and put planet in solar system

This commit is contained in:
arnaucube
2019-06-13 22:41:47 +02:00
parent 0cd29328b4
commit 5965f2dec1
11 changed files with 141 additions and 45 deletions

View File

@@ -0,0 +1,86 @@
package gamesrv
import (
"fmt"
"github.com/arnaucube/gogame/database"
"github.com/arnaucube/gogame/models"
"github.com/arnaucube/gogame/utils"
"gopkg.in/mgo.v2/bson"
)
type Service struct {
db *database.Db
}
func New(db *database.Db) *Service {
return &Service{
db,
}
}
// CreatePlanet is used when a user conquers a planet
func (srv Service) CreatePlanet(userId bson.ObjectId) (*models.SolarSystem, *models.Planet, error) {
size := int64(250) // TODO get rand inside a range
name := "planetname" // TODO get random name
newPlanet := models.Planet{
Size: size,
Name: name,
OwnerId: userId,
}
err := srv.db.Planets.Insert(newPlanet)
if err != nil {
return nil, nil, err
}
var planet *models.Planet
err = srv.db.Planets.Find(bson.M{"name": name}).One(&planet)
if err != nil {
return nil, nil, err
}
// now put the planet in a solar system
// get random solar system
systemPosition := utils.RandInRange(0, models.GALAXYSIZE)
solarSystem, err := srv.PutPlanetInSolarSystem(systemPosition, planet)
// TODO if error is returned because there is no empty slots for planets in the solar system in systemPosition, get another systemPosition and try again
return solarSystem, planet, err
}
func (srv Service) PutPlanetInSolarSystem(position int64, planet *models.Planet) (*models.SolarSystem, error) {
var solarSystem models.SolarSystem
err := srv.db.SolarSystems.Find(bson.M{"position": position}).One(&solarSystem)
if err != nil {
// solar system non existing yet
// create a solarsystem with empty planets
var emptyPlanets []bson.ObjectId
for i := 0; i < models.SOLARSYSTEMSIZE; i++ {
emptyPlanets = append(emptyPlanets, "")
}
newSolarSystem := models.SolarSystem{
Position: position,
Planets: emptyPlanets[:15],
}
err = srv.db.SolarSystems.Insert(newSolarSystem)
if err != nil {
return nil, err
}
err := srv.db.SolarSystems.Find(bson.M{"position": position}).One(&solarSystem)
return &solarSystem, err
}
// get free slots in solarSystem
posInSolarSystem := utils.RandInRange(0, models.SOLARSYSTEMSIZE)
if solarSystem.Planets[posInSolarSystem] != "" {
// not empty slot, take another one TODO
// if there are no empty slots, return error
fmt.Println("not empty slot")
}
// store planet in solar system
solarSystem.Planets[posInSolarSystem] = planet.Id
err = srv.db.SolarSystems.Update(bson.M{"position": position}, solarSystem)
return &solarSystem, err
}

View File

@@ -57,15 +57,16 @@ func (srv Service) Register(name, password, email string) (*models.User, error)
var signingKey = []byte("TODO") // TODO
func (srv Service) Login(email, password string) (*models.User, error) {
func (srv Service) Login(email, password string) (*string, *models.User, error) {
var user models.User
err := srv.db.Users.Find(bson.M{"email": email}).One(&user)
if err != nil {
return nil, errors.New("user not exist")
return nil, nil, errors.New("user not exist")
}
if !checkPasswordHash(password, user.Password) {
return nil, errors.New("error with password")
return nil, nil, errors.New("error with password")
}
user.Password = ""
// create jwt
token := jwt.New(jwt.SigningMethodHS256)
@@ -76,12 +77,8 @@ func (srv Service) Login(email, password string) (*models.User, error) {
tokenString, err := token.SignedString(signingKey)
if err != nil {
return nil, errors.New("error creating token")
return nil, nil, errors.New("error creating token")
}
// TODO
// reuse Password parameter, to put there the token
user.Password = tokenString
return &user, err
return &tokenString, &user, err
}