124 lines
2.6 KiB
Go
124 lines
2.6 KiB
Go
/*
|
|
Package items is the active record implementation for items in the game.
|
|
|
|
# Basic Usage
|
|
|
|
To retrieve an item by ID:
|
|
|
|
item, err := items.Find(db, 1)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("Found item: %s (value: %d)\n", item.Name, item.Value)
|
|
|
|
To get all items:
|
|
|
|
allItems, err := items.All(db)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, item := range allItems {
|
|
fmt.Printf("Item: %s\n", item.Name)
|
|
}
|
|
|
|
To filter items by type:
|
|
|
|
weapons, err := items.ByType(db, items.TypeWeapon)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
# Creating Items with Builder Pattern
|
|
|
|
The package provides a fluent builder interface for creating new items:
|
|
|
|
item, err := items.NewBuilder(db).
|
|
WithType(items.TypeWeapon).
|
|
WithName("Excalibur").
|
|
WithValue(5000).
|
|
WithAtt(100).
|
|
WithSpecial("strength,25").
|
|
Create()
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("Created item with ID: %d\n", item.ID)
|
|
|
|
# Updating Items
|
|
|
|
Items can be modified and saved back to the database:
|
|
|
|
item, _ := items.Find(db, 1)
|
|
item.Name = "Enhanced Sword"
|
|
item.Value += 100
|
|
|
|
err := item.Save()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
# Deleting Items
|
|
|
|
Items can be removed from the database:
|
|
|
|
item, _ := items.Find(db, 1)
|
|
err := item.Delete()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
# Item Types
|
|
|
|
The package defines three item types as constants:
|
|
|
|
items.TypeWeapon = 1 // Swords, axes, etc.
|
|
items.TypeArmor = 2 // Protective gear
|
|
items.TypeShield = 3 // Shields and bucklers
|
|
|
|
Helper methods are available to check item types:
|
|
|
|
if item.IsWeapon() {
|
|
fmt.Println("This is a weapon")
|
|
}
|
|
fmt.Printf("Item type: %s\n", item.TypeName())
|
|
|
|
# Database Schema
|
|
|
|
The items table has the following structure:
|
|
|
|
CREATE TABLE items (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
type INTEGER NOT NULL DEFAULT 0,
|
|
name TEXT NOT NULL,
|
|
value INTEGER NOT NULL DEFAULT 0,
|
|
att INTEGER NOT NULL DEFAULT 0,
|
|
special TEXT NOT NULL DEFAULT ''
|
|
)
|
|
|
|
Where:
|
|
- id: Unique identifier
|
|
- type: Item type (1=weapon, 2=armor, 3=shield)
|
|
- name: Display name of the item
|
|
- value: Gold value/cost
|
|
- att: Attack or defense attribute bonus
|
|
- special: Special attributes in "key,value" format
|
|
|
|
# Special Attributes
|
|
|
|
The special field contains comma-separated key-value pairs for item bonuses:
|
|
|
|
"strength,10" // +10 strength
|
|
"maxhp,25" // +25 max health
|
|
"expbonus,5" // +5% experience bonus
|
|
"maxhp,50,strength,25" // Multiple bonuses
|
|
|
|
# Error Handling
|
|
|
|
All functions return appropriate errors for common failure cases:
|
|
- Item not found (Find returns error for non-existent IDs)
|
|
- Database connection issues
|
|
- Invalid operations (e.g., saving/deleting items without IDs)
|
|
*/
|
|
package items
|