db.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package main
  2. import (
  3. "log"
  4. "time"
  5. "gorm.io/driver/sqlite"
  6. "gorm.io/gorm"
  7. )
  8. type DataBase struct {
  9. DB *gorm.DB
  10. }
  11. func OpenDB(filename string) (*DataBase, error) {
  12. db, err := gorm.Open(sqlite.Open(filename), &gorm.Config{})
  13. if err != nil {
  14. return nil, err
  15. }
  16. DB := &DataBase{DB: db}
  17. err = DB.DB.AutoMigrate(&ShipModel{}, &Ship{}, &User{})
  18. return DB, err
  19. }
  20. func (db *DataBase) Close() error {
  21. sqldb, err := db.DB.DB()
  22. if err != nil {
  23. return err
  24. }
  25. return sqldb.Close()
  26. }
  27. type Model struct {
  28. ID uint64 `gorm:"primaryKey"`
  29. CreatedAt time.Time
  30. UpdatedAt time.Time
  31. }
  32. type ShipModel struct {
  33. Model
  34. Name string `gorm:"unique"`
  35. BaseHull uint64 // Armor Plates bump this, typically by 4 points per plate
  36. BaseShields uint64 // Capacitors bump this, typically by 3 points per cap
  37. // Buying costs and limits
  38. Purchaseable bool // Determines if the ShipModel can be bought
  39. RequiredXp uint64 // Preventing Users from purchasing very powerful ships without doing any combat
  40. CostMetal uint64 // Normally selling a ship in metal is 40%, if it can't be bought however it's 60%
  41. CostCircuit uint64 // Noormally selling a ship in circuits is 50%, if it can't be bought however it's 75%
  42. // Limits
  43. MaxGuns uint64
  44. MaxRocketLaunchers uint64
  45. MaxArmorPlates uint64
  46. MaxRepairBays uint64
  47. MaxShieldCapacitors uint64
  48. MaxShieldGenerators uint64
  49. MaxEngines uint64
  50. // Starting values, assigned to Ship once bought/created
  51. StartingGuns uint64
  52. StartingRocketLaunchers uint64
  53. StartingArmorPlates uint64
  54. StartingRepairBays uint64
  55. StartingRepairDrones uint64
  56. StartingShieldCapacitors uint64
  57. StartingShieldGenerators uint64
  58. StartingEngines uint64
  59. }
  60. func (db *DataBase) SaveShipModel(model *ShipModel) error {
  61. result := db.DB.Save(model)
  62. return result.Error
  63. }
  64. func (db *DataBase) GetShipModelById(id uint64) *ShipModel {
  65. var model *ShipModel
  66. result := db.DB.Find(model, id)
  67. if result.Error != nil {
  68. log.Printf("DataBase.GetShipModelById(id=%d) > %v", id, result.Error)
  69. return nil
  70. }
  71. return model
  72. }
  73. func (db *DataBase) GetShipModelByName(name string) *ShipModel {
  74. var model *ShipModel
  75. result := db.DB.Find(model, ShipModel{Name: name})
  76. if result.Error != nil {
  77. log.Printf("DataBase.GetShipModelByName(name='%s') > %v", name, result.Error)
  78. return nil
  79. }
  80. return model
  81. }
  82. type Ship struct {
  83. Model
  84. UserID uint64 // Who owns this ship
  85. ShipModel uint64 // ShipModel.ID, Limits of the ship
  86. HullPoints uint64 // Current HP
  87. ShieldPoints uint64 // Current SP
  88. Guns uint64 // Guns deal 0-2 damage per gun
  89. RocketLaunchers uint64 // A "special" attack, Each rocket deals 0-10 damage per rocket
  90. ArmorPlates uint64 // Bumps HP up
  91. RepairBays uint64 // Allows you to hold Repair Drones to repair HP in combat
  92. RepairDrones uint64 // Each repair drone can repair 0-1 HP per turn in combat (but if you take damage there is a chance to loose a drone instead)
  93. ShieldCapacitors uint64 // Bumps SP up, each Capacitor also adds a resistance to the amout of SP regained in combat
  94. ShieldGenerators uint64 // Regains SP of 0-1 SP per turn, it is calculated together then applies a resistance rating based on the number of capacitors installed
  95. Engines uint64 // Affects dodge and escape chances, "mass" is the number of installed parts
  96. }
  97. func (db *DataBase) SaveShip(ship *Ship) error {
  98. result := db.DB.Save(ship)
  99. return result.Error
  100. }
  101. type User struct {
  102. Model
  103. Name string `gorm:"unique"` // Nickname, commonly used
  104. RealName string `gorm:"UniqueIndex,colate:nocase"` // BBS Realname used to identify User, typically not used/displayed
  105. // Currency-likes
  106. Xp uint64 // How experienced the User is, determines how tough random opponents will be
  107. Metal uint64 // Common Currency
  108. Circuit uint64 // Rare Currency
  109. Rockets uint64 // Not a currency, a weapon, but there is technically no limit except the number that can be fired per "special" attack
  110. Fuel uint64 // Not a currency, amount of fights left (A normal fight will consume 2, a fight where you run away is 3, and a fight where you died consumes all but 1 and kicks you off)
  111. // Stuff
  112. Ship *Ship
  113. // Use UpdatedAt as LastOn
  114. }
  115. func (db *DataBase) SaveUser(u *User) error {
  116. result := db.DB.Save(u)
  117. return result.Error
  118. }
  119. func (db *DataBase) GetUserByName(name string) *User {
  120. var user *User
  121. result := db.DB.Find(user, User{RealName: name})
  122. if result.Error != nil {
  123. log.Printf("DataBase.GetUserByName(name='%s') > %v", name, result.Error)
  124. return nil
  125. }
  126. return user
  127. }