summaryrefslogtreecommitdiff
blob: 2a3df55c3afe4a92895b19208fd0260c6c077997 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
module BattleCharacters.Struct.Equipment exposing
   (
      Type,
      Unresolved,
      get_primary_weapon,
      get_secondary_weapon,
      get_armor,
      get_portrait,
      get_glyph_board,
      get_glyphs,
      set_primary_weapon,
      set_secondary_weapon,
      set_armor,
      set_portrait,
      set_glyph_board,
      set_glyphs,
      set_glyph,
      ref_decoder,
      ref_encoder,
      resolve,
      to_ref
   )

-- Elm -------------------------------------------------------------------------
import Array

import List

import Json.Decode
import Json.Decode.Pipeline

import Json.Encode

-- Battle ----------------------------------------------------------------------
import BattleCharacters.Struct.Weapon
import BattleCharacters.Struct.Armor
import BattleCharacters.Struct.Portrait
import BattleCharacters.Struct.Glyph
import BattleCharacters.Struct.GlyphBoard

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------
type alias Type =
   {
      primary : BattleCharacters.Struct.Weapon.Type,
      secondary : BattleCharacters.Struct.Weapon.Type,
      armor : BattleCharacters.Struct.Armor.Type,
      portrait : BattleCharacters.Struct.Portrait.Type,
      glyph_board : BattleCharacters.Struct.GlyphBoard.Type,
      glyphs : (Array.Array BattleCharacters.Struct.Glyph.Type)
   }

type alias Unresolved =
   {
      primary : BattleCharacters.Struct.Weapon.Ref,
      secondary : BattleCharacters.Struct.Weapon.Ref,
      armor : BattleCharacters.Struct.Armor.Ref,
      portrait : BattleCharacters.Struct.Portrait.Ref,
      glyph_board : BattleCharacters.Struct.GlyphBoard.Ref,
      glyphs : (Array.Array BattleCharacters.Struct.Glyph.Ref)
   }

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
get_primary_weapon : Type -> BattleCharacters.Struct.Weapon.Type
get_primary_weapon equipment = equipment.primary

get_secondary_weapon : Type -> BattleCharacters.Struct.Weapon.Type
get_secondary_weapon equipment = equipment.secondary

get_armor : Type -> BattleCharacters.Struct.Armor.Type
get_armor equipment = equipment.armor

get_portrait : Type -> BattleCharacters.Struct.Portrait.Type
get_portrait equipment = equipment.portrait

get_glyph_board : Type -> BattleCharacters.Struct.GlyphBoard.Type
get_glyph_board equipment = equipment.glyph_board

get_glyphs : Type -> (Array.Array BattleCharacters.Struct.Glyph.Type)
get_glyphs equipment = equipment.glyphs

set_primary_weapon : BattleCharacters.Struct.Weapon.Type -> Type -> Type
set_primary_weapon wp equipment = { equipment | primary = wp }

set_secondary_weapon : BattleCharacters.Struct.Weapon.Type -> Type -> Type
set_secondary_weapon wp equipment = { equipment | secondary = wp }

set_armor : BattleCharacters.Struct.Armor.Type -> Type -> Type
set_armor ar equipment = { equipment | armor = ar }

set_portrait : BattleCharacters.Struct.Portrait.Type -> Type -> Type
set_portrait pt equipment = { equipment | portrait = pt }

set_glyph_board : BattleCharacters.Struct.GlyphBoard.Type -> Type -> Type
set_glyph_board gb equipment =
   {equipment |
      glyph_board = gb,
      glyphs =
         (Array.repeat
            (List.length (BattleCharacters.Struct.GlyphBoard.get_slots gb))
            (BattleCharacters.Struct.Glyph.none)
         )
   }

set_glyphs : (Array.Array BattleCharacters.Struct.Glyph.Type) -> Type -> Type
set_glyphs gl equipment = { equipment | glyphs = gl }

set_glyph : Int -> BattleCharacters.Struct.Glyph.Type -> Type -> Type
set_glyph index glyph equipment =
   { equipment | glyphs = (Array.set index glyph equipment.glyphs) }

decoder : (Json.Decode.Decoder Unresolved)
decoder =
   (Json.Decode.succeed
      Unresolved
      |> (Json.Decode.Pipeline.required "pr" Json.Decode.string)
      |> (Json.Decode.Pipeline.required "sc" Json.Decode.string)
      |> (Json.Decode.Pipeline.required "ar" Json.Decode.string)
      |> (Json.Decode.Pipeline.required "pt" Json.Decode.string)
      |> (Json.Decode.Pipeline.required "gb" Json.Decode.string)
      |>
         (Json.Decode.Pipeline.required
            "gl"
            (Json.Decode.array (Json.Decode.string))
         )
   )

encode : Unresolved -> Json.Encode.Value
encode ref =
   (Json.Encode.object
      [
         ("pr", (Json.Encode.string ref.primary)),
         ("sc", (Json.Encode.string ref.secondary)),
         ("ar", (Json.Encode.string ref.armor)),
         ("pt", (Json.Encode.string ref.portrait)),
         ("gb", (Json.Encode.string ref.glyph_board)),
         ("gl", (Json.Encode.array (Array.map (Json.Encode.string) ref.gl)))
      ]
   )

resolve : (
      (
         BattleCharacters.Struct.Weapon.Ref ->
         BattleCharacters.Struct.Weapon.Type
      ) ->
      (
         BattleCharacters.Struct.Armor.Ref ->
         BattleCharacters.Struct.Armor.Type
      ) ->
      (
         BattleCharacters.Struct.Portrait.Ref ->
         BattleCharacters.Struct.Portrait.Type
      ) ->
      (
         BattleCharacters.Struct.GlyphBoard.Ref ->
         BattleCharacters.Struct.GlyphBoard.Type
      ) ->
      (
         BattleCharacters.Struct.Glyph.Ref ->
         BattleCharacters.Struct.Glyph.Type
      ) ->
      Unresolved ->
      Type
   )
resolve resolve_wp resolve_ar resolve_pt resolve_gb resolve_gl ref =
   {
      primary = (resolve_wp ref.primary),
      secondary = (resolve_wp ref.secondary),
      armor = (resolve_ar ref.armor),
      portrait = (resolve_pt ref.portrait),
      glyph_board = (resolve_gb ref.glyph_board),
      glyphs = (Array.map (resolve_gl) ref.glyphs)
   }

to_unresolved : Type -> Unresolved
to_unresolved equipment =
   {
      primary = (BattleCharacters.Struct.Weapon.get_id equipment.primary),
      secondary = (BattleCharacters.Struct.Weapon.get_id equipment.secondary),
      armor = (BattleCharacters.Struct.Armor.get_id equipment.armor),
      portrait = (BattleCharacters.Struct.Portrait.get_id equipment.portrait),
      glyph_board =
         (BattleCharacters.Struct.GlyphBoard.get_id equipment.glyph_board),
      glyphs =
         (Array.map (BattleCharacters.Struct.Glyph.get_id) equipment.glyphs)
   }