diff options
author | defanor <defanor@uberspace.net> | 2018-12-23 13:36:45 +0300 |
---|---|---|
committer | defanor <defanor@uberspace.net> | 2018-12-23 13:36:45 +0300 |
commit | 6f13487eeb6e001d7b9e4b9311b8b719407bcd5a (patch) | |
tree | f3ec92f87182a91f9b6eab9103c7142d3bdb5dd9 | |
parent | 8679cb3ae59800f15c44e057de01097cb8ac9a3c (diff) |
Fix map name lookup
-rw-r--r-- | DWProxy.hs | 14 |
1 files changed, 12 insertions, 2 deletions
@@ -26,6 +26,7 @@ import Data.Aeson (FromJSON, decodeStrict) import GHC.Generics (Generic) import Data.Monoid import Data.List +import Data.Maybe type RoomID = T.Text @@ -65,6 +66,15 @@ mapNames = , "Skund Wolf Trail" ] +elemAtIndex :: Int -> [a] -> Maybe a +elemAtIndex 0 (x:_) = Just x +elemAtIndex _ [] = Nothing +elemAtIndex n (_:xs) = elemAtIndex (n - 1) xs + +mapNameByID :: Int -> T.Text +mapNameByID n = fromMaybe "unknown" $ elemAtIndex (n - 1) mapNames + + instance SQLite.FromRow Room where fromRow = Room <$> SQLite.field <*> SQLite.field <*> SQLite.field <*> SQLite.field <*> SQLite.field <*> SQLite.field @@ -130,7 +140,7 @@ mkClient = do showRoom :: Room -> BS.ByteString showRoom r = TE.encodeUtf8 $ T.concat [ roomShort r, " (" - , mapNames !! (roomMapID r - 1), ", " + , mapNameByID (roomMapID r), ", " , T.pack (show $ roomX r), "x", T.pack (show $ roomY r) , ")"] @@ -180,7 +190,7 @@ c2s c leftover = do , ", " <> (T.pack $ show $ length p') <> " steps away")) sendAll (cClient c) $ TE.encodeUtf8 $ T.concat ["[", T.pack n, "] " - , roomS, " (", mapNames !! mapI, l, ")" + , roomS, " (", mapNameByID mapI, l, ")" , ": ", item, " for ", price, "\r\n"] SpeedWalk f t -> do cs <- readMVar (connState c) |