refacto: renaming variables and functions
diff --git a/src/battleship/core.clj b/src/battleship/core.clj
index 041623a..58b04ad 100644
--- a/src/battleship/core.clj
+++ b/src/battleship/core.clj
@@ -21,7 +21,7 @@
[]
{:has-enemy? (random-bool) :shot-by :none})
-(defn generate-battlefield "Generates a 5x5 matrix. Each cell of this matrix looks like this: {:has-enemy? true/false :shot-by playerX}"
+(defn generate-game "Generates a 5x5 matrix game. Each cell of this matrix looks like this: {:has-enemy? true/false :shot-by playerX}"
[]
(into [] (repeatedly (* matrix-row matrix-row) init-cell)))
diff --git a/src/battleship/handler.clj b/src/battleship/handler.clj
index 3f0aad4..e8c029f 100644
--- a/src/battleship/handler.clj
+++ b/src/battleship/handler.clj
@@ -22,35 +22,31 @@
;;;;; Admin APIs
- ;; --- Getting infos about the global context.
+ ;; --- Getting infos about all games.
(GET "/admin/info" [] (response (logic/get-games-info games)))
- ;; --- Releasing the terminated games from the global context.
- (DELETE "/admin/gc" [] (do (logic/gc games) (response {:clean-up :done})))
+ ;; --- Cleans up the finished games from the 'all games context'.
+ (DELETE "/admin/clean-up" [] (do (logic/clean-up games) (response {:clean-up :done})))
;;;;; Players APIs
;; --- Retrieves the battlefield for the given game id.
- (GET "/games/:game/battlefield" [game] (response (core/battlefield-string @(@games game))))
- ;; --- Generates a new game context.
- (POST "/games" []
- (response {:game-id (logic/register-new-game games)}))
+ (GET "/games/:game-id/battlefield" [game-id] (response (core/battlefield-string @(@games game-id))))
+ ;; --- Registers a new game to the all games context.
+ (POST "/games" [] (response {:game-id (logic/register-new-game games)}))
;; --- Attemps an attack by a given player on a given location.
(PUT "/games/:game-id/players/:player/fire"
{{row :row col :col player :player game-id :game-id} :params}
- (response (logic/launch-attack
+ (response (logic/fire
(read-string row)
(read-string col)
player
(lookup-game game-id))))
-
;; --- Retrieves game info about a game
(GET "/games/:game-id/stats" [game-id] (let [game (lookup-game game-id)]
(if (nil? game)
(not-found {:error (str "No game found with the given id: " game-id)})
(response (assoc (logic/get-game-stats @game) :game-id game-id)))))
- ;; response logic/get-game-context game
-
;;;;; Others
(route/resources "/")
(route/not-found "Not Found"))
diff --git a/src/battleship/logic.clj b/src/battleship/logic.clj
index ad013dd..f4fd37c 100644
--- a/src/battleship/logic.clj
+++ b/src/battleship/logic.clj
@@ -8,46 +8,47 @@
(str (java.util.UUID/randomUUID)))
(defn register-new-game "Registers a new game into the global context."
- [battlefields]
+ [games]
(let [game-id (generate-game-id)]
- (swap! battlefields assoc game-id (atom (generate-battlefield)))
+ (swap! games assoc game-id (atom (generate-game)))
game-id))
(defn shoot-enemy "Marks a cell as shot with the name of the player."
- [row col player battlefield]
- (swap! battlefield update-in [(compute-index row col) :shot-by] (fn [x] player )))
+ [row col player game]
+ (swap! game update-in [(compute-index row col) :shot-by] (fn [x] player )))
(defn attempt-attack "A player attempt to shoot an enemy. If an enemy is shot the fun returns :success otherwise :failure"
- [row col player battlefield]
- (if (found-active-enemy? row col @battlefield)
- (do (shoot-enemy row col player battlefield)
+ [row col player game]
+ (if (found-active-enemy? row col @game)
+ (do (shoot-enemy row col player game)
:success)
:failure))
-(defn launch-attack "Launches an attack against an enemy supposed to be at a given location and returns a result as following: {:attack-status :success|:failure :game-status :running|:over}"
- [row col player battlefield]
- {:fire-status (attempt-attack row col player battlefield)
- :game-status ({true :over false :running} (is-game-over? @battlefield))
- :score (score @battlefield)})
+(defn fire "Fires against an enemy supposed to be at a given location.
+Returns a result as following: {:fire-status :success|:failure :game-status :running|:over :score {...}}"
+ [row col player game]
+ {:fire-status (attempt-attack row col player game)
+ :game-status ({true :over false :running} (is-game-over? @game))
+ :score (score @game)})
-(defn get-games-info "Shows the global context.Ex: {game-id {player1 score1 player2 score2}}"
- [battlefields]
- (for [ctx @battlefields
+(defn get-games-info "Retrieves the informations about all games. Ex: {game-id {player1 score1 player2 score2}}"
+ [games]
+ (for [ctx @games
:let [game-id (key ctx) bf @(val ctx) status {false :running true :over}]]
{game-id (assoc (score bf) :status (status (is-game-over? bf)))}))
(defn get-game-stats "Retrieves a game context."
- [battlefield]
- {:status ({true :over false :running} (is-game-over? battlefield))
- :score (score battlefield)})
+ [game]
+ {:status ({true :over false :running} (is-game-over? game))
+ :score (score game)})
(defn terminated-games "Returns all the games in a over state"
- [battlefields]
- (for [ctx @battlefields
+ [games]
+ (for [ctx @games
:let [game-id (key ctx) bf @(val ctx)]
:when (is-game-over? bf)]
game-id))
-(defn gc "Releases the terminated games."
- [battlefields]
- (swap! battlefields #(apply dissoc % (terminated-games battlefields))))
+(defn clean-up "Releases the terminated games memory."
+ [games]
+ (swap! games #(apply dissoc % (terminated-games games))))
diff --git a/test/battleship/test/logic.clj b/test/battleship/test/logic.clj
index 9675f22..1c864ee 100644
--- a/test/battleship/test/logic.clj
+++ b/test/battleship/test/logic.clj
@@ -110,19 +110,19 @@
{:has-enemy? false :shot-by :none}
{:has-enemy? true :shot-by "player1"}])]
(testing "5x5 matrice: Fire on enemy located at row=0 col=0"
- (let [actual (launch-attack 0 0 "plx" battlefield)]
+ (let [actual (fire 0 0 "plx" battlefield)]
(is (= actual {:fire-status :failure :game-status :running :score {"player1" 4 "player2" 3 "playerX2" 2}} ))))
(testing "5x5 matrice: Fire on enemy located at row=0 col=1"
- (let [actual (launch-attack 0 1 "plx" battlefield)]
+ (let [actual (fire 0 1 "plx" battlefield)]
(is (= actual {:fire-status :failure :game-status :running :score {"player1" 4 "player2" 3 "playerX2" 2}}))))
(testing "5x5 matrice: Fire on enemy located at row=0 col=2"
- (let [actual (launch-attack 0 2 "plx" battlefield)]
+ (let [actual (fire 0 2 "plx" battlefield)]
(is (= actual {:fire-status :success :game-status :running :score {"player1" 4 "player2" 3 "playerX2" 2 "plx" 1}}))))
(testing "5x5 matrice: Fire on enemy located at row=4 col=0"
- (let [actual (launch-attack 4 0 "plx" battlefield)]
+ (let [actual (fire 4 0 "plx" battlefield)]
(is (= actual {:fire-status :success :game-status :over :score {"player1" 4 "player2" 3 "playerX2" 2 "plx" 2}}))))))
(deftest test-generate-game-id
@@ -270,9 +270,9 @@
(is (= (count actual) 2))
(is (= actual ["game-id2" "game-id3"])))))
-(deftest test-gc
+(deftest test-clean-up
(testing "5x5 matrices: 3 games context, 1 running and 2 terminated"
- (let [actual (gc (atom { "game-id1" (atom [{:has-enemy? true :shot-by "player1"}
+ (let [actual (clean-up (atom { "game-id1" (atom [{:has-enemy? true :shot-by "player1"}
{:has-enemy? false :shot-by :none}
{:has-enemy? true :shot-by :none}
{:has-enemy? false :shot-by :none}