package main import ( "encoding/json" "flag" "fmt" "io/ioutil" "os" "gitlab.com/EternalWanderer/dice-roller/Colors" "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 ) 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.BoolVar(&advantage, "advantage", advantage, "Roll with advantage") flag.BoolVar(&disadvantage, "disadvantage", disadvantage, "Roll with disadvantage") flag.Parse() } func isError(err error) bool { if err != nil { fmt.Println(err.Error()) } return (err != nil) } func readJson() { fmt.Println("Opening file:", path) var file, err = os.Open(path) if isError(err) { return } defer file.Close() byteValue, _ := ioutil.ReadAll(file) json.Unmarshal(byteValue, &char) } 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 } } func main() { parseFlags() 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()) } func getProficiency() int { return (char.Misc.Level-1)/4 + 2 } func getSkill(skillName string) Skill { return char.Skills[skillMap[skillName]] } func getStat(statName string) Stat { return char.Stats[statMap[statName]] } func getModifier(stat Stat) int { return (stat.Score - 10) / 2 } func rollDice() int { var die, x, y int switch { case advantage: die, x, y = Dice.Advantage() case 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) return die } func statCheck(stat Stat) int { return rollDice() + getModifier(stat) } func savingThrow(stat Stat) int { die := rollDice() if stat.Proficient { die += getModifier(stat) } return die } func skillCheck(skill Skill) int { var die, x, y int switch { case advantage: die, x, y = Dice.Advantage() case 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) die += getModifier(getStat(skill.BaseStat)) switch { case skill.Expertise: die += getProficiency() * 2 case skill.Proficient: die += getProficiency() } return die }