commit 3e27c33472ae993b9cf757716cd75243ba22ce0d Author: Tissevert Date: Thu Nov 8 18:36:25 2018 +0100 Draft version only creating a connection and sending its name before hanging up diff --git a/ChangeLog.md b/ChangeLog.md new file mode 100644 index 0000000..89485d4 --- /dev/null +++ b/ChangeLog.md @@ -0,0 +1,5 @@ +# Revision history for hannah + +## 0.1.0.0 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..961dd8f --- /dev/null +++ b/LICENSE @@ -0,0 +1,30 @@ +Copyright (c) 2018, Tissevert + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Tissevert nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/hannah.cabal b/hannah.cabal new file mode 100644 index 0000000..b007666 --- /dev/null +++ b/hannah.cabal @@ -0,0 +1,29 @@ +-- Initial hannah.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/ + +name: hannah +version: 0.1.0.0 +synopsis: A bot to play koikoi +-- description: +homepage: https://git.marvid.fr/hanafuda +license: BSD3 +license-file: LICENSE +author: Tissevert +maintainer: tissevert+devel@marvid.fr +-- copyright: +category: Game +build-type: Simple +extra-source-files: ChangeLog.md +cabal-version: >=1.10 + +executable hannah + main-is: Main.hs + -- other-modules: + -- other-extensions: + build-depends: base >=4.9 && <4.10 + , bytestring + , containers + , mtl + , websockets + hs-source-dirs: src + default-language: Haskell2010 diff --git a/src/Automaton.hs b/src/Automaton.hs new file mode 100644 index 0000000..77fc8e0 --- /dev/null +++ b/src/Automaton.hs @@ -0,0 +1,43 @@ +{-# LANGUAGE OverloadedStrings #-} +module Automaton ( + initialState + , start + ) where + +import Network.WebSockets (Connection, receiveData, sendTextData) +import Data.Map (Map, empty) +import Data.ByteString.Lazy.Char8 (ByteString, putStrLn) +import Control.Concurrent (threadDelay) +import Control.Monad.Reader (ReaderT, ask) +import Control.Monad.State (StateT) +import Control.Monad.Trans (lift) +import Prelude hiding (putStrLn) + +data State = State { + key :: Maybe Int + , room :: Map Int String + } + +initialState :: State +initialState = State { + key = Nothing + , room = empty + } + +type App a = ReaderT Connection (StateT State IO) a + +liftIO :: IO a -> App a +liftIO = lift . lift + +start :: App () +start = do + connection <- ask + s <- liftIO $ receiveData connection + liftIO $ putStrLn s + liftIO $ sendTextData connection ("{\"tag\": \"LogIn\", \"name\": \"Hannah\"}" :: ByteString) + s2 <- liftIO $ receiveData connection + liftIO $ putStrLn s2 + liftIO $ threadDelay $ 1000 * ms + where + ms = 1000 + diff --git a/src/Config.hs b/src/Config.hs new file mode 100644 index 0000000..f6cad11 --- /dev/null +++ b/src/Config.hs @@ -0,0 +1,14 @@ +module Config ( + host + , path + , port + ) where + +host :: String +host = "koikoi.menf.in" + +path :: String +path = "/play/" + +port :: Int +port = 80 diff --git a/src/Main.hs b/src/Main.hs new file mode 100644 index 0000000..6ef5a2a --- /dev/null +++ b/src/Main.hs @@ -0,0 +1,15 @@ +module Main where + +import Network.WebSockets (ClientApp, runClient) +import Control.Monad.Reader (runReaderT) +import Control.Monad.State (runStateT) +import Config (host, port, path) +import Automaton (initialState, start) + +bot :: ClientApp () +bot connection = fst <$> runStateT (runReaderT Automaton.start connection) initialState + + +main :: IO () +main = + runClient host port path bot