summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornsensfel <SpamShield0@noot-noot.org>2018-07-10 16:44:13 +0200
committernsensfel <SpamShield0@noot-noot.org>2018-07-10 16:44:13 +0200
commitf63602557a2f7320a7e02a3bf7dd9b339efaf4d1 (patch)
tree360f47978854409073718b23a5189b62acea0e9e /src/map-editor/src/Comm
parentcc7e5d48a82eac5d6643702e84a4ed9ac2bb15d3 (diff)
Starting work on the map editor...
Diffstat (limited to 'src/map-editor/src/Comm')
-rw-r--r--src/map-editor/src/Comm/AddTile.elm24
-rw-r--r--src/map-editor/src/Comm/LoadBattlemap.elm42
-rw-r--r--src/map-editor/src/Comm/Send.elm76
-rw-r--r--src/map-editor/src/Comm/SetMap.elm62
4 files changed, 204 insertions, 0 deletions
diff --git a/src/map-editor/src/Comm/AddTile.elm b/src/map-editor/src/Comm/AddTile.elm
new file mode 100644
index 0000000..d0a3ce8
--- /dev/null
+++ b/src/map-editor/src/Comm/AddTile.elm
@@ -0,0 +1,24 @@
+module Comm.AddTile exposing (decode)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Decode
+
+-- Battlemap -------------------------------------------------------------------
+import Struct.Tile
+import Struct.ServerReply
+
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+internal_decoder : Struct.Tile.Type -> Struct.ServerReply.Type
+internal_decoder wp = (Struct.ServerReply.AddTile wp)
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+decode : (Json.Decode.Decoder Struct.ServerReply.Type)
+decode = (Json.Decode.map (internal_decoder) (Struct.Tile.decoder))
diff --git a/src/map-editor/src/Comm/LoadBattlemap.elm b/src/map-editor/src/Comm/LoadBattlemap.elm
new file mode 100644
index 0000000..cd29acf
--- /dev/null
+++ b/src/map-editor/src/Comm/LoadBattlemap.elm
@@ -0,0 +1,42 @@
+module Comm.LoadBattlemap exposing (try)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Encode
+
+-- Battlemap -------------------------------------------------------------------
+import Comm.Send
+
+import Constants.IO
+
+import Struct.Event
+import Struct.Model
+
+--------------------------------------------------------------------------------
+-- TYPES ------------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+try_encoding : Struct.Model.Type -> (Maybe Json.Encode.Value)
+try_encoding model =
+ (Just
+ (Json.Encode.object
+ [
+ ("stk", (Json.Encode.string model.session_token)),
+ ("pid", (Json.Encode.string model.player_id)),
+ ("bmi", (Json.Encode.string model.battlemap_id))
+ ]
+ )
+ )
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+try : Struct.Model.Type -> (Maybe (Cmd Struct.Event.Type))
+try model =
+ (Comm.Send.try_sending
+ model
+ Constants.IO.battlemap_loading_handler
+ try_encoding
+ )
diff --git a/src/map-editor/src/Comm/Send.elm b/src/map-editor/src/Comm/Send.elm
new file mode 100644
index 0000000..a6129cd
--- /dev/null
+++ b/src/map-editor/src/Comm/Send.elm
@@ -0,0 +1,76 @@
+module Comm.Send exposing (try_sending)
+
+-- Elm -------------------------------------------------------------------------
+import Http
+
+import Json.Decode
+import Json.Encode
+
+-- Battlemap -------------------------------------------------------------------
+import Comm.AddArmor
+import Comm.AddChar
+import Comm.AddTile
+import Comm.AddWeapon
+import Comm.SetMap
+import Comm.SetTimeline
+import Comm.TurnResults
+
+import Struct.Event
+import Struct.ServerReply
+import Struct.Model
+
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+internal_decoder : String -> (Json.Decode.Decoder Struct.ServerReply.Type)
+internal_decoder reply_type =
+ case reply_type of
+ "add_tile" -> (Comm.AddTile.decode)
+ "add_armor" -> (Comm.AddArmor.decode)
+ "add_char" -> (Comm.AddChar.decode)
+ "add_weapon" -> (Comm.AddWeapon.decode)
+ "set_map" -> (Comm.SetMap.decode)
+ "turn_results" -> (Comm.TurnResults.decode)
+ "set_timeline" -> (Comm.SetTimeline.decode)
+ other ->
+ (Json.Decode.fail
+ (
+ "Unknown server command \""
+ ++ other
+ ++ "\""
+ )
+ )
+
+decode : (Json.Decode.Decoder Struct.ServerReply.Type)
+decode =
+ (Json.Decode.field "msg" Json.Decode.string)
+ |> (Json.Decode.andThen (internal_decoder))
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+try_sending : (
+ Struct.Model.Type ->
+ String ->
+ (Struct.Model.Type -> (Maybe Json.Encode.Value)) ->
+ (Maybe (Cmd Struct.Event.Type))
+ )
+try_sending model recipient try_encoding_fun =
+ case (try_encoding_fun model) of
+ (Just serial) ->
+ (Just
+ (Http.send
+ Struct.Event.ServerReplied
+ (Http.post
+ recipient
+ (Http.jsonBody serial)
+ (Json.Decode.list (decode))
+ )
+ )
+ )
+
+ Nothing -> Nothing
diff --git a/src/map-editor/src/Comm/SetMap.elm b/src/map-editor/src/Comm/SetMap.elm
new file mode 100644
index 0000000..dc499cf
--- /dev/null
+++ b/src/map-editor/src/Comm/SetMap.elm
@@ -0,0 +1,62 @@
+module Comm.SetMap exposing (decode)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Decode
+
+-- Battlemap -------------------------------------------------------------------
+import Struct.Battlemap
+import Struct.ServerReply
+import Struct.Tile
+
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+type alias MapData =
+ {
+ w : Int,
+ h : Int,
+ t : (List Int)
+ }
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+deserialize_tile_instance : Int -> Int -> Int -> Struct.Tile.Instance
+deserialize_tile_instance map_width index id =
+ (Struct.Tile.new_instance
+ (index % map_width)
+ (index // map_width)
+ id
+ -1
+ -1
+ )
+
+internal_decoder : MapData -> Struct.ServerReply.Type
+internal_decoder map_data =
+ (Struct.ServerReply.SetMap
+ (Struct.Battlemap.new
+ map_data.w
+ map_data.h
+ (List.indexedMap
+ (deserialize_tile_instance map_data.w)
+ map_data.t
+ )
+ )
+ )
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+decode : (Json.Decode.Decoder Struct.ServerReply.Type)
+decode =
+ (Json.Decode.map
+ internal_decoder
+ (Json.Decode.map3 MapData
+ (Json.Decode.field "w" Json.Decode.int)
+ (Json.Decode.field "h" Json.Decode.int)
+ (Json.Decode.field
+ "t"
+ (Json.Decode.list Json.Decode.int)
+ )
+ )
+ )