You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.9 KiB

  1. package gamesrv
  2. import (
  3. "fmt"
  4. "github.com/arnaucube/gogame/constants"
  5. "github.com/arnaucube/gogame/database"
  6. "github.com/arnaucube/gogame/models"
  7. "github.com/arnaucube/gogame/utils"
  8. "gopkg.in/mgo.v2/bson"
  9. )
  10. type Service struct {
  11. db *database.Db
  12. }
  13. func New(db *database.Db) *Service {
  14. return &Service{
  15. db,
  16. }
  17. }
  18. // CreatePlanet is used when a user conquers a planet
  19. func (srv Service) CreatePlanet(userId bson.ObjectId) (*models.SolarSystem, *models.Planet, error) {
  20. size := int64(250) // TODO get rand inside a range
  21. name := "planetname" // TODO get random name
  22. planet, err := models.NewPlanet(srv.db, size, name, userId)
  23. if err != nil {
  24. return nil, nil, err
  25. }
  26. // now put the planet in a solar system
  27. // get random solar system
  28. systemPosition := utils.RandInRange(0, constants.GALAXYSIZE)
  29. solarSystem, err := srv.PutPlanetInSolarSystem(systemPosition, planet)
  30. // 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
  31. return solarSystem, planet, err
  32. }
  33. func (srv Service) PutPlanetInSolarSystem(position int64, planet *models.Planet) (*models.SolarSystem, error) {
  34. var solarSystem models.SolarSystem
  35. err := srv.db.SolarSystems.Find(bson.M{"position": position}).One(&solarSystem)
  36. if err != nil {
  37. // solar system non existing yet
  38. // create a solarsystem with empty planets
  39. var emptyPlanets []string
  40. for i := 0; i < constants.SOLARSYSTEMSIZE; i++ {
  41. emptyPlanets = append(emptyPlanets, "empty")
  42. }
  43. newSolarSystem := models.SolarSystem{
  44. Position: position,
  45. Planets: emptyPlanets[:15],
  46. }
  47. err = srv.db.SolarSystems.Insert(newSolarSystem)
  48. if err != nil {
  49. return nil, err
  50. }
  51. err := srv.db.SolarSystems.Find(bson.M{"position": position}).One(&solarSystem)
  52. return &solarSystem, err
  53. }
  54. // get free slots in solarSystem
  55. posInSolarSystem := utils.RandInRange(0, constants.SOLARSYSTEMSIZE)
  56. if solarSystem.Planets[posInSolarSystem] != "" {
  57. // not empty slot, take another one TODO
  58. // if there are no empty slots, return error
  59. fmt.Println("not empty slot")
  60. }
  61. // store planet in solar system
  62. solarSystem.Planets[posInSolarSystem] = planet.Id.String()
  63. err = srv.db.SolarSystems.Update(bson.M{"position": position}, solarSystem)
  64. return &solarSystem, err
  65. }
  66. func (srv Service) GetPlanet(user *models.User, planetid bson.ObjectId) (*models.Planet, error) {
  67. var planet models.Planet
  68. err := srv.db.Planets.Find(bson.M{"_id": planetid, "ownerid": user.Id}).One(&planet)
  69. if err != nil {
  70. return nil, err
  71. }
  72. planet.Db = srv.db
  73. _, err = planet.CheckCurrentBuild()
  74. if err != nil {
  75. return nil, err
  76. }
  77. _, err = planet.GetResources()
  78. return &planet, err
  79. }
  80. func (srv Service) UpgradeBuilding(user *models.User, planetid bson.ObjectId, building string) (*models.Planet, error) {
  81. // get planet
  82. planet, err := srv.GetPlanet(user, planetid)
  83. if err != nil {
  84. return nil, err
  85. }
  86. err = planet.UpgradeBuilding(building)
  87. return planet, err
  88. }