148 lines
3.4 KiB
Go
148 lines
3.4 KiB
Go
/*
|
|
Package drops is the active record implementation for drop items in the game.
|
|
|
|
# Basic Usage
|
|
|
|
To retrieve a drop by ID:
|
|
|
|
drop, err := drops.Find(db, 1)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("Found drop: %s (level: %d)\n", drop.Name, drop.Level)
|
|
|
|
To get all drops:
|
|
|
|
allDrops, err := drops.All(db)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, drop := range allDrops {
|
|
fmt.Printf("Drop: %s\n", drop.Name)
|
|
}
|
|
|
|
To filter drops by level (items available at or below a level):
|
|
|
|
availableDrops, err := drops.ByLevel(db, 25)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
To filter drops by type:
|
|
|
|
consumables, err := drops.ByType(db, drops.TypeConsumable)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
# Creating Drops with Builder Pattern
|
|
|
|
The package provides a fluent builder interface for creating new drops:
|
|
|
|
drop, err := drops.NewBuilder(db).
|
|
WithName("Ruby").
|
|
WithLevel(50).
|
|
WithType(drops.TypeConsumable).
|
|
WithAtt("maxhp,150").
|
|
Create()
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("Created drop with ID: %d\n", drop.ID)
|
|
|
|
# Updating Drops
|
|
|
|
Drops can be modified and saved back to the database:
|
|
|
|
drop, _ := drops.Find(db, 1)
|
|
drop.Name = "Enhanced Life Pebble"
|
|
drop.Level = 5
|
|
drop.Att = "maxhp,15"
|
|
|
|
err := drop.Save()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
# Deleting Drops
|
|
|
|
Drops can be removed from the database:
|
|
|
|
drop, _ := drops.Find(db, 1)
|
|
err := drop.Delete()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
# Drop Types
|
|
|
|
The package defines drop type constants:
|
|
|
|
drops.TypeConsumable = 1 // Consumable items like potions, gems, etc.
|
|
|
|
Helper methods are available to check drop types:
|
|
|
|
if drop.IsConsumable() {
|
|
fmt.Println("This is a consumable item")
|
|
}
|
|
fmt.Printf("Drop type: %s\n", drop.TypeName())
|
|
|
|
# Database Schema
|
|
|
|
The drops table has the following structure:
|
|
|
|
CREATE TABLE drops (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL DEFAULT '',
|
|
level INTEGER NOT NULL DEFAULT 0,
|
|
type INTEGER NOT NULL DEFAULT 0,
|
|
att TEXT NOT NULL DEFAULT ''
|
|
)
|
|
|
|
Where:
|
|
- id: Unique identifier
|
|
- name: Display name of the drop
|
|
- level: Minimum monster level to find this drop
|
|
- type: Drop type (1=consumable)
|
|
- att: Comma-separated attributes in "key,value,key,value" format
|
|
|
|
# Drop Attributes
|
|
|
|
The att field contains attribute bonuses in comma-separated "key,value" pairs:
|
|
|
|
"maxhp,10" // +10 max health
|
|
"maxmp,25" // +25 max mana
|
|
"strength,50" // +50 strength
|
|
"defensepower,25" // +25 defense power
|
|
"expbonus,10" // +10% experience bonus
|
|
"goldbonus,5" // +5% gold bonus
|
|
|
|
Many drops have multiple attributes in a single field:
|
|
|
|
drop.Att = "maxhp,25,strength,25" // +25 max health AND +25 strength
|
|
drop.Att = "maxmp,-50,strength,100" // -50 max mana AND +100 strength
|
|
|
|
The attributes are parsed as alternating key-value pairs separated by commas.
|
|
|
|
# Level Requirements
|
|
|
|
Drops have level requirements that determine when players can use them:
|
|
|
|
// Get all drops available from level 10 and above monsters
|
|
availableDrops, err := drops.ByLevel(db, 10)
|
|
|
|
// This returns drops with level <= 10
|
|
for _, drop := range availableDrops {
|
|
fmt.Printf("%s (level %d)\n", drop.Name, drop.Level)
|
|
}
|
|
|
|
# Error Handling
|
|
|
|
All functions return appropriate errors for common failure cases:
|
|
- Drop not found (Find returns error for non-existent IDs)
|
|
- Database connection issues
|
|
- Invalid operations (e.g., saving/deleting drops without IDs)
|
|
*/
|
|
package drops
|