Hi! I roll dice for other people's games
travis-g / dice Goto Github PK
View Code? Open in Web Editor NEWGolang library and CLI for rolling dice
License: MIT License
Golang library and CLI for rolling dice
License: MIT License
Hi! I roll dice for other people's games
The package utilizes Golang contexts within its API, but doesn't effectively utilize them for tracking individual roll requests' data.
contextKey
valuesI noticed people having issues forgetting to supply a number to keep when rolling with (dis)advantage. Often going for kh/kl instead of kh1/kl1.
It would be nice if the library took this into account and defaulted to 1 when requesting to just keep the highest or lowest value.
Request contexts should be used to prevent unnecessary/excess rolls of dice when a threshold is reached or the roll request is cancelled, to conserve system entropy.
This is to gear up for service-oriented rolling (CLI should pass context.Background()
)
cmd/dice
on default branch develop
doesn't build.
Output:
$ go get -u github.com/travis-g/dice/...
# github.com/travis-g/dice/cmd/dice
go/src/github.com/travis-g/dice/cmd/dice/main.go:24:17: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
go/src/github.com/travis-g/dice/cmd/dice/main.go:28:4: unknown field 'EnvVar' in struct literal of type cli.StringFlag
go/src/github.com/travis-g/dice/cmd/dice/main.go:30:17: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
go/src/github.com/travis-g/dice/cmd/dice/main.go:34:4: unknown field 'EnvVar' in struct literal of type cli.StringFlag
go/src/github.com/travis-g/dice/cmd/dice/main.go:39:17: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
go/src/github.com/travis-g/dice/cmd/dice/main.go:43:4: unknown field 'EnvVar' in struct literal of type cli.StringFlag
go/src/github.com/travis-g/dice/cmd/dice/main.go:47:15: cannot use []cli.Command literal (type []cli.Command) as type []*cli.Command in assignment
Example:
$ ./dice roll d999999999
6.1946175e+07 => 6.1946175e+07
It's a problem where float64
(but still integer) dice results are being formatted with a %v
formatting directive; use %.0f
for any integer dice results, and strconv.FormatFloat
for any non-integer floats.
Add functions to walk and process dice in a Roller tree recursively.
For efficiency in string building/concatenation post-Go 1.10, use strings.Builder
s rather than bytes.Buffer
s.
Example strings.Builder
code:
var b strings.Builder
for i := 0; i < len(keys); i++ {
b.WriteString(keys[i])
}
return b.String()
Example of bytes.Buffer
and WriteString
: https://github.com/travis-g/dice/blob/develop/math/math.go#L106-L112
References:
{
"roll": "2d6+3d4+3",
"result": 17,
"rolls": [
{
"roll": "2d6",
"result": 7,
"rolls": [
{
"roll": "d6",
"result": 6
},
{
"roll": "d6",
"result": 1
}
],
"expression": "6+1"
},
{
"roll": "3d4",
"result": 7,
"rolls": [
{
"roll": "d4",
"result": 1
},
{
"roll": "d4",
"result": 4
},
{
"roll": "d4",
"result": 2
}
],
"expression": "1+4+2"
}
],
"expression": "(6+1)+(1+4+2)+3"
}
Die
struct is already available but unimplemented.
roll
properties could become size
integers or remain a toString()
-style representation2d6+3d4+3
is broken down into child rolls: 2d6
and 3d4
.2d6
and 3d4
individually.d6
rolls and three (3) d4
rolls,d6
dice and d4
dice (5 child requests total)2d6
is replaced with (7+1)
3d4
is replaced with (1+4+2)
(6+1)+(1+4+2)+3
→ 17
17
is returned to the requester along with full roll stack.Add checks to ensure that dice structs satisfy interfaces at compile time.
var _ Rollable = (*Die)(nil) // Verify *Die implements Rollable
var _ Rollable = (*FateDie)(nil) // Verify *FateDie implements Rollable
Refs:
A GitHub Action for CI testing would be excellent. This repository is not active enough to run up costs but actions to test PRs on submit would reduce manual effort to pull, rebase and make test
a contribution.
GET /roll/:id
requests return a specific roll or a 404
if roll does not exists/has expiredDie
objectsSupport exploding dice with added comparison operators.
Examples:
3d6!
to explode on 6s,3d6!>5
to explode on rolls of 5 or 6Notes:
Validate
function to ensure modifier is not impossible to settleGroup.Less
does not take into account result values of dropped dice, leading to inconsistently sorted dice groups.
Lines 209 to 230 in 3c4680c
Unit tests are missing for various functions to ensure that string parsing to various object types ignore capitalization.
dice
package is lib is for dice; CLI is sub-package main
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.