sheet-parser/main.go

103 lines
2.2 KiB
Go

package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"time"
"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 isError(err error) bool {
if err != nil {
fmt.Println(err.Error())
}
return (err != nil)
}
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 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() {
rand.Seed(time.Now().Unix())
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)
switch {
case skill.Expertise:
return die + GetModifier(GetStat(skill.BaseStat)) + char.Misc.Proficiency*2
case skill.Proficient:
return die + GetModifier(GetStat(skill.BaseStat)) + char.Misc.Proficiency
default:
return die + GetModifier(GetStat(skill.BaseStat))
}
}