2022-08-19 18:09:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
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 16:32:50 +02:00
|
|
|
path, skillString, saveString string
|
2022-08-19 18:09:05 +02:00
|
|
|
modifier, diceThrows, surfaces int
|
|
|
|
char Character
|
2022-08-20 16:32:50 +02:00
|
|
|
skillMap = make(map[string]int)
|
|
|
|
statMap = make(map[string]int)
|
|
|
|
advantage, disadvantage bool
|
2022-08-19 18:09:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2022-08-20 16:32:50 +02:00
|
|
|
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")
|
2022-08-19 18:09:05 +02:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
parseFlags()
|
|
|
|
readJson()
|
2022-08-20 16:32:50 +02:00
|
|
|
initMaps()
|
|
|
|
|
|
|
|
fmt.Println(SkillCheck(GetSkill(skillString)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSkill(skillName string) Skill {
|
|
|
|
return char.Skills[skillMap[skillName]]
|
|
|
|
}
|
2022-08-19 18:09:05 +02:00
|
|
|
|
2022-08-20 16:32:50 +02:00
|
|
|
func GetSkillModifier(skill Skill) int {
|
|
|
|
return GetStat(skill.BaseStat).Modifier
|
2022-08-19 18:09:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 16:32:50 +02:00
|
|
|
func GetStat(statName string) Stat {
|
|
|
|
return char.Stats[statMap[statName]]
|
|
|
|
}
|
|
|
|
|
|
|
|
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 + GetSkillModifier(skill) + char.Misc.Proficiency*2
|
|
|
|
case skill.Proficient:
|
|
|
|
return die + GetSkillModifier(skill) + char.Misc.Proficiency
|
|
|
|
default:
|
|
|
|
return die + GetSkillModifier(skill)
|
2022-08-19 18:09:05 +02:00
|
|
|
}
|
|
|
|
}
|