Skip to content
Snippets Groups Projects
Commit 8969adcc authored by davean's avatar davean
Browse files

Public release

parents
Branches master
No related tags found
No related merge requests found
Showing with 295 additions and 0 deletions
[submodule "libs/broadcast-chan"]
path = libs/broadcast-chan
url = https://code.xkrd.net/haskell/broadcast-chan.git
[submodule "libs/hyperloglog"]
path = libs/hyperloglog
url = https://code.xkrd.net/haskell/hyperloglog.git
# Revision history for kurita
## 0 -- YYYY-mm-dd
* First version. Released on an unsuspecting world.
LICENSE 0 → 100644
Copyright (c) 2019, davean
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 davean 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.
import Distribution.Simple
main = defaultMain
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Main where
import Control.Concurrent
import Control.Concurrent.STM
import Control.Time
import qualified Data.ByteString.Char8 as Char8
import Data.Either (rights)
import qualified Data.Sequence as Seq
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.IO as Text
import Data.Time
import Kurita.Bot.Types
import Kurita.Emoji
import Kurita.Prompt
import Kurita.Protocol
import Kurita.Server
import qualified Network.Wai.Handler.Warp as Warp
import Crypto.MAC.SipHash
import Network.Linklater.Types
gameTime :: a -> NominalDiffTime
gameTime = const 15
slackChannel :: Channel
slackChannel = Channel "ChannelID" "Channel name"
slackConfig :: Config
slackConfig = Config "Hook url here"
slackToken :: APIToken
slackToken = APIToken "Slack bot token here"
botCfg :: BotConfig
botCfg = BotConfig slackToken (slackChannel, slackConfig)
type HLLSZ = 7
initialComments :: UTCTime -> [(UTCTime, Text)]
initialComments t = [
(t, "3) If I was creative around this time of night that would be helpful")
, (t, "2) Some fantastic commentary that's really funny")
, (t, "1) Starting the match between A and B!")
]
introPath :: FilePath
introPath = "intros.txt"
main :: IO ()
main = do
let Right emoji = mapM uncodes ["U+1F600", "U+1F601", "U+1F606", "U+1F605"
,"U+1F923", "U+1F602", "U+1F642", "U+1F643"]
intros <- newTMVarIO Seq.empty
introResults <- fmap parsePrompt . Text.lines <$> Text.readFile introPath
let unSipHash (SipHash h) = h
let
is = rights introResults
randomIntro t = do
let w = unSipHash $ hash (SipKey 4 7) $ Char8.pack (show t)
let r = (is !! (fromIntegral w `mod` (length is)))
pure r
initState <- loadState (\st _ -> KGame st (initialComments st)) (\n _ _ -> KGame (15 `addUTCTime` n) (initialComments n)) emoji::IO (KuritaState HLLSZ Text)
centralApp <- kuritaCentral "prompts.txt" "terms.txt" (unSipHash . hash (SipKey 4 7)) botCfg id 1 initState
_ <- forkIO $ Warp.run 8081 $ centralApp
delay (120::Int)
module Main where
import qualified Control.Concurrent.Async as Async
import Control.Monad
import Control.Monad.Loops
import qualified Data.Aeson as JS
import qualified Data.ByteString.Lazy as BL
import Data.Foldable
import Data.List (sort)
import Data.Text (Text)
import qualified Data.Text.Encoding as TE
import Data.Time
import Kurita.Protocol
import qualified Network.WebSockets as WS
import System.Environment (getArgs)
import System.Exit (exitSuccess)
vote :: (TDown Text -> IO ()) -> (String, Int, String) -> NominalDiffTime -> IO ()
vote onDown (host, port, path) rate = do
initTime <- getCurrentTime
WS.runClient host port path $ \conn -> (`iterateM_` initTime) $ \lastVoteTime -> do
Just td <- (JS.decode' . BL.fromStrict . TE.encodeUtf8) <$> WS.receiveData conn
onDown td
now <- getCurrentTime
case td of
BattleStart (Bracket {_bCurrent=Just c}) -> when ((rate `addUTCTime` lastVoteTime) <= now) $ do
WS.sendTextData conn . TE.decodeUtf8 . BL.toStrict . JS.encode .
Vote . head . sort . map snd . toList . _gameSorted $ c
BattleStart (Bracket {_bCurrent=Nothing}) -> exitSuccess
ScoreUpdate scs -> when ((rate `addUTCTime` lastVoteTime) <= now) $ do
WS.sendTextData conn . TE.decodeUtf8 . BL.toStrict . JS.encode .
Vote . head . sort . map fst $ scs
-- _ -> putStrLn "Unknown TDown"
pure now
main :: IO ()
main = do
let target = ("127.0.0.1", 8080, "/")
let rate = 1.0
args <- getArgs
case args of
[] -> do
vote print target rate
[n] -> do
Async.replicateConcurrently_ (read n) $ vote (const $ pure ()) target rate
_ -> putStrLn "Unknow args."
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Main where
import Control.Concurrent
import Control.Time
import Crypto.MAC.SipHash
import Data.Bits.Extras (w32)
import Data.Bytes.Put (runPutS)
import Data.Bytes.Serial
import Data.Time
import Data.Text (Text)
import Kurita.Emoji
import Kurita.Protocol
import Kurita.Server
import qualified Network.Wai.Handler.Warp as Warp
gameTime :: a -> NominalDiffTime
gameTime = const 15
type HLLSZ = 7
main :: IO ()
main = do
let Right emoji = mapM uncodes ["U+1F600", "U+1F601", "U+1F606", "U+1F605"
,"U+1F923", "U+1F602", "U+1F642", "U+1F643"]
initState <- loadState (\st _ -> KGame st []) (\n _ _ -> KGame (15 `addUTCTime` n) []) emoji::IO (KuritaState HLLSZ Text)
c <- localCore 1 initState
_ <- forkIO $ Warp.run 8080 $ kuritaRelay
(\_ -> (w32 . (\(SipHash h) -> h) . hash (SipKey 4 7) . runPutS . serialize) <$> getCurrentTime)
c
delay (120::Int)
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Main where
import Crypto.MAC.SipHash
import Data.Bits.Extras (w32)
import Data.Bytes.Put (runPutS)
import Data.Bytes.Serial
import Data.Proxy
import Data.Text (Text)
import Data.Time
import Kurita.Server
import qualified Network.Wai.Handler.Warp as Warp
type HLLSZ = 7
main :: IO ()
main = do
c <- relayCore (Proxy::Proxy HLLSZ) ("127.0.0.1", 8081, "/")::IO (Core Text)
Warp.run 8080 $ kuritaRelay
(\_ -> (w32 . (\(SipHash h) -> h) . hash (SipKey 4 7) . runPutS . serialize) <$> getCurrentTime)
c
packages: .
./libs/hyperloglog/
./libs/broadcast-chan/broadcast-chan/
allow-newer: bytes:time, unix:time
package kurita
ghc-options: -Wall
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>kurita</title>
<style>
@font-face {
font-family: 'xkcd-Regular-v2';
src: url('xkcd-Regular-v2.woff') format('woff');
}
#comic {
height: 500px;
}
</style>
</head>
<body>
<div id="comic">
<script src="dev.js"></script>
</div>
</body>
</html>
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>kurita</title>
<style>
@font-face {
font-family: 'xkcd-Regular-v2';
src: url('/2067/asset/xkcd-Regular-v2.woff') format('woff');
}
#comic {
height: 500px;
}
</style>
</head>
<body>
<div id="comic">
<script src="comic.js"></script>
</div>
</body>
</html>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path fill="#E6E7E8" d="M32 32c0 2.209-1.791 4-4 4H8c-2.209 0-4-1.791-4-4V4c0-2.209 1.791-4 4-4h20c2.209 0 4 1.791 4 4v28z"/><path fill="#BE1931" d="M8.397 11.891c.53 0 .996.078 1.392.232.244.066.453.143.63.231.796-.044 1.612-.122 2.453-.231 1.104-.134 2.463-.297 4.076-.497V9.307c0-1.613-.188-2.948-.563-4.01-.155-.332-.233-.552-.233-.662 0-.331.133-.498.398-.498.706 0 1.359.089 1.954.266.686.198 1.028.453 1.028.761 0 .111-.042.342-.134.696-.265.685-.395 1.955-.395 3.811v1.723c1.299-.132 2.723-.276 4.271-.43.706-.045 1.381-.166 2.023-.366.463-.088.739-.132.83-.132.264 0 .837.222 1.72.662.928.553 1.394.984 1.394 1.293 0 .242-.12.486-.362.729-.819.881-1.408 1.809-1.759 2.782l-.798 1.691c-.13.329-.276.583-.426.76l.128.067c.508.311.761.586.761.829 0 .198-.183.309-.56.332-1.699 0-3.236.088-4.606.266l-2.614.196-.034 4.442c0 2.009-.069 3.62-.198 4.838-.133 1.368-.333 2.341-.598 2.914-.245.488-.445.733-.598.733-.089 0-.232-.266-.431-.798-.132-.617-.198-1.687-.198-3.214v-8.749l-1.789.167c-1.194.111-2.111.168-2.75.168-.266 0-.488-.024-.664-.069-.044.2-.11.376-.198.533-.133.287-.277.43-.432.43-.198 0-.385-.154-.562-.465-.287-.438-.442-.892-.464-1.358l-.398-2.585c-.155-1.348-.332-2.286-.531-2.818-.111-.551-.409-1.081-.894-1.59-.177-.11-.266-.186-.266-.23 0-.354.133-.531.397-.531zm3.347 7.221c1.812-.085 3.545-.196 5.203-.328v-6.132c-1.702.199-3.347.464-4.937.796-.31.111-.597.188-.862.232 0 .265.022.53.067.793.265 2.013.441 3.561.529 4.639zm7.259-6.69v6.163c.728-.087 1.435-.179 2.119-.265 1.326-.178 2.286-.286 2.879-.331.048-.11.103-.232.168-.363.199-.641.463-1.646.798-3.016.264-1.084.394-1.768.394-2.056 0-.353-.339-.529-1.024-.529-1.041 0-2.696.121-4.973.363-.13-.001-.254.012-.361.034z"/></svg>
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path fill="#E6E7E8" d="M32 32c0 2.209-1.791 4-4 4H8c-2.209 0-4-1.791-4-4V4c0-2.209 1.791-4 4-4h20c2.209 0 4 1.791 4 4v28z"/><path fill="#DD2E44" d="M11 7c-2.519 0-4.583 1.87-4.929 4.293C6.802 10.503 7.839 10 9 10c2.209 0 4 1.791 4 4 0 2 1.497 2.198.707 2.929C16.13 16.583 16 14.519 16 12c0-2.761-2.239-5-5-5z"/><path fill="#55ACEE" d="M23 14c0-2.209 1.791-4 4-4 1.161 0 2.198.503 2.929 1.293C29.583 8.87 27.52 7 25 7c-2.762 0-5 2.239-5 5 0 2.519-.131 4.583 2.293 4.929C21.503 16.198 23 16 23 14z"/><path fill="#FFAC33" d="M14 12c0-4.971 4-9 4-9s4 4.029 4 9-1.791 9-4 9-4-4.029-4-9z"/><path fill="#553788" d="M11.707 21.071C12.497 21.802 13 22.839 13 24c0 2.209-1.791 4-4 4-1.161 0-2.198-.503-2.929-1.293C6.417 29.131 8.481 31 11 31c2.761 0 5-2.238 5-5 0-2.52-1.87-4.583-4.293-4.929zM27 28c-2.209 0-4-1.791-4-4 0-1.161.503-2.198 1.293-2.929C21.869 21.417 20 23.48 20 26c0 2.762 2.238 5 5 5 2.52 0 4.583-1.869 4.929-4.293C29.198 27.497 28.161 28 27 28z"/><path fill="#9266CC" d="M14 24c0 4.971 3 9 4 9s4-4.029 4-9c0-.874-.055-1.719-.159-2.519C21.357 17.737 19.82 15 18 15c-1.82 0-3.357 2.737-3.841 6.481-.104.8-.159 1.645-.159 2.519z"/><path fill="#EDBB9F" d="M13 17c0-3.866 3-4 5-4s5 .134 5 4c0 3.865-2.238 7-5 7-2.761 0-5-3.135-5-7z"/><circle fill="#662113" cx="16" cy="17" r="1"/><circle fill="#662113" cx="20" cy="17" r="1"/><path fill="#662113" d="M18 22c1.104 0 2-.896 2-2h-4c0 1.104.896 2 2 2z"/><circle fill="#A0041E" cx="6" cy="11" r="1"/><circle fill="#269" cx="30" cy="11" r="1"/><circle fill="#DD2E44" cx="18" cy="3" r="1"/></svg>
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path fill="#DD2E44" d="M36 32c0 2.209-1.791 4-4 4H4c-2.209 0-4-1.791-4-4V4c0-2.209 1.791-4 4-4h28c2.209 0 4 1.791 4 4v28z"/><path fill="#FFF" d="M14.747 9.125c.527-1.426 1.736-2.573 3.317-2.573 1.643 0 2.792 1.085 3.318 2.573l6.077 16.867c.186.496.248.931.248 1.147 0 1.209-.992 2.046-2.139 2.046-1.303 0-1.954-.682-2.264-1.611l-.931-2.915h-8.62l-.93 2.884c-.31.961-.961 1.642-2.232 1.642-1.24 0-2.294-.93-2.294-2.17 0-.496.155-.868.217-1.023l6.233-16.867zm.34 11.256h5.891l-2.883-8.992h-.062l-2.946 8.992z"/></svg>
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path fill="#DD2E44" d="M36 32c0 2.209-1.791 4-4 4H4c-2.209 0-4-1.791-4-4V4c0-2.209 1.791-4 4-4h28c2.209 0 4 1.791 4 4v28z"/><path fill="#FFF" d="M10.498 9.249c0-1.488 1.023-2.325 2.449-2.325H18.9c3.224 0 5.83 2.17 5.83 5.457 0 2.17-.9 3.628-2.885 4.558v.062c2.637.372 4.713 2.573 4.713 5.271 0 4.372-2.914 6.729-7.193 6.729h-6.386c-1.427 0-2.481-.899-2.481-2.356V9.249zm4.651 6.418h2.419c1.519 0 2.511-.899 2.511-2.45 0-1.457-1.147-2.201-2.511-2.201h-2.419v4.651zm0 9.24h3.659c1.674 0 2.915-.961 2.915-2.697 0-1.458-1.117-2.45-3.287-2.45h-3.287v5.147z"/></svg>
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path fill="#DD2E44" d="M36 32c0 2.209-1.791 4-4 4H4c-2.209 0-4-1.791-4-4V4c0-2.209 1.791-4 4-4h28c2.209 0 4 1.791 4 4v28z"/><path fill="#FFF" d="M6.993 18.001c0-6.656 4.48-11.776 11.007-11.776 6.432 0 11.008 5.28 11.008 11.776 0 6.623-4.449 11.774-11.008 11.774-6.496 0-11.007-5.151-11.007-11.774zm17.023 0c0-3.872-2.016-7.36-6.016-7.36s-6.015 3.488-6.015 7.36c0 3.903 1.952 7.359 6.015 7.359 4.065 0 6.016-3.456 6.016-7.359z"/></svg>
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path fill="#269" d="M36 32c0 2.209-1.791 4-4 4H4c-2.209 0-4-1.791-4-4V4c0-2.209 1.791-4 4-4h28c2.209 0 4 1.791 4 4v28z"/><path fill="#FFF" d="M11 9.496C11 7.992 11.896 7 13.496 7h5.665c4.703 0 8.191 2.944 8.191 7.52 0 4.67-3.617 7.48-8 7.48H16v5.479c0 1.6-1.024 2.496-2.4 2.496s-2.6-.897-2.6-2.496V9.496zM16 18h3.062c2.018 0 3.297-1.465 3.297-3.385 0-1.92-1.279-3.392-3.297-3.392H16V18z"/></svg>
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path fill="#DD2E44" d="M36 32c0 2.209-1.791 4-4 4H4c-2.209 0-4-1.791-4-4V4c0-2.209 1.791-4 4-4h28c2.209 0 4 1.791 4 4v28z"/><path fill="#FFF" d="M7.15 10.072c.476-1.288 1.567-2.324 2.996-2.324 1.483 0 2.52.979 2.996 2.324l5.488 15.231c.168.448.224.84.224 1.036 0 1.092-.896 1.848-1.932 1.848-1.177 0-1.765-.616-2.044-1.456L14 24H6.28l-.866 2.703c-.28.868-.868 1.484-2.016 1.484-1.12 0-2.072-.84-2.072-1.96 0-.448.14-.784.196-.924L7.15 10.072zm.308 10.163h5.32l-2.604-8.119h-.056l-2.66 8.119zM20 10.1c0-1.344.924-2.1 2.212-2.1h5.376c2.912 0 5.265 1.96 5.265 4.928 0 1.96-.812 3.276-2.605 4.116v.056c2.38.336 4.256 2.424 4.256 4.859 0 3.948-2.632 6.041-6.496 6.041H22.24c-1.288 0-2.24-.876-2.24-2.192V10.1zm4.2 5.9h2.184c1.372 0 2.268-.815 2.268-2.216 0-1.315-1.036-2.088-2.268-2.088H24.2V16zm0 8h3.304c1.513 0 2.632-.729 2.632-2.296 0-1.315-1.008-2.112-2.968-2.112H24.2V24z"/></svg>
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path fill="#DD2E44" d="M36 32c0 2.209-1.791 4-4 4H4c-2.209 0-4-1.791-4-4V4c0-2.209 1.791-4 4-4h28c2.209 0 4 1.791 4 4v28z"/><path d="M12.81 6.552c2.263 0 6.667.744 6.667 3.473 0 1.116-.776 2.077-1.923 2.077-1.271 0-2.139-1.085-4.744-1.085-3.845 0-5.829 3.256-5.829 7.038 0 3.689 2.015 6.852 5.829 6.852 2.605 0 3.658-1.302 4.93-1.302 1.395 0 2.046 1.395 2.046 2.107 0 2.977-4.682 3.659-6.976 3.659-6.294 0-10.666-4.992-10.666-11.41 0-6.448 4.341-11.409 10.666-11.409zm8.522 2.604c0-1.55.992-2.418 2.326-2.418s2.326.868 2.326 2.418V24.72h5.518c1.582 0 2.264 1.179 2.232 2.232-.06 1.025-.867 2.048-2.232 2.048h-7.75c-1.52 0-2.42-.992-2.42-2.543V9.156z" fill="#FFF"/></svg>
\ No newline at end of file
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path fill="#3B88C3" d="M36 32c0 2.209-1.791 4-4 4H4c-2.209 0-4-1.791-4-4V4c0-2.209 1.791-4 4-4h28c2.209 0 4 1.791 4 4v28z"/><g fill="#FFF"><path d="M5.97 12.858c1.022 0 3.012.336 3.012 1.569 0 .504-.35.939-.869.939-.574 0-.966-.49-2.143-.49-1.737 0-2.633 1.471-2.633 3.18 0 1.667.911 3.096 2.633 3.096 1.177 0 1.653-.589 2.227-.589.63 0 .925.631.925.953 0 1.345-2.115 1.652-3.152 1.652-2.843 0-4.818-2.255-4.818-5.154-.001-2.914 1.96-5.156 4.818-5.156z"/><path d="M8.865 18.014c0-2.914 1.961-5.155 4.818-5.155 2.815 0 4.819 2.311 4.819 5.155 0 2.899-1.947 5.154-4.819 5.154-2.843 0-4.818-2.255-4.818-5.154zm7.452 0c0-1.695-.882-3.222-2.633-3.222s-2.633 1.526-2.633 3.222c0 1.709.855 3.222 2.633 3.222s2.633-1.513 2.633-3.222zm2.656 0c0-2.914 1.96-5.155 4.818-5.155 2.816 0 4.818 2.311 4.818 5.155 0 2.899-1.945 5.154-4.818 5.154-2.843 0-4.818-2.255-4.818-5.154zm7.453 0c0-1.695-.883-3.222-2.635-3.222-1.75 0-2.633 1.526-2.633 3.222 0 1.709.854 3.222 2.633 3.222 1.779-.001 2.635-1.513 2.635-3.222zm2.767-3.979c0-.7.447-1.093 1.051-1.093.602 0 1.051.393 1.051 1.093v7.032h2.492c.715 0 1.023.532 1.01 1.008-.029.463-.393.925-1.01.925h-3.502c-.686 0-1.092-.448-1.092-1.148v-7.817z"/></g></svg>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment