Add function with tests to dispatch IRC commands

Signed-off-by: Sebastian Crane <seabass-labrax@gmx.com>
This commit is contained in:
Sebastian Crane 2022-02-09 15:47:24 +00:00
parent ac1a8d9c94
commit 5c0918f990
2 changed files with 44 additions and 0 deletions

View File

@ -40,3 +40,33 @@
" from the list of players for "
game
"!"))
(defn split-message [message]
(let [message-parts (str/split message #"\s")
command (if-let [x (first message-parts)] (str/lower-case x) "")
game (second message-parts)
game-keyword (when game (keyword (str/lower-case game)))]
{:command command
:game game
:game-keyword game-keyword}))
(defn dispatch-command [state sender message]
(let [{command :command
game :game
game-keyword :game-keyword} (split-message message)]
(when game
(condp = command
"!match"
(match-string @state game nil)
"!add"
(do (swap! state game/add-player-of-game game-keyword sender)
(add-player-string nil game sender))
"!list"
(list-players-string @state game nil)
"!remove"
(do (swap! state game/remove-player-of-game game-keyword sender)
(remove-player-string nil game sender))
nil))))

View File

@ -24,3 +24,17 @@
(deftest remove-player-string-test
(is (= "Removed abc from the list of players for hypothetical-shooter!"
(remove-player-string nil "hypothetical-shooter" "abc"))))
(deftest split-message-test
(is (= {:command "!match" :game "Quasi-Rts" :game-keyword :quasi-rts}
(split-message "!match Quasi-Rts "))))
(deftest dispatch-command-test
(let [state-atom (atom test-state)]
(is (and
(= "Added 123 to the list of players for Imaginary-RPG!"
(dispatch-command state-atom "123" "!add Imaginary-RPG"))
(= {:games {:hypothetical-shooter #{"abc" "xyz" "123"}
:quasi-rts #{"abc" "123"}
:imaginary-rpg #{"xyz" "abc" "123"}}}
@state-atom)))))