123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package dmmo
- import (
- "encoding/json"
- "os"
- )
- type Index struct {
- Tiles []*Tile `json:"tiles"`
- }
- func NewIndex() *Index {
- return &Index{
- Tiles: []*Tile{},
- }
- }
- func (i *Index) Save(filename string) error {
- b, err := json.Marshal(i.Tiles)
- if err != nil {
- return err
- }
- err = os.WriteFile(filename+".json", b, os.ModePerm)
- return err
- }
- func LoadIndex(filename string) (*Index, error) {
- pay, err := os.ReadFile(filename + ".json")
- if err != nil {
- return nil, err
- }
- var idx *Index = &Index{
- Tiles: []*Tile{},
- }
- err = json.Unmarshal(pay, &idx.Tiles)
- return idx, err
- }
- func (i *Index) Has(name string) bool {
- for _, t := range i.Tiles {
- if t != nil && t.Name == name {
- return true
- }
- }
- return false
- }
- func (i *Index) Get(name string) *Tile {
- for _, t := range i.Tiles {
- if t != nil && t.Name == name {
- return t
- }
- }
- return nil
- }
- func (i *Index) Id(name string) (uint64, bool) {
- for id, t := range i.Tiles {
- if t != nil && t.Name == name {
- return uint64(id), true
- }
- }
- return 0, false
- }
- func (i *Index) Set(name, symbol, color string, auto ...bool) {
- if i.Has(name) {
- for id, t := range i.Tiles {
- if t != nil && t.Name == name {
- i.Tiles[id].Symbol = symbol
- i.Tiles[id].Color = color
- if len(auto) != 0 {
- i.Tiles[id].AutoLight = auto[0]
- }
- break
- }
- }
- } else {
- if len(auto) == 0 {
- i.Tiles = append(i.Tiles, NewTile(name, symbol, color, true))
- } else {
- i.Tiles = append(i.Tiles, NewTile(name, symbol, color, auto[0]))
- }
- }
- }
- func (i *Index) HasId(id uint64) bool {
- return len(i.Tiles) < int(id) || id > 0
- }
- func (i *Index) GetId(id uint64) *Tile {
- idy := int(id)
- for idx, t := range i.Tiles {
- if idx == idy {
- return t
- }
- }
- return nil
- }
- func (i *Index) SetId(id uint64, name, symbol, color string, auto ...bool) {
- if i.HasId(id) {
- i.Tiles[id].Name = name
- i.Tiles[id].Symbol = symbol
- i.Tiles[id].Color = color
- if len(auto) != 0 {
- i.Tiles[id].AutoLight = auto[0]
- }
- } else {
- if len(auto) == 0 {
- i.Tiles = append(i.Tiles, NewTile(name, symbol, color, true))
- } else {
- i.Tiles = append(i.Tiles, NewTile(name, symbol, color, auto[0]))
- }
- }
- }
|