-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFragment.go
133 lines (112 loc) · 2.72 KB
/
Fragment.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package eternal
import (
"fmt"
"image"
"log"
"github.com/CharlesHolbrow/synk"
)
// Fragment stores the contents of a subscription key
type Fragment struct {
Mutator synk.Mutator
MapName string
On [128]*Note
Cells map[string]*Cell
Keys []string
KeyMap map[string]bool
}
// NewFragment - create a Fragment
//
// Requires a clean Mutator
// image.Rectangle is inclusive of the minimum, exclusive of max
func NewFragment(mapName string, area image.Rectangle, mutator synk.Mutator) *Fragment {
size := area.Size()
chunkCount := size.X * size.Y
chunkKeys := make([]string, 0, chunkCount)
chunkKeyMap := make(map[string]bool, chunkCount)
for y := area.Min.Y; y < area.Max.Y; y++ {
for x := area.Min.X; x < area.Max.X; x++ {
subKey := makeSubKey(mapName, x, y)
chunkKeys = append(chunkKeys, subKey)
chunkKeyMap[subKey] = true
}
}
frag := &Fragment{
Keys: chunkKeys,
KeyMap: chunkKeyMap,
MapName: mapName,
Mutator: mutator,
Cells: make(map[string]*Cell),
}
objects, err := frag.Mutator.Load(frag.Keys)
if err != nil {
panic("Error initializing eternal Fragment: " + err.Error())
}
for _, v := range objects {
switch obj := v.(type) {
case *Cell:
frag.Cells[obj.TagID] = obj
case *Note:
if obj.Number < 0 || obj.Number > 127 {
mutator.Delete(obj)
continue
}
if frag.On[obj.Number] != nil {
mutator.Delete(obj)
continue
}
frag.On[obj.Number] = obj
}
}
fmt.Printf("Found %d Cells\n", len(frag.Cells))
return frag
}
// AddCell to the Fragment
func (frag *Fragment) AddCell(c *Cell) {
c.SetMapName(frag.MapName)
frag.Mutator.Create(c) // Ensures TagID
frag.Cells[c.TagID] = c
}
// RemoveCell from the fragment
func (frag *Fragment) RemoveCell(c *Cell) {
err := frag.Mutator.Delete(c)
if err != nil {
panic("error deleting cell with ID: " + c.TagID)
}
delete(frag.Cells, c.TagID)
}
// AddNote to the Fragment. The note's subscription key will be set.
// No-op if we already have a note with n.Number
func (frag *Fragment) AddNote(n *Note) {
// Ensure SubKey
if n.GetSubKey() == "" {
log.Println("you must now spceify a subkey when adding a Note")
return
}
if frag.On[n.Number] != nil {
return
}
frag.Mutator.Create(n) // Ensures ID
frag.On[n.Number] = n
}
// RemoveNote removes any note in the fragment with number equal to n.Number
func (frag *Fragment) RemoveNote(n *Note) {
if n.Number < 0 || n.Number > 127 {
return
}
removeMe := frag.On[n.Number]
if removeMe == nil {
return
}
frag.Mutator.Delete(removeMe)
frag.On[n.Number] = nil
}
// RemoveAllNotes is self explanitory
func (frag *Fragment) RemoveAllNotes() {
for id, note := range frag.On {
if note == nil {
continue
}
frag.Mutator.Delete(note)
frag.On[id] = nil
}
}