Ironed out the solo-draft definitions

This commit is contained in:
marcin.sciesinski
2018-04-13 07:42:09 -07:00
parent b3066109c4
commit ec01b99b3e
9 changed files with 333 additions and 90 deletions

View File

@@ -1,30 +1,39 @@
package com.gempukku.lotro.draft2;
import com.gempukku.lotro.draft2.builder.CardCollectionProducer;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.DefaultCardCollection;
import java.util.List;
public class DefaultSoloDraft implements SoloDraft {
private String _format;
private SoloDraftPicks _picks;
private CardCollectionProducer _newCollection;
private List<DraftChoiceDefinition> _draftChoiceDefinitions;
public DefaultSoloDraft(String format, SoloDraftPicks picks) {
public DefaultSoloDraft(String format, CardCollectionProducer newCollection, List<DraftChoiceDefinition> draftChoiceDefinitions) {
_format = format;
_picks = picks;
_newCollection = newCollection;
_draftChoiceDefinitions = draftChoiceDefinitions;
}
@Override
public CardCollection initializeNewCollection(long seed) {
return new DefaultCardCollection();
return (_newCollection != null) ? _newCollection.getCardCollection(seed) : null;
}
@Override
public CardCollection getAvailableChoices(long seed, int stage) {
return _picks.getChoices(seed, stage);
public Iterable<DraftChoice> getAvailableChoices(long seed, int stage) {
return _draftChoiceDefinitions.get(stage).getDraftChoice(seed, stage);
}
@Override
public CardCollection getCardsForChoiceId(String choiceId, long seed, int stage) {
return _draftChoiceDefinitions.get(stage).getCardsForChoiceId(choiceId, seed, stage);
}
@Override
public boolean hasNextStage(long seed, int stage) {
return _picks.hasNextStage(seed, stage);
return stage + 1 < _draftChoiceDefinitions.size();
}
@Override

View File

@@ -0,0 +1,9 @@
package com.gempukku.lotro.draft2;
import com.gempukku.lotro.game.CardCollection;
public interface DraftChoiceDefinition {
Iterable<SoloDraft.DraftChoice> getDraftChoice(long seed, int stage);
CardCollection getCardsForChoiceId(String choiceId, long seed, int stage);
}

View File

@@ -5,9 +5,17 @@ import com.gempukku.lotro.game.CardCollection;
public interface SoloDraft {
CardCollection initializeNewCollection(long seed);
CardCollection getAvailableChoices(long seed, int stage);
Iterable<DraftChoice> getAvailableChoices(long seed, int stage);
CardCollection getCardsForChoiceId(String choiceId, long seed, int stage);
boolean hasNextStage(long seed, int stage);
String getFormat();
interface DraftChoice {
String getChoiceId();
String getBlueprintId();
String getChoiceUrl();
}
}

View File

@@ -1,6 +1,8 @@
package com.gempukku.lotro.draft2;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.draft2.builder.CardCollectionProducer;
import com.gempukku.lotro.draft2.builder.DraftChoiceBuilder;
import com.gempukku.lotro.draft2.builder.StartingPoolBuilder;
import com.gempukku.lotro.game.formats.LotroFormatLibrary;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -9,11 +11,12 @@ import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
public class SoloDraftDefinitions {
private Map<String, SoloDraft> draftTypes = new HashMap<String, SoloDraft>();
private StartingPoolBuilder startingPoolBuilder = new StartingPoolBuilder();
private DraftChoiceBuilder draftChoiceBuilder = new DraftChoiceBuilder();
public SoloDraftDefinitions() {
try {
@@ -22,7 +25,7 @@ public class SoloDraftDefinitions {
JSONParser parser = new JSONParser();
JSONArray object = (JSONArray) parser.parse(reader);
for (Object draftDefObj : object) {
String type = (String)((JSONObject) draftDefObj).get("type");
String type = (String) ((JSONObject) draftDefObj).get("type");
String location = (String) ((JSONObject) draftDefObj).get("location");
draftTypes.put(type, loadDraft(location));
}
@@ -41,12 +44,29 @@ public class SoloDraftDefinitions {
JSONParser parser = new JSONParser();
JSONObject object = (JSONObject) parser.parse(reader);
String format = (String) object.get("format");
return null;
CardCollectionProducer cardCollectionProducer = null;
JSONObject startingPool = (JSONObject) object.get("startingPool");
if (startingPool != null)
cardCollectionProducer = startingPoolBuilder.buildCardCollectionProducer(startingPool);
List<DraftChoiceDefinition> draftChoiceDefinitions = new ArrayList<DraftChoiceDefinition>();
JSONArray choices = (JSONArray) object.get("choices");
Iterator<JSONObject> choicesIterator = choices.iterator();
while (choicesIterator.hasNext()) {
JSONObject choice = choicesIterator.next();
DraftChoiceDefinition draftChoiceDefinition = draftChoiceBuilder.buildDraftChoiceDefinition(choice);
int repeatCount = ((Number) choice.get("repeat")).intValue();
for (int i = 0; i < repeatCount; i++)
draftChoiceDefinitions.add(draftChoiceDefinition);
}
return new DefaultSoloDraft(format, cardCollectionProducer, draftChoiceDefinitions);
} catch (ParseException exp) {
throw new RuntimeException("Problem loading solo draft "+file, exp);
throw new RuntimeException("Problem loading solo draft " + file, exp);
}
} catch (IOException exp) {
throw new RuntimeException("Problem loading solo draft "+file, exp);
throw new RuntimeException("Problem loading solo draft " + file, exp);
}
}

View File

@@ -1,13 +0,0 @@
package com.gempukku.lotro.draft2;
import com.gempukku.lotro.game.CardCollection;
public class SoloDraftPicks {
public CardCollection getChoices(long seed, int stage) {
return null;
}
public boolean hasNextStage(long seed, int stage) {
return false;
}
}

View File

@@ -0,0 +1,7 @@
package com.gempukku.lotro.draft2.builder;
import com.gempukku.lotro.game.CardCollection;
public interface CardCollectionProducer {
CardCollection getCardCollection(long seed);
}

View File

@@ -0,0 +1,169 @@
package com.gempukku.lotro.draft2.builder;
import com.gempukku.lotro.draft2.DraftChoiceDefinition;
import com.gempukku.lotro.draft2.SoloDraft;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.DefaultCardCollection;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.*;
public class DraftChoiceBuilder {
public DraftChoiceDefinition buildDraftChoiceDefinition(JSONObject choiceDefinition) {
return constructDraftChoiceDefinition(choiceDefinition);
}
private DraftChoiceDefinition constructDraftChoiceDefinition(JSONObject choiceDefinition) {
String choiceDefinitionType = (String) choiceDefinition.get("type");
JSONObject data = (JSONObject) choiceDefinition.get("data");
if (choiceDefinitionType.equals("singleCollectionPick"))
return buildSingleCollectionPickDraftChoiceDefinition(data);
else if (choiceDefinitionType.equals("multipleCardPick"))
return buildMultipleCardPickDraftChoiceDefinition(data);
else if (choiceDefinitionType.equals("randomSwitch"))
return buildRandomSwitchDraftChoiceDeifinition(data);
else
throw new RuntimeException("Unknown choiceDefinitionType: " + choiceDefinitionType);
}
private DraftChoiceDefinition buildSingleCollectionPickDraftChoiceDefinition(JSONObject data) {
JSONArray switchResult = (JSONArray) data.get("possiblePicks");
final Map<String, List<String>> cardsMap = new HashMap<String, List<String>>();
final List<SoloDraft.DraftChoice> draftChoices = new ArrayList<SoloDraft.DraftChoice>();
for (JSONObject pickDefinition : (Iterable<JSONObject>) switchResult) {
final String choiceId = (String) pickDefinition.get("choiceId");
final String url = (String) pickDefinition.get("url");
JSONArray cards = (JSONArray) pickDefinition.get("cards");
List<String> cardIds = new ArrayList<String>();
for (String card : (Iterable<String>) cards)
cardIds.add(card);
draftChoices.add(
new SoloDraft.DraftChoice() {
@Override
public String getChoiceId() {
return choiceId;
}
@Override
public String getBlueprintId() {
return null;
}
@Override
public String getChoiceUrl() {
return url;
}
});
cardsMap.put(choiceId, cardIds);
}
return new DraftChoiceDefinition() {
@Override
public Iterable<SoloDraft.DraftChoice> getDraftChoice(long seed, int stage) {
return draftChoices;
}
@Override
public CardCollection getCardsForChoiceId(String choiceId, long seed, int stage) {
List<String> cardIds = cardsMap.get(choiceId);
DefaultCardCollection cardCollection = new DefaultCardCollection();
for (String cardId : cardIds)
cardCollection.addItem(cardId, 1);
return cardCollection;
}
};
}
private DraftChoiceDefinition buildMultipleCardPickDraftChoiceDefinition(JSONObject data) {
final long selectionSeed = ((Number) data.get("seed")).longValue();
final int count = ((Number) data.get("count")).intValue();
JSONArray availableCards = (JSONArray) data.get("availableCards");
final List<String> cards = new ArrayList<String>();
for (String availableCard : (Iterable<String>) availableCards)
cards.add(availableCard);
return new DraftChoiceDefinition() {
@Override
public Iterable<SoloDraft.DraftChoice> getDraftChoice(long seed, int stage) {
final List<String> shuffledCards = getShuffledCards(seed, stage);
List<SoloDraft.DraftChoice> draftableCards = new ArrayList<SoloDraft.DraftChoice>(count);
for (int i=0; i<count; i++) {
final int finalI = i;
draftableCards.add(
new SoloDraft.DraftChoice() {
@Override
public String getChoiceId() {
return shuffledCards.get(finalI);
}
@Override
public String getBlueprintId() {
return shuffledCards.get(finalI);
}
@Override
public String getChoiceUrl() {
return null;
}
});
}
return draftableCards;
}
@Override
public CardCollection getCardsForChoiceId(String choiceId, long seed, int stage) {
List<String> shuffledCards = getShuffledCards(seed, stage);
for (int i=0; i<count; i++) {
if (shuffledCards.get(i).equals(choiceId)) {
DefaultCardCollection result = new DefaultCardCollection();
result.addItem(choiceId, 1);
}
}
return new DefaultCardCollection();
}
private List<String> getShuffledCards(long seed, int stage) {
Random rnd = new Random(seed + selectionSeed + stage);
final List<String> shuffledCards = new ArrayList<String>(cards);
Collections.shuffle(shuffledCards, rnd);
return shuffledCards;
}
};
}
private DraftChoiceDefinition buildRandomSwitchDraftChoiceDeifinition(JSONObject data) {
final long selectionSeed = ((Number) data.get("seed")).longValue();
JSONArray switchResult = (JSONArray) data.get("switchResult");
final List<DraftChoiceDefinition> draftChoiceDefinitionList = new ArrayList<DraftChoiceDefinition>();
Iterator<JSONObject> switchResultIterator = switchResult.iterator();
while (switchResultIterator.hasNext()) {
JSONObject switchResultObject = switchResultIterator.next();
draftChoiceDefinitionList.add(constructDraftChoiceDefinition(switchResultObject));
}
return new DraftChoiceDefinition() {
@Override
public Iterable<SoloDraft.DraftChoice> getDraftChoice(long seed, int stage) {
Random rnd = new Random(seed + selectionSeed);
return draftChoiceDefinitionList.get(rnd.nextInt(draftChoiceDefinitionList.size())).getDraftChoice(seed, stage);
}
@Override
public CardCollection getCardsForChoiceId(String choiceId, long seed, int stage) {
Random rnd = new Random(seed + selectionSeed);
return draftChoiceDefinitionList.get(rnd.nextInt(draftChoiceDefinitionList.size())).getCardsForChoiceId(choiceId, seed, stage);
}
};
}
}

View File

@@ -0,0 +1,47 @@
package com.gempukku.lotro.draft2.builder;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.DefaultCardCollection;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class StartingPoolBuilder {
public CardCollectionProducer buildCardCollectionProducer(JSONObject startingPool) {
String cardCollectionProducerType = (String) startingPool.get("type");
if (cardCollectionProducerType.equals("randomCardPool")) {
return buildRandomCardPool((JSONObject) startingPool.get("data"));
}
throw new RuntimeException("Unknown cardCollectionProducer type: "+cardCollectionProducerType);
}
private CardCollectionProducer buildRandomCardPool(JSONObject randomCardPool) {
final long selectionSeed = ((Number) randomCardPool.get("seed")).longValue();
JSONArray cardPools = (JSONArray) randomCardPool.get("randomResult");
final List<CardCollection> cardCollections = new ArrayList<CardCollection>();
Iterator<JSONArray> iterator = cardPools.iterator();
while (iterator.hasNext()) {
JSONArray cards = iterator.next();
DefaultCardCollection cardCollection = new DefaultCardCollection();
Iterator<String> cardIterator = cards.iterator();
while (cardIterator.hasNext()) {
cardCollection.addItem(cardIterator.next(), 1);
}
cardCollections.add(cardCollection);
}
return new CardCollectionProducer() {
@Override
public CardCollection getCardCollection(long seed) {
Random rnd = new Random(seed+selectionSeed);
return cardCollections.get(rnd.nextInt(cardCollections.size()));
}
};
}
}

View File

@@ -1,71 +1,58 @@
{
"format": "limited_hobbit",
"startingPool": [
{
"type": "random",
"data": [
{
"type": "cardPool",
"data": [
"card1", "card2"
]
},
{
"type": "cardPool",
"data": [
"card1", "card2"
]
}
"startingPool": {
"type": "randomCardPool",
"data": {
"seed": 0,
"randomResult": [
["card1", "card2"],
["card1", "card2"]
]
}
],
"picks": [
},
"choices": [
{
"type": "random",
"data": [
{
"type": "sequence",
"data": [
{
"type": "multiple",
"data": {
"count": 10,
"type": "randomPick",
"data": ["card1", "card2"]
}
},
{
"type": "multiple",
"data": {
"count": 5,
"type": "randomPick",
"data": ["card1", "card2"]
}
"type": "singleCollectionPick",
"repeat": 1,
"data": {
"possiblePicks": [
{
"choiceId": "starter1",
"url": "/images/draft1_starter1.jpg",
"cards": ["card1", "card2"]
},
{
"choiceId": "starter2",
"url": "/images/draft1_starter2.jpg",
"cards": ["card1", "card2"]
}
]
}
},
{
"type": "randomSwitch",
"repeat": 10,
"data": {
"seed": 0,
"switchResult": [
{
"type": "multipleCardPick",
"data": {
"seed": 2,
"count": 3,
"availableCards": ["card1", "card2", "card3", "card4"]
}
]
},
{
"type": "sequence",
"data": [
{
"type": "multiple",
"data": {
"count": 10,
"type": "randomPick",
"data": ["card1", "card2"]
}
},
{
"type": "multiple",
"data": {
"count": 5,
"type": "randomPick",
"data": ["card1", "card2"]
}
},
{
"type": "multipleCardPick",
"data": {
"seed": 2,
"count": 3,
"availableCards": ["card1", "card2", "card3", "card4"]
}
]
}
]
}
]
}
}
]
}