@ -1,10 +0,0 @@ |
|||||
package models |
|
||||
|
|
||||
type Galaxy struct { |
|
||||
SolarSystem []string |
|
||||
} |
|
||||
|
|
||||
type SolarSystem struct { |
|
||||
Id string |
|
||||
Planets []string // array with ids of the planets
|
|
||||
} |
|
@ -1,8 +1,10 @@ |
|||||
package models |
package models |
||||
|
|
||||
|
import "gopkg.in/mgo.v2/bson" |
||||
|
|
||||
type Planet struct { |
type Planet struct { |
||||
Id string |
|
||||
Size int64 // fields
|
|
||||
|
Id bson.ObjectId `json:"id", bson:"_id, omitempty"` |
||||
|
Size int64 // fields/slots
|
||||
Name string |
Name string |
||||
OwnerId string |
|
||||
|
OwnerId bson.ObjectId |
||||
} |
} |
@ -0,0 +1,12 @@ |
|||||
|
package models |
||||
|
|
||||
|
import "gopkg.in/mgo.v2/bson" |
||||
|
|
||||
|
const GALAXYSIZE = 50 |
||||
|
const SOLARSYSTEMSIZE = 15 |
||||
|
|
||||
|
type SolarSystem struct { |
||||
|
Id bson.ObjectId `json:"id", bson:"_id, omitempty"` |
||||
|
Position int64 // position of the solar system in the galaxy, the maximum position is GALAXYSIZE-1
|
||||
|
Planets []bson.ObjectId // array with ids of the planets, if empty is equal to ""
|
||||
|
} |
@ -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 |
||||
|
|
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
package utils |
||||
|
|
||||
|
import "math/rand" |
||||
|
|
||||
|
func RandInRange(min, max int) int64 { |
||||
|
return int64(rand.Intn(max-min) + min) |
||||
|
} |