Example #1
0
func (d dice) Roll() int {
	log.WithFields(log.Fields{
		"dice": d,
	}).Debug("rol")

	r := 0
	for i := 0; i < d.num; i++ {
		val := d.die.Roll()
		log.WithFields(log.Fields{
			"dice": d.die,
			"got":  val,
		}).Debug("rol")
		r += val
	}

	log.WithFields(log.Fields{
		"dice": d,
		"got":  r,
	}).Debug("rol")

	return r
}
Example #2
0
func Parse(notation string) (d *dice, err error) {
	log.WithFields(log.Fields{
		"notation": notation,
	}).Debug("parse")

	var num int
	var faces int

	dc := strings.Split(notation, "d")
	if len(dc) == 2 {
		s := dc[0]
		if s == "" {
			num = 1
		} else {
			num, err = strconv.Atoi(s)
			if err != nil {
				return nil, err
			}
		}
		faces, err = strconv.Atoi(dc[1])
		if err != nil {
			return nil, err
		}
	} else {
		return nil, errors.New("bad notation \"" + notation + "\"")
	}

	log.WithFields(log.Fields{
		"size":  num,
		"faces": faces,
	}).Debug("parse")

	d = &dice{
		num: num,
		die: die{Faces: faces},
	}
	return d, nil
}
Example #3
0
func main() {
	log.SetHandler(logfmt.New(os.Stderr))
	log.SetLevel(log.DebugLevel)

	port := os.Getenv("PORT")
	if port == "" {
		log.Fatal("$PORT was not set")
	}

	token = os.Getenv("SLACK_TOKEN")
	if token == "" {
		log.Info("$SLACK_TOKEN was not set")
	} else {
		log.WithFields(log.Fields{
			"token": token,
		}).Debug("token")
	}

	http.HandleFunc("/roll", rollDice)
	err := http.ListenAndServe(":"+port, nil)
	if err != nil {
		log.WithError(err).Fatal("Cannot listen")
	}
}