246 lines
5.6 KiB
Go

package control
import (
"fmt"
"dk/internal/database"
"dk/internal/helpers/scanner"
"zombiezen.com/go/sqlite"
)
// Control represents the game control settings in the database
type Control struct {
database.BaseModel
ID int `db:"id" json:"id"`
WorldSize int `db:"world_size" json:"world_size"`
Open int `db:"open" json:"open"`
AdminEmail string `db:"admin_email" json:"admin_email"`
Class1Name string `db:"class_1_name" json:"class_1_name"`
Class2Name string `db:"class_2_name" json:"class_2_name"`
Class3Name string `db:"class_3_name" json:"class_3_name"`
}
func (c *Control) GetTableName() string {
return "control"
}
func (c *Control) GetID() int {
return c.ID
}
func (c *Control) SetID(id int) {
c.ID = id
}
func (c *Control) Set(field string, value any) error {
return database.Set(c, field, value)
}
func (c *Control) Save() error {
return database.Save(c)
}
func (c *Control) Delete() error {
return database.Delete(c)
}
// Creates a new Control with sensible defaults
func New() *Control {
return &Control{
WorldSize: 200, // Default world size
Open: 1, // Default open for registration
AdminEmail: "", // No admin email by default
Class1Name: "Mage", // Default class names
Class2Name: "Warrior",
Class3Name: "Paladin",
}
}
var controlScanner = scanner.New[Control]()
// Returns the column list for control queries
func controlColumns() string {
return controlScanner.Columns()
}
// Populates a Control struct using the fast scanner
func scanControl(stmt *sqlite.Stmt) *Control {
control := &Control{}
controlScanner.Scan(stmt, control)
return control
}
// Retrieves the control record by ID (typically only ID 1 exists)
func Find(id int) (*Control, error) {
var control *Control
query := `SELECT ` + controlColumns() + ` FROM control WHERE id = ?`
err := database.Query(query, func(stmt *sqlite.Stmt) error {
control = scanControl(stmt)
return nil
}, id)
if err != nil {
return nil, fmt.Errorf("failed to find control: %w", err)
}
if control == nil {
return nil, fmt.Errorf("control with ID %d not found", id)
}
return control, nil
}
// Retrieves the main control record (ID 1)
func Get() (*Control, error) {
return Find(1)
}
// Saves a new control to the database and sets the ID
func (c *Control) Insert() error {
columns := `world_size, open, admin_email, class_1_name, class_2_name, class_3_name`
values := []any{c.WorldSize, c.Open, c.AdminEmail, c.Class1Name, c.Class2Name, c.Class3Name}
return database.Insert(c, columns, values...)
}
// Returns true if the game world is open for new players
func (c *Control) IsOpen() bool {
return c.Open == 1
}
// Sets whether the game world is open for new players
func (c *Control) SetOpen(open bool) {
if open {
c.Set("Open", 1)
} else {
c.Set("Open", 0)
}
}
// Closes the game world to new players
func (c *Control) Close() {
c.Set("Open", 0)
}
// Opens the game world to new players
func (c *Control) OpenWorld() {
c.Set("Open", 1)
}
// Returns all class names as a slice
func (c *Control) GetClassNames() []string {
classes := make([]string, 0, 3)
if c.Class1Name != "" {
classes = append(classes, c.Class1Name)
}
if c.Class2Name != "" {
classes = append(classes, c.Class2Name)
}
if c.Class3Name != "" {
classes = append(classes, c.Class3Name)
}
return classes
}
// Sets all class names from a slice
func (c *Control) SetClassNames(classes []string) {
// Reset all class names
c.Set("Class1Name", "")
c.Set("Class2Name", "")
c.Set("Class3Name", "")
// Set provided class names
if len(classes) > 0 {
c.Set("Class1Name", classes[0])
}
if len(classes) > 1 {
c.Set("Class2Name", classes[1])
}
if len(classes) > 2 {
c.Set("Class3Name", classes[2])
}
}
// Returns the name of a specific class (1-3)
func (c *Control) GetClassName(classNum int) string {
switch classNum {
case 1:
return c.Class1Name
case 2:
return c.Class2Name
case 3:
return c.Class3Name
default:
return ""
}
}
// Sets the name of a specific class (1-3)
func (c *Control) SetClassName(classNum int, name string) bool {
switch classNum {
case 1:
c.Set("Class1Name", name)
return true
case 2:
c.Set("Class2Name", name)
return true
case 3:
c.Set("Class3Name", name)
return true
default:
return false
}
}
// Returns true if the given name matches one of the configured classes
func (c *Control) IsValidClassName(name string) bool {
if name == "" {
return false
}
return name == c.Class1Name || name == c.Class2Name || name == c.Class3Name
}
// Returns the class number (1-3) for a given class name, or 0 if not found
func (c *Control) GetClassNumber(name string) int {
if name == c.Class1Name && name != "" {
return 1
}
if name == c.Class2Name && name != "" {
return 2
}
if name == c.Class3Name && name != "" {
return 3
}
return 0
}
// Returns true if an admin email is configured
func (c *Control) HasAdminEmail() bool {
return c.AdminEmail != ""
}
// Returns true if the world size is within reasonable bounds
func (c *Control) IsWorldSizeValid() bool {
return c.WorldSize > 0 && c.WorldSize <= 10000
}
// Returns the world radius (half the world size)
func (c *Control) GetWorldRadius() int {
return c.WorldSize / 2
}
// Returns true if the given coordinates are within world bounds
func (c *Control) IsWithinWorldBounds(x, y int) bool {
radius := c.GetWorldRadius()
return x >= -radius && x <= radius && y >= -radius && y <= radius
}
// Returns the minimum and maximum coordinates for the world
func (c *Control) GetWorldBounds() (minX, minY, maxX, maxY int) {
radius := c.GetWorldRadius()
return -radius, -radius, radius, radius
}