summaryrefslogtreecommitdiff
blob: ab2107504f7ec5214cf3d2c4c74820cea42d0266 (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
module Model.HandleServerReply.SetMap exposing (apply_to)

-- Elm -------------------------------------------------------------------------
import Array
import Json.Decode

-- Battlemap -------------------------------------------------------------------
import Battlemap
import Battlemap.Tile

import Model

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------
type alias MapData =
   {
      width : Int,
      height : Int,
      content : (List (List Int))
   }

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
deserialize_tile : Int -> Int -> (List Int) -> Battlemap.Tile.Type
deserialize_tile map_width index data =
   case data of
      [icon_id, cost] ->
         (Battlemap.Tile.new
            (index % map_width)
            (index // map_width)
            (toString icon_id)
            cost
         )

      _ ->
         (Battlemap.Tile.error_tile
            (index % map_width)
            (index // map_width)
         )

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
apply_to : Model.Type -> String -> Model.Type
apply_to model serialized_map =
   case
      (Json.Decode.decodeString
         (Json.Decode.map3 MapData
            (Json.Decode.field "width" Json.Decode.int)
            (Json.Decode.field "height" Json.Decode.int)
            (Json.Decode.field
               "content"
               (Json.Decode.list
                  (Json.Decode.list Json.Decode.int)
               )
            )
         )
         serialized_map
      )
   of
      (Result.Ok map_data) ->
         (Model.reset
            {model |
               battlemap =
                  (Battlemap.new
                     map_data.width
                     map_data.height
                     (List.indexedMap
                        (deserialize_tile map_data.width)
                        map_data.content
                     )
                  )
            }
            model.characters
         )

      _ -> model