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

-- Elm -------------------------------------------------------------------------
import Dict
import Json.Decode

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

import Data.Tile

import Model

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

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
deserialize_tile : Int -> Int -> Int -> Battlemap.Tile.Type
deserialize_tile map_width index id =
   (Battlemap.Tile.new
      (index % map_width)
      (index // map_width)
      (Data.Tile.get_icon id)
      (Data.Tile.get_cost id)
   )

--------------------------------------------------------------------------------
-- 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.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
                     )
                  )
            }
            (Dict.empty)
         )

      _ -> model