aboutsummaryrefslogtreecommitdiffstats
path: root/src/Reaktor/Utils.hs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2019-01-13 23:52:05 +0100
committertv <tv@krebsco.de>2019-01-21 02:12:00 +0100
commitce276eee82ec0b8c4106beb4c51d6f9eb77335c4 (patch)
treee41019c40471a45659fefba1671fa68395f062d6 /src/Reaktor/Utils.hs
parentdffc580ca255cd118a0dfcdae7a5bb67f4824dcc (diff)
src: initv0.0.0
Diffstat (limited to 'src/Reaktor/Utils.hs')
-rw-r--r--src/Reaktor/Utils.hs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/Reaktor/Utils.hs b/src/Reaktor/Utils.hs
new file mode 100644
index 0000000..bc08a81
--- /dev/null
+++ b/src/Reaktor/Utils.hs
@@ -0,0 +1,37 @@
+module Reaktor.Utils where
+
+import qualified Data.ByteString.Char8 as BS
+import Data.Char (chr)
+import Data.Char (isDigit)
+import Reaktor.Types
+import System.Random (getStdRandom, randomR)
+
+
+nextNick :: Nickname -> Nickname
+nextNick nick_ = nick'
+ where
+ splitNick s =
+ (prefix, maybe 0 fst (BS.readInt suffix))
+ where
+ prefix = BS.take (BS.length s - BS.length suffix) s
+ suffix = BS.reverse . BS.takeWhile isDigit . BS.reverse $ s
+ (nickPrefix, nickSuffix) = splitNick nick_
+ nick' = nickPrefix <> (BS.pack . show $ nickSuffix + 1)
+
+
+randomNick :: IO Nickname
+randomNick = do
+ h_chr <- getRandomChar nickhead
+ t_len <- getStdRandom (randomR (4,8)) :: IO Int
+ t_str <- mapM (const $ getRandomChar nicktail) [1..t_len]
+ return $ BS.pack (h_chr:t_str)
+ where
+ getRandomChar cs = (cs!!) <$> getStdRandom (randomR (0, length cs - 1))
+
+ nickhead = letters <> specials
+ nicktail = letters <> digits <> specials <> minus
+
+ letters = map chr $ [0x41..0x5A] <> [0x61..0x7A]
+ digits = map chr $ [0x30..0x39]
+ specials = map chr $ [0x5B..0x60] <> [0x7B..0x7D]
+ minus = map chr $ [0x2D]