Skip to content

Commit 9ddf10f

Browse files
committed
Use String id for deck-building
This breaks AI Medium and AI Fighter in Mythos
1 parent f5fcaab commit 9ddf10f

File tree

8 files changed

+79
-49
lines changed

8 files changed

+79
-49
lines changed

cardshifter-api/src/main/java/com/cardshifter/api/config/DeckConfig.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,37 @@
1111

1212
public class DeckConfig implements PlayerConfig {
1313

14-
private Map<Integer, CardInfoMessage> cardData;
15-
private Map<Integer, Integer> chosen = new HashMap<Integer, Integer>();
16-
private Map<Integer, Integer> max = new HashMap<Integer, Integer>();
14+
private Map<String, CardInfoMessage> cardData;
15+
private Map<String, Integer> chosen = new HashMap<String, Integer>();
16+
private Map<String, Integer> max = new HashMap<String, Integer>();
1717
private int minSize;
1818
private int maxSize;
1919
private int maxPerCard;
2020

2121
public DeckConfig() {
22-
this(0, 0, new HashMap<Integer, CardInfoMessage>(), 0);
22+
this(0, 0, new HashMap<String, CardInfoMessage>(), 0);
2323
}
24-
public DeckConfig(int minSize, int maxSize, Map<Integer, CardInfoMessage> cardData, int maxPerCard) {
24+
public DeckConfig(int minSize, int maxSize, Map<String, CardInfoMessage> cardData, int maxPerCard) {
2525
this.minSize = minSize;
2626
this.maxSize = maxSize;
2727
this.maxPerCard = maxPerCard;
28-
this.cardData = new HashMap<Integer, CardInfoMessage>(cardData);
28+
this.cardData = new HashMap<String, CardInfoMessage>(cardData);
2929
}
3030

31-
public void setMax(int id, int max) {
31+
public void setMax(String id, int max) {
3232
this.max.put(id, max);
3333
}
3434

35-
public void setChosen(int id, int chosen) {
35+
public void setChosen(String id, int chosen) {
3636
this.chosen.put(id, chosen);
3737
}
3838

39-
public int getChosen(int id) {
40-
Integer chosen = this.chosen.get(id);
41-
return chosen == null ? 0 : chosen;
39+
public int getChosen(String id) {
40+
Integer value = this.chosen.get(id);
41+
return value == null ? 0 : value;
4242
}
4343

44-
public void removeChosen(int id) {
44+
public void removeChosen(String id) {
4545
if (this.chosen.get(id) > 1) {
4646
this.setChosen(id, this.getChosen().get(id) - 1);
4747
} else {
@@ -57,20 +57,20 @@ public int getMaxSize() {
5757
return maxSize;
5858
}
5959

60-
public Map<Integer, CardInfoMessage> getCardData() {
60+
public Map<String, CardInfoMessage> getCardData() {
6161
return Collections.unmodifiableMap(cardData);
6262
}
6363

64-
public Map<Integer, Integer> getChosen() {
64+
public Map<String, Integer> getChosen() {
6565
return Collections.unmodifiableMap(chosen);
6666
}
6767

6868
public void clearChosen() {
6969
chosen.clear();
7070
}
7171

72-
public Map<Integer, Integer> getMax() {
73-
return new HashMap<Integer, Integer>(max);
72+
public Map<String, Integer> getMax() {
73+
return new HashMap<String, Integer>(max);
7474
}
7575

7676
public int getMaxPerCard() {
@@ -92,30 +92,30 @@ public String toString() {
9292

9393
public void generateRandom() {
9494
Random random = new Random();
95-
List<Integer> ids = new ArrayList<Integer>(this.getCardData().keySet());
95+
List<String> ids = new ArrayList<String>(this.getCardData().keySet());
9696
while (this.total() < this.getMinSize()) {
97-
int randomId = ids.get(random.nextInt(ids.size()));
97+
String randomId = ids.get(random.nextInt(ids.size()));
9898
this.setChosen(randomId, this.getMaxFor(randomId));
9999
}
100100
}
101101

102-
public void add(int cardId) {
102+
public void add(String cardId) {
103103
Integer current = chosen.get(cardId);
104104
if (current == null) {
105105
current = 0;
106106
}
107107
chosen.put(cardId, current + 1);
108108
}
109109

110-
public int getMaxFor(int id) {
110+
public int getMaxFor(String id) {
111111
Integer value = this.max.get(id);
112112
return value == null ? maxPerCard : value;
113113
}
114114

115115
@Override
116116
public void beforeSend() {
117117
// Don't send information about cards that cannot be chosen
118-
for (Map.Entry<Integer, Integer> ee : this.max.entrySet()) {
118+
for (Map.Entry<String, Integer> ee : this.max.entrySet()) {
119119
if (ee.getValue() <= 0) {
120120
this.cardData.remove(ee.getKey());
121121
}

cardshifter-core/src/main/java/com/cardshifter/ai/AIs.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ public static ScoreConfigFactory<Entity, ECSAction> fighter() {
5353

5454
public static void mediumDeck(Entity entity, ConfigComponent config) {
5555
DeckConfig deck = config.getConfig(DeckConfig.class);
56-
createDeckFullWith(deck, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); // All B0Ts
56+
createDeckFullWith(deck, "spareparts", "gyrodroid", "the-chopper", "shieldmech", "robot-guard",
57+
"humadroid", "assassinatrix", "fortimech", "scout-mech", "supply-mech"); // Some Mechs
5758
}
5859

59-
private static void createDeckFullWith(DeckConfig deck, int... ids) {
60+
private static void createDeckFullWith(DeckConfig deck, String... ids) {
6061
if (deck == null) {
6162
return;
6263
}
63-
for (int id : ids) {
64+
for (String id : ids) {
6465
if (deck.total() >= deck.getMaxSize()) {
6566
return;
6667
}
@@ -70,15 +71,17 @@ private static void createDeckFullWith(DeckConfig deck, int... ids) {
7071

7172
public static void fighterDeck(Entity entity, ConfigComponent config) {
7273
DeckConfig deck = config.getConfig(DeckConfig.class);
73-
createDeckFullWith(deck, 4, 7, 8, 12, 16, 18, 24, 26, 27);
74-
deckAdd(deck, 9, 9, 19);
74+
createDeckFullWith(deck, "humadroid", "fortimech", "upgrado-mk-i", "body-armor");
75+
deckAdd(deck, "robot-guard", "robot-guard", "scout-mech", "scout-mech", "supply-mech");
76+
deckAdd(deck, "vetter", "wastelander", "cyberpimp", "web-boss", "inside-man");
77+
deckAdd(deck, "reinforced-cranial-implants", "full-body-cybernetics-upgrade");
7578
}
7679

77-
private static void deckAdd(DeckConfig deck, int... ids) {
80+
private static void deckAdd(DeckConfig deck, String... ids) {
7881
if (deck == null) {
7982
return;
8083
}
81-
for (int id : ids) {
84+
for (String id : ids) {
8285
if (deck.total() >= deck.getMaxSize()) {
8386
return;
8487
}

cardshifter-core/src/main/java/com/cardshifter/core/groovy/MaxInDeck.groovy

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.cardshifter.core.groovy
22

33
import com.cardshifter.api.config.DeckConfig
4+
import com.cardshifter.modapi.attributes.Attributes
5+
import com.cardshifter.modapi.attributes.ECSAttributeMap
46
import com.cardshifter.modapi.base.ECSGame
57
import com.cardshifter.modapi.base.Entity
68
import com.cardshifter.modapi.players.Players
@@ -11,7 +13,7 @@ import net.zomis.cardshifter.ecs.config.ConfigComponent
1113
*/
1214
class MaxInDeck {
1315

14-
private Map<Integer, Integer> cardCounts = [:]
16+
private Map<String, Integer> cardCounts = [:]
1517

1618
def setMaxCardCounts = {e ->
1719
ConfigComponent config = e.getComponent(ConfigComponent)
@@ -26,11 +28,11 @@ class MaxInDeck {
2628
void initialize(ECSGame game) {
2729
CardDelegate.metaClass.maxInDeck << {int count ->
2830
Entity e = entity()
29-
cardCounts.put(e.id, count)
31+
cardCounts.put(e.getComponent(ECSAttributeMap).get(Attributes.ID).get().get(), count)
3032
}
3133
CardDelegate.metaClass.token << {
3234
Entity e = entity()
33-
cardCounts.put(e.id, 0)
35+
cardCounts.put(e.getComponent(ECSAttributeMap).get(Attributes.ID).get().get(), 0)
3436
}
3537
}
3638

cardshifter-core/src/main/java/com/cardshifter/core/groovy/ZoneDelegate.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class ZoneDelegate {
2626

2727
def card(String name, Closure<?> closure) {
2828
def card = entity.game.newEntity()
29-
ECSAttributeMap.createOrGetFor(card).set(Attributes.NAME, name)
29+
def attributes = ECSAttributeMap.createOrGetFor(card)
30+
attributes.set(Attributes.NAME, name)
31+
attributes.set(Attributes.ID, name.toLowerCase().replaceAll(" ", "-"))
3032
cardDelegate.createCard(card, closure, Closure.OWNER_FIRST)
3133
zone.addOnBottom(card)
3234
}

cardshifter-fx/src/main/java/com/cardshifter/client/DeckBuilderWindow.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class DeckBuilderWindow {
5757

5858
private static final int CARDS_PER_PAGE = 12;
5959
private int currentPage = 0;
60-
private Map<Integer, CardInfoMessage> cardList = new HashMap<>();
60+
private Map<String, CardInfoMessage> cardList = new HashMap<>();
6161
private List<List<CardInfoMessage>> pageList = new ArrayList<>();
6262
private DeckConfig activeDeckConfig;
6363
private CardInfoMessage cardBeingDragged;
@@ -117,7 +117,9 @@ private void displayCurrentPage() {
117117
if (this.activeDeckConfig.getChosen().get(message.getId()) != null) {
118118
numChosenCards = this.activeDeckConfig.getChosen().get(message.getId());
119119
}
120-
Label numberOfCardsLabel = new Label(String.format("%d / %d", numChosenCards, this.activeDeckConfig.getMaxFor(message.getId())));
120+
String id = (String) message.getProperties().get("id");
121+
122+
Label numberOfCardsLabel = new Label(String.format("%d / %d", numChosenCards, this.activeDeckConfig.getMaxFor(id)));
121123
numberOfCardsLabel.setTextFill(Color.WHITE);
122124
numberOfCardsBox.relocate(cardPane.getPrefWidth()/2.6, cardPane.getPrefHeight() - cardPane.getPrefHeight()/18);
123125
numberOfCardsLabel.relocate(cardPane.getPrefWidth()/2.3, cardPane.getPrefHeight() - cardPane.getPrefHeight()/18);
@@ -158,9 +160,9 @@ private String cleanName(String name) {
158160

159161
private void displayActiveDeck() {
160162
this.activeDeckBox.getChildren().clear();
161-
List<Integer> sortedKeys = new ArrayList<>(this.activeDeckConfig.getChosen().keySet());
162-
Collections.sort(sortedKeys, Comparator.comparingInt(key -> key));
163-
for (Integer cardId : sortedKeys) {
163+
List<String> sortedKeys = new ArrayList<>(this.activeDeckConfig.getChosen().keySet());
164+
Collections.sort(sortedKeys);
165+
for (String cardId : sortedKeys) {
164166
if (!cardList.containsKey(cardId)) {
165167
activeDeckConfig.setChosen(cardId, 0);
166168
continue;
@@ -174,20 +176,25 @@ private void displayActiveDeck() {
174176
}
175177

176178
private void addCardToActiveDeck(MouseEvent event, CardInfoMessage message) {
179+
String id = idFor(message);
177180
if (this.activeDeckConfig.total() < this.activeDeckConfig.getMaxSize()) {
178-
if(this.activeDeckConfig.getChosen().get(message.getId()) == null) {
179-
this.activeDeckConfig.setChosen(message.getId(), 1);
181+
if(this.activeDeckConfig.getChosen().get(id) == null) {
182+
this.activeDeckConfig.setChosen(id, 1);
180183
} else {
181-
if (this.activeDeckConfig.getChosen().get(message.getId()) < this.activeDeckConfig.getMaxFor(message.getId())) {
182-
this.activeDeckConfig.add(message.getId());
184+
if (this.activeDeckConfig.getChosen().get(id) < this.activeDeckConfig.getMaxFor(id)) {
185+
this.activeDeckConfig.add(id);
183186
}
184187
}
185188
}
186189
this.displayActiveDeck();
187190
this.displayCurrentPage();
188191
}
189-
190-
private void removeCardFromDeck(MouseEvent event, int cardId) {
192+
193+
private String idFor(CardInfoMessage message) {
194+
return (String) message.getProperties().get("id");
195+
}
196+
197+
private void removeCardFromDeck(MouseEvent event, String cardId) {
191198
if (this.activeDeckConfig.getChosen().get(cardId) != null) {
192199
this.activeDeckConfig.removeChosen(cardId);
193200
}
@@ -236,7 +243,7 @@ private void buildDeckInfoBox() {
236243
Map<Integer, Integer> scrapCostValues = new HashMap<>();
237244
Map<String, Integer> creatureTypes = new HashMap<>();
238245

239-
for (int cardId : this.activeDeckConfig.getChosen().keySet()) {
246+
for (String cardId : this.activeDeckConfig.getChosen().keySet()) {
240247

241248
CardInfoMessage card = this.cardList.get(cardId);
242249
int cardCount = this.activeDeckConfig.getChosen().get(cardId);

cardshifter-modapi/src/main/java/com/cardshifter/modapi/attributes/Attributes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
public final class Attributes {
1010
public static final ECSAttribute NAME = new ECSAttributeDefault("name");
11+
public static final ECSAttribute ID = new ECSAttributeDefault("id");
1112
public static final ECSAttribute FLAVOR = new ECSAttributeDefault("flavor");
1213
public static final ECSAttribute IMAGE_PATH = new ECSAttributeDefault("imagePath");
1314

cardshifter-modapi/src/main/java/com/cardshifter/modapi/cards/DeckComponent.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package com.cardshifter.modapi.cards;
22

33
import com.cardshifter.api.config.DeckConfig;
4+
import com.cardshifter.modapi.attributes.Attributes;
5+
import com.cardshifter.modapi.attributes.ECSAttributeData;
6+
import com.cardshifter.modapi.attributes.ECSAttributeMap;
47
import com.cardshifter.modapi.base.ECSGame;
58
import com.cardshifter.modapi.base.Entity;
69
import net.zomis.cardshifter.ecs.config.ConfigComponent;
710

811
import java.util.Map;
12+
import java.util.Set;
13+
import java.util.stream.Stream;
914

1015
public class DeckComponent extends ZoneComponent {
1116

@@ -21,12 +26,19 @@ public void createFromConfig(String name) {
2126

2227
private static void setupDeck(DeckComponent deck, DeckConfig deckConf) {
2328
ECSGame game = deck.getOwner().getGame();
24-
for (Map.Entry<Integer, Integer> chosen : deckConf.getChosen().entrySet()) {
25-
int entityId = chosen.getKey();
29+
for (Map.Entry<String, Integer> chosen : deckConf.getChosen().entrySet()) {
30+
String cardId = chosen.getKey();
2631
int count = chosen.getValue();
2732

2833
for (int i = 0; i < count; i++) {
29-
Entity existing = game.getEntity(entityId);
34+
Stream<Entity> entities = game.getEntitiesWithComponent(ECSAttributeMap.class).stream().filter(e ->
35+
e.getComponent(ECSAttributeMap.class).get(Attributes.ID)
36+
.map(ECSAttributeData::get)
37+
.orElse("")
38+
.equals(cardId)
39+
);
40+
41+
Entity existing = entities.findFirst().orElseThrow(() -> new RuntimeException("Unable to find an entity with id " + cardId));
3042
Entity copy = existing.copy();
3143
deck.addOnBottom(copy);
3244
}

cardshifter-modapi/src/main/java/net/zomis/cardshifter/ecs/config/DeckConfigFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.cardshifter.api.config.DeckConfig;
44
import com.cardshifter.api.outgoing.CardInfoMessage;
5+
import com.cardshifter.modapi.attributes.Attributes;
6+
import com.cardshifter.modapi.attributes.ECSAttributeMap;
57
import com.cardshifter.modapi.base.Entity;
68
import net.zomis.cardshifter.ecs.EntitySerialization;
79

@@ -12,9 +14,10 @@
1214
public class DeckConfigFactory {
1315

1416
public static DeckConfig create(int minSize, int maxSize, List<Entity> cards, int maxCardsPerType) {
15-
Map<Integer, CardInfoMessage> map = new HashMap<>();
17+
Map<String, CardInfoMessage> map = new HashMap<>();
1618
for (Entity entity : cards) {
17-
map.put(entity.getId(), EntitySerialization.serialize(0, entity));
19+
String id = ECSAttributeMap.createOrGetFor(entity).getAttribute(Attributes.ID).get();
20+
map.put(id, EntitySerialization.serialize(0, entity));
1821
}
1922
return new DeckConfig(minSize, maxSize, map, maxCardsPerType);
2023
}

0 commit comments

Comments
 (0)