-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
75 lines (66 loc) · 2.66 KB
/
state.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"math"
)
var state *globalState
type globalState struct {
NumSides int `default:"20"`
NumRows int `default:"5"`
SideRollCounts []int
SideRollCounters []SideRollCounterData // Will need set after initState() is called and numSides is known.
CounterGrid [][]SideRollCounterData // TODO: Is this needed? Will it help with Styling the counter grid?
TotalRollCount int `default:"0"`
IsMinNumRollsMet bool
DieBalanceComputationValues *ComputedPearsonsChiSqValues
}
func initState() {
state = &globalState{}
}
func (s *globalState) initGrid(numSides int) {
s.NumSides = numSides
s.NumRows = int(math.Ceil(float64(numSides / 4)))
s.SideRollCounters, s.SideRollCounts = newRollCounters(numSides)
s.DieBalanceComputationValues = NewComputedPCSValues(numSides)
s.TotalRollCount = 0
}
func (s *globalState) GetRollCounterDataForSide(sideNumber int) *SideRollCounterData {
return &s.SideRollCounters[sideNumber-1]
}
func newRollCounters(numSides int) (srcData []SideRollCounterData, counts []int) {
srcData = make([]SideRollCounterData, numSides)
counts = make([]int, numSides)
for i := 0; i < numSides; i++ {
counts[i] = 0
srcData[i] = SideRollCounterData{
SideNumber: i + 1,
RowIndex: i % 4,
IndexOfRow: int(i / 4),
StyleContentString: fmt.Sprintf(`.pressed-%v span:before, .pressed-%v span:after {content:"%v"}`, i+1, i+1, i+1),
Count: &counts[i],
}
}
return
}
func IncrementRollCountsInState(sideNumber int) {
state.SideRollCounts[sideNumber-1]++
state.TotalRollCount++
state.IsMinNumRollsMet = state.DieBalanceComputationValues.DieConstants.MinNumberOfRolls <= state.TotalRollCount
if state.IsMinNumRollsMet {
state.DieBalanceComputationValues.ComputePChSqValues(state.TotalRollCount, state.SideRollCounts)
}
}
func DecrementRollCountsInState(sideNumber int) {
if state.TotalRollCount > 0 && state.SideRollCounts[sideNumber-1] > 0 {
previousTotal := state.TotalRollCount
state.SideRollCounts[sideNumber-1]--
state.TotalRollCount--
state.IsMinNumRollsMet = state.DieBalanceComputationValues.DieConstants.MinNumberOfRolls <= state.TotalRollCount
if state.DieBalanceComputationValues.DieConstants.MinNumberOfRolls <= state.TotalRollCount {
state.DieBalanceComputationValues.ComputePChSqValues(state.TotalRollCount, state.SideRollCounts)
}
if !state.IsMinNumRollsMet && previousTotal == state.DieBalanceComputationValues.DieConstants.MinNumberOfRolls {
state.DieBalanceComputationValues.ComputePChSqValues(state.TotalRollCount, state.SideRollCounts)
}
}
}