-
Notifications
You must be signed in to change notification settings - Fork 1
/
espers.go
127 lines (118 loc) · 2.19 KB
/
espers.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
// Copyright 2014
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
)
type (
Esper uint8
EspersOwned uint32
)
const (
Ramuh Esper = iota
Ifrit
Shiva
Siren
Midgardsormr
Maduin
Catoblepas
Bismark
CaitSith
Quetzalli
Valigarmanda
Odin
Raiden
Bahamut
Alexandr
Crusader
RagnarokEsper
Kirin
ZonaSeeker
Carbunkl
Phantom
Seraph
Golem
Unicorn
Fenrir
Lakshmi
Phoenix
Leviathan
Cactuar
Diabolos
Gilgamesh
NoEsper Esper = 255
)
var ename = map[Esper]string{
Ramuh: "Ramuh",
Ifrit: "Ifrit",
Shiva: "Shiva",
Siren: "Siren",
Midgardsormr: "Midgardsormr",
Maduin: "Maduin",
Catoblepas: "Catoblepas",
Bismark: "Bismark",
CaitSith: "Cait Sith",
Quetzalli: "Quetzalli",
Valigarmanda: "Valigarmanda",
Odin: "Odin",
Raiden: "Raiden",
Bahamut: "Bahamut",
Alexandr: "Alexandr",
Crusader: "Crusader",
RagnarokEsper: "Ragnarok",
Kirin: "Kirin",
ZonaSeeker: "Zona Seeker",
Carbunkl: "Carbunkl",
Phantom: "Phantom",
Seraph: "Seraph",
Golem: "Golem",
Unicorn: "Unicorn",
Fenrir: "Fenrir",
Lakshmi: "Lakshmi",
Phoenix: "Phoenix",
Leviathan: "Leviathan",
Gilgamesh: "Gilgamesh",
Cactuar: "Cactuar",
Diabolos: "Diabolos",
}
func (e Esper) MarshalJSON() ([]byte, error) {
return json.Marshal(ename[e])
}
func (e *Esper) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
if s == "" {
*e = NoEsper
return nil
}
for k, v := range ename {
if v == s {
*e = k
return nil
}
}
return fmt.Errorf("\"%s\" isn't a known Esper", s)
}
func (e EspersOwned) MarshalJSON() ([]byte, error) {
d := make(map[string]bool)
for k, v := range ename {
d[v] = e&(1<<uint32(k)) != 0
}
return json.Marshal(d)
}
func (e *EspersOwned) UnmarshalJSON(data []byte) error {
d := make(map[string]bool)
if err := json.Unmarshal(data, &d); err != nil {
return err
}
for k, v := range ename {
if d[v] {
*e |= (1 << uint32(k))
}
}
return nil
}