From 5966beed85349edcf5fa4abf0532cbe490e36d38 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Mon, 17 Dec 2018 12:42:00 +0100 Subject: [PATCH] Add pockets to Characters and remove the parts of this type only relevant to the client --- src/Automaton.hs | 19 +++++----------- src/Character.hs | 52 ++++++++++++++++++++++++++++++++++++++----- src/Game.hs | 1 - src/Item.hs | 15 ++++++++++--- src/Message/Client.hs | 6 ----- 5 files changed, 63 insertions(+), 30 deletions(-) diff --git a/src/Automaton.hs b/src/Automaton.hs index 44a1f16..ab70b68 100644 --- a/src/Automaton.hs +++ b/src/Automaton.hs @@ -10,26 +10,15 @@ import Data.Aeson (encode) import Data.ByteString.Lazy.Char8 (ByteString, putStrLn, unpack) import Prelude hiding (log, putStrLn) import qualified Area (Key(..)) -import Character (Character(..)) +import qualified Character (new) import Game (Game(..), Position(..), State(..)) import Message (Connected, receive, send) import qualified Message.Client as Client (Message(..)) import qualified Message.Server as Server (Message(..)) -import Tool.Array (malloc) edges :: Game -> Client.Message -> Connected Game - -edges Empty Client.NewGame = return Initializing - -edges Empty (Client.Resume {Client.game}) = return game - -edges Initializing message@(Client.Initialize {}) = return $ Game { - character = Character { - name = Client.name message - , skin = Client.skin message - , gender = Client.gender message - , pokemons = malloc 6 - } +edges Empty Client.NewGame = return $ Game { + character = Character.new , state = At $ Position { area = Area.Key 0 , x = 0 @@ -37,6 +26,8 @@ edges Initializing message@(Client.Initialize {}) = return $ Game { } } +edges Empty (Client.Resume {Client.game}) = return game + edges game message = do let errorMessage = "inacceptable message " <> encode message <> " in this context" send (Server.Error $ unpack errorMessage) diff --git a/src/Character.hs b/src/Character.hs index 67183c7..6006d95 100644 --- a/src/Character.hs +++ b/src/Character.hs @@ -4,19 +4,47 @@ module Character ( Character(..) , Key , dex + , new ) where -import Data.Aeson (ToJSON(..), FromJSON(..), genericToEncoding, genericParseJSON, defaultOptions) +import Data.Aeson (ToJSONKey(..), FromJSONKey(..), ToJSON(..), FromJSON(..), genericToEncoding, genericParseJSON, defaultOptions) +import Data.Map (Map) import Data.Vector (Vector) import GHC.Generics (Generic) -import Gender (Gender(..)) +import qualified Item (Group) import Pokemon (Pokemon) -import Tool.Array (Array) +import Tool.Array (Array, malloc) + +data Badge = + Earth + | Fire + | Water + | Plant + | Electricity + | Psy + | Normal + deriving (Generic) + +instance ToJSON Badge where + toEncoding = genericToEncoding defaultOptions +instance FromJSON Badge + +data Pocket = + Items + | Rare + | Techniques + | Special + deriving (Eq, Generic, Ord) + +instance ToJSON Pocket where + toEncoding = genericToEncoding defaultOptions +instance FromJSON Pocket +instance ToJSONKey Pocket +instance FromJSONKey Pocket data Character = Character { - name :: String - , skin :: FilePath - , gender :: Gender + items :: Map Pocket (Array Item.Group) + , badges :: Array Badge , pokemons :: Array Pokemon } deriving (Generic) @@ -32,5 +60,17 @@ instance ToJSON Key where instance FromJSON Key where parseJSON = genericParseJSON defaultOptions +new :: Character +new = Character { + items = [ + (Items, malloc 30) + , (Rare, malloc 10) + , (Techniques, malloc 50) + , (Special, malloc 5) + ] + , badges = malloc 8 + , pokemons = malloc 6 + } + dex :: Vector Character dex = [] diff --git a/src/Game.hs b/src/Game.hs index 5fd5e05..19fabad 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -45,7 +45,6 @@ instance FromJSON State where data Game = Empty - | Initializing | Game { character :: Character , state :: State diff --git a/src/Item.hs b/src/Item.hs index 1593dba..7683b69 100644 --- a/src/Item.hs +++ b/src/Item.hs @@ -2,12 +2,13 @@ {-# LANGUAGE OverloadedLists #-} module Item ( Item(..) + , Group(..) , Key , Type(..) , dex ) where -import Data.Aeson (ToJSON(..), FromJSON(..), genericToEncoding, genericParseJSON, defaultOptions) +import Data.Aeson (ToJSON(..), FromJSON(..), genericToEncoding, defaultOptions) import Data.Vector (Vector) import GHC.Generics (Generic) import Pokemon (Pokemon) @@ -25,8 +26,16 @@ newtype Key = Key Int deriving (Generic) instance ToJSON Key where toEncoding = genericToEncoding defaultOptions -instance FromJSON Key where - parseJSON = genericParseJSON defaultOptions +instance FromJSON Key + +data Group = Group { + item :: Key + , count :: Int + } deriving (Generic) + +instance ToJSON Group where + toEncoding = genericToEncoding defaultOptions +instance FromJSON Group dex :: Vector Item dex = [] diff --git a/src/Message/Client.hs b/src/Message/Client.hs index bea6d58..f96a101 100644 --- a/src/Message/Client.hs +++ b/src/Message/Client.hs @@ -6,15 +6,9 @@ module Message.Client ( import Data.Aeson (FromJSON(..), ToJSON(..), genericParseJSON, genericToEncoding, defaultOptions) import GHC.Generics (Generic) import Game (Game) -import Gender (Gender) data Message = NewGame - | Initialize { - name :: String - , skin :: String - , gender :: Gender - } | Resume { game :: Game } deriving (Generic)