Simplify character initialization to be all in one step

This commit is contained in:
Tissevert 2018-12-15 07:53:03 +01:00
parent 355c7d08be
commit 2a3a56ad01
6 changed files with 35 additions and 45 deletions

View file

@ -2,7 +2,7 @@
{-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedLists #-}
module Area ( module Area (
Area(..) Area(..)
, Key , Key(..)
, dex , dex
) where ) where

View file

@ -9,23 +9,32 @@ import Control.Monad.IO.Class (liftIO)
import Data.Aeson (encode) import Data.Aeson (encode)
import Data.ByteString.Lazy.Char8 (ByteString, putStrLn, unpack) import Data.ByteString.Lazy.Char8 (ByteString, putStrLn, unpack)
import Prelude hiding (log, putStrLn) import Prelude hiding (log, putStrLn)
import qualified Area (Key(..))
import Character (Character(..)) import Character (Character(..))
import Game (Game(..), InitProcess(..), State(..), stub) import Game (Game(..), Position(..), State(..))
import Message (Connected, receive, send) import Message (Connected, receive, send)
import qualified Message.Client as Client (Message(..)) import qualified Message.Client as Client (Message(..))
import qualified Message.Server as Server (Message(..)) import qualified Message.Server as Server (Message(..))
import Tool.Array (malloc)
edges :: Game -> Client.Message -> Connected Game edges :: Game -> Client.Message -> Connected Game
edges (Game {state = Start}) Client.NewGame = return $ Game.stub { edges Empty Client.NewGame = return Initializing
state = Initializing Name
}
edges (Game {state = Start}) (Client.Resume {Client.game}) = return game edges Empty (Client.Resume {Client.game}) = return game
edges (Game {state = Initializing Name, character}) (Client.Initialize {Client.step = Name, Client.value}) = return $ Game { edges Initializing message@(Client.Initialize {}) = return $ Game {
character = character { name = value } character = Character {
, state = Initializing Skin name = Client.name message
, skin = Client.skin message
, gender = Client.gender message
, pokemons = malloc 6
}
, state = At $ Position {
area = Area.Key 0
, x = 0
, y = 0
}
} }
edges game message = do edges game message = do

View file

@ -4,7 +4,6 @@ module Character (
Character(..) Character(..)
, Key , Key
, dex , dex
, stub
) where ) where
import Data.Aeson (ToJSON(..), FromJSON(..), genericToEncoding, genericParseJSON, defaultOptions) import Data.Aeson (ToJSON(..), FromJSON(..), genericToEncoding, genericParseJSON, defaultOptions)
@ -12,7 +11,7 @@ import Data.Vector (Vector)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Gender (Gender(..)) import Gender (Gender(..))
import qualified Pokemon (T) import qualified Pokemon (T)
import Tool.Array (Array, malloc) import Tool.Array (Array)
data Character = Character { data Character = Character {
name :: String name :: String
@ -35,10 +34,3 @@ instance FromJSON Key where
dex :: Vector Character dex :: Vector Character
dex = [] dex = []
stub :: Character
stub = Character {
name = ""
, skin = ""
, pokemons = malloc 6
}

View file

@ -1,16 +1,14 @@
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric #-}
module Game ( module Game (
Game(..) Game(..)
, InitProcess(..) , Position(..)
, State(..) , State(..)
, stub
) where ) where
import Data.Aeson (ToJSON(..), FromJSON(..), genericToEncoding, genericParseJSON, defaultOptions) import Data.Aeson (ToJSON(..), FromJSON(..), genericToEncoding, genericParseJSON, defaultOptions)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Character (Character) import Character (Character)
import qualified Character (stub)
import qualified Area (Key) import qualified Area (Key)
data Position = Position { data Position = Position {
@ -31,13 +29,6 @@ instance ToJSON Turn where
instance FromJSON Turn where instance FromJSON Turn where
parseJSON = genericParseJSON defaultOptions parseJSON = genericParseJSON defaultOptions
data InitProcess = Gender | Name | Skin deriving (Generic)
instance ToJSON InitProcess where
toEncoding = genericToEncoding defaultOptions
instance FromJSON InitProcess where
parseJSON = genericParseJSON defaultOptions
data State = data State =
At Position At Position
| Fighting { | Fighting {
@ -45,8 +36,6 @@ data State =
, opponent :: Character , opponent :: Character
, turn :: Turn , turn :: Turn
} }
| Initializing InitProcess
| Start
deriving (Generic) deriving (Generic)
instance ToJSON State where instance ToJSON State where
@ -54,18 +43,16 @@ instance ToJSON State where
instance FromJSON State where instance FromJSON State where
parseJSON = genericParseJSON defaultOptions parseJSON = genericParseJSON defaultOptions
data Game = Game { data Game =
character :: Character Empty
, state :: State | Initializing
} deriving (Generic) | Game {
character :: Character
, state :: State
}
deriving (Generic)
instance ToJSON Game where instance ToJSON Game where
toEncoding = genericToEncoding defaultOptions toEncoding = genericToEncoding defaultOptions
instance FromJSON Game where instance FromJSON Game where
parseJSON = genericParseJSON defaultOptions parseJSON = genericParseJSON defaultOptions
stub :: Game
stub = Game {
character = Character.stub
, state = Start
}

View file

@ -11,7 +11,7 @@ import Network.Wai.Handler.WebSockets (websocketsOr)
import Network.WebSockets (ServerApp, acceptRequest, defaultConnectionOptions, forkPingThread) import Network.WebSockets (ServerApp, acceptRequest, defaultConnectionOptions, forkPingThread)
import qualified Automaton (run) import qualified Automaton (run)
import Config (listenPort) import Config (listenPort)
import qualified Game (stub) import qualified Game (Game(..))
makeApp :: IO ServerApp makeApp :: IO ServerApp
makeApp = makeApp =
@ -19,7 +19,7 @@ makeApp =
connection <- acceptRequest pending connection <- acceptRequest pending
putStrLn "New connection" putStrLn "New connection"
forkPingThread connection 20 forkPingThread connection 20
runReaderT (evalStateT Automaton.run Game.stub) connection runReaderT (evalStateT Automaton.run Game.Empty) connection
main :: IO () main :: IO ()
main = do main = do

View file

@ -4,14 +4,16 @@ module Message.Client (
) where ) where
import Data.Aeson (FromJSON(..), ToJSON(..), genericParseJSON, genericToEncoding, defaultOptions) import Data.Aeson (FromJSON(..), ToJSON(..), genericParseJSON, genericToEncoding, defaultOptions)
import Game (Game, InitProcess(..))
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Game (Game)
import Gender (Gender)
data Message = data Message =
NewGame NewGame
| Initialize { | Initialize {
step :: InitProcess name :: String
, value :: String , skin :: String
, gender :: Gender
} }
| Resume { | Resume {
game :: Game game :: Game