102 lines
1.9 KiB
Go

package drops
import (
"fmt"
"dk/internal/database"
)
// Drop represents a drop item in the game
type Drop struct {
ID int
Name string
Level int
Type int
Att string
}
// DropType constants for drop types
const (
TypeConsumable = 1
)
// New creates a new Drop with sensible defaults
func New() *Drop {
return &Drop{
Name: "",
Level: 1,
Type: TypeConsumable,
Att: "",
}
}
// Validate checks if drop has valid values
func (d *Drop) Validate() error {
if d.Name == "" {
return fmt.Errorf("drop name cannot be empty")
}
if d.Level < 1 {
return fmt.Errorf("drop Level must be at least 1")
}
if d.Type < TypeConsumable {
return fmt.Errorf("invalid drop type: %d", d.Type)
}
return nil
}
// CRUD operations
func (d *Drop) Delete() error {
return database.Exec("DELETE FROM drops WHERE id = %d", d.ID)
}
func (d *Drop) Insert() error {
id, err := database.Insert("drops", d, "ID")
if err != nil {
return err
}
d.ID = int(id)
return nil
}
// Query functions
func Find(id int) (*Drop, error) {
var drop Drop
err := database.Get(&drop, "SELECT * FROM drops WHERE id = %d", id)
if err != nil {
return nil, fmt.Errorf("drop with ID %d not found", id)
}
return &drop, nil
}
func All() ([]*Drop, error) {
var drops []*Drop
err := database.Select(&drops, "SELECT * FROM drops ORDER BY id ASC")
return drops, err
}
func ByLevel(minLevel int) ([]*Drop, error) {
var drops []*Drop
err := database.Select(&drops, "SELECT * FROM drops WHERE level <= %d ORDER BY id ASC", minLevel)
return drops, err
}
func ByType(dropType int) ([]*Drop, error) {
var drops []*Drop
err := database.Select(&drops, "SELECT * FROM drops WHERE type = %d ORDER BY id ASC", dropType)
return drops, err
}
// Helper methods
func (d *Drop) IsConsumable() bool {
return d.Type == TypeConsumable
}
func (d *Drop) TypeName() string {
switch d.Type {
case TypeConsumable:
return "Consumable"
default:
return "Unknown"
}
}