db.go 5.2 KB

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