summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/battle/src/Comm')
-rw-r--r--src/battle/src/Comm/AddArmor.elm24
-rw-r--r--src/battle/src/Comm/AddChar.elm28
-rw-r--r--src/battle/src/Comm/AddTile.elm24
-rw-r--r--src/battle/src/Comm/AddWeapon.elm24
-rw-r--r--src/battle/src/Comm/CharacterTurn.elm129
-rw-r--r--src/battle/src/Comm/LoadBattle.elm42
-rw-r--r--src/battle/src/Comm/Send.elm76
-rw-r--r--src/battle/src/Comm/SetMap.elm62
-rw-r--r--src/battle/src/Comm/SetTimeline.elm27
-rw-r--r--src/battle/src/Comm/TurnResults.elm28
10 files changed, 464 insertions, 0 deletions
diff --git a/src/battle/src/Comm/AddArmor.elm b/src/battle/src/Comm/AddArmor.elm
new file mode 100644
index 0000000..480b823
--- /dev/null
+++ b/src/battle/src/Comm/AddArmor.elm
@@ -0,0 +1,24 @@
+module Comm.AddArmor exposing (decode)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Decode
+
+-- Map -------------------------------------------------------------------
+import Struct.Armor
+import Struct.ServerReply
+
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+internal_decoder : Struct.Armor.Type -> Struct.ServerReply.Type
+internal_decoder ar = (Struct.ServerReply.AddArmor ar)
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+decode : (Json.Decode.Decoder Struct.ServerReply.Type)
+decode = (Json.Decode.map (internal_decoder) (Struct.Armor.decoder))
diff --git a/src/battle/src/Comm/AddChar.elm b/src/battle/src/Comm/AddChar.elm
new file mode 100644
index 0000000..32227a8
--- /dev/null
+++ b/src/battle/src/Comm/AddChar.elm
@@ -0,0 +1,28 @@
+module Comm.AddChar exposing (decode)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Decode
+
+-- Map -------------------------------------------------------------------
+import Struct.Character
+import Struct.ServerReply
+
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+internal_decoder : (
+ (Struct.Character.Type, Int, Int, Int) ->
+ Struct.ServerReply.Type
+ )
+internal_decoder char_and_refs = (Struct.ServerReply.AddCharacter char_and_refs)
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+decode : (Json.Decode.Decoder Struct.ServerReply.Type)
+decode = (Json.Decode.map (internal_decoder) (Struct.Character.decoder))
diff --git a/src/battle/src/Comm/AddTile.elm b/src/battle/src/Comm/AddTile.elm
new file mode 100644
index 0000000..64cf0ea
--- /dev/null
+++ b/src/battle/src/Comm/AddTile.elm
@@ -0,0 +1,24 @@
+module Comm.AddTile exposing (decode)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Decode
+
+-- Map -------------------------------------------------------------------
+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/battle/src/Comm/AddWeapon.elm b/src/battle/src/Comm/AddWeapon.elm
new file mode 100644
index 0000000..7061dea
--- /dev/null
+++ b/src/battle/src/Comm/AddWeapon.elm
@@ -0,0 +1,24 @@
+module Comm.AddWeapon exposing (decode)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Decode
+
+-- Map -------------------------------------------------------------------
+import Struct.Weapon
+import Struct.ServerReply
+
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+internal_decoder : Struct.Weapon.Type -> Struct.ServerReply.Type
+internal_decoder wp = (Struct.ServerReply.AddWeapon wp)
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+decode : (Json.Decode.Decoder Struct.ServerReply.Type)
+decode = (Json.Decode.map (internal_decoder) (Struct.Weapon.decoder))
diff --git a/src/battle/src/Comm/CharacterTurn.elm b/src/battle/src/Comm/CharacterTurn.elm
new file mode 100644
index 0000000..36dfd96
--- /dev/null
+++ b/src/battle/src/Comm/CharacterTurn.elm
@@ -0,0 +1,129 @@
+module Comm.CharacterTurn exposing (try)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Encode
+
+-- Map -------------------------------------------------------------------
+import Constants.IO
+
+import Comm.Send
+
+import Struct.Character
+import Struct.CharacterTurn
+import Struct.Direction
+import Struct.Event
+import Struct.Model
+
+--------------------------------------------------------------------------------
+-- TYPES ------------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+encode_move : Struct.Model.Type -> (Maybe Json.Encode.Value)
+encode_move model =
+ case (Struct.CharacterTurn.get_path model.char_turn) of
+ [] -> Nothing
+ path ->
+ (Just
+ (Json.Encode.object
+ [
+ ("t", (Json.Encode.string "mov")),
+ (
+ "p",
+ (Json.Encode.list
+ (List.map
+ (
+ (Json.Encode.string)
+ <<
+ (Struct.Direction.to_string)
+ )
+ (List.reverse path)
+ )
+ )
+ )
+ ]
+ )
+ )
+
+encode_weapon_switch : Struct.Model.Type -> (Maybe Json.Encode.Value)
+encode_weapon_switch model =
+ if (Struct.CharacterTurn.has_switched_weapons model.char_turn)
+ then
+ (Just
+ (Json.Encode.object
+ [
+ ("t", (Json.Encode.string "swp"))
+ ]
+ )
+ )
+ else
+ Nothing
+
+encode_attack : Struct.Model.Type -> (Maybe Json.Encode.Value)
+encode_attack model =
+ case (Struct.CharacterTurn.try_getting_target model.char_turn) of
+ Nothing -> Nothing
+
+ (Just ix) ->
+ (Just
+ (Json.Encode.object
+ [
+ ("t", (Json.Encode.string "atk")),
+ ("tix", (Json.Encode.int ix))
+ ]
+ )
+ )
+
+encode_actions : Struct.Model.Type -> (List Json.Encode.Value)
+encode_actions model =
+ case
+ (
+ (encode_move model),
+ (encode_weapon_switch model),
+ (encode_attack model)
+ )
+ of
+ ((Just move), Nothing, Nothing) -> [move]
+ ((Just move), Nothing, (Just attack)) -> [move, attack]
+ (Nothing, (Just switch_weapon), Nothing) -> [switch_weapon]
+ (Nothing, (Just switch_weapon), (Just attack)) -> [switch_weapon, attack]
+ (Nothing, Nothing, (Just attack)) -> [attack]
+ _ -> []
+
+try_encoding : Struct.Model.Type -> (Maybe Json.Encode.Value)
+try_encoding model =
+ case (Struct.CharacterTurn.try_getting_active_character model.char_turn) of
+ (Just char) ->
+ (Just
+ (Json.Encode.object
+ [
+ ("stk", (Json.Encode.string model.session_token)),
+ ("pid", (Json.Encode.string model.player_id)),
+ ("bid", (Json.Encode.string model.battle_id)),
+ (
+ "cix",
+ (Json.Encode.int (Struct.Character.get_index char))
+ ),
+ (
+ "act",
+ (Json.Encode.list (encode_actions model))
+ )
+ ]
+ )
+ )
+
+ _ ->
+ Nothing
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+try : Struct.Model.Type -> (Maybe (Cmd Struct.Event.Type))
+try model =
+ (Comm.Send.try_sending
+ model
+ Constants.IO.character_turn_handler
+ try_encoding
+ )
diff --git a/src/battle/src/Comm/LoadBattle.elm b/src/battle/src/Comm/LoadBattle.elm
new file mode 100644
index 0000000..df4e9dd
--- /dev/null
+++ b/src/battle/src/Comm/LoadBattle.elm
@@ -0,0 +1,42 @@
+module Comm.LoadBattle exposing (try)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Encode
+
+-- Map -------------------------------------------------------------------
+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)),
+ ("bid", (Json.Encode.string model.battle_id))
+ ]
+ )
+ )
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+try : Struct.Model.Type -> (Maybe (Cmd Struct.Event.Type))
+try model =
+ (Comm.Send.try_sending
+ model
+ Constants.IO.map_loading_handler
+ try_encoding
+ )
diff --git a/src/battle/src/Comm/Send.elm b/src/battle/src/Comm/Send.elm
new file mode 100644
index 0000000..98e3ba4
--- /dev/null
+++ b/src/battle/src/Comm/Send.elm
@@ -0,0 +1,76 @@
+module Comm.Send exposing (try_sending)
+
+-- Elm -------------------------------------------------------------------------
+import Http
+
+import Json.Decode
+import Json.Encode
+
+-- Map -------------------------------------------------------------------
+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/battle/src/Comm/SetMap.elm b/src/battle/src/Comm/SetMap.elm
new file mode 100644
index 0000000..7bfd56f
--- /dev/null
+++ b/src/battle/src/Comm/SetMap.elm
@@ -0,0 +1,62 @@
+module Comm.SetMap exposing (decode)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Decode
+
+-- Map -------------------------------------------------------------------
+import Struct.Map
+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.Map.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)
+ )
+ )
+ )
diff --git a/src/battle/src/Comm/SetTimeline.elm b/src/battle/src/Comm/SetTimeline.elm
new file mode 100644
index 0000000..3956ec3
--- /dev/null
+++ b/src/battle/src/Comm/SetTimeline.elm
@@ -0,0 +1,27 @@
+module Comm.SetTimeline exposing (decode)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Decode
+
+-- Map -------------------------------------------------------------------
+import Struct.ServerReply
+import Struct.TurnResult
+
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+internal_decoder : (List Struct.TurnResult.Type) -> Struct.ServerReply.Type
+internal_decoder trl = (Struct.ServerReply.SetTimeline trl)
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+decode : (Json.Decode.Decoder Struct.ServerReply.Type)
+decode =
+ (Json.Decode.map
+ (internal_decoder)
+ (Json.Decode.field "cnt" (Json.Decode.list Struct.TurnResult.decoder))
+ )
diff --git a/src/battle/src/Comm/TurnResults.elm b/src/battle/src/Comm/TurnResults.elm
new file mode 100644
index 0000000..f8727e1
--- /dev/null
+++ b/src/battle/src/Comm/TurnResults.elm
@@ -0,0 +1,28 @@
+module Comm.TurnResults exposing (decode)
+
+-- Elm -------------------------------------------------------------------------
+import Json.Decode
+
+-- Map -------------------------------------------------------------------
+import Struct.ServerReply
+import Struct.TurnResult
+
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+internal_decoder : (List Struct.TurnResult.Type) -> Struct.ServerReply.Type
+internal_decoder trl = (Struct.ServerReply.TurnResults trl)
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+decode : (Json.Decode.Decoder Struct.ServerReply.Type)
+decode =
+ (Json.Decode.map
+ (internal_decoder)
+ (Json.Decode.field "cnt" (Json.Decode.list Struct.TurnResult.decoder))
+ )