From 48b0972f3027676047ff0549f41aa3b8b48abe07 Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Sat, 20 Aug 2022 22:01:13 +0200 Subject: [PATCH] Accounted for user interaction and switched to other color package --- go.mod | 7 +++++ go.sum | 11 +++++++ main.go | 94 +++++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 90 insertions(+), 22 deletions(-) diff --git a/go.mod b/go.mod index e1fb735..a79292d 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,10 @@ module gitlab.com/EternalWanderer/sheet-parser/v2 go 1.19 require gitlab.com/EternalWanderer/dice-roller v0.0.0-20220820120015-fa11a906e0aa + +require ( + github.com/fatih/color v1.13.0 // indirect + github.com/mattn/go-colorable v0.1.9 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect +) diff --git a/go.sum b/go.sum index 91df172..60328bc 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,17 @@ +github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= gitlab.com/EternalWanderer/dice-roller v0.0.0-20220819131953-2d560c42a087 h1:0UCn74xm94G26W+6/LNb+Zc2dFFHKVgCVhVgam4zw/A= gitlab.com/EternalWanderer/dice-roller v0.0.0-20220819131953-2d560c42a087/go.mod h1:SNEEOhMarhxX2gBUZ4RIDgEvlKaZorPKfhaqpD09/bs= gitlab.com/EternalWanderer/dice-roller v0.0.0-20220820113707-1e57a27bcf70 h1:Q3qDUsGJ+tEXz2vlC2ypIoipkcP34TvMs1vPw2p09LE= gitlab.com/EternalWanderer/dice-roller v0.0.0-20220820113707-1e57a27bcf70/go.mod h1:SNEEOhMarhxX2gBUZ4RIDgEvlKaZorPKfhaqpD09/bs= gitlab.com/EternalWanderer/dice-roller v0.0.0-20220820120015-fa11a906e0aa h1:sSHBJ9+7N9m1gk+npe6wqs2Ghn4bYhyMN5eXWsKBH5Y= gitlab.com/EternalWanderer/dice-roller v0.0.0-20220820120015-fa11a906e0aa/go.mod h1:SNEEOhMarhxX2gBUZ4RIDgEvlKaZorPKfhaqpD09/bs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index 65986cf..8fa90dd 100644 --- a/main.go +++ b/main.go @@ -7,25 +7,26 @@ import ( "io/ioutil" "os" - "gitlab.com/EternalWanderer/dice-roller/Colors" + "github.com/fatih/color" "gitlab.com/EternalWanderer/dice-roller/Dice" ) var ( - path, skillString, saveString, checkString string - modifier, diceThrows, surfaces int - char Character - skillMap = make(map[string]int) - statMap = make(map[string]int) - advantage, disadvantage bool + path, skillString, saveString, statString string + modifier, diceThrows, surfaces int + X, Y int + char Character + skillMap = make(map[string]int) + statMap = make(map[string]int) + advantage, disadvantage bool ) func parseFlags() { flag.StringVar(&path, "file", "stats.json", "fock me") flag.StringVar(&path, "f", "stats.json", "fock me") - flag.StringVar(&skillString, "skill", "athletics", "Skill to parse") - flag.StringVar(&checkString, "check", "strength", "Stat check") - flag.StringVar(&saveString, "save", "strength", "Saving throw to... throw") + 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.Parse() @@ -65,14 +66,58 @@ func main() { readJson() initMaps() - fmt.Println("Skill check:", skillString) - fmt.Println(skillCheck(getSkill(skillString))) - fmt.Println("Stat check", checkString) - fmt.Println(statCheck(getStat(checkString))) - fmt.Println("Saving throw", saveString) - fmt.Println(savingThrow(getStat(saveString))) - fmt.Println("Proficiency") - fmt.Println(getProficiency()) + switch { + case advantage && disadvantage: + fmt.Println("You can't roll with both advantage and disadvantage") + os.Exit(1) + case len(saveString) > 0: + result := savingThrow(getStat(saveString)) + if advantage { + color.Yellow("Rolling %s saving throw with advantage...", saveString) + fmt.Printf("x: %d\ty: %d\n", X, Y) + 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) + color.Red("%d\n", result) + } else { + color.Yellow("Rolling %s saving throw...", saveString) + color.Green("%d\n", result) + } + + case len(skillString) > 0: + result := skillCheck(getSkill(skillString)) + if advantage { + color.Yellow("Rolling %s saving throw with advantage...", skillString) + fmt.Printf("x: %d\ty: %d\n", X, Y) + color.Green("%d\n", result) + } else if disadvantage { + color.Yellow("Rolling %s saving throw with disadvantage...", skillString) + fmt.Printf("x: %d\ty: %d\n", X, Y) + color.Red("%d\n", result) + } else { + color.Yellow("Rolling %s check...", skillString) + color.Green("%d\n", result) + } + + case len(statString) > 0: + result := statCheck(getStat(statString)) + if advantage { + color.Yellow("Rolling %s check with advantage...", statString) + fmt.Printf("x: %d\ty: %d\n", X, Y) + 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) + color.Red("%d\n", result) + } else { + color.Yellow("Rolling %s check...", statString) + color.Green("%d\n", result) + } + + default: + fmt.Println("bollock") + } } func getProficiency() int { @@ -94,16 +139,21 @@ func getModifier(stat Stat) int { } func rollDice() int { - var die, x, y int + var die int switch { case advantage: - die, x, y = Dice.Advantage() + die, X, Y = Dice.Advantage() case disadvantage: - die, x, y = Dice.Disadvantage() + die, X, Y = Dice.Disadvantage() default: die = Dice.SimpleCast() } - fmt.Printf("%s%d%s\tx: %d\ty: %d\n", Colors.ColorGreen, die, Colors.ColorReset, x, y) + switch die { + case 20: + color.Magenta("Natural 20!\n") + case 1: + color.Magenta("Natural 1!\n") + } return die }