dice-roller/main.go
2022-08-19 10:15:31 +02:00

89 lines
2.7 KiB
Go

package main
import (
"flag"
"fmt"
"math/rand"
"os"
"time"
"gitlab.com/EternalWanderer/dice-roller/Coin"
"gitlab.com/EternalWanderer/dice-roller/Colors"
"gitlab.com/EternalWanderer/dice-roller/Dice"
)
var (
surfaces, modifier, diceThrows, attacks int
advantage, disadvantage, coin bool
)
func main() {
rand.Seed(time.Now().Unix())
ParseFlags()
switch {
// few basic checks
case attacks < 1:
fmt.Println("Attack amount cannot be below 1")
os.Exit(1)
case surfaces < 2:
fmt.Println("Die surfaces cannot be below 2")
os.Exit(1)
case diceThrows < 1:
fmt.Println("Dice thrown cannot be below 1")
os.Exit(1)
// die with 2 sides might as well be a coin
case surfaces == 2 || coin:
Coin.Toss(diceThrows)
case advantage:
if modifier > 0 {
fmt.Printf("%sRolling 1d20 + %d with advantage...%s\n", Colors.ColorGreen, modifier, Colors.ColorReset)
} else {
fmt.Printf("%sRolling 1d20 with advantage...%s\n", Colors.ColorGreen, Colors.ColorReset)
}
fmt.Printf("\t%s%d%s\n", Colors.ColorGreen, Dice.Advantage(Dice.SimpleCast(modifier), Dice.SimpleCast(modifier)), Colors.ColorReset)
case disadvantage:
if modifier != 0 {
fmt.Printf("%sRolling 1d20 + %d with disadvantage...%s\n", Colors.ColorRed, modifier, Colors.ColorReset)
} else {
fmt.Printf("%sRolling 1d20 with disadvantage...%s\n", Colors.ColorRed, Colors.ColorReset)
}
fmt.Printf("\t%s%d%s\n", Colors.ColorRed, Dice.Disadvantage(Dice.SimpleCast(modifier), Dice.SimpleCast(modifier)), Colors.ColorReset)
case attacks > 1:
for i := 0; i < attacks; i++ {
fmt.Printf("%sAttack %d:%s\n", Colors.ColorBlue, i+1, Colors.ColorReset)
Dice.Cast(surfaces, diceThrows, modifier)
}
default:
Dice.Cast(surfaces, diceThrows, modifier)
}
}
func ParseFlags() {
flag.IntVar(&surfaces, "surfaces", 20, "Use to specify die surfaces, does not apply to advantage and disadvantage")
flag.IntVar(&surfaces, "s", 20, "Use to specify die surfaces, defaults to 20")
flag.IntVar(&diceThrows, "throws", 1, "Specify amount of dice to cast")
flag.IntVar(&diceThrows, "c", 1, "Specify amount of dice to cast")
flag.IntVar(&modifier, "modifier", 0, "Add modifier to result of rolls")
flag.IntVar(&modifier, "m", 0, "Add modifier to result of rolls")
flag.IntVar(&attacks, "attacks", 1, "Roll a set rules multiple times, does not apply to advantage and disadvantage")
flag.IntVar(&attacks, "a", 1, "Roll a set rules multiple times, does not apply to advantage and disadvantage")
flag.BoolVar(&advantage, "advantage", false, "Roll with advantage")
flag.BoolVar(&disadvantage, "disadvantage", false, "Roll with disadvantage")
flag.BoolVar(&coin, "coin", false, "Toss a coin, same as '-s 2'")
flag.Parse()
}