sheet-parser/main.go

138 lines
3.1 KiB
Go
Raw Normal View History

2022-08-19 18:09:05 +02:00
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
2022-08-20 16:32:50 +02:00
"gitlab.com/EternalWanderer/dice-roller/Colors"
2022-08-19 18:09:05 +02:00
"gitlab.com/EternalWanderer/dice-roller/Dice"
)
var (
2022-08-20 17:56:01 +02:00
path, skillString, saveString, checkString string
modifier, diceThrows, surfaces int
char Character
skillMap = make(map[string]int)
statMap = make(map[string]int)
advantage, disadvantage bool
2022-08-19 18:09:05 +02:00
)
func parseFlags() {
flag.StringVar(&path, "file", "stats.json", "fock me")
2022-08-20 16:32:50 +02:00
flag.StringVar(&path, "f", "stats.json", "fock me")
2022-08-20 17:56:01 +02:00
flag.StringVar(&skillString, "skill", "athletics", "Skill to parse")
flag.StringVar(&checkString, "check", "strength", "Stat check")
flag.StringVar(&saveString, "save", "strength", "Saving throw to... throw")
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-19 18:09:05 +02:00
flag.Parse()
}
2022-08-20 17:07:03 +02:00
func isError(err error) bool {
if err != nil {
fmt.Println(err.Error())
}
return (err != nil)
}
2022-08-19 18:09:05 +02:00
func readJson() {
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)
if isError(err) {
return
}
defer file.Close()
byteValue, _ := ioutil.ReadAll(file)
json.Unmarshal(byteValue, &char)
}
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 17:56:01 +02:00
fmt.Println("Skill check:", skillString)
2022-08-20 17:07:03 +02:00
fmt.Println(skillCheck(getSkill(skillString)))
2022-08-20 17:56:01 +02:00
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 {
// 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 {
// 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 {
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 += getProficiency()
2022-08-20 17:56:01 +02:00
}
return die + getModifier(stat)
2022-08-20 17:56:01 +02:00
}
2022-08-20 17:07:03 +02:00
func skillCheck(skill Skill) int {
var die int
die = rollDice()
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-19 18:09:05 +02:00
}
2022-08-20 17:07:03 +02:00
return die
2022-08-19 18:09:05 +02:00
}