2022-08-19 18:09:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-08-27 21:57:41 +02:00
|
|
|
"errors"
|
2022-08-19 18:09:05 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2022-08-20 22:01:13 +02:00
|
|
|
"github.com/fatih/color"
|
2022-08-19 18:09:05 +02:00
|
|
|
"gitlab.com/EternalWanderer/dice-roller/Dice"
|
|
|
|
)
|
|
|
|
|
2022-08-28 15:45:47 +02:00
|
|
|
const exampleFile string = "/etc/sheet-parser/example.json"
|
|
|
|
|
2022-08-19 18:09:05 +02:00
|
|
|
var (
|
2022-08-28 15:45:47 +02:00
|
|
|
path, pathFlag, skillString, saveString, statString string
|
2022-08-22 15:17:58 +02:00
|
|
|
modifier, diceThrows, surfaces int
|
|
|
|
X, Y int
|
|
|
|
char Character
|
|
|
|
skillMap = make(map[string]int)
|
|
|
|
statMap = make(map[string]int)
|
|
|
|
advantage, disadvantage, stat_list, skill_list, verbose, trivia bool
|
2022-08-19 18:09:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func parseFlags() {
|
2022-08-28 15:45:47 +02:00
|
|
|
flag.StringVar(&pathFlag, "file", pathFlag, "Used to point to character sheet")
|
|
|
|
flag.StringVar(&pathFlag, "f", pathFlag, "Used to point to character sheet")
|
2022-08-20 22:01:13 +02:00
|
|
|
flag.StringVar(&skillString, "skill", "", "Skill to parse")
|
|
|
|
flag.StringVar(&statString, "stat", "", "Stat check")
|
|
|
|
flag.StringVar(&saveString, "save", "", "Saving throw to... throw")
|
2022-08-20 22:13:12 +02:00
|
|
|
|
2022-08-20 16:32:50 +02:00
|
|
|
flag.BoolVar(&advantage, "advantage", advantage, "Roll with advantage")
|
|
|
|
flag.BoolVar(&disadvantage, "disadvantage", disadvantage, "Roll with disadvantage")
|
2022-08-20 22:13:12 +02:00
|
|
|
|
|
|
|
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")
|
2022-08-21 23:07:43 +02:00
|
|
|
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")
|
2022-08-20 22:13:12 +02:00
|
|
|
|
2022-08-22 15:17:58 +02:00
|
|
|
flag.BoolVar(&trivia, "t", false, "Print character name, level and proficiency")
|
|
|
|
flag.BoolVar(&trivia, "trivia", false, "Print character name, level and proficiency")
|
|
|
|
|
2022-08-19 18:09:05 +02:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2022-09-01 12:41:48 +02:00
|
|
|
func handleError(err error) {
|
2022-08-20 17:07:03 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
2022-08-27 22:57:07 +02:00
|
|
|
os.Exit(1)
|
2022-08-20 17:07:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:09:05 +02:00
|
|
|
func readJson() {
|
2022-08-28 15:45:47 +02:00
|
|
|
envPath, isSet := os.LookupEnv("CHARSHEET")
|
|
|
|
switch {
|
2022-08-28 16:45:30 +02:00
|
|
|
case len(pathFlag) > 0:
|
2022-08-28 15:45:47 +02:00
|
|
|
path = pathFlag
|
|
|
|
case isSet:
|
|
|
|
path = envPath
|
|
|
|
default:
|
|
|
|
path = exampleFile
|
|
|
|
}
|
|
|
|
|
2022-08-20 16:32:50 +02:00
|
|
|
fmt.Println("Opening file:", path)
|
2022-08-19 18:09:05 +02:00
|
|
|
var file, err = os.Open(path)
|
2022-09-01 12:41:48 +02:00
|
|
|
handleError(err)
|
2022-08-19 18:09:05 +02:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
byteValue, _ := ioutil.ReadAll(file)
|
2022-09-01 12:41:48 +02:00
|
|
|
|
|
|
|
err = json.Unmarshal(byteValue, &char)
|
|
|
|
handleError(err)
|
|
|
|
|
2022-08-19 18:09:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 16:32:50 +02:00
|
|
|
func initMaps() {
|
|
|
|
for i := 0; i < len(char.Skills); i++ {
|
|
|
|
skillMap[char.Skills[i].SkillName] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(char.Stats); i++ {
|
|
|
|
statMap[char.Stats[i].StatName] = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:09:05 +02:00
|
|
|
func main() {
|
|
|
|
parseFlags()
|
|
|
|
readJson()
|
2022-08-20 16:32:50 +02:00
|
|
|
initMaps()
|
|
|
|
|
2022-08-20 22:01:13 +02:00
|
|
|
switch {
|
2022-08-21 23:07:43 +02:00
|
|
|
|
2022-08-22 15:17:58 +02:00
|
|
|
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)
|
|
|
|
|
2022-08-27 17:39:32 +02:00
|
|
|
case stat_list && skill_list:
|
|
|
|
printStatList(verbose)
|
|
|
|
printSkillList(verbose)
|
2022-08-20 22:13:12 +02:00
|
|
|
case stat_list:
|
2022-08-27 17:39:32 +02:00
|
|
|
printStatList(verbose)
|
2022-08-20 22:13:12 +02:00
|
|
|
case skill_list:
|
2022-08-27 17:39:32 +02:00
|
|
|
printSkillList(verbose)
|
2022-08-21 23:07:43 +02:00
|
|
|
|
2022-08-20 22:01:13 +02:00
|
|
|
case advantage && disadvantage:
|
|
|
|
fmt.Println("You can't roll with both advantage and disadvantage")
|
|
|
|
os.Exit(1)
|
|
|
|
case len(saveString) > 0:
|
2022-08-27 21:57:41 +02:00
|
|
|
result, planeResult, err := savingThrow(getStat(saveString))
|
|
|
|
badCheck(saveString, err)
|
2022-08-20 22:01:13 +02:00
|
|
|
if advantage {
|
|
|
|
color.Yellow("Rolling %s saving throw with advantage...", saveString)
|
|
|
|
fmt.Printf("x: %d\ty: %d\n", X, Y)
|
2022-08-29 22:23:58 +02:00
|
|
|
fmt.Printf("Modifier: %d\n", result-planeResult)
|
2022-08-20 22:01:13 +02:00
|
|
|
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)
|
2022-08-29 22:23:58 +02:00
|
|
|
fmt.Printf("Modifier: %d\n", result-planeResult)
|
2022-08-20 22:01:13 +02:00
|
|
|
color.Red("%d\n", result)
|
|
|
|
} else {
|
|
|
|
color.Yellow("Rolling %s saving throw...", saveString)
|
2022-08-29 22:23:58 +02:00
|
|
|
fmt.Printf("Without modifier: %d\tModifier: %d\n", planeResult, result-planeResult)
|
2022-08-20 22:01:13 +02:00
|
|
|
color.Green("%d\n", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
case len(skillString) > 0:
|
2022-08-27 21:57:41 +02:00
|
|
|
result, planeResult, err := skillCheck(getSkill(skillString))
|
|
|
|
badCheck(skillString, err)
|
2022-08-20 22:01:13 +02:00
|
|
|
if advantage {
|
2022-08-23 22:19:25 +02:00
|
|
|
color.Yellow("Rolling %s check with advantage...", skillString)
|
2022-08-20 22:01:13 +02:00
|
|
|
fmt.Printf("x: %d\ty: %d\n", X, Y)
|
2022-08-29 22:23:58 +02:00
|
|
|
fmt.Printf("Modifier: %d\n", result-planeResult)
|
2022-08-20 22:01:13 +02:00
|
|
|
color.Green("%d\n", result)
|
|
|
|
} else if disadvantage {
|
2022-08-23 22:19:25 +02:00
|
|
|
color.Yellow("Rolling %s check with disadvantage...", skillString)
|
2022-08-20 22:01:13 +02:00
|
|
|
fmt.Printf("x: %d\ty: %d\n", X, Y)
|
2022-08-29 22:23:58 +02:00
|
|
|
fmt.Printf("Modifier: %d\n", result-planeResult)
|
2022-08-20 22:01:13 +02:00
|
|
|
color.Red("%d\n", result)
|
|
|
|
} else {
|
|
|
|
color.Yellow("Rolling %s check...", skillString)
|
2022-08-29 22:23:58 +02:00
|
|
|
fmt.Printf("Without modifier: %d\tModifier: %d\n", planeResult, result-planeResult)
|
2022-08-20 22:01:13 +02:00
|
|
|
color.Green("%d\n", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
case len(statString) > 0:
|
2022-08-27 21:57:41 +02:00
|
|
|
result, planeResult, err := statCheck(getStat(statString))
|
|
|
|
badCheck(statString, err)
|
2022-08-20 22:01:13 +02:00
|
|
|
if advantage {
|
|
|
|
color.Yellow("Rolling %s check with advantage...", statString)
|
|
|
|
fmt.Printf("x: %d\ty: %d\n", X, Y)
|
2022-08-29 22:23:58 +02:00
|
|
|
fmt.Printf("Modifier: %d\n", result-planeResult)
|
2022-08-20 22:01:13 +02:00
|
|
|
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)
|
2022-08-29 22:23:58 +02:00
|
|
|
fmt.Printf("Modifier: %d\n", result-planeResult)
|
2022-08-20 22:01:13 +02:00
|
|
|
color.Red("%d\n", result)
|
|
|
|
} else {
|
|
|
|
color.Yellow("Rolling %s check...", statString)
|
2022-08-29 22:23:58 +02:00
|
|
|
fmt.Printf("Without modifier: %d\tModifier: %d\n", planeResult, result-planeResult)
|
2022-08-20 22:01:13 +02:00
|
|
|
color.Green("%d\n", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2022-08-20 22:13:12 +02:00
|
|
|
flag.Usage()
|
2022-08-20 22:01:13 +02:00
|
|
|
}
|
2022-08-20 17:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getProficiency() int {
|
2022-08-20 18:56:23 +02:00
|
|
|
// https://worldbuildersjunction.com/what-is-proficiency-bonus-in-dd-5e-how-it-works-calculated/
|
2022-08-20 17:56:01 +02:00
|
|
|
return (char.Misc.Level-1)/4 + 2
|
2022-08-20 16:32:50 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 17:07:03 +02:00
|
|
|
func getSkill(skillName string) Skill {
|
2022-08-20 16:32:50 +02:00
|
|
|
return char.Skills[skillMap[skillName]]
|
|
|
|
}
|
2022-08-19 18:09:05 +02:00
|
|
|
|
2022-08-20 17:07:03 +02:00
|
|
|
func getStat(statName string) Stat {
|
2022-08-20 16:32:50 +02:00
|
|
|
return char.Stats[statMap[statName]]
|
|
|
|
}
|
|
|
|
|
2022-08-20 17:07:03 +02:00
|
|
|
func getModifier(stat Stat) int {
|
2022-08-20 18:56:23 +02:00
|
|
|
// https://worldbuildersjunction.com/dungeon-and-dragons-ability-scores-explained-for-beginners/
|
2022-08-20 16:44:47 +02:00
|
|
|
return (stat.Score - 10) / 2
|
|
|
|
}
|
|
|
|
|
2022-08-20 17:56:01 +02:00
|
|
|
func rollDice() int {
|
2022-08-20 22:01:13 +02:00
|
|
|
var die int
|
2022-08-20 17:56:01 +02:00
|
|
|
switch {
|
|
|
|
case advantage:
|
2022-08-20 22:01:13 +02:00
|
|
|
die, X, Y = Dice.Advantage()
|
2022-08-20 17:56:01 +02:00
|
|
|
case disadvantage:
|
2022-08-20 22:01:13 +02:00
|
|
|
die, X, Y = Dice.Disadvantage()
|
2022-08-20 17:56:01 +02:00
|
|
|
default:
|
|
|
|
die = Dice.SimpleCast()
|
|
|
|
}
|
2022-08-20 22:01:13 +02:00
|
|
|
switch die {
|
|
|
|
case 20:
|
|
|
|
color.Magenta("Natural 20!\n")
|
|
|
|
case 1:
|
|
|
|
color.Magenta("Natural 1!\n")
|
|
|
|
}
|
2022-08-20 17:56:01 +02:00
|
|
|
return die
|
|
|
|
}
|
2022-08-27 21:57:41 +02:00
|
|
|
func badCheck(errorMessage string, err error) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println(errorMessage)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
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:")
|
|
|
|
}
|
2022-08-20 17:56:01 +02:00
|
|
|
|
2022-08-27 21:57:41 +02:00
|
|
|
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()
|
2022-08-20 17:56:01 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-27 21:57:41 +02:00
|
|
|
func savingThrow(stat Stat) (int, int, error) {
|
2022-08-20 17:56:01 +02:00
|
|
|
die := rollDice()
|
2022-08-27 21:57:41 +02:00
|
|
|
planeDie := die
|
2022-08-20 17:56:01 +02:00
|
|
|
|
|
|
|
if stat.Proficient {
|
2022-08-20 18:56:23 +02:00
|
|
|
die += getProficiency()
|
2022-08-20 17:56:01 +02:00
|
|
|
}
|
2022-08-27 21:57:41 +02:00
|
|
|
return die + getModifier(stat), planeDie, statErrorCheck()
|
2022-08-20 17:56:01 +02:00
|
|
|
}
|
|
|
|
|
2022-08-27 21:57:41 +02:00
|
|
|
func skillCheck(skill Skill) (int, int, error) {
|
2022-08-27 16:57:54 +02:00
|
|
|
die := rollDice()
|
2022-08-28 16:03:07 +02:00
|
|
|
if char.Misc.ReliableTalent && skill.Proficient && die < 10 {
|
|
|
|
die = 10
|
|
|
|
}
|
2022-08-27 21:57:41 +02:00
|
|
|
planeDie := die
|
2022-08-20 17:07:03 +02:00
|
|
|
|
|
|
|
die += getModifier(getStat(skill.BaseStat))
|
|
|
|
|
2022-08-20 16:32:50 +02:00
|
|
|
switch {
|
|
|
|
case skill.Expertise:
|
2022-08-20 17:56:01 +02:00
|
|
|
die += getProficiency() * 2
|
2022-08-20 16:32:50 +02:00
|
|
|
case skill.Proficient:
|
2022-08-20 17:56:01 +02:00
|
|
|
die += getProficiency()
|
2022-08-28 16:03:07 +02:00
|
|
|
case !skill.Proficient && char.Misc.JackOfAllTrades:
|
|
|
|
die += (getProficiency() / 2)
|
2022-08-19 18:09:05 +02:00
|
|
|
}
|
2022-08-27 21:57:41 +02:00
|
|
|
return die, planeDie, skillErrorCheck()
|
2022-08-19 18:09:05 +02:00
|
|
|
}
|
2022-08-27 21:57:41 +02:00
|
|
|
|
2022-08-27 17:39:32 +02:00
|
|
|
func printStatList(verbose bool) {
|
|
|
|
color.Magenta("Listing stats...")
|
|
|
|
if verbose {
|
|
|
|
var proficiency string
|
|
|
|
for i := 0; i < len(char.Stats); i++ {
|
|
|
|
name := char.Stats[i].StatName
|
|
|
|
isProficient := char.Stats[i].Proficient
|
|
|
|
if isProficient {
|
|
|
|
proficiency = "Proficient"
|
|
|
|
} else {
|
|
|
|
proficiency = "Not proficient"
|
|
|
|
}
|
|
|
|
score := char.Stats[i].Score
|
2022-09-07 16:04:41 +02:00
|
|
|
fmt.Printf("Stat: %13s\t%s\tStat score: %2d\tStat modifier: %2d\n", name, proficiency, score, getModifier(char.Stats[i]))
|
2022-08-27 17:39:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := 0; i < len(char.Stats); i++ {
|
|
|
|
fmt.Println(char.Stats[i].StatName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printSkillList(verbose bool) {
|
|
|
|
color.Magenta("Listing skills...")
|
|
|
|
var proficiency string
|
|
|
|
var expertise string
|
|
|
|
if verbose {
|
|
|
|
for i := 0; i < len(char.Skills); i++ {
|
|
|
|
name := char.Skills[i].SkillName
|
|
|
|
localModifier := getModifier(getStat(char.Skills[i].BaseStat))
|
|
|
|
if char.Skills[i].Proficient {
|
|
|
|
proficiency = "Proficient"
|
|
|
|
localModifier += getProficiency()
|
|
|
|
} else {
|
|
|
|
proficiency = "Not proficient"
|
|
|
|
}
|
|
|
|
if char.Skills[i].Expertise {
|
|
|
|
expertise = "Has expertise"
|
|
|
|
localModifier += getProficiency()
|
|
|
|
} else {
|
|
|
|
expertise = "Doesn't have expertise"
|
|
|
|
}
|
2022-09-07 16:04:41 +02:00
|
|
|
fmt.Printf("Skill:%16s\tSkill modifier:%3d\t%s\t%s\n", name, localModifier, proficiency, expertise)
|
2022-08-27 17:39:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := 0; i < len(char.Skills); i++ {
|
|
|
|
fmt.Println(char.Skills[i].SkillName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|