roll

package module
v0.0.0-...-bae033a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2016 License: MIT Imports: 8 Imported by: 1

README

Roll Go Report Card License GoDoc Build Status

A simple dice roll parsing engine that (mostly) supports the Roll20 Dice Rolling Language Specification

Usage

package main

import (
	"fmt"
	"math/rand"
	"os"
	"time"

	"github.com/darkliquid/roll"
)

func main() {
	rand.Seed(time.Now().UnixNano())
	out, err := roll.Parse(os.Stdin)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	fmt.Println(out)
}

then run echo '3d6+1' | go run main.go to get some output like:

Rolled "3d6+1" and got 6, 6, 4 for a total of 17

Documentation

Index

Constants

View Source
const (
	// FateBlank is the symbol for a blank Fate die face
	FateBlank = "☐"
	// FateMinus is the symbol for a minus Fate die face
	FateMinus = "⊟"
	// FatePlus is the symbol for a plus Fate die face
	FatePlus = "⊞"
)

Variables

This section is empty.

Functions

func Parse

func Parse(r io.Reader) (string, error)

Parse reads from an io.Reader and generates a dice roll result string

func ParseString

func ParseString(rollStr string) (string, error)

ParseString takes a string and does a dice roll

Types

type ComparisonOp

type ComparisonOp struct {
	Type  ComparisonType
	Value int
}

ComparisonOp is the operation that defines how you compare against a roll to determine whether the result counts

func (*ComparisonOp) Match

func (op *ComparisonOp) Match(val int) bool

Match returns true if the given value compares positively against the op val

func (*ComparisonOp) String

func (op *ComparisonOp) String() string

String returns the string representation of the comparison operator

type ComparisonType

type ComparisonType int

ComparisonType is the type of comparison

const (
	// Equals matches only values that are equal to the comparison value
	Equals ComparisonType = iota
	// GreaterThan matches only values greater than the comparison value
	GreaterThan
	// LessThan matches only values less than the comparison value
	LessThan
)

type DiceRoll

type DiceRoll struct {
	Multiplier int
	Die        Die
	Modifier   int
	Exploding  *ExplodingOp
	Limit      *LimitOp
	Success    *ComparisonOp
	Failure    *ComparisonOp
	Rerolls    []RerollOp
	Sort       SortType
}

DiceRoll is an individual Dice Roll

func (*DiceRoll) Roll

func (dr *DiceRoll) Roll() (result Result)

Roll gets the results of rolling the dice that make up a dice roll

func (*DiceRoll) String

func (dr *DiceRoll) String() string

String represents the dice roll as a string

type Die

type Die interface {
	Roll() DieRoll
	String() string
}

Die is the interface allDice must confirm to

type DieRoll

type DieRoll struct {
	Result int
	Symbol string
}

DieRoll is the result of a die roll

type ErrAmbiguousModifier

type ErrAmbiguousModifier int

ErrAmbiguousModifier is raised when a multiplier was misread as a modifier

func (ErrAmbiguousModifier) Error

func (e ErrAmbiguousModifier) Error() string

type ErrEndOfRoll

type ErrEndOfRoll string

ErrEndOfRoll is raised when parsing a roll has reached a terminating token

func (ErrEndOfRoll) Error

func (e ErrEndOfRoll) Error() string

type ErrUnexpectedToken

type ErrUnexpectedToken string

ErrUnexpectedToken is raised on unexpected tokens

func (ErrUnexpectedToken) Error

func (e ErrUnexpectedToken) Error() string

type ErrUnknownDie

type ErrUnknownDie string

ErrUnknownDie is raised on unrecognised die types

func (ErrUnknownDie) Error

func (e ErrUnknownDie) Error() string

type ExplodingOp

type ExplodingOp struct {
	*ComparisonOp
	Type ExplodingType
}

ExplodingOp is the operation that defines how a dice roll explodes

