first pass on classes model, move database.sql to first migration file to implement migrations

This commit is contained in:
Sky Johnson 2025-08-22 12:05:45 -05:00
parent 49bf5f28cf
commit 02158041e8
2 changed files with 77 additions and 9 deletions

View File

@ -0,0 +1,69 @@
package classes
import (
"dk/internal/database"
"fmt"
"strings"
)
type Classes struct {
ID int
Name string
Lore string
BaseHP int
BaseMP int
BaseSTR int
BaseDEX int
RateHP int
RateMP int
RateSTR int
RateDEX int
}
func New() *Classes {
return &Classes{
BaseHP: 15,
BaseMP: 10,
BaseSTR: 1,
BaseDEX: 1,
RateHP: 2,
RateMP: 2,
RateSTR: 2,
RateDEX: 2,
}
}
func (c *Classes) Validate() error {
if strings.TrimSpace(c.Name) == "" {
return fmt.Errorf("class name cannot be empty")
}
if c.BaseHP < 1 {
return fmt.Errorf("class base HP must be at least 1")
}
if c.BaseMP < 0 {
return fmt.Errorf("class base MP must be at least 0")
}
if c.BaseSTR < 0 {
return fmt.Errorf("class base strength must be at least 0")
}
if c.BaseDEX < 0 {
return fmt.Errorf("class base dexterity must be at least 0")
}
if c.RateHP < 0 {
return fmt.Errorf("class HP rate must be at least 0")
}
if c.RateMP < 0 {
return fmt.Errorf("class MP rate must be at least 0")
}
if c.RateSTR < 0 {
return fmt.Errorf("class strength rate must be at least 0")
}
if c.RateDEX < 0 {
return fmt.Errorf("class dexterity rate must be at least 0")
}
return nil
}
func (c *Classes) Delete() error {
return database.Exec("DELETE FROM classes WHERE id = %d", c.ID)
}

View File

@ -109,21 +109,20 @@ CREATE TABLE classes (
'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'name' TEXT NOT NULL,
'lore' TEXT NOT NULL,
'exp_rate' INTEGER NOT NULL DEFAULT 3,
'base_hp' INTEGER NOT NULL DEFAULT 15,
'base_mp' INTEGER NOT NULL DEFAULT 10,
'base_str' INTEGER NOT NULL DEFAULT 1,
'base_dex' INTEGER NOT NULL DEFAULT 1,
'hp_rate' INTEGER NOT NULL DEFAULT 2,
'mp_rate' INTEGER NOT NULL DEFAULT 2,
'str_rate' INTEGER NOT NULL DEFAULT 2,
'dex_rate' INTEGER NOT NULL DEFAULT 2
'rate_hp' INTEGER NOT NULL DEFAULT 2,
'rate_mp' INTEGER NOT NULL DEFAULT 2,
'rate_str' INTEGER NOT NULL DEFAULT 2,
'rate_dex' INTEGER NOT NULL DEFAULT 2
);
INSERT INTO classes VALUES
(1, 'Adventurer', '', 3, 15, 10, 4, 4, 2, 2, 2, 2),
(2, 'Mage', '', 1, 10, 15, 1, 7, 1, 3, 1, 2),
(3, 'Warrior', '', 2, 20, 5, 7, 1, 3, 1, 3, 1),
(4, 'Paladin', '', 5, 15, 15, 5, 5, 2, 2, 2, 2);
(1, 'Adventurer', '', 15, 10, 4, 4, 2, 2, 2, 2),
(2, 'Mage', '', 10, 15, 1, 7, 1, 3, 1, 2),
(3, 'Warrior', '', 20, 5, 7, 1, 3, 1, 3, 1),
(4, 'Paladin', '', 15, 15, 5, 5, 2, 2, 2, 2);
DROP TABLE IF EXISTS monsters;
CREATE TABLE monsters (