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.

116 lines
3.5 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. newPlanet := models.Planet{
  23. Size: size,
  24. Name: name,
  25. OwnerId: userId,
  26. }
  27. // in case that wants to start with resources plants
  28. // newPlanet.Buildings = make(map[string]int64)
  29. // newPlanet.Buildings["metalplant"] = 1
  30. // newPlanet.Buildings["crystalplant"] = 1
  31. // newPlanet.Buildings["deuteriumplant"] = 1
  32. // newPlanet.Buildings["energyplant"] = 1
  33. err := srv.db.Planets.Insert(newPlanet)
  34. if err != nil {
  35. return nil, nil, err
  36. }
  37. var planet *models.Planet
  38. err = srv.db.Planets.Find(bson.M{"name": name}).One(&planet)
  39. if err != nil {
  40. return nil, nil, err
  41. }
  42. // now put the planet in a solar system
  43. // get random solar system
  44. systemPosition := utils.RandInRange(0, constants.GALAXYSIZE)
  45. solarSystem, err := srv.PutPlanetInSolarSystem(systemPosition, planet)
  46. // 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
  47. return solarSystem, planet, err
  48. }
  49. func (srv Service) PutPlanetInSolarSystem(position int64, planet *models.Planet) (*models.SolarSystem, error) {
  50. var solarSystem models.SolarSystem
  51. err := srv.db.SolarSystems.Find(bson.M{"position": position}).One(&solarSystem)
  52. if err != nil {
  53. // solar system non existing yet
  54. // create a solarsystem with empty planets
  55. var emptyPlanets []string
  56. for i := 0; i < constants.SOLARSYSTEMSIZE; i++ {
  57. emptyPlanets = append(emptyPlanets, "empty")
  58. }
  59. newSolarSystem := models.SolarSystem{
  60. Position: position,
  61. Planets: emptyPlanets[:15],
  62. }
  63. err = srv.db.SolarSystems.Insert(newSolarSystem)
  64. if err != nil {
  65. return nil, err
  66. }
  67. err := srv.db.SolarSystems.Find(bson.M{"position": position}).One(&solarSystem)
  68. return &solarSystem, err
  69. }
  70. // get free slots in solarSystem
  71. posInSolarSystem := utils.RandInRange(0, constants.SOLARSYSTEMSIZE)
  72. if solarSystem.Planets[posInSolarSystem] != "" {
  73. // not empty slot, take another one TODO
  74. // if there are no empty slots, return error
  75. fmt.Println("not empty slot")
  76. }
  77. // store planet in solar system
  78. solarSystem.Planets[posInSolarSystem] = planet.Id.String()
  79. err = srv.db.SolarSystems.Update(bson.M{"position": position}, solarSystem)
  80. return &solarSystem, err
  81. }
  82. func (srv Service) UpgradeBuilding(user *models.User, planetid bson.ObjectId, building string) (*models.Planet, error) {
  83. // get planet
  84. var planet models.Planet
  85. err := srv.db.Planets.Find(bson.M{"_id": planetid}).One(&planet)
  86. if err != nil {
  87. return nil, err
  88. }
  89. // get current building level, and get the needed resources for next level
  90. resourcesNeeded := constants.BuildingsNeededResources[building][planet.Buildings[building]+1]
  91. // if user have enough resources to upgrade the building, upgrade the building
  92. err = user.SpendResources(resourcesNeeded)
  93. if err != nil {
  94. return nil, err
  95. }
  96. // upgrade level of building in planet
  97. planet.Buildings[building] += 1
  98. // store planet in db
  99. err = srv.db.Planets.Update(bson.M{"_id": planet.Id}, planet)
  100. return &planet, nil
  101. }