Coder Social home page Coder Social logo

dice's Introduction

Hi! I roll dice for other people's games

dice's People

Contributors

travis-g avatar woutermeuwis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dice's Issues

Track dice rolls within request contexts

The package utilizes Golang contexts within its API, but doesn't effectively utilize them for tracking individual roll requests' data.

  • Use package contextKey values
  • Determine aptitude or method for tracking dice rolls outside of a standard request context, such as for asynchronous background rolls

Enable defaults for kh/kd

I 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.

REPL usability/QoL improvements

  • Enter on a blank prompt should re-evaluate the most recent expression
  • and should traverse REPL's eval history
  • Ctrl + D (^D/EOF) should exit REPL

pass request contexts to rolls

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 develop branch doesn't build

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

stringifying large dice roll results expands to scientific notation

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.

use strings.Builder rather than bytes.Buffer

For efficiency in string building/concatenation post-Go 1.10, use strings.Builders rather than bytes.Buffers.

References:

Design doc for desired API

Example Response

{
    "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"
}
  • The request handler should recurse and dogfood itself.
  • Die struct is already available but unimplemented.
    • roll properties could become size integers or remain a toString()-style representation

Request Flow

  1. Request for 2d6+3d4+3 is broken down into child rolls: 2d6 and 3d4.
  2. Handler calls the API to roll 2d6 and 3d4 individually.
  3. Handler's requests are broken down to down into child rolls: two (2) d6 rolls and three (3) d4 rolls,
  4. Child requests made again to roll the individual d6 dice and d4 dice (5 child requests total)
  5. Child roll results are summed/evaluated and returned back up through the handler stack:
    • 2d6 is replaced with (7+1)
    • 3d4 is replaced with (1+4+2)
  6. Original request is evaluated: (6+1)+(1+4+2)+317
  7. Final 17 is returned to the requester along with full roll stack.

add GitHub Action to test and compile the library

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.

Cache roll results in memory

Requirements

  • Use go-cache as an in-memory store for rolled dice.
    • go-cache supports expiration times
  • Each roll is assigned a UUID before it's UUID and pointer are cached with a TTL
  • GET /roll/:id requests return a specific roll or a 404 if roll does not exists/has expired
  • Should be expanded down to individual Die objects

support exploding dice

Support exploding dice with added comparison operators.

Examples:

  • 3d6! to explode on 6s,
  • 3d6!>5 to explode on rolls of 5 or 6

Notes:

  • New dice should be added to parent dice Group,
  • Validate function to ensure modifier is not impossible to settle

Dropped dice of Groups not properly sorted

Group.Less does not take into account result values of dropped dice, leading to inconsistently sorted dice groups.

dice/rollable.go

Lines 209 to 230 in 3c4680c

// Less determines the sort order of Rollers in a Group.
func (g Group) Less(i, j int) bool {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// if i is dropped, sort i after
if g[i].IsDropped(ctx) {
return true
}
// if j is dropped, sort j after
if g[j].IsDropped(ctx) {
return false
}
// if i's total is less than j, sort i after
it, _ := g[i].Total(ctx)
jt, _ := g[j].Total(ctx)
if it < jt {
return true
}
return false
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.