mirror of
https://github.com/arnaucube/gogame.git
synced 2026-02-07 03:26:39 +01:00
add some User functions, move api tests from js to python
This commit is contained in:
@@ -3,6 +3,7 @@ package gamesrv
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/arnaucube/gogame/constants"
|
||||
"github.com/arnaucube/gogame/database"
|
||||
"github.com/arnaucube/gogame/models"
|
||||
"github.com/arnaucube/gogame/utils"
|
||||
@@ -41,7 +42,7 @@ func (srv Service) CreatePlanet(userId bson.ObjectId) (*models.SolarSystem, *mod
|
||||
|
||||
// now put the planet in a solar system
|
||||
// get random solar system
|
||||
systemPosition := utils.RandInRange(0, models.GALAXYSIZE)
|
||||
systemPosition := utils.RandInRange(0, constants.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
|
||||
|
||||
@@ -54,9 +55,9 @@ func (srv Service) PutPlanetInSolarSystem(position int64, planet *models.Planet)
|
||||
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, "")
|
||||
var emptyPlanets []string
|
||||
for i := 0; i < constants.SOLARSYSTEMSIZE; i++ {
|
||||
emptyPlanets = append(emptyPlanets, "empty")
|
||||
}
|
||||
newSolarSystem := models.SolarSystem{
|
||||
Position: position,
|
||||
@@ -71,16 +72,15 @@ func (srv Service) PutPlanetInSolarSystem(position int64, planet *models.Planet)
|
||||
return &solarSystem, err
|
||||
}
|
||||
// get free slots in solarSystem
|
||||
posInSolarSystem := utils.RandInRange(0, models.SOLARSYSTEMSIZE)
|
||||
posInSolarSystem := utils.RandInRange(0, constants.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
|
||||
solarSystem.Planets[posInSolarSystem] = planet.Id.String()
|
||||
err = srv.db.SolarSystems.Update(bson.M{"position": position}, solarSystem)
|
||||
|
||||
return &solarSystem, err
|
||||
|
||||
}
|
||||
|
||||
21
services/gamesrv/gamesrv_test.go
Normal file
21
services/gamesrv/gamesrv_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package gamesrv
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/arnaucube/gogame/database"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
func TestCreatePlanet(t *testing.T) {
|
||||
db, err := database.New("127.0.0.1:27017", "gogametests")
|
||||
assert.Nil(t, err)
|
||||
srv := New(db)
|
||||
|
||||
solarSystem, planet, err := srv.CreatePlanet(bson.ObjectIdHex("5d029a6ff18ba24f406168fe"))
|
||||
assert.Nil(t, err)
|
||||
fmt.Println(solarSystem)
|
||||
fmt.Println(planet)
|
||||
}
|
||||
Reference in New Issue
Block a user