forked from travis-g/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
50 lines (43 loc) · 1.23 KB
/
result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package dice
import (
"context"
"github.com/pkg/errors"
)
// A Result is a value a die has rolled. By default, CritSuccess and CritFailure
// should be set to true if the maximum or minimum value of a die is rolled
// respectively, but the range in which a critical success/failure must be
// overridable through modifiers.
type Result struct {
Value float64 `json:"value"`
Dropped bool `json:"dropped,omitempty"`
CritSuccess bool `json:"crit,omitempty"`
CritFailure bool `json:"fumble,omitempty"`
}
// NewResult returns a new un-dropped, non-critical Result.
func NewResult(result float64) *Result {
return &Result{
Value: result,
}
}
// Drop marks a Result as dropped.
func (r *Result) Drop(ctx context.Context, drop bool) {
r.Dropped = drop
}
// IsDropped returns whether a Result was dropped.
func (r *Result) IsDropped(ctx context.Context) bool {
// If there's no result, the die can't have been dropped; it's unrolled.
if r == nil {
return false
}
return r.Dropped
}
// Total returns the Result's value or 0 if the result was dropped.
func (r *Result) Total(ctx context.Context) (float64, error) {
if r == nil {
return 0.0, errors.New("nil result")
}
if r.Dropped {
return 0.0, nil
}
return r.Value, nil
}