func (ExplodingOp) String

func (e ExplodingOp) String() (output string)

String returns the string representation of the exploding dice operation

type ExplodingType

type ExplodingType int

ExplodingType is the type of exploding die

const (
	// Exploding adds new dice for each roll satisfying the exploding condition
	Exploding ExplodingType = iota
	// Compounded adds to a single new result for each roll
	Compounded
	// Penetrating is like Exploding, except each die result has a -1 modifier
	Penetrating
)

type FateDie

type FateDie int

FateDie is a die representing the typical Fate/Fudge die

func (FateDie) Roll

func (d FateDie) Roll() DieRoll

Roll generates a random number and the appropriate symbol

func (FateDie) String

func (d FateDie) String() string

String returns the string representation of the FateDie type

type GroupedRoll

type GroupedRoll struct {
	Rolls    []Roll
	Modifier int
	Limit    *LimitOp
	Success  *ComparisonOp
	Failure  *ComparisonOp
	Combined bool
	Negative bool
}

GroupedRoll is a group of other rolls. You can have nested groups.

func (*GroupedRoll) Roll

func (gr *GroupedRoll) Roll() (result Result)

Roll gets the results of rolling the dice that make up a dice roll

func (*GroupedRoll) String

func (gr *GroupedRoll) String() (output string)

String represents the grouped roll as a string

type LimitOp

type LimitOp struct {
	Amount int
	Type   LimitType
}

LimitOp is the operation that defines how dice roll results are limited

func (LimitOp) String

func (op LimitOp) String() (output string)

type LimitType

type LimitType int

LimitType is the type of roll limitation

const (
	// KeepHighest indicated we should keep the highest results
	KeepHighest LimitType = iota
	// KeepLowest indicated we should keep the lowest results
	KeepLowest
	// DropHighest indicated we should drop the highest results
	DropHighest
	// DropLowest indicated we should drop the lowest results
	DropLowest
)

type NormalDie

type NormalDie int

NormalDie is a die representing an N-sided die

func (NormalDie) Roll

func (d NormalDie) Roll() DieRoll

Roll generates a random number and the appropriate symbol

func (NormalDie) String

func (d NormalDie) String() string

String returns the string representation of the NormalDie type

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is our dice rolling parser

func NewParser

func NewParser(r io.Reader) *Parser

NewParser returns a Parser instance

func (*Parser) Parse

func (p *Parser) Parse() (roll Roll, err error)

Parse parses a Roll statement.

type RerollOp

type RerollOp struct {
	*ComparisonOp
	Once bool
}

RerollOp is the operation that defines how dice are rerolled

func (RerollOp) String

func (e RerollOp) String() (output string)

String returns the string representation of the exploding dice operation

type Result

type Result struct {
	Results   []DieRoll
	Total     int
	Successes int
}

Result is a collection of die rolls and a count of successes

func (*Result) Len

func (r *Result) Len() int

Len is the number of results

func (*Result) Less

func (r *Result) Less(i, j int) bool

Less return true if DieRoll at index i is less than the one at index j

func (*Result) Swap

func (r *Result) Swap(i, j int)

Swap swaps the DieRoll at index i with the one at index j

type Roll

type Roll interface {
	Roll() Result
	String() string
}

Roll is any kind of roll

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner is our lexical scanner for dice roll strings

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a new instance of scanner

func (*Scanner) Scan

func (s *Scanner) Scan() (tok Token, lit string)

Scan returns the next token and literal value

type SortType

type SortType int

SortType is the type of sorting to use for dice roll results

const (
	// Unsorted doesn't sort dice rolls
	Unsorted SortType = iota
	// Ascending sorts dice rolls from lowest to highest
	Ascending
	// Descending sorts dice rolls from highest to lowest
	Descending
)

func (SortType) String

func (t SortType) String() string

String return the string representation of a SortType value

type Token

type Token int

Token is an input token

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL