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