From de656074d0056f061c571bb624331e8f1193444d Mon Sep 17 00:00:00 2001 From: Noah Date: Wed, 12 Aug 2020 10:18:41 -0400 Subject: [PATCH] Move cost records and protocol back into game.core --- src/clj/game/core.clj | 13 +---- src/clj/game/core/costs/cost_impls.clj | 78 +++++++++++++++----------- src/clj/game/cost_interfaces.clj | 47 ---------------- test/clj/game/engine/costs_test.clj | 21 ++++--- 4 files changed, 57 insertions(+), 102 deletions(-) delete mode 100644 src/clj/game/cost_interfaces.clj diff --git a/src/clj/game/core.clj b/src/clj/game/core.clj index 4d683b0e1f..c8da19083d 100644 --- a/src/clj/game/core.clj +++ b/src/clj/game/core.clj @@ -21,19 +21,10 @@ [jinteki.cards :refer [all-cards]] [tasks.nrdb :refer [replace-collection update-config]] [tasks.altart :refer [add-art]] - [game.quotes :as quotes] - [game.cost-interfaces :refer :all]) + [game.quotes :as quotes]) (:import [game.core.state State] [game.core.player Corp Runner] - [game.core.card Card] - [game.cost_interfaces Click Credit Trash Forfeit ForfeitSelf Tag - ReturnToHand RemoveFromGame RfgProgram - TrashInstalledRunnerCard TrashInstalledHardware TrashInstalledProgram TrashInstalledResource TrashInstalledConnection - TrashRezzedIce TrashFromDeck TrashFromHand RandomlyTrashFromHand TrashEntireHand - TrashHardwareFromHand TrashProgramFromHand TrashResourceFromHand - NetDamage MeatDamage BrainDamage - ShuffleInstalledToDeck AddInstalledToBottomOfDeck - AnyAgendaCounter AnyVirusCounter AdvancementCounter AgendaCounter PowerCounter VirusCounter])) + [game.core.card Card])) (load "core/events") ; triggering of events (load "core/cards") ; retrieving and updating cards diff --git a/src/clj/game/core/costs/cost_impls.clj b/src/clj/game/core/costs/cost_impls.clj index d559743d23..efe985e1d6 100644 --- a/src/clj/game/core/costs/cost_impls.clj +++ b/src/clj/game/core/costs/cost_impls.clj @@ -6,13 +6,25 @@ lose-tags number-of-virus-counters pick-virus-counters-to-spend) +(defprotocol CostFns + (cost-name [this]) + (label [this]) + (rank [this]) + (value [this]) + (payable? [this state side card] + [this state side eid card] + [this state side eid card extra]) + (handler [this state side card actions] + [this state side eid card actions] + [this state side eid card actions extra])) + (def cost-records {}) (defn register-cost [cost-constructor] (alter-var-root #'cost-records assoc (cost-name (cost-constructor 1)) cost-constructor)) -(extend-type Click +(defrecord Click [amount] CostFns (cost-name [this] :click) (label [this] (->> (repeat "[Click]") @@ -64,7 +76,7 @@ (-> (card-def %) :interactions :pay-credits ((fn [x] (:custom-amount x 0)))))) (reduce +)))) -(extend-type Credit +(defrecord Credit [amount] CostFns (cost-name [this] :credit) (label [this] (str (:amount this) " [Credits]")) @@ -88,7 +100,7 @@ (complete-with-result state side eid (str "pays 0 [Credits]")))))) (register-cost ->Credit) -(extend-type Trash +(defrecord Trash [amount] CostFns (cost-name [this] :trash) (label [this] "[trash]") @@ -110,7 +122,7 @@ (wait-for (forfeit state side agenda {:msg false}) (forfeit-multiple state side eid (rest agendas) (conj acc agenda)))))) -(extend-type Forfeit +(defrecord Forfeit [amount] CostFns (cost-name [this] :forfeit) (label [this] (str "forfeit " (quantify (:amount this) "Agenda"))) @@ -134,7 +146,7 @@ card nil))) (register-cost ->Forfeit) -(extend-type ForfeitSelf +(defrecord ForfeitSelf [amount] CostFns (cost-name [this] :forfeit-self) (label [this] "forfeit this Agenda") @@ -149,7 +161,7 @@ (str "forfeits " (:title card)))))) (register-cost ->ForfeitSelf) -(extend-type Tag +(defrecord Tag [amount] CostFns (cost-name [this] :tag) (label [this] (str "remove " (quantify (:amount this) "tag"))) @@ -162,7 +174,7 @@ (complete-with-result state side eid (str "removes " (quantify (:amount this) "tag")))))) (register-cost ->Tag) -(extend-type ReturnToHand +(defrecord ReturnToHand [amount] CostFns (cost-name [this] :return-to-hand) (label [this] "return this card to your hand") @@ -178,7 +190,7 @@ " to " (if (= :corp side) "HQ" "their grip"))))) (register-cost ->ReturnToHand) -(extend-type RemoveFromGame +(defrecord RemoveFromGame [amount] CostFns (cost-name [this] :remove-from-game) (label [this] "remove this card from the game") @@ -193,7 +205,7 @@ (str "removes " (:title card) " from the game")))) (register-cost ->RemoveFromGame) -(extend-type RfgProgram +(defrecord RfgProgram [amount] CostFns (cost-name [this] :rfg-program) (label [this] (str "remove " (quantify (:amount this) "installed program") @@ -221,7 +233,7 @@ card nil))) (register-cost ->RfgProgram) -(extend-type TrashInstalledRunnerCard +(defrecord TrashInstalledRunnerCard [amount] CostFns (cost-name [this] :installed) (label [this] (str "trash " (quantify (:amount this) "installed card"))) @@ -245,7 +257,7 @@ card nil))) (register-cost ->TrashInstalledRunnerCard) -(extend-type TrashInstalledHardware +(defrecord TrashInstalledHardware [amount] CostFns (cost-name [this] :hardware) (label [this] (str "trash " (quantify (:amount this) "installed piece") " of hardware")) @@ -269,7 +281,7 @@ card nil))) (register-cost ->TrashInstalledHardware) -(extend-type TrashInstalledProgram +(defrecord TrashInstalledProgram [amount] CostFns (cost-name [this] :program) (label [this] (str "trash " (quantify (:amount this) "installed program"))) @@ -293,7 +305,7 @@ card nil))) (register-cost ->TrashInstalledProgram) -(extend-type TrashInstalledResource +(defrecord TrashInstalledResource [amount] CostFns (cost-name [this] :resource) (label [this] (str "trash " (quantify (:amount this) "installed resource"))) @@ -317,7 +329,7 @@ card nil))) (register-cost ->TrashInstalledResource) -(extend-type TrashInstalledConnection +(defrecord TrashInstalledConnection [amount] CostFns (cost-name [this] :connection) (label [this] (str "trash " (str "trash " (quantify (:amount this) "installed connection resource")))) @@ -344,7 +356,7 @@ card nil))) (register-cost ->TrashInstalledConnection) -(extend-type TrashRezzedIce +(defrecord TrashRezzedIce [amount] CostFns (cost-name [this] :ice) (label [this] (str "trash " (str "trash " (quantify (:amount this) "installed rezzed ICE" "")))) @@ -368,7 +380,7 @@ card nil))) (register-cost ->TrashRezzedIce) -(extend-type TrashFromDeck +(defrecord TrashFromDeck [amount] CostFns (cost-name [this] :trash-from-deck) (label [this] (str "trash " (quantify (:amount this) "card") " from the top of your deck")) @@ -384,7 +396,7 @@ (if (= :corp side) "R&D" "the stack")))))) (register-cost ->TrashFromDeck) -(extend-type TrashFromHand +(defrecord TrashFromHand [amount] CostFns (cost-name [this] :trash-from-hand) (label [this] (str "trash " (quantify (:amount this) "card") " from your hand")) @@ -413,7 +425,7 @@ nil nil)))) (register-cost ->TrashFromHand) -(extend-type RandomlyTrashFromHand +(defrecord RandomlyTrashFromHand [amount] CostFns (cost-name [this] :randomly-trash-from-hand) (label [this] (str "trash " (quantify (:amount this) "card") " randomly from your hand")) @@ -429,7 +441,7 @@ (if (= :corp side) "HQ" "the grip")))))) (register-cost ->RandomlyTrashFromHand) -(extend-type TrashEntireHand +(defrecord TrashEntireHand [amount] CostFns (cost-name [this] :trash-entire-hand) (label [this] "trash all cards in your hand") @@ -448,7 +460,7 @@ (str " (" (join ", " (map :title async-result)) ")")))))))) (register-cost ->TrashEntireHand) -(extend-type TrashHardwareFromHand +(defrecord TrashHardwareFromHand [amount] CostFns (cost-name [this] :trash-hardware-from-hand) (label [this] (str "trash " (quantify (:amount this) "piece") " of hardware in your grip")) @@ -473,7 +485,7 @@ nil nil))) (register-cost ->TrashHardwareFromHand) -(extend-type TrashProgramFromHand +(defrecord TrashProgramFromHand [amount] CostFns (cost-name [this] :trash-program-from-hand) (label [this] (str "trash " (quantify (:amount this) "program") " in your grip")) @@ -498,7 +510,7 @@ nil nil))) (register-cost ->TrashProgramFromHand) -(extend-type TrashResourceFromHand +(defrecord TrashResourceFromHand [amount] CostFns (cost-name [this] :trash-resource-from-hand) (label [this] (str "trash " (quantify (:amount this) "resource") " in your grip")) @@ -523,7 +535,7 @@ nil nil))) (register-cost ->TrashResourceFromHand) -(extend-type NetDamage +(defrecord NetDamage [amount] CostFns (cost-name [this] :net) (label [this] (str "suffer " (:amount this) " net damage")) @@ -538,7 +550,7 @@ (str "suffers " (:amount this) " net damage"))))) (register-cost ->NetDamage) -(extend-type MeatDamage +(defrecord MeatDamage [amount] CostFns (cost-name [this] :meat) (label [this] (str "suffer " (:amount this) " meat damage")) @@ -553,7 +565,7 @@ (str "suffers " (:amount this) " meat damage"))))) (register-cost ->MeatDamage) -(extend-type BrainDamage +(defrecord BrainDamage [amount] CostFns (cost-name [this] :brain) (label [this] (str "suffer " (:amount this) " brain damage")) @@ -568,7 +580,7 @@ (str "suffers " (:amount this) " brain damage"))))) (register-cost ->BrainDamage) -(extend-type ShuffleInstalledToDeck +(defrecord ShuffleInstalledToDeck [amount] CostFns (cost-name [this] :shuffle-installed-to-stack) (label [this] (str "shuffle " (quantify (:amount this) "installed card") " into your deck")) @@ -596,7 +608,7 @@ nil nil))) (register-cost ->ShuffleInstalledToDeck) -(extend-type AddInstalledToBottomOfDeck +(defrecord AddInstalledToBottomOfDeck [amount] CostFns (cost-name [this] :add-installed-to-bottom-of-deck) (label [this] (str "add " (quantify (:amount this) "installed card") " to the bottom of your deck")) @@ -624,7 +636,7 @@ card nil)))) (register-cost ->AddInstalledToBottomOfDeck) -(extend-type AnyAgendaCounter +(defrecord AnyAgendaCounter [amount] CostFns (cost-name [this] :any-agenda-counter) (label [this] "any agenda counter") @@ -646,7 +658,7 @@ nil nil))) (register-cost ->AnyAgendaCounter) -(extend-type AnyVirusCounter +(defrecord AnyVirusCounter [amount] CostFns (cost-name [this] :any-virus-counter) (label [this] (str "any " (quantify (:amount this) "virus counter"))) @@ -659,7 +671,7 @@ (complete-with-result state side eid (str "spends " (:msg async-result)))))) (register-cost ->AnyVirusCounter) -(extend-type AdvancementCounter +(defrecord AdvancementCounter [amount] CostFns (cost-name [this] :advancement) (label [this] (if (< 1 (:amount this)) @@ -679,7 +691,7 @@ " from on " (:title card)))))) (register-cost ->AdvancementCounter) -(extend-type AgendaCounter +(defrecord AgendaCounter [amount] CostFns (cost-name [this] :agenda) (label [this] (if (< 1 (:amount this)) @@ -699,7 +711,7 @@ " from on " (:title card)))))) (register-cost ->AgendaCounter) -(extend-type PowerCounter +(defrecord PowerCounter [amount] CostFns (cost-name [this] :power) (label [this] (if (< 1 (:amount this)) @@ -719,7 +731,7 @@ " from on " (:title card)))))) (register-cost ->PowerCounter) -(extend-type VirusCounter +(defrecord VirusCounter [amount] CostFns (cost-name [this] :virus) (label [this] (if (< 1 (:amount this)) diff --git a/src/clj/game/cost_interfaces.clj b/src/clj/game/cost_interfaces.clj deleted file mode 100644 index cc9faab3c6..0000000000 --- a/src/clj/game/cost_interfaces.clj +++ /dev/null @@ -1,47 +0,0 @@ -(ns game.cost-interfaces) - -(defrecord Click [amount]) -(defrecord Credit [amount]) -(defrecord Trash [amount]) -(defrecord Forfeit [amount]) -(defrecord ForfeitSelf [amount]) -(defrecord Tag [amount]) -(defrecord ReturnToHand [amount]) -(defrecord RemoveFromGame [amount]) -(defrecord RfgProgram [amount]) -(defrecord TrashInstalledRunnerCard [amount]) -(defrecord TrashInstalledHardware [amount]) -(defrecord TrashInstalledProgram [amount]) -(defrecord TrashInstalledResource [amount]) -(defrecord TrashInstalledConnection [amount]) -(defrecord TrashRezzedIce [amount]) -(defrecord TrashFromDeck [amount]) -(defrecord TrashFromHand [amount]) -(defrecord RandomlyTrashFromHand [amount]) -(defrecord TrashEntireHand [amount]) -(defrecord TrashHardwareFromHand [amount]) -(defrecord TrashProgramFromHand [amount]) -(defrecord TrashResourceFromHand [amount]) -(defrecord NetDamage [amount]) -(defrecord MeatDamage [amount]) -(defrecord BrainDamage [amount]) -(defrecord ShuffleInstalledToDeck [amount]) -(defrecord AddInstalledToBottomOfDeck [amount]) -(defrecord AnyAgendaCounter [amount]) -(defrecord AnyVirusCounter [amount]) -(defrecord AdvancementCounter [amount]) -(defrecord AgendaCounter [amount]) -(defrecord PowerCounter [amount]) -(defrecord VirusCounter [amount]) - -(defprotocol CostFns - (cost-name [this]) - (label [this]) - (rank [this]) - (value [this]) - (payable? [this state side card] - [this state side eid card] - [this state side eid card extra]) - (handler [this state side card actions] - [this state side eid card actions] - [this state side eid card actions extra])) diff --git a/test/clj/game/engine/costs_test.clj b/test/clj/game/engine/costs_test.clj index 8d3726d83e..2c9c8dd0ff 100644 --- a/test/clj/game/engine/costs_test.clj +++ b/test/clj/game/engine/costs_test.clj @@ -1,6 +1,5 @@ (ns game.engine.costs-test (:require [game.core :as core] - [game.cost-interfaces :refer :all] [game.core-test :refer :all] [game.utils-test :refer :all] [game.macros-test :refer :all] @@ -44,25 +43,25 @@ (deftest merge-and-convert-costs (testing "No defaults, already merged" - (is (= [(->Credit 1)] (core/merge-and-convert-costs [[:credit 1]])))) + (is (= [(core/->Credit 1)] (core/merge-and-convert-costs [[:credit 1]])))) (testing "Costs are already flattened" - (is (= [(->Click 1) (->Credit 1)] (core/merge-and-convert-costs [[:credit 1 :click 1]])))) + (is (= [(core/->Click 1) (core/->Credit 1)] (core/merge-and-convert-costs [[:credit 1 :click 1]])))) (testing "Passed as a flattened vec" - (is (= [(->Credit 1)] (core/merge-and-convert-costs [:credit 1])))) + (is (= [(core/->Credit 1)] (core/merge-and-convert-costs [:credit 1])))) (testing "Default type is only element" - (is (= [(->Credit 1)] (core/merge-and-convert-costs [[:credit]])))) + (is (= [(core/->Credit 1)] (core/merge-and-convert-costs [[:credit]])))) (testing "Default plus explicit" - (is (= [(->Click 1) (->Credit 1)] (core/merge-and-convert-costs [[:click :credit 1]])))) + (is (= [(core/->Click 1) (core/->Credit 1)] (core/merge-and-convert-costs [[:click :credit 1]])))) (testing "Costs ending with defaults expand" - (is (= [(->Click 1) (->Credit 1)] (core/merge-and-convert-costs [[:credit 1 :click]])))) + (is (= [(core/->Click 1) (core/->Credit 1)] (core/merge-and-convert-costs [[:credit 1 :click]])))) (testing "Costs aren't reordered" - (is (not= [(->Credit 1) (->Click 1)] (core/merge-and-convert-costs [[:click 1 :credit 1]])))) + (is (not= [(core/->Credit 1) (core/->Click 1)] (core/merge-and-convert-costs [[:click 1 :credit 1]])))) (testing "Costs with all defaults are expanded" - (is (= [(->Click 1) (->Credit 1)] (core/merge-and-convert-costs [[:click :credit]])))) + (is (= [(core/->Click 1) (core/->Credit 1)] (core/merge-and-convert-costs [[:click :credit]])))) (testing "Costs are combined" - (is (= [(->Click 4) (->Credit 2)] (core/merge-and-convert-costs [[:click 1] [:click 3] [:credit 1] [:credit 1]])))) + (is (= [(core/->Click 4) (core/->Credit 2)] (core/merge-and-convert-costs [[:click 1] [:click 3] [:credit 1] [:credit 1]])))) (testing "Deeply nested costs are flattened" - (is (= [(->Click 3)] (core/merge-and-convert-costs [[[[[:click 1]]] [[[[[:click 1]]]]]] :click 1])))) + (is (= [(core/->Click 3)] (core/merge-and-convert-costs [[[[[:click 1]]] [[[[[:click 1]]]]]] :click 1])))) (testing "Empty costs return an empty list" (is (= [] (core/merge-and-convert-costs [])))) (testing "nil costs return an empty list"