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

Row types #164

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
4 changes: 1 addition & 3 deletions backend/src/api/player_view_functions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
(vec (map
#(cond
(and (= (:owner %) player-id)
(contains? % :ability)
(= (get-in % [:location 0]) :hand))
(merge (partial-card % [:power :location] "me")
((:ability %)))
(partial-card % [:power :location :description :target] "me")

(= (:owner %) player-id)
(partial-card % [:power :location] "me")
Expand Down
14 changes: 7 additions & 7 deletions backend/src/configs/hands.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

(def default-hand
"Default hand (can't be empty)"
[{:power 10}
{:power 9}
{:power 8}
[{:power 8}
{:power 7}
{:power 6 :ability (ability/add-power 1)}
{:power 6 :ability (ability/add-power -1)}
{:power 4 :ability (ability/add-power 2)}
{:power 4 :ability (ability/add-power -2)}])
(merge {:power 6} (ability/type-add-power "overpower" 1))
(merge {:power 5} (ability/type-add-power "overpower" 3))
(merge {:power 6} (ability/add-power 1))
(merge {:power 6} (ability/add-power -1))
(merge {:power 4} (ability/add-power 2))
(merge {:power 4} (ability/add-power -2))])

(def default-hands
"Default hands"
Expand Down
8 changes: 6 additions & 2 deletions backend/src/configs/rows.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
(ns configs.rows)


(def default-limits
(vec (repeat 5 4)))
(def default-rows
[{:limit 3 :type "overpower"}
{:limit 3}
{:limit 3}
{:limit 3}
{:limit 3}])
41 changes: 31 additions & 10 deletions backend/src/rules/abilities.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,37 @@
(defn add-power
"Creates the ability function"
[increase]
(fn
; "Alters a cards' power, by adding the a defined value to it.
; This power may be negative, resulting in a decrease"
([game-state target]
{:ability
(fn
; "Alters a cards' power, by adding the a defined value to it.
; This power may be negative, resulting in a decrease"
[game-state play]
(update-in
game-state
[:cards target :power]
[:cards (:target play) :power]
#(+ % increase)))
([]
{:description (if (neg? increase)
(str "Weaken " (- increase))
(str "Enhance " increase))
:target 1})))
:description (if (neg? increase)
(str "Weaken " (- increase))
(str "Enhance " increase))
:target 1})

(defn type-add-power
"Creates the ability function"
[row-type increase]
{:ability
(fn
; "When played in a row-type row,
; alters a its power, by adding the a defined value to it.
; This power may be negative, resulting in a decrease"
[game-state play]
(if (= (get-in game-state [:rows (:row-id play) :type])
row-type)
(update-in
game-state
[:cards (:card-id play) :power]
#(+ % increase))

game-state))
:description (if (neg? increase)
(str "Weaken " (- increase) " on " row-type)
(str "Enhance " increase " on " row-type))})
5 changes: 1 addition & 4 deletions backend/src/rules/create_game.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
:cards (let [hands (:hands ini-config hands/default-hands)]
(vec (concat (locate-in-hand (first hands) (first player-ids))
(locate-in-hand (second hands) (second player-ids)))))
:rows (vec (reduce
#(concat %1 [{:limit %2}])
[]
(:limits ini-config rows/default-limits)))
:rows (:rows ini-config rows/default-rows)
:next-play {}
})))
4 changes: 2 additions & 2 deletions backend/src/rules/play_card.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[game-state play]
(let [ability (get-in game-state [:cards (:card-id play) :ability])]
(if (some? ability)
(ability game-state (:target play))
(ability game-state play)
game-state)))

(defn ^:private apply-all-plays
Expand All @@ -37,7 +37,7 @@

(defn ^:private requires-target?
[game-state card-id]
(some? (get-in game-state [:cards card-id :ability])))
(contains? (get-in game-state [:cards card-id]) :target))

(defn play-card
"Takes a playing of a card from hand onto a game row and makes it wait until both players had played"
Expand Down
14 changes: 7 additions & 7 deletions backend/test/api/player_view_functions_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@
{:location [:hand] :owner "opp"}
{:power -20 :location [:row 0] :owner "me"}
{:power 999 :location [:row 1] :owner "opp"}]
(functions/get-cards {:cards [{:power 1 :location [:hand] :owner "I" :ability (ability/add-power 100)}
{:power 17 :location [:hand] :owner "I" :ability (ability/add-power -1)}
{:power 17 :location [:hand] :owner "U" :ability (ability/add-power -1)}
{:power -20 :location [:row 0] :owner "I" :ability (ability/add-power 76)}
{:power 999 :location [:row 1] :owner "U" :ability (ability/add-power 22)}]}
(functions/get-cards {:cards [(merge {:power 1 :location [:hand] :owner "I"} (ability/add-power 100))
(merge {:power 17 :location [:hand] :owner "I"} (ability/add-power -1))
(merge {:power 17 :location [:hand] :owner "U"} (ability/add-power -1))
(merge {:power -20 :location [:row 0] :owner "I"} (ability/add-power 76))
(merge {:power 999 :location [:row 1] :owner "U"} (ability/add-power 22))]}
"I")))

(defexpect get-rows

; Gives rows with scores
(expect
[{:limit 1 :scores [0 0]}
{:limit 4 :scores [1 2]}
{:limit 4 :type "annoyer" :scores [1 2]}
{:scores [12 0]}]
(functions/get-rows {:cards [{:power 1 :location [:row 1] :owner "I"}
{:power 2 :location [:row 1] :owner "U"}
{:power 4 :location [:row 2] :owner "I"}
{:power 8 :location [:row 2] :owner "I"}]
:rows [{:limit 1}{:limit 4}{}]
:rows [{:limit 1}{:limit 4 :type "annoyer"}{}]
:player-ids ["I" "U"]}
"I")))

