Explorar el Código

Added some db models, started main

The doorgo library is now in the README under the Build steps
david hace 11 meses
padre
commit
4ef4471026
Se han modificado 6 ficheros con 292 adiciones y 2 borrados
  1. 1 0
      .gitignore
  2. 12 2
      README.md
  3. 149 0
      db.go
  4. 17 0
      go.mod
  5. 16 0
      go.sum
  6. 97 0
      main.go

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+doorgo/

+ 12 - 2
README.md

@@ -1,3 +1,13 @@
-# Space-Construct
+# Space Construct
 
-A Space Game! In Go!
+A Space Game! In Go!
+
+- [Repo](https://git.red-green.com/david/Space-Construct)
+- [Issues](https://git.red-green.com/david/Space-Construct/issues)
+
+## Building
+
+1. Clone this repo
+2. Clone the [doorgo](https://git.red-green.com/RedGreen/doorgo) lib
+3. Run `go build`
+4. Profit

+ 149 - 0
db.go

@@ -0,0 +1,149 @@
+package main
+
+import (
+	"log"
+	"time"
+
+	"gorm.io/driver/sqlite"
+	"gorm.io/gorm"
+)
+
+type DataBase struct {
+	DB *gorm.DB
+}
+
+func OpenDB(filename string) (*DataBase, error) {
+	db, err := gorm.Open(sqlite.Open(filename), &gorm.Config{})
+	if err != nil {
+		return nil, err
+	}
+	DB := &DataBase{DB: db}
+	err = DB.DB.AutoMigrate(&ShipModel{}, &Ship{}, &User{})
+	return DB, err
+}
+
+func (db *DataBase) Close() error {
+	sqldb, err := db.DB.DB()
+	if err != nil {
+		return err
+	}
+	return sqldb.Close()
+}
+
+type Model struct {
+	ID        uint64 `gorm:"primaryKey"`
+	CreatedAt time.Time
+	UpdatedAt time.Time
+}
+
+type ShipModel struct {
+	Model
+	Name string `gorm:"unique"`
+
+	BaseHull    uint64 // Armor Plates bump this, typically by 4 points per plate
+	BaseShields uint64 // Capacitors bump this, typically by 3 points per cap
+
+	// Buying costs and limits
+	Purchaseable bool   // Determines if the ShipModel can be bought
+	RequiredXp   uint64 // Preventing Users from purchasing very powerful ships without doing any combat
+	CostMetal    uint64 // Normally selling a ship in metal is 40%, if it can't be bought however it's 60%
+	CostCircuit  uint64 // Noormally selling a ship in circuits is 50%, if it can't be bought however it's 75%
+
+	// Limits
+	MaxGuns             uint64
+	MaxRocketLaunchers  uint64
+	MaxArmorPlates      uint64
+	MaxRepairBays       uint64
+	MaxShieldCapacitors uint64
+	MaxShieldGenerators uint64
+	MaxEngines          uint64
+
+	// Starting values, assigned to Ship once bought/created
+	StartingGuns             uint64
+	StartingRocketLaunchers  uint64
+	StartingArmorPlates      uint64
+	StartingRepairBays       uint64
+	StartingRepairDrones     uint64
+	StartingShieldCapacitors uint64
+	StartingShieldGenerators uint64
+	StartingEngines          uint64
+}
+
+func (db *DataBase) SaveShipModel(model *ShipModel) error {
+	result := db.DB.Save(model)
+	return result.Error
+}
+
+func (db *DataBase) GetShipModelById(id uint64) *ShipModel {
+	var model *ShipModel
+	result := db.DB.Find(model, id)
+	if result.Error != nil {
+		log.Printf("DataBase.GetShipModelById(id=%d) > %v", id, result.Error)
+		return nil
+	}
+	return model
+}
+
+func (db *DataBase) GetShipModelByName(name string) *ShipModel {
+	var model *ShipModel
+	result := db.DB.Find(model, ShipModel{Name: name})
+	if result.Error != nil {
+		log.Printf("DataBase.GetShipModelByName(name='%s') > %v", name, result.Error)
+		return nil
+	}
+	return model
+}
+
+type Ship struct {
+	Model
+	UserID    uint64 // Who owns this ship
+	ShipModel uint64 // ShipModel.ID, Limits of the ship
+
+	HullPoints   uint64 // Current HP
+	ShieldPoints uint64 // Current SP
+
+	Guns            uint64 // Guns deal 0-2 damage per gun
+	RocketLaunchers uint64 // A "special" attack, Each rocket deals 0-10 damage per rocket
+
+	ArmorPlates      uint64 // Bumps HP up
+	RepairBays       uint64 // Allows you to hold Repair Drones to repair HP in combat
+	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)
+	ShieldCapacitors uint64 // Bumps SP up, each Capacitor also adds a resistance to the amout of SP regained in combat
+	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
+	Engines          uint64 // Affects dodge and escape chances, "mass" is the number of installed parts
+}
+
+func (db *DataBase) SaveShip(ship *Ship) error {
+	result := db.DB.Save(ship)
+	return result.Error
+}
+
+type User struct {
+	Model
+	Name     string `gorm:"unique"`                    // Nickname, commonly used
+	RealName string `gorm:"UniqueIndex,colate:nocase"` // BBS Realname used to identify User, typically not used/displayed
+	// Currency-likes
+	Xp      uint64 // How experienced the User is, determines how tough random opponents will be
+	Metal   uint64 // Common Currency
+	Circuit uint64 // Rare Currency
+	Rockets uint64 // Not a currency, a weapon, but there is technically no limit except the number that can be fired per "special" attack
+	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)
+	// Stuff
+	Ship *Ship
+	// Use UpdatedAt as LastOn
+}
+
+func (db *DataBase) SaveUser(u *User) error {
+	result := db.DB.Save(u)
+	return result.Error
+}
+
+func (db *DataBase) GetUserByName(name string) *User {
+	var user *User
+	result := db.DB.Find(user, User{RealName: name})
+	if result.Error != nil {
+		log.Printf("DataBase.GetUserByName(name='%s') > %v", name, result.Error)
+		return nil
+	}
+	return user
+}

+ 17 - 0
go.mod

@@ -0,0 +1,17 @@
+module git.red-green.com/david/space_construct
+
+go 1.22.4
+
+require (
+	git.red-green.com/RedGreen/doorgo/door v0.0.0-20230521205550-1aab0f75bdd6 // indirect
+	github.com/jinzhu/inflection v1.0.0 // indirect
+	github.com/jinzhu/now v1.1.5 // indirect
+	github.com/mattn/go-sqlite3 v1.14.22 // indirect
+	golang.org/x/sys v0.2.0 // indirect
+	golang.org/x/term v0.2.0 // indirect
+	golang.org/x/text v0.4.0 // indirect
+	gorm.io/driver/sqlite v1.5.6 // indirect
+	gorm.io/gorm v1.25.10 // indirect
+)
+
+replace git.red-green.com/RedGreen/doorgo/door => ./doorgo/door

+ 16 - 0
go.sum

@@ -0,0 +1,16 @@
+github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
+github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
+github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
+github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
+github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
+github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
+golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
+golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM=
+golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
+golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
+golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
+gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
+gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
+gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
+gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=

+ 97 - 0
main.go

@@ -0,0 +1,97 @@
+package main
+
+import (
+	"errors"
+	"strings"
+	"time"
+
+	door "git.red-green.com/RedGreen/doorgo/door"
+)
+
+var (
+	d  door.Door
+	db *DataBase
+)
+
+func Notice(msg string) {
+	if d.Disconnected {
+		return
+	}
+	d.WriteS(door.SavePos + door.GotoS(0, door.Height-1) + door.Reset + strings.Repeat(" ", door.Width) + door.GotoS(0, door.Height-1) + msg + door.RestorePos)
+}
+
+func main() {
+	d := door.Door{}
+	d.Init("space_construct")
+	defer d.Close()
+	d.WriteS(door.Reset + door.Clrscr)
+
+	// This entire thing takes 3/4 of a second (And stops immediately if any key or extended key occurs)
+	txt := "Space Construct v1.0 Apollo@21:1/236"
+	d.WriteS(door.SavePos + door.GotoS(door.Width/2-(len(txt)/2), door.Height/2) + door.ColorTextS("BRI WHI ON BLA") + txt + door.RestorePos)
+	cont_display := true
+	r, ext, err := d.WaitKey(time.Duration(250) * time.Millisecond)
+	if err != nil {
+		if errors.Is(err, door.ErrDisconnected) {
+			return
+		}
+	}
+	if r != 0 || ext != door.NOP {
+		cont_display = false
+	}
+	if cont_display {
+		d.WriteS(door.SavePos + door.GotoS(door.Width/2-(len(txt)/2), door.Height/2) + door.ColorTextS("WHI ON BLA") + txt + door.RestorePos)
+		r, ext, err = d.WaitKey(time.Duration(250) * time.Millisecond)
+		if err != nil {
+			if errors.Is(err, door.ErrDisconnected) {
+				return
+			}
+		}
+		if r != 0 || ext != door.NOP {
+			cont_display = false
+		}
+		if cont_display {
+			d.WriteS(door.SavePos + door.GotoS(door.Width/2-(len(txt)/2), door.Height/2) + door.ColorTextS("BRI BLA ON BLA") + txt + door.RestorePos)
+			_, _, err = d.WaitKey(time.Duration(250) * time.Millisecond)
+			if err != nil {
+				if errors.Is(err, door.ErrDisconnected) {
+					return
+				}
+			}
+		}
+	}
+	d.WriteS(door.Clrscr)
+	db, err = OpenDB("data.db3")
+	if err != nil {
+		Notice(door.ColorTextS("BRI RED ON BLA") + "Err: " + err.Error())
+		d.WaitKey(door.Inactivity)
+		return
+	}
+	defer db.Close()
+
+	if db.GetShipModelById(1) == nil {
+		db.SaveShipModel(&ShipModel{
+			Name:                     "Neptune I",
+			BaseHull:                 10,
+			BaseShields:              0,
+			Purchaseable:             false,
+			CostMetal:                10,
+			CostCircuit:              0,
+			MaxGuns:                  2,
+			MaxRocketLaunchers:       1,
+			MaxArmorPlates:           1,
+			MaxRepairBays:            0,
+			MaxShieldCapacitors:      1,
+			MaxShieldGenerators:      1,
+			MaxEngines:               1,
+			StartingGuns:             1,
+			StartingRocketLaunchers:  0,
+			StartingArmorPlates:      1,
+			StartingRepairBays:       0,
+			StartingRepairDrones:     0,
+			StartingShieldCapacitors: 0,
+			StartingShieldGenerators: 0,
+			StartingEngines:          1,
+		})
+	}
+}