152 lines
3.3 KiB
Go
152 lines
3.3 KiB
Go
package items
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"dk/internal/database"
|
|
|
|
"zombiezen.com/go/sqlite"
|
|
)
|
|
|
|
// Item represents an item in the database
|
|
type Item struct {
|
|
ID int `json:"id"`
|
|
Type int `json:"type"`
|
|
Name string `json:"name"`
|
|
Value int `json:"value"`
|
|
Att int `json:"att"`
|
|
Special string `json:"special"`
|
|
}
|
|
|
|
// ItemType constants for item types
|
|
const (
|
|
TypeWeapon = 1
|
|
TypeArmor = 2
|
|
TypeShield = 3
|
|
)
|
|
|
|
// Find retrieves an item by ID
|
|
func Find(id int) (*Item, error) {
|
|
item := &Item{}
|
|
|
|
query := "SELECT id, type, name, value, att, special FROM items WHERE id = ?"
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
item.ID = stmt.ColumnInt(0)
|
|
item.Type = stmt.ColumnInt(1)
|
|
item.Name = stmt.ColumnText(2)
|
|
item.Value = stmt.ColumnInt(3)
|
|
item.Att = stmt.ColumnInt(4)
|
|
item.Special = stmt.ColumnText(5)
|
|
return nil
|
|
}, id)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to find item: %w", err)
|
|
}
|
|
|
|
if item.ID == 0 {
|
|
return nil, fmt.Errorf("item with ID %d not found", id)
|
|
}
|
|
|
|
return item, nil
|
|
}
|
|
|
|
// All retrieves all items
|
|
func All() ([]*Item, error) {
|
|
var items []*Item
|
|
|
|
query := "SELECT id, type, name, value, att, special FROM items ORDER BY id"
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
item := &Item{
|
|
ID: stmt.ColumnInt(0),
|
|
Type: stmt.ColumnInt(1),
|
|
Name: stmt.ColumnText(2),
|
|
Value: stmt.ColumnInt(3),
|
|
Att: stmt.ColumnInt(4),
|
|
Special: stmt.ColumnText(5),
|
|
}
|
|
items = append(items, item)
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve all items: %w", err)
|
|
}
|
|
|
|
return items, nil
|
|
}
|
|
|
|
// ByType retrieves items by type
|
|
func ByType(itemType int) ([]*Item, error) {
|
|
var items []*Item
|
|
|
|
query := "SELECT id, type, name, value, att, special FROM items WHERE type = ? ORDER BY id"
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
item := &Item{
|
|
ID: stmt.ColumnInt(0),
|
|
Type: stmt.ColumnInt(1),
|
|
Name: stmt.ColumnText(2),
|
|
Value: stmt.ColumnInt(3),
|
|
Att: stmt.ColumnInt(4),
|
|
Special: stmt.ColumnText(5),
|
|
}
|
|
items = append(items, item)
|
|
return nil
|
|
}, itemType)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve items by type: %w", err)
|
|
}
|
|
|
|
return items, nil
|
|
}
|
|
|
|
// Save updates an existing item in the database
|
|
func (i *Item) Save() error {
|
|
if i.ID == 0 {
|
|
return fmt.Errorf("cannot save item without ID")
|
|
}
|
|
|
|
query := `UPDATE items SET type = ?, name = ?, value = ?, att = ?, special = ? WHERE id = ?`
|
|
return database.Exec(query, i.Type, i.Name, i.Value, i.Att, i.Special, i.ID)
|
|
}
|
|
|
|
// Delete removes the item from the database
|
|
func (i *Item) Delete() error {
|
|
if i.ID == 0 {
|
|
return fmt.Errorf("cannot delete item without ID")
|
|
}
|
|
|
|
query := "DELETE FROM items WHERE id = ?"
|
|
return database.Exec(query, i.ID)
|
|
}
|
|
|
|
// IsWeapon returns true if the item is a weapon
|
|
func (i *Item) IsWeapon() bool {
|
|
return i.Type == TypeWeapon
|
|
}
|
|
|
|
// IsArmor returns true if the item is armor
|
|
func (i *Item) IsArmor() bool {
|
|
return i.Type == TypeArmor
|
|
}
|
|
|
|
// IsShield returns true if the item is a shield
|
|
func (i *Item) IsShield() bool {
|
|
return i.Type == TypeShield
|
|
}
|
|
|
|
// TypeName returns the string representation of the item type
|
|
func (i *Item) TypeName() string {
|
|
switch i.Type {
|
|
case TypeWeapon:
|
|
return "Weapon"
|
|
case TypeArmor:
|
|
return "Armor"
|
|
case TypeShield:
|
|
return "Shield"
|
|
default:
|
|
return "Unknown"
|
|
}
|
|
}
|