Expand Down
53 changes: 46 additions & 7 deletions backend/test/rules/abilities_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,59 @@
; Adds power
(expect
7
(get-in ((ability/add-power 5) {:cards [{:power 2}]} 0)
(get-in ((:ability (ability/add-power 5)) {:cards [{:power 2}]} {:target 0})
[:cards 0 :power]))

; Removes power
(expect
-10
(get-in ((ability/add-power -13) {:cards [{}{:power 3}]} 1)
(get-in ((:ability (ability/add-power -13)) {:cards [{}{:power 3}]} {:target 1 :card-id 91 :something-else "whatever"})
[:cards 1 :power]))

; Returns info
(expect
"Enhance 99"
(:description (ability/add-power 99)))

(expect
"Weaken 1"
(:description (ability/add-power -1)))

(expect
1
(:target (ability/add-power 0))))

(defexpect type-add-power

; Does nothing when shouldn't
(expect
42
(get-in ((:ability (ability/type-add-power "invi" 10)) {:cards [{:power 42}] :rows [{}]} {:row-id 0 :card-id 0})
[:cards 0 :power]))
(expect
99
(get-in ((:ability (ability/type-add-power "invi" 10)) {:cards [{}{:power 99}] :rows [{}{:type "type-0"}]} {:row-id 1 :card-id 1})
[:cards 1 :power]))

; Adds power when should
(expect
52
(get-in ((:ability (ability/type-add-power "invi" 10)) {:cards [{:power 42}] :rows [{:type "invi"}]} {:row-id 0 :card-id 0})
[:cards 0 :power]))

(expect
-2
(get-in ((:ability (ability/type-add-power "vivi" -3)) {:cards [{:power 42} {:power 1}] :rows [{}{}{:type "vivi"}]} {:row-id 2 :card-id 1})
[:cards 1 :power]))

; Returns info
(expect
{:description "Enhance 99" :target 1}
((ability/add-power 99)))
"Enhance 7 on iris"
(:description (ability/type-add-power "iris" 7)))

(expect
"Weaken 3 on negat"
(:description (ability/type-add-power "negat" -3)))

(expect
{:description "Weaken 1" :target 1}
((ability/add-power -1))))
(expect
(not (contains? (ability/type-add-power "notarget" 1) :target))))
8 changes: 4 additions & 4 deletions backend/test/rules/create_game_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@
(:cards (create-game/new-game {:hands [[{:p 0}][{:attr 12 :sometext ""}]]})))

(expect
[{:limit 0} {:limit 3}]
(:rows (create-game/new-game {:limits [0 3]})))
[{:limit 92} {} {:type "yabidi"}]
(:rows (create-game/new-game {:rows [{:limit 92} {} {:type "yabidi"}]})))

; Game uses default config
(expect
(concat hands/default-hand hands/default-hand)
(map #(apply dissoc % [:location :owner]) (:cards (create-game/new-game))))

(expect
rows/default-limits
(vec (map :limit (:rows (create-game/new-game)))))
rows/default-rows
(:rows (create-game/new-game)))

(expect
player-ids/default-player-ids
Expand Down
14 changes: 9 additions & 5 deletions backend/test/rules/play_card_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
[rules.abilities :as ability]
[configs.messages :as messages]))

(def game-state {:cards [{:power 0 :location [:hand]} {:power 10 :ability (ability/add-power -1)} {:power 100 :ability (ability/add-power -100)}]
(def game-state {:cards [{:power 0 :location [:hand]}
(merge {:power 10} (ability/add-power -1))
(merge {:power 100} (ability/add-power -100))]
:rows [{}{}{}{}]
:next-play {:p0 {:card-id 0 :row-id 0} :p1 {:card-id 1 :row-id 3 :target 2}}
:player-ids ["p0" "p1"]})
Expand Down Expand Up @@ -85,13 +87,13 @@
{:error messages/need-target}
(play-card/play-card {:next-play {}
:rows [{}]
:cards [{:ability "some-ability" :owner "dumb"}]} "dumb" 0 0))
:cards [{:target 1 :owner "dumb"}]} "dumb" 0 0))

(expect
{:error messages/invalid-target}
(play-card/play-card {:next-play {}
:rows [{}]
:cards [{:ability "another-ability" :owner "McCheaty"}{:location ["nowhere"]}]} "McCheaty" 0 0 1))
:cards [{:target 1 :owner "McCheaty"}{:location ["nowhere"]}]} "McCheaty" 0 0 1))

; Saves next-play
(expect
Expand All @@ -106,7 +108,7 @@
(:next-play
(play-card/play-card {:next-play {}
:rows [{}{}{}{}]
:cards [{:location [:row 0]}{}{:owner "p" :add-power 1}]} "p" 2 3 0)))
:cards [{:location [:row 0]}{}{:owner "p" :target 1}]} "p" 2 3 0)))

(expect
[{:owner "p"}]
Expand All @@ -118,7 +120,9 @@

(let [new-game-state
(play-card/play-card
{:cards [{:power 0 :location [:hand] :owner "p0"} {:power 10 :ability (ability/add-power -1) :owner "p1"} {:power 100 :ability (ability/add-power -100)}]
{:cards [{:power 0 :location [:hand] :owner "p0"}
(merge {:power 10 :owner "p1"} (ability/add-power -1))
(merge {:power 100} (ability/add-power -100))]
:rows [{}{}{}{}{}]
:next-play {:p1 {:card-id 1 :row-id 3 :target 2}}}
"p0" 0 0)]
Expand Down