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 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, "s", "athletics", "Skill to parse") 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(skillCheck(getSkill(skillString))) } 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 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 += char.Misc.Proficiency * 2 case skill.Proficient: die += char.Misc.Proficiency } return die }