Add pockets to Characters and remove the parts of this type only relevant to the client

This commit is contained in:
Tissevert 2018-12-17 12:42:00 +01:00
parent 6633f0221b
commit 5966beed85
5 changed files with 63 additions and 30 deletions

View file

@ -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)

View file

@ -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 = []

View file

@ -45,7 +45,6 @@ instance FromJSON State where
data Game =
Empty
| Initializing
| Game {
character :: Character
, state :: State

View file

@ -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 = []

View file

@ -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)