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 {
|
|
|
|
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 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 += getModifier(stat)
|
|
|
|
}
|
|
|
|
return die
|
|
|
|
}
|
|
|
|
|
2022-08-20 17:07:03 +02:00
|
|
|
func skillCheck(skill Skill) int {
|
2022-08-20 16:32:50 +02:00
|
|
|
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)
|
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
|
|
|
}
|