Removal of user interaction as part of abstraction

This commit is contained in:
Nox Sluijtman 2022-12-30 15:09:39 +01:00
parent 2389825b02
commit 105843f53f

View file

@ -3,11 +3,9 @@ package Parser
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"github.com/fatih/color" "github.com/fatih/color"
"gitlab.com/EternalWanderer/dice-roller/Dice" "gitlab.com/EternalWanderer/dice-roller/Dice"
@ -25,27 +23,6 @@ var (
advantage, disadvantage, stat_list, skill_list, verbose, trivia bool advantage, disadvantage, stat_list, skill_list, verbose, trivia bool
) )
func parseFlags() {
flag.StringVar(&pathFlag, "file", pathFlag, "Used to point to character sheet")
flag.StringVar(&pathFlag, "f", pathFlag, "Used to point to character sheet")
flag.StringVar(&skillString, "skill", "", "Skill to parse")
flag.StringVar(&statString, "stat", "", "Stat check")
flag.StringVar(&saveString, "save", "", "Saving throw to... throw")
flag.BoolVar(&advantage, "advantage", advantage, "Roll with advantage")
flag.BoolVar(&disadvantage, "disadvantage", disadvantage, "Roll with disadvantage")
flag.BoolVar(&stat_list, "stat-list", false, "Print list of stats, can also be used with -save flag")
flag.BoolVar(&skill_list, "skill-list", false, "Print list of skills to be used in combination with the -skill flag")
flag.BoolVar(&verbose, "verbose", false, "Print stat numbers, to be used in combination with -stat-list or -skill-list")
flag.BoolVar(&verbose, "v", false, "Print stat numbers, to be used in combination with -stat-list or -skill-list")
flag.BoolVar(&trivia, "t", false, "Print character name, level and proficiency")
flag.BoolVar(&trivia, "trivia", false, "Print character name, level and proficiency")
flag.Parse()
}
func handleError(err error) { func handleError(err error) {
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
@ -73,7 +50,6 @@ func readJson() {
err = json.Unmarshal(byteValue, &char) err = json.Unmarshal(byteValue, &char)
handleError(err) handleError(err)
} }
func initMaps() { func initMaps() {
@ -86,124 +62,6 @@ func initMaps() {
} }
} }
func main() {
parseFlags()
readJson()
initMaps()
switch {
case trivia:
fmt.Printf("Name: %s\tLevel: %d\tProficiency: %d\n", char.Misc.Name, char.Misc.Level, GetProficiency())
fmt.Printf("Race: %s\tClass: %s\tBackground: %s\n", char.Misc.Race, char.Misc.Class, char.Misc.Background)
// passive perception
var passivePerception int
switch {
case GetSkill("perception").Proficient && GetSkill("perception").Expertise:
passivePerception = 10 + GetModifier(GetStat("wisdom")) + GetProficiency()*2
case GetSkill("perception").Proficient:
passivePerception = 10 + GetModifier(GetStat("wisdom")) + GetProficiency()
default:
passivePerception = 10 + GetModifier(GetStat("wisdom"))
}
fmt.Printf("Passive perception: %d\n", passivePerception)
case stat_list && skill_list:
printStatList(verbose)
printSkillList(verbose)
case stat_list:
printStatList(verbose)
case skill_list:
printSkillList(verbose)
case advantage && disadvantage:
fmt.Println("You can't roll with both advantage and disadvantage")
os.Exit(1)
case len(saveString) > 0:
result, plainResult, 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("Modifier: %d\n", result-plainResult)
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("Modifier: %d\n", result-plainResult)
color.Red("%d\n", result)
} else {
color.Yellow("Rolling %s saving throw...", saveString)
fmt.Printf("Without modifier: %d\tModifier: %d\n", plainResult, result-plainResult)
color.Green("%d\n", result)
}
case len(skillString) > 0 && char.Misc.IsKurthog && strings.Contains(skillString, "performance"):
result, plainResult, 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("Modifier: %d\n", result-plainResult)
fmt.Printf("Kurthogifier: %d\n", -1)
color.Green("%d\n", result*-1)
} else if disadvantage {
color.Yellow("Rolling %s check with disadvantage...", skillString)
fmt.Printf("x: %d\ty: %d\n", X, Y)
fmt.Printf("Modifier: %d\n", result-plainResult)
fmt.Printf("Kurthogifier: %d\n", -1)
color.Red("%d\n", result*-1)
} else {
color.Yellow("Rolling %s check...", skillString)
fmt.Printf("Without modifier: %d\tModifier: %d\n", plainResult, result-plainResult)
fmt.Printf("Kurthogifier: %d\n", -1)
color.Green("%d\n", result*-1)
}
case len(skillString) > 0:
result, plainResult, 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("Modifier: %d\n", result-plainResult)
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("Modifier: %d\n", result-plainResult)
color.Red("%d\n", result)
} else {
color.Yellow("Rolling %s check...", skillString)
fmt.Printf("Without modifier: %d\tModifier: %d\n", plainResult, result-plainResult)
color.Green("%d\n", result)
}
case len(statString) > 0:
result, plainResult, 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("Modifier: %d\n", result-plainResult)
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("Modifier: %d\n", result-plainResult)
color.Red("%d\n", result)
} else {
color.Yellow("Rolling %s check...", statString)
fmt.Printf("Without modifier: %d\tModifier: %d\n", plainResult, result-plainResult)
color.Green("%d\n", result)
}
default:
flag.Usage()
}
}
func GetProficiency() int { func GetProficiency() int {
// https://worldbuildersjunction.com/what-is-proficiency-bonus-in-dd-5e-how-it-works-calculated/ // https://worldbuildersjunction.com/what-is-proficiency-bonus-in-dd-5e-how-it-works-calculated/
return (char.Misc.Level-1)/4 + 2 return (char.Misc.Level-1)/4 + 2
@ -240,6 +98,7 @@ func rollDice() int {
} }
return die return die
} }
func badCheck(errorMessage string, err error) { func badCheck(errorMessage string, err error) {
if err == nil { if err == nil {
return return