diff --git a/docs/LICENSE b/LICENSE similarity index 100% rename from docs/LICENSE rename to LICENSE diff --git a/backend/old_tests/api/effects_test.clj b/backend/old_tests/api/effects_test.clj deleted file mode 100644 index a125d40..0000000 --- a/backend/old_tests/api/effects_test.clj +++ /dev/null @@ -1,23 +0,0 @@ -(ns api.effects-test - (:require [expectations.clojure.test :refer :all] - [clojure.test :as ctest] - [mocking :as mocking] - [api.base :as api])) - -(ctest/use-fixtures :each mocking/mock-persistence) - -(defexpect playing-effects - ; Can play cards with effects and they act as expected - (expect - -100 - (let [game (api/create-game {:hands [[{:power 0} {:power 100}] - [{:power 1} {:power 10 :add-power -100}]]}) - game-id (:game-id game) - player-id (:player-id game) - opponent-id (:player-id (api/add-player game-id))] - (api/play-card-as-player game-id player-id 0 0) - (api/play-card-as-player game-id opponent-id 0 1) - (api/play-card-as-player game-id player-id 0 1) - (api/play-card-as-player game-id opponent-id 0 1 [:rows 0 :cards 0]) - (get-in (api/get-game game-id player-id) - [:rows 0 :cards 0 :power])))) diff --git a/backend/old_tests/api/messages_test.clj b/backend/old_tests/api/messages_test.clj deleted file mode 100644 index 89df6b4..0000000 --- a/backend/old_tests/api/messages_test.clj +++ /dev/null @@ -1,71 +0,0 @@ -(ns api.messages-test - (:require [expectations.clojure.test :refer :all] - [clojure.test :as ctest] - [mocking :as mocking] - [api.base :as api] - [configs.messages :as messages])) - -(ctest/use-fixtures :each mocking/mock-persistence) - -(defexpect no-opp-on-creation - (let [game (api/create-game)] - (expect - messages/no-opp - (:status game)) - (expect - messages/no-opp - (:status - (api/get-game (:game-id game) (:player-id game)))))) - -(defexpect play-on-both-players - (expect - messages/play - (-> (api/create-game) - :game-id - (api/add-player) - :status))) - -(defexpect wait-on-card-played - (expect - messages/wait - (let [game (api/create-game) - game-id (:game-id game) - player-id (:player-id game)] - (do (api/add-player game-id) - (:status - (api/play-card-as-player game-id player-id 0 0)))))) - -(defexpect play-on-not-played - (let [game (api/create-game) - game-id (:game-id game) - player-id (:player-id game) - opponent-id (:player-id (api/add-player game-id))] - (expect - messages/play - (do (api/play-card-as-player game-id player-id 0 0) - (:status - (api/play-card-as-player game-id opponent-id 0 0)))) - - (expect - messages/play - (do (api/play-card-as-player game-id opponent-id 0 0) - (:status - (api/get-game game-id player-id)))))) - -(defexpect out-of-turn - ; Game gives error when playing and shouldn't - (let [game (api/create-game) - game-id (:game-id game) - player-id (:player-id game)] - - (expect - {:error messages/out-of-turn} - (api/play-card-as-player game-id player-id 0 0)) - - (expect - {:error messages/out-of-turn} - (do (api/add-player game-id) - (api/play-card-as-player game-id player-id 0 0) - (api/play-card-as-player game-id player-id 0 0))))) - - diff --git a/backend/old_tests/api/play_test.clj b/backend/old_tests/api/play_test.clj deleted file mode 100644 index f66919f..0000000 --- a/backend/old_tests/api/play_test.clj +++ /dev/null @@ -1,41 +0,0 @@ -(ns api.play-test - (:require [expectations.clojure.test :refer :all] - [clojure.test :as ctest] - [mocking :as mocking] - [api.base :as api] - [configs.hands :as hands])) - -(ctest/use-fixtures :each mocking/mock-persistence) - -(defexpect playing-cards - (let [game (api/create-game) - game-id (:game-id game) - player-id (:player-id game) - opponent (api/add-player game-id) - opponent-id (:player-id opponent)] - ; Cards in hand have power - (expect - #(empty? (filter (fn [e] (nil? (:power e))) %)) - (:hand game)) - - ; Card is not removed if opponent has not yet played - (expect - #(= (count (:hand %)) (count hands/default-hand)) - (do - (api/play-card-as-player game-id player-id 0 0) - (api/get-game game-id player-id))) - - ; Cards are removed from hands upon both having played - (expect - #(= (count (:hand %)) (dec (count hands/default-hand))) - (api/play-card-as-player game-id opponent-id 0 1)) - - ; card played is owned by self - (expect - #(= :me (get-in % [:rows 0 :cards 0 :owner])) - (api/get-game game-id player-id)) - - ; opponent's card is owned by him - (expect - #(= :opponent (get-in % [:rows 1 :cards 0 :owner])) - (api/get-game game-id player-id)))) diff --git a/backend/old_tests/api/scores_test.clj b/backend/old_tests/api/scores_test.clj deleted file mode 100644 index 10aaa4a..0000000 --- a/backend/old_tests/api/scores_test.clj +++ /dev/null @@ -1,59 +0,0 @@ -(ns api.scores-test - (:require [expectations.clojure.test :refer :all] - [clojure.test :as ctest] - [mocking :as mocking] - [api.base :as api] - [test-hand :as hand] - [autoplay :as autoplay] - [configs.hands :as hands])) - -(ctest/use-fixtures :each mocking/mock-persistence) - -(defexpect rows-won - (expect - #(= [3 1] - (:scores %)) - (autoplay/as-api - (fn [i] (mod i 4)) - (fn [i] 0))) - - (defexpect rows-tied - (expect - #(= [2 2] - (:scores %)) - (autoplay/as-api - (fn [i] (mod i 2)) - (fn [i] (+ (mod i 2) 2)))))) - -(defexpect power-in-rows - (let [game-state (api/create-game) - game-id (:game-id game-state) - player-id (:player-id game-state) - opponent-id (:player-id (api/add-player game-id))] - - ; Game begins with scores at 0 - (expect - [[0 0] [0 0] [0 0] [0 0] [0 0]] - (:rows-power (api/get-game game-id player-id))) - - ; Play on same row - (expect - [[(hand/power-of-nth 0) (hand/power-of-nth 1)] - [0 0] [0 0] [0 0] [0 0]] - (do - (api/play-card-as-player game-id player-id 0 0) - (api/play-card-as-player game-id opponent-id 1 0) - (:rows-power (api/get-game game-id player-id)))) - - ; Play on different rows - (expect - [[ - (+ (hand/power-of-nth 0) (hand/power-of-nth 1)) - (hand/power-of-nth 0)] - [0 0] - [0 (hand/power-of-nth 1)] - [0 0] [0 0]] - (do - (api/play-card-as-player game-id player-id 0 2) - (api/play-card-as-player game-id opponent-id 0 0) - (:rows-power (api/get-game game-id opponent-id)))))) diff --git a/backend/old_tests/api/winner_test.clj b/backend/old_tests/api/winner_test.clj deleted file mode 100644 index 2a64904..0000000 --- a/backend/old_tests/api/winner_test.clj +++ /dev/null @@ -1,31 +0,0 @@ -(ns api.winner-test - (:require [expectations.clojure.test :refer :all] - [clojure.test :as ctest] - [mocking :as mocking] - [autoplay :as autoplay])) - -(ctest/use-fixtures :each mocking/mock-persistence) - -(defexpect tie - ; Both stack all cards on one row -> tie - (expect - #(= :tie (:winner %)) - (autoplay/as-api - (fn [i] 0) - (fn [i] 0)))) - -(defexpect i-win - ; Opponent stacks all cards on one row and I spread -> I win - (expect - #(= :me (:winner %)) - (autoplay/as-api - (fn [i] (mod i 4)) - (fn [i] 0)))) - -(defexpect i-lose - ; I stack all cards on one row and opponent spreads -> Opponent wins - (expect - #(= :opponent (:winner %)) - (autoplay/as-api - (fn [i] 0) - (fn [i] (mod i 4))))) diff --git a/backend/old_tests/autoplay.clj b/backend/old_tests/autoplay.clj deleted file mode 100644 index 5f32a90..0000000 --- a/backend/old_tests/autoplay.clj +++ /dev/null @@ -1,23 +0,0 @@ -(ns autoplay - (:require [configs.hands :as hands] - [configs.rows :as rows] - [api.base :as api] - [rules.create-game :as create-game] - [rules.play-card :as play-card])) - -(defn as-api - [strategy1 strategy2] - (let [game (api/create-game {:hand hands/simple-hand - :limits rows/limitless}) - game-id (:game-id game) - player-id (:player-id game) - opponent-id (:player-id (api/add-player game-id))] - (loop [game-state game - iteration (count (:hand game))] - (if (= 0 iteration) - (api/get-game game-id player-id) - (recur - (do - (api/play-card-as-player game-id player-id 0 (strategy1 iteration)) - (api/play-card-as-player game-id opponent-id 0 (strategy2 iteration))) - (dec iteration)))))) diff --git a/backend/old_tests/test_hand.clj b/backend/old_tests/test_hand.clj deleted file mode 100644 index 0847f05..0000000 --- a/backend/old_tests/test_hand.clj +++ /dev/null @@ -1,8 +0,0 @@ -(ns test-hand - (:require [configs.hands :as hands])) - -(defn power-of-nth - ([x] - (power-of-nth x hands/default-hand)) - ([x hand] - (get-in hand [x :power]))) diff --git a/backend/src/api/base.clj b/backend/src/api/base.clj index 464ee43..dee1e05 100644 --- a/backend/src/api/base.clj +++ b/backend/src/api/base.clj @@ -13,13 +13,21 @@ (player-view/get-game-as-player (persistence/fetch-game game-id) player-id)) (defn play-card-as-player + "Plays a card give and returns the game sate as seen by the player" [game-id player-id card-id row-id & target] (let [game-state (persistence/fetch-game game-id)] + (if (and (= game-id (:game-id game-state)) + (or (= player-id (first (:player-ids game-state))) + (= player-id (second (:player-ids game-state))))) (if (= (:status (get-game game-id player-id)) messages/play) - (do - (persistence/save-game (play-card/play-card game-state player-id card-id row-id (first target))) - (get-game game-id player-id)) - {:error messages/out-of-turn}))) + (let [new-game-state (play-card/play-card game-state player-id card-id row-id (first target))] + (if (contains? new-game-state :error) + new-game-state + (do + (persistence/save-game new-game-state) + (get-game game-id player-id)))) + {:error messages/out-of-turn}) + {:error messages/invalid-id}))) (defn ^:private create-empty-game "Creates a new instance of a game lobby" diff --git a/backend/src/configs/messages.clj b/backend/src/configs/messages.clj index 1f32a01..f7b11af 100644 --- a/backend/src/configs/messages.clj +++ b/backend/src/configs/messages.clj @@ -23,3 +23,5 @@ (def no-row (get-in config-file [:messages :no-row])) (def lobby-not-created (get-in config-file [:messages :no-lobby-not-created])) + +(def invalid-id (get-in config-file [:messages :invalid-id])) diff --git a/backend/test/api/base_test.clj b/backend/test/api/base_test.clj index a2ce642..0745c2c 100644 --- a/backend/test/api/base_test.clj +++ b/backend/test/api/base_test.clj @@ -38,3 +38,37 @@ (defexpect join-empty (expect {:error messages/lobby-not-created} (base/add-player 0))) + +(defexpect play-card-messages + + (let [game (base/create-game) + game-id (:game-id game) + p1 (:player-id game) + p2 (:player-id (base/add-player game-id))] + + (expect {:error messages/invalid-id} + (base/play-card-as-player 9999999999 p1 0 0)) + + (expect {:error messages/invalid-id} + (base/play-card-as-player game-id "MrInvalid" 0 0)) + + (expect {:error messages/no-row} + (base/play-card-as-player game-id p1 1 99)) + + (expect {:error messages/not-owned-card} + (base/play-card-as-player game-id p1 15 0)) + + (expect {:error messages/not-owned-card} + (base/play-card-as-player game-id p2 0 0)) + + (expect messages/wait + (:status (base/play-card-as-player game-id p1 1 0))) + + (expect {:error messages/out-of-turn} + (base/play-card-as-player game-id p1 1 0)) + + (expect messages/play + (:status (base/play-card-as-player game-id p2 11 2))) + + (expect messages/wait + (:status (base/play-card-as-player game-id p2 11 1))))) diff --git a/configs/config.yml b/configs/config.yml index 5839213..8c9a42f 100644 --- a/configs/config.yml +++ b/configs/config.yml @@ -9,3 +9,4 @@ messages: need-target: "A target is needed" not-owned-card: "You don't own this card" no-row: "This row doesn't exist" + invalid-id: "Wrong id"