267 lines
6.0 KiB
Go

/*
Package spells is the active record implementation for spells in the game.
# Basic Usage
To retrieve a spell by ID:
spell, err := spells.Find(db, 1)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found spell: %s (MP: %d, Power: %d)\n", spell.Name, spell.MP, spell.Attribute)
To get all spells:
allSpells, err := spells.All(db)
if err != nil {
log.Fatal(err)
}
for _, spell := range allSpells {
fmt.Printf("Spell: %s\n", spell.Name)
}
To find a spell by name:
heal, err := spells.ByName(db, "Heal")
if err != nil {
log.Fatal(err)
}
To filter spells by type:
healingSpells, err := spells.ByType(db, spells.TypeHealing)
if err != nil {
log.Fatal(err)
}
To get spells within MP budget:
affordableSpells, err := spells.ByMaxMP(db, 10)
if err != nil {
log.Fatal(err)
}
To get spells of specific type within MP budget:
cheapHurtSpells, err := spells.ByTypeAndMaxMP(db, spells.TypeHurt, 15)
if err != nil {
log.Fatal(err)
}
# Creating Spells with Builder Pattern
The package provides a fluent builder interface for creating new spells:
spell, err := spells.NewBuilder(db).
WithName("Lightning Bolt").
WithMP(25).
WithAttribute(70).
WithType(spells.TypeHurt).
Create()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created spell with ID: %d\n", spell.ID)
# Updating Spells
Spells can be modified and saved back to the database:
spell, _ := spells.Find(db, 1)
spell.Name = "Greater Heal"
spell.MP = 8
spell.Attribute = 20
err := spell.Save()
if err != nil {
log.Fatal(err)
}
# Deleting Spells
Spells can be removed from the database:
spell, _ := spells.Find(db, 1)
err := spell.Delete()
if err != nil {
log.Fatal(err)
}
# Spell Types
The package defines spell type constants:
spells.TypeHealing = 1 // Healing spells (Heal, Revive, Life, Breath, Gaia)
spells.TypeHurt = 2 // Hurt spells (Hurt, Pain, Maim, Rend, Chaos)
spells.TypeSleep = 3 // Sleep spells (Sleep, Dream, Nightmare)
spells.TypeAttackBoost = 4 // Attack boost spells (Craze, Rage, Fury)
spells.TypeDefenseBoost = 5 // Defense boost spells (Ward, Fend, Barrier)
Helper methods are available to check spell types:
if spell.IsHealing() {
fmt.Println("This spell heals the caster")
}
if spell.IsHurt() {
fmt.Println("This spell damages enemies")
}
if spell.IsSleep() {
fmt.Println("This spell puts enemies to sleep")
}
if spell.IsAttackBoost() {
fmt.Println("This spell boosts attack power")
}
if spell.IsDefenseBoost() {
fmt.Println("This spell boosts defense")
}
fmt.Printf("Spell type: %s\n", spell.TypeName())
# Database Schema
The spells table has the following structure:
CREATE TABLE spells (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
mp INTEGER NOT NULL DEFAULT 0,
attribute INTEGER NOT NULL DEFAULT 0,
type INTEGER NOT NULL DEFAULT 0
)
Where:
- id: Unique identifier
- name: Display name of the spell
- mp: Mana points required to cast the spell
- attribute: Power/effectiveness of the spell
- type: Spell type (1-5 as defined above)
# Spell Mechanics
## Mana Point System
All spells require MP to cast:
if spell.CanCast(player.CurrentMP) {
fmt.Printf("You can cast %s\n", spell.Name)
} else {
fmt.Printf("Not enough MP to cast %s (need %d, have %d)\n",
spell.Name, spell.MP, player.CurrentMP)
}
## Spell Efficiency
Calculate efficiency as attribute per MP cost:
efficiency := spell.Efficiency()
fmt.Printf("%s efficiency: %.2f power per MP\n", spell.Name, efficiency)
This helps players choose the most cost-effective spells.
## Offensive vs Support Spells
Spells are categorized by their primary purpose:
if spell.IsOffensive() {
// Hurt and Sleep spells - used against enemies
fmt.Println("Offensive spell - targets enemies")
}
if spell.IsSupport() {
// Healing and boost spells - used to help player
fmt.Println("Support spell - helps the caster")
}
# Spell Categories
## Healing Spells (Type 1)
Restore hit points to the caster:
- Heal: Basic healing
- Revive: Moderate healing
- Life: Strong healing
- Breath: Very strong healing
- Gaia: Ultimate healing
## Hurt Spells (Type 2)
Deal damage to enemies:
- Hurt: Basic damage
- Pain: Moderate damage
- Maim: Strong damage
- Rend: Very strong damage
- Chaos: Ultimate damage
Note: Some monsters have immunity to Hurt spells.
## Sleep Spells (Type 3)
Put enemies to sleep, preventing them from attacking:
- Sleep: Basic sleep effect
- Dream: Moderate sleep effect
- Nightmare: Strong sleep effect
Note: Some monsters have immunity to Sleep spells.
## Attack Boost Spells (Type 4)
Temporarily increase the caster's attack power:
- Craze: Basic attack boost
- Rage: Moderate attack boost
- Fury: Strong attack boost
## Defense Boost Spells (Type 5)
Temporarily increase the caster's defense:
- Ward: Basic defense boost
- Fend: Moderate defense boost
- Barrier: Strong defense boost
# Query Patterns
## Finding Castable Spells
Get spells a player can cast with their current MP:
currentMP := 25
castableSpells, err := spells.ByMaxMP(db, currentMP)
// Filter by type and MP budget
castableHurtSpells, err := spells.ByTypeAndMaxMP(db, spells.TypeHurt, currentMP)
## Spell Progression
Players typically learn spells in order of power/cost:
// Get all healing spells ordered by cost
healingSpells, err := spells.ByType(db, spells.TypeHealing)
// Results are ordered by MP cost automatically
## Combat Spell Selection
For combat AI or recommendations:
// Get most efficient hurt spells within budget
hurtSpells, err := spells.ByTypeAndMaxMP(db, spells.TypeHurt, availableMP)
// Find most efficient spell
var bestSpell *spells.Spell
var bestEfficiency float64
for _, spell := range hurtSpells {
if eff := spell.Efficiency(); eff > bestEfficiency {
bestEfficiency = eff
bestSpell = spell
}
}
# Error Handling
All functions return appropriate errors for common failure cases:
- Spell not found (Find/ByName returns error for non-existent spells)
- Database connection issues
- Invalid operations (e.g., saving/deleting spells without IDs)
*/
package spells