84 lines
1.8 KiB
Go

package actions
import (
"dk/internal/models/fights"
"dk/internal/models/spells"
"dk/internal/models/users"
"math/rand"
"strconv"
)
func HandleAttack(fight *fights.Fight, user *users.User) {
// 20% chance to miss
if rand.Float32() < 0.2 {
fight.AddActionAttackMiss()
return
}
fight.DamageMonster(1)
fight.AddActionAttackHit(1)
if fight.MonsterHP <= 0 {
fight.WinFight(10, 5)
}
}
func HandleSpell(fight *fights.Fight, user *users.User, spellID int) {
spell, err := spells.Find(spellID)
if err != nil {
fight.AddAction("Spell not found!")
return
}
// Check if user has enough MP
if user.MP < spell.MP {
fight.AddAction("Not enough MP to cast " + spell.Name + "!")
return
}
// Check if user knows this spell
if !user.HasSpell(spellID) {
fight.AddAction("You don't know that spell!")
return
}
// Deduct MP
user.MP -= spell.MP
switch spell.Type {
case spells.TypeHealing:
// Heal user
healAmount := spell.Attribute
user.HP += healAmount
if user.HP > user.MaxHP {
user.HP = user.MaxHP
}
fight.AddAction("You cast " + spell.Name + " and healed " + strconv.Itoa(healAmount) + " HP!")
case spells.TypeHurt:
// Damage monster
damage := spell.Attribute
fight.DamageMonster(damage)
fight.AddAction("You cast " + spell.Name + " and dealt " + strconv.Itoa(damage) + " damage!")
// Check if monster is defeated
if fight.MonsterHP <= 0 {
fight.WinFight(10, 5) // Basic rewards
}
default:
fight.AddAction("You cast " + spell.Name + " but nothing happened!")
}
}
func HandleRun(fight *fights.Fight, user *users.User) {
// 20% chance to successfully run away
if rand.Float32() < 0.2 {
fight.RunAway()
user.FightID = 0
fight.AddAction("You successfully ran away!")
} else {
fight.AddAction("You failed to run away!")
}
}