1 package battlemap 2 3 import ( 4 "encoding/json" 5 "strings" 6 ) 7 8 type links struct { 9 images, audio, chars, maps, music linkManager 10 } 11 12 func newLinks() links { 13 return links{ 14 images: make(linkManager), 15 audio: make(linkManager), 16 chars: make(linkManager), 17 maps: make(linkManager), 18 music: make(linkManager), 19 } 20 } 21 22 func (l *links) getLinkKey(key string) linkManager { 23 if strings.HasPrefix(key, "store-image") { 24 return l.images 25 } else if strings.HasPrefix(key, "store-audio") { 26 return l.audio 27 } else if strings.HasPrefix(key, "store-character") { 28 return l.chars 29 } else if strings.HasPrefix(key, "store-music") { 30 return l.music 31 } 32 return nil 33 } 34 35 type linkManager map[uint64]struct{} 36 37 func (l linkManager) setLink(id uint64) { 38 l[id] = struct{}{} 39 } 40 41 type tokenID struct { 42 Source uint64 `json:"src"` 43 } 44 45 func (l linkManager) setJSONLinks(j json.RawMessage) { 46 if len(j) > 0 { 47 switch j[0] { 48 case '[': 49 var ids []uint64 50 if err := json.Unmarshal(j, &ids); err == nil { 51 for _, id := range ids { 52 l.setLink(id) 53 } 54 } else { 55 var ids []tokenID 56 if err := json.Unmarshal(j, &ids); err == nil { 57 for _, id := range ids { 58 l.setLink(id.Source) 59 } 60 } 61 } 62 case '{': 63 var id tokenID 64 if err := json.Unmarshal(j, &id); err == nil { 65 l.setLink(id.Source) 66 } else { 67 ids := make(map[string]uint64) 68 if err := json.Unmarshal(j, &ids); err == nil { 69 for _, id := range ids { 70 l.setLink(id) 71 } 72 } else { 73 ids := make(map[string]tokenID) 74 if err := json.Unmarshal(j, &ids); err == nil { 75 for _, id := range ids { 76 l.setLink(id.Source) 77 } 78 } 79 } 80 } 81 default: 82 var id uint64 83 if err := json.Unmarshal(j, &id); err == nil { 84 l.setLink(id) 85 } 86 } 87 } 88 } 89