feat: started with bonus hp feature

This commit is contained in:
Itsigo
2024-09-01 18:31:01 +02:00
parent dd548e36a3
commit 1d4c46214c
6 changed files with 218 additions and 72 deletions

View File

@@ -7,6 +7,7 @@ import (
"strconv"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
"github.com/itsig0/tallytome/view/hptracker"
)
@@ -82,9 +83,14 @@ func TrackerUpdate(c fiber.Ctx) error {
return render(c, hptracker.BaseStats(data, bd))
}
//calculate max hp
intBase, _ := strconv.Atoi(data.HPBase)
data.HPMax = fmt.Sprint(float64(intBase)*0.2 + float64(intBase))
pre := "tracker_"
sess.Set(pre+"HP", data.HP)
sess.Set(pre+"HPBase", data.HPBase)
sess.Set(pre+"HPMax", data.HPMax)
sess.Set(pre+"HPPercentage", data.HPPercentage)
sess.Set(pre+"HPStartPercentage", data.HPPercentage)
@@ -142,16 +148,36 @@ func TrackerDamage(c fiber.Ctx) error {
// no error handling here because it's already done for currentHP
baseHP, _ := strconv.Atoi(fmt.Sprint(sess.Get("tracker_HPBase")))
maxHP, _ := strconv.Atoi(fmt.Sprint(sess.Get("tracker_HPMax")))
heal := c.FormValue("heal")
savingThrow := c.FormValue("savingthrow")
bonusHPSet := c.FormValue("bonushp")
hasBonusHP := false
bonusRounds, _ := strconv.Atoi(c.FormValue("bonusInput"))
if bonusRounds < 1 {
bonusRounds = 1
}
if currentHP > baseHP {
hasBonusHP = true
}
if bonusHPSet == "on" {
damage = damage / bonusRounds
sess.Set("tracker_HPBonusRounds", fmt.Sprint(bonusRounds-1))
sess.Set("tracker_HPBonusHeal", fmt.Sprint(damage))
}
if savingThrow == "on" && heal != "true" {
damageFloat := float64(damage) * 0.33333
damage -= int(math.Round(damageFloat))
}
if heal == "true" {
if heal == "true" && hasBonusHP == true {
damage = 0
} else if heal == "true" {
damage *= -1
}
@@ -161,17 +187,28 @@ func TrackerDamage(c fiber.Ctx) error {
newHP = 0
}
if newHP > baseHP {
if newHP > baseHP && bonusHPSet != "on" && hasBonusHP == false {
newHP = baseHP
}
if newHP > maxHP {
newHP = maxHP
}
bonusHPPercentage := 0
newPercentage := (newHP * 100) / baseHP
if newPercentage >= 100 {
bonusHPPercentage = newPercentage - 100
newPercentage = 100
}
data := hptracker.TrackerData{
HP: fmt.Sprint(newHP),
HPBase: fmt.Sprint(sess.Get("tracker_HPBase")),
HPStartPercentage: fmt.Sprint(sess.Get("tracker_HPPercentage")),
HPPercentage: fmt.Sprint(newPercentage),
HPBonusPercentage: fmt.Sprint(bonusHPPercentage),
}
sess.Set("tracker_HP", newHP)
@@ -184,6 +221,10 @@ func TrackerDamage(c fiber.Ctx) error {
return render(c, hptracker.HPTracker(data))
}
func TrackerBonusHP(c fiber.Ctx) error {
return c.SendString("I'm tracking you")
}
func TrackerMana(c fiber.Ctx) error {
sess, err := store.Get(c)
if err != nil {
@@ -261,7 +302,7 @@ func TrackerRound(c fiber.Ctx) error {
data := hptracker.TrackerData{}
fields := []string{"HP", "HPBase", "HPStartPercentage", "HPPercentage", "Mana", "ManaBase", "ManaRegen", "ManaStartPercentage", "ManaPercentage", "Round"}
fields := []string{"HP", "HPBase", "HPStartPercentage", "HPPercentage", "Mana", "ManaBase", "ManaRegen", "ManaStartPercentage", "ManaPercentage", "Round", "HPBonusRounds", "HPBonusHeal", "HPMax"}
for _, field := range fields {
if val := sess.Get("tracker_" + field); val != nil {
reflect.ValueOf(&data).Elem().FieldByName(field).SetString(fmt.Sprint(val))
@@ -273,11 +314,29 @@ func TrackerRound(c fiber.Ctx) error {
manaRegen, _ := strconv.Atoi(data.ManaRegen)
round, _ := strconv.Atoi(data.Round)
hp, _ := strconv.Atoi(data.HP)
hpMax, _ := strconv.Atoi(data.HPMax)
hpBonusRounds, _ := strconv.Atoi(data.HPBonusRounds)
hpBonusHeal, _ := strconv.Atoi(data.HPBonusHeal)
log.Info(hpBonusRounds)
log.Info(hpBonusHeal)
if manaBase == 0 || manaRegen == 0 {
c.Status(418)
return render(c, hptracker.TrackerHeader(data))
}
if hpBonusRounds > 0 {
hp += hpBonusHeal
hpBonusRounds -= 1
}
log.Info(hpMax)
if hp > hpMax {
hp = hpMax
}
mana += (manaRegen * manaBase) / 100
if mana > manaBase {
@@ -291,11 +350,15 @@ func TrackerRound(c fiber.Ctx) error {
data.Round = fmt.Sprint(round + 1)
data.ManaStartPercentage = data.ManaPercentage
data.ManaPercentage = fmt.Sprint((mana * 100) / manaBase)
data.HP = fmt.Sprint(hp)
data.HPBonusRounds = fmt.Sprint(hpBonusRounds)
sess.Set("tracker_Mana", data.Mana)
sess.Set("tracker_ManaPercentage", data.ManaPercentage)
sess.Set("tracker_ManaStartPercentage", data.ManaStartPercentage)
sess.Set("tracker_Round", data.Round)
sess.Set("tracker_HP", data.HP)
sess.Set("tracker_HPBonusRounds", data.HPBonusRounds)
sess.Save()