Error checking and return rolls without modifier

This commit is contained in:
Nox Sluijtman 2022-08-27 21:57:41 +02:00
parent ac56573b33
commit cd7b6c39b2

61
main.go
View file

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
@ -95,47 +96,59 @@ func main() {
fmt.Println("You can't roll with both advantage and disadvantage")
os.Exit(1)
case len(saveString) > 0:
result := savingThrow(getStat(saveString))
result, planeResult, err := savingThrow(getStat(saveString))
badCheck(saveString, err)
if advantage {
color.Yellow("Rolling %s saving throw with advantage...", saveString)
fmt.Printf("x: %d\ty: %d\n", X, Y)
fmt.Printf("Without modifier: %d\n", planeResult)
color.Green("%d\n", result)
} else if disadvantage {
color.Yellow("Rolling %s saving throw with disadvantage...", saveString)
fmt.Printf("x: %d\ty: %d\n", X, Y)
fmt.Printf("Without modifier: %d\n", planeResult)
color.Red("%d\n", result)
} else {
color.Yellow("Rolling %s saving throw...", saveString)
fmt.Printf("Without modifier: %d\n", planeResult)
color.Green("%d\n", result)
}
case len(skillString) > 0:
result := skillCheck(getSkill(skillString))
result, planeResult, err := skillCheck(getSkill(skillString))
badCheck(skillString, err)
if advantage {
color.Yellow("Rolling %s check with advantage...", skillString)
fmt.Printf("x: %d\ty: %d\n", X, Y)
fmt.Printf("Without modifier: %d\n", planeResult)
color.Green("%d\n", result)
} else if disadvantage {
color.Yellow("Rolling %s check with disadvantage...", skillString)
fmt.Printf("x: %d\ty: %d\n", X, Y)
fmt.Printf("Without modifier: %d\n", planeResult)
color.Red("%d\n", result)
} else {
color.Yellow("Rolling %s check...", skillString)
fmt.Printf("Without modifier: %d\n", planeResult)
color.Green("%d\n", result)
}
case len(statString) > 0:
result := statCheck(getStat(statString))
result, planeResult, err := statCheck(getStat(statString))
badCheck(statString, err)
if advantage {
color.Yellow("Rolling %s check with advantage...", statString)
fmt.Printf("x: %d\ty: %d\n", X, Y)
fmt.Printf("Without modifier: %d\n", planeResult)
color.Green("%d\n", result)
} else if disadvantage {
color.Yellow("Rolling %s check with disadvantage...", statString)
fmt.Printf("x: %d\ty: %d\n", X, Y)
fmt.Printf("Without modifier: %d\n", planeResult)
color.Red("%d\n", result)
} else {
color.Yellow("Rolling %s check...", statString)
fmt.Printf("Without modifier: %d\n", planeResult)
color.Green("%d\n", result)
}
@ -180,23 +193,52 @@ func rollDice() int {
}
return die
}
func badCheck(errorMessage string, err error) {
if err == nil {
return
}
fmt.Println(err)
fmt.Println(errorMessage)
os.Exit(1)
}
func statCheck(stat Stat) int {
return rollDice() + getModifier(stat)
func statErrorCheck() error {
for i := 0; i < len(char.Stats); i++ {
if statString == char.Stats[i].StatName || saveString == char.Stats[i].StatName {
return nil
}
}
return errors.New("Unknown stat:")
}
func skillErrorCheck() error {
for i := 0; i < len(char.Skills); i++ {
if skillString == char.Skills[i].SkillName {
return nil
}
}
return errors.New("Unknown skill:")
}
func statCheck(stat Stat) (int, int, error) {
die := rollDice()
return die + getModifier(stat), die, statErrorCheck()
}
func savingThrow(stat Stat) int {
func savingThrow(stat Stat) (int, int, error) {
die := rollDice()
planeDie := die
if stat.Proficient {
die += getProficiency()
}
return die + getModifier(stat)
return die + getModifier(stat), planeDie, statErrorCheck()
}
func skillCheck(skill Skill) int {
func skillCheck(skill Skill) (int, int, error) {
die := rollDice()
planeDie := die
die += getModifier(getStat(skill.BaseStat))
@ -206,8 +248,9 @@ func skillCheck(skill Skill) int {
case skill.Proficient:
die += getProficiency()
}
return die
return die, planeDie, skillErrorCheck()
}
func printStatList(verbose bool) {
color.Magenta("Listing stats...")
if verbose {