Skip to content
This repository has been archived by the owner on Oct 2, 2018. It is now read-only.

Commit

Permalink
First part for Indexes (+ other Issues) (#108)
Browse files Browse the repository at this point in the history
* id included

* Diff hands (#4)

* backend allows different hands

* single-argument ini-configs

* Mod cards (#5)

* rules can play add-power cards

* testing api

* Fe limit (#3)

* api passes limit

* cards retrieved from rows

* single-argument ini-config

* useless spaces

* new test

* ups!

* use mapv
  • Loading branch information
Masclins authored Apr 6, 2018
1 parent 3b13e40 commit ba8635d
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 42 deletions.
4 changes: 3 additions & 1 deletion backend/src/api/player_view_functions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"Return the rows as seen by the player"
[game-state player-id]
(mapv (fn [row]
(get-row-cards (:cards row) game-state player-id))
(assoc row
:cards
(get-row-cards (:cards row) game-state player-id)))
(:rows game-state)))

(defn get-rows-power
Expand Down
4 changes: 4 additions & 0 deletions backend/src/configs/hands.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
{:power 6 :add-power -1}
{:power 4 :add-power 2}
{:power 4 :add-power -2}])

(def default-hands
"Default hands"
[default-hand default-hand])
19 changes: 12 additions & 7 deletions backend/src/rules/create_game.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@
(:require [configs.hands :as hands]
[configs.rows :as rows]))

(defn ^:private new-player
(defn new-player
"Creates a new player object"
([hand]
{
:hand hand
}))
[hand player]
{
:hand (mapv (fn [card id] (assoc card :id id))
hand
(iterate inc (* player 1000)))})

(defn new-game
"Creates a new game object"
([] (new-game {}))
([ini-config]
{
:players (vec (repeat 2 (new-player (:hand ini-config hands/default-hand))))

:players (let [hands (:hands ini-config hands/default-hands)]
[(new-player (first hands) 0)
(new-player (second hands) 1)])

:rows (vec (reduce
#(concat %1 [{:limit %2 :cards []}])
[]
(:limits ini-config rows/default-limits)))
:next-play [nil nil]
}))
}))
43 changes: 30 additions & 13 deletions backend/src/rules/play_card.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns rules.play-card
(:require [configs.messages :as messages]))
(:require [configs.messages :as messages]
[rules.alter-card :as alter-card]))

(defn ^:private add-card-to-row
"Adds a card onto the specified row"
Expand All @@ -23,14 +24,26 @@
[game-state player index]
(modify-hand game-state player (item-remover index)))

(defn ^:private apply-play-card
(defn ^:private apply-play-card-abilities
"Applies the abilities of the played card"
[game-state play]
(if (nil? (:target play))
game-state
(alter-card/add-power
game-state
(:target play)
(get-in game-state [:players (:player play)
:hand (:index play)
:add-power]))))

(defn apply-play-card
"Plays a card waiting to be played onto the board"
[game-state play]
(let [player-id (:player play)
index (:index play)
row-id (:row play)
card (get-in game-state [:players player-id :hand index])]
(-> game-state
(-> (apply-play-card-abilities game-state play)
(add-card-to-row (assoc card :owner player-id) row-id)
(remove-card player-id index))))

Expand Down Expand Up @@ -61,13 +74,17 @@
"Takes a playing of a card from hand onto a game row and makes it wait until both players had played"
[game-state player-id index row-id & target]
; Uses stored :next-play to know who is supposed to play
(cond (some? (get-in game-state [:next-play player-id]))
{:error messages/out-of-turn}
(crowded-row? (get-in game-state [:rows row-id]) player-id)
{:error messages/row-limit}
:else
(if (every? nil? (:next-play game-state))
(assoc-in game-state [:next-play player-id] {:player player-id :index index :row row-id :target (first target)})
(-> game-state
(assoc-in [:next-play player-id] {:player player-id :index index :row row-id :target (first target)})
(apply-all-plays)))))
(cond
(some? (get-in game-state [:next-play player-id]))
{:error messages/out-of-turn}

(crowded-row? (get-in game-state [:rows row-id]) player-id)
{:error messages/row-limit}

(every? nil? (:next-play game-state))
(assoc-in game-state [:next-play player-id] {:player player-id :index index :row row-id :target (first target)})

:else
(-> game-state
(assoc-in [:next-play player-id] {:player player-id :index index :row row-id :target (first target)})
(apply-all-plays))))
23 changes: 23 additions & 0 deletions backend/test/api/effects_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(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]))))
4 changes: 2 additions & 2 deletions backend/test/api/play_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@

; card played is owned by self
(expect
#(= :me (get-in % [:rows 0 0 :owner]))
#(= :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 0 :owner]))
#(= :opponent (get-in % [:rows 1 :cards 0 :owner]))
(api/get-game game-id player-id))))
20 changes: 19 additions & 1 deletion backend/test/rules/effects_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns rules.effects-test
(:require [expectations.clojure.test :refer :all]
[rules.alter-card :as alter-card]))
[rules.alter-card :as alter-card]
[rules.play-card :as play-card]))

(defexpect card-altering
(expect
Expand Down Expand Up @@ -33,3 +34,20 @@
[nil {:croissant "chocolate" :enchilada {:power 1 :salsa "mild"}}]
[1 :enchilada]
55)))

(defexpect playing-add-power
; Cards that should add power do so
(expect
42
(-> (play-card/apply-play-card {:rows [{:cards [{:power 24}]}]
:players [{:hand [{:add-power 18}]}]}
{:player 0 :index 0 :row 0 :target [:rows 0 :cards 0]})
(get-in [:rows 0 :cards 0 :power])))
(expect
{:power -2 :coolness 100}
(-> (play-card/apply-play-card {:rows [{}{:cards [{:power 4 :coolness 100}]}]
:players [{:hand [{:add-power -6}]}]}
{:player 0 :index 0 :row 0 :target [:rows 1 :cards 0]})
(get-in [:rows 1 :cards 0]))))


37 changes: 20 additions & 17 deletions backend/test/rules/new_game_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,31 @@
(-> (create-game/new-game)
:rows)))

(defexpect starting-hand
;(defexpect starting-hand
; Players start with expected hand
(expect
hands/default-hand
(-> (create-game/new-game)
:players
first
:hand))
(expect
hands/default-hand
(-> (create-game/new-game)
:players
second
:hand)))
; (expect
; hands/default-hand
; (-> (create-game/new-game)
; :players
; first
; :hand))
; (expect
; hands/default-hand
; (-> (create-game/new-game)
; :players
; second
; :hand)))

(defexpect configs.game
; Game can start with different configs
(expect
[{:power 0 :attribute 9001}{:power 1}]
(-> (create-game/new-game {:hand [{:power 0 :attribute 9001}
{:power 1}]})
(get-in [:players 0 :hand])))
[{:hand [{:power 0 :attribute 9001 :id 0}{:power 1 :id 1}]}
{:hand [{:whatever 10 :id 1000}]}]
(-> (create-game/new-game {:hands [[{:power 0 :attribute 9001}
{:power 1}]
[{:whatever 10}]]})
:players))

(expect
[{:limit 0 :cards []} {:limit 2 :cards []} {:limit 4 :cards []} {:limit 6 :cards []} {:limit 8 :cards []}]
(-> (create-game/new-game {:limits [0 2 4 6 8]})
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/game/board/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
})

boardState.forEach(function (row, rownum) {
row.forEach(function (cardInRow) {
row["cards"].forEach(function (cardInRow) {
var newCard = builder.buildCard(templates.baseCard, cardInRow);

if (cardInRow["owner"] === "me") {
Expand Down

0 comments on commit ba8635d

Please sign in to comment.