Draft version only creating a connection and sending its name before hanging up
This commit is contained in:
commit
3e27c33472
7 changed files with 138 additions and 0 deletions
5
ChangeLog.md
Normal file
5
ChangeLog.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# Revision history for hannah
|
||||||
|
|
||||||
|
## 0.1.0.0 -- YYYY-mm-dd
|
||||||
|
|
||||||
|
* First version. Released on an unsuspecting world.
|
30
LICENSE
Normal file
30
LICENSE
Normal file
|
@ -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.
|
2
Setup.hs
Normal file
2
Setup.hs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
import Distribution.Simple
|
||||||
|
main = defaultMain
|
29
hannah.cabal
Normal file
29
hannah.cabal
Normal file
|
@ -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
|
43
src/Automaton.hs
Normal file
43
src/Automaton.hs
Normal file
|
@ -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
|
||||||
|
|
14
src/Config.hs
Normal file
14
src/Config.hs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
module Config (
|
||||||
|
host
|
||||||
|
, path
|
||||||
|
, port
|
||||||
|
) where
|
||||||
|
|
||||||
|
host :: String
|
||||||
|
host = "koikoi.menf.in"
|
||||||
|
|
||||||
|
path :: String
|
||||||
|
path = "/play/"
|
||||||
|
|
||||||
|
port :: Int
|
||||||
|
port = 80
|
15
src/Main.hs
Normal file
15
src/Main.hs
Normal file
|
@ -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
|
Loading…
Reference in a new issue