Search in sources :

Example 1 with TripleKeyMapToList

use of pcgen.base.util.TripleKeyMapToList in project pcgen by PCGen.

the class AbilityLst method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Collection<CDOMReference<? extends CDOMList<?>>> changedLists = context.getListContext().getChangedLists(obj, AbilityList.class);
    Changes<ListKey<ChooseSelectionActor<?>>> actors = context.getObjectContext().getListChanges(obj, ListKey.GA_CAKEYS);
    Set<String> returnSet = new TreeSet<>();
    TripleKeyMapToList<Nature, CDOMSingleRef<AbilityCategory>, List<Prerequisite>, CDOMReference<Ability>> m = new TripleKeyMapToList<>();
    TripleKeyMapToList<Nature, CDOMSingleRef<AbilityCategory>, List<Prerequisite>, CDOMReference<Ability>> clear = new TripleKeyMapToList<>();
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    if (listAdded != null && !listAdded.isEmpty()) {
        for (ChooseSelectionActor<?> csa : listAdded) {
            if (csa.getSource().equals(getTokenName())) {
                try {
                    AbilitySelector as = (AbilitySelector) csa;
                    StringBuilder sb = new StringBuilder();
                    sb.append(as.getAbilityCategory().getLSTformat(false)).append(Constants.PIPE);
                    sb.append(as.getNature()).append(Constants.PIPE);
                    sb.append(as.getLstFormat());
                    returnSet.add(sb.toString());
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage(getTokenName() + " encountered error: " + e.getMessage());
                    return null;
                }
            }
        }
    }
    for (CDOMReference ref : changedLists) {
        AssociatedChanges<CDOMReference<Ability>> changes = context.getListContext().getChangesInList(getTokenName(), obj, ref);
        if (changes.includesGlobalClear()) {
            CDOMDirectSingleRef<AbilityList> dr = (CDOMDirectSingleRef<AbilityList>) ref;
            AbilityList al = dr.get();
            StringBuilder sb = new StringBuilder();
            sb.append(al.getCategory().getLSTformat(false)).append(Constants.PIPE);
            sb.append(al.getNature()).append(Constants.PIPE);
            sb.append(Constants.LST_DOT_CLEAR);
            returnSet.add(sb.toString());
        }
        MapToList<CDOMReference<Ability>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
        if (mtl != null) {
            for (CDOMReference<Ability> ab : mtl.getKeySet()) {
                for (AssociatedPrereqObject assoc : mtl.getListFor(ab)) {
                    Nature nature = assoc.getAssociation(AssociationKey.NATURE);
                    CDOMSingleRef<AbilityCategory> cat = assoc.getAssociation(AssociationKey.CATEGORY);
                    m.addToListFor(nature, cat, assoc.getPrerequisiteList(), ab);
                }
            }
        }
        mtl = changes.getRemovedAssociations();
        if (mtl != null) {
            for (CDOMReference<Ability> ab : mtl.getKeySet()) {
                for (AssociatedPrereqObject assoc : mtl.getListFor(ab)) {
                    Nature nature = assoc.getAssociation(AssociationKey.NATURE);
                    CDOMSingleRef<AbilityCategory> cat = assoc.getAssociation(AssociationKey.CATEGORY);
                    clear.addToListFor(nature, cat, assoc.getPrerequisiteList(), ab);
                }
            }
        }
    }
    for (Nature nature : m.getKeySet()) {
        for (CDOMSingleRef<AbilityCategory> category : m.getSecondaryKeySet(nature)) {
            for (List<Prerequisite> prereqs : m.getTertiaryKeySet(nature, category)) {
                StringBuilder sb = new StringBuilder();
                sb.append(category.getLSTformat(false)).append(Constants.PIPE);
                sb.append(nature);
                List<CDOMReference<Ability>> clearList = clear.removeListFor(nature, category, prereqs);
                if (clearList != null && !clearList.isEmpty()) {
                    sb.append(Constants.PIPE);
                    sb.append(Constants.LST_DOT_CLEAR_DOT);
                    sb.append(ReferenceUtilities.joinLstFormat(clearList, Constants.PIPE + Constants.LST_DOT_CLEAR_DOT));
                }
                sb.append(Constants.PIPE);
                sb.append(ReferenceUtilities.joinLstFormat(m.getListFor(nature, category, prereqs), Constants.PIPE));
                if (prereqs != null && !prereqs.isEmpty()) {
                    sb.append(Constants.PIPE);
                    sb.append(getPrerequisiteString(context, prereqs));
                }
                returnSet.add(sb.toString());
            }
        }
    }
    for (Nature nature : clear.getKeySet()) {
        for (CDOMSingleRef<AbilityCategory> category : clear.getSecondaryKeySet(nature)) {
            for (List<Prerequisite> prereqs : clear.getTertiaryKeySet(nature, category)) {
                StringBuilder sb = new StringBuilder();
                sb.append(category.getLSTformat(false)).append(Constants.PIPE);
                sb.append(nature).append(Constants.PIPE).append(Constants.LST_DOT_CLEAR_DOT);
                sb.append(ReferenceUtilities.joinLstFormat(clear.getListFor(nature, category, prereqs), Constants.PIPE + Constants.LST_DOT_CLEAR_DOT));
                if (prereqs != null && !prereqs.isEmpty()) {
                    sb.append(Constants.PIPE);
                    sb.append(getPrerequisiteString(context, prereqs));
                }
                returnSet.add(sb.toString());
            }
        }
    }
    Collection<ListKey<ChooseSelectionActor<?>>> addedActors = actors.getAdded();
    if (addedActors != null) {
        for (ListKey<ChooseSelectionActor<?>> lk : addedActors) {
            Changes<ChooseSelectionActor<?>> cras = context.getObjectContext().getListChanges(obj, lk);
            for (ChooseSelectionActor<?> cra : cras.getAdded()) {
                if (getTokenName().equals(cra.getSource())) {
                    try {
                        AbilityTargetSelector ats = (AbilityTargetSelector) cra;
                        StringBuilder sb = new StringBuilder();
                        sb.append(ats.getAbilityCategory().getLSTformat(false)).append(Constants.PIPE);
                        sb.append(ats.getNature()).append(Constants.PIPE).append(cra.getLstFormat());
                        List<Prerequisite> prereqs = ats.getPrerequisiteList();
                        if (prereqs != null && !prereqs.isEmpty()) {
                            sb.append(Constants.PIPE);
                            sb.append(getPrerequisiteString(context, prereqs));
                        }
                        returnSet.add(sb.toString());
                    } catch (PersistenceLayerException e) {
                        context.addWriteMessage(getTokenName() + " encountered error: " + e.getMessage());
                        return null;
                    }
                }
            }
        }
    }
    if (returnSet.isEmpty()) {
        return null;
    }
    return returnSet.toArray(new String[returnSet.size()]);
}
Also used : ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) AbilitySelector(pcgen.cdom.helper.AbilitySelector) ListKey(pcgen.cdom.enumeration.ListKey) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) TripleKeyMapToList(pcgen.base.util.TripleKeyMapToList) CDOMList(pcgen.cdom.base.CDOMList) List(java.util.List) MapToList(pcgen.base.util.MapToList) AbilityList(pcgen.cdom.list.AbilityList) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) Nature(pcgen.cdom.enumeration.Nature) Ability(pcgen.core.Ability) AbilityTargetSelector(pcgen.cdom.helper.AbilityTargetSelector) CDOMDirectSingleRef(pcgen.cdom.reference.CDOMDirectSingleRef) AbilityList(pcgen.cdom.list.AbilityList) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) TripleKeyMapToList(pcgen.base.util.TripleKeyMapToList) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) AbilityCategory(pcgen.core.AbilityCategory) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 2 with TripleKeyMapToList

use of pcgen.base.util.TripleKeyMapToList in project pcgen by PCGen.

the class CompanionListLst method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Changes<FollowerOption> changes = context.getObjectContext().getListChanges(obj, ListKey.COMPANIONLIST);
    Collection<FollowerOption> removedItems = changes.getRemoved();
    if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
        context.addWriteMessage(getTokenName() + " does not support .CLEAR");
        return null;
    }
    Collection<FollowerOption> added = changes.getAdded();
    if (added == null || added.isEmpty()) {
        // Zero indicates no Token (and no global clear, so nothing to do)
        return null;
    }
    TripleKeyMapToList<Set<Prerequisite>, CDOMReference<? extends CDOMList<?>>, Integer, CDOMReference<Race>> m = new TripleKeyMapToList<>();
    for (FollowerOption fo : added) {
        m.addToListFor(new HashSet<>(fo.getPrerequisiteList()), fo.getListRef(), fo.getAdjustment(), fo.getRaceRef());
    }
    Set<String> set = new TreeSet<>();
    StringBuilder sb = new StringBuilder();
    for (Set<Prerequisite> prereqs : m.getKeySet()) {
        String prereqString = null;
        if (prereqs != null && !prereqs.isEmpty()) {
            prereqString = getPrerequisiteString(context, prereqs);
        }
        for (CDOMReference<? extends CDOMList<?>> cl : m.getSecondaryKeySet(prereqs)) {
            for (Integer fa : m.getTertiaryKeySet(prereqs, cl)) {
                sb.setLength(0);
                sb.append(cl.getLSTformat(false));
                sb.append(Constants.PIPE);
                Set<CDOMReference<Race>> raceSet = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
                raceSet.addAll(m.getListFor(prereqs, cl, fa));
                sb.append(ReferenceUtilities.joinLstFormat(raceSet, Constants.COMMA, true));
                if (fa != null && fa != 0) {
                    sb.append(Constants.PIPE);
                    sb.append("FOLLOWERADJUSTMENT:");
                    sb.append(fa);
                }
                if (prereqString != null) {
                    sb.append(Constants.PIPE);
                    sb.append(prereqString);
                }
                set.add(sb.toString());
            }
        }
    }
    return set.toArray(new String[set.size()]);
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) FollowerOption(pcgen.core.FollowerOption) TreeSet(java.util.TreeSet) TripleKeyMapToList(pcgen.base.util.TripleKeyMapToList) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 3 with TripleKeyMapToList

use of pcgen.base.util.TripleKeyMapToList in project pcgen by PCGen.

the class AbstractSpellListToken method getMap.

/**
	 * Gets the map.
	 *
	 * @param context the context
	 * @param obj the obj
	 * @param changedLists the changed lists
	 * @param knownSpells Should this scan be for known spells
	 * @return the map
	 */
protected TripleKeyMapToList<String, Integer, CDOMReference<? extends CDOMList<?>>, CDOMReference<Spell>> getMap(LoadContext context, CDOMObject obj, Collection<CDOMReference<? extends CDOMList<?>>> changedLists, boolean knownSpells) {
    TripleKeyMapToList<String, Integer, CDOMReference<? extends CDOMList<?>>, CDOMReference<Spell>> map = new TripleKeyMapToList<>();
    for (CDOMReference listRef : changedLists) {
        AssociatedChanges<CDOMReference<Spell>> changes = context.getListContext().getChangesInList(getTokenName(), obj, listRef);
        Collection<?> removedItems = changes.getRemoved();
        if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
            context.addWriteMessage(getTokenName() + " does not support .CLEAR");
            return null;
        }
        MapToList<CDOMReference<Spell>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
        if (mtl == null || mtl.isEmpty()) {
            // TODO Error message - unexpected?
            continue;
        }
        for (CDOMReference<Spell> added : mtl.getKeySet()) {
            for (AssociatedPrereqObject assoc : mtl.getListFor(added)) {
                Integer lvl = assoc.getAssociation(AssociationKey.SPELL_LEVEL);
                String prereqString = getPrerequisiteString(context, assoc.getPrerequisiteList());
                Boolean known = assoc.getAssociation(AssociationKey.KNOWN);
                boolean isKnown = known != null && known.booleanValue();
                if (knownSpells == isKnown) {
                    map.addToListFor(prereqString, lvl, listRef, added);
                }
            }
        }
    }
    return map;
}
Also used : Spell(pcgen.core.spell.Spell) TripleKeyMapToList(pcgen.base.util.TripleKeyMapToList) CDOMList(pcgen.cdom.base.CDOMList) CDOMReference(pcgen.cdom.base.CDOMReference) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject)

Aggregations

TripleKeyMapToList (pcgen.base.util.TripleKeyMapToList)3 CDOMList (pcgen.cdom.base.CDOMList)3 CDOMReference (pcgen.cdom.base.CDOMReference)3 TreeSet (java.util.TreeSet)2 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)2 Prerequisite (pcgen.core.prereq.Prerequisite)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 MapToList (pcgen.base.util.MapToList)1 ChooseSelectionActor (pcgen.cdom.base.ChooseSelectionActor)1 ListKey (pcgen.cdom.enumeration.ListKey)1 Nature (pcgen.cdom.enumeration.Nature)1 AbilitySelector (pcgen.cdom.helper.AbilitySelector)1 AbilityTargetSelector (pcgen.cdom.helper.AbilityTargetSelector)1 AbilityList (pcgen.cdom.list.AbilityList)1 CDOMDirectSingleRef (pcgen.cdom.reference.CDOMDirectSingleRef)1 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)1 Ability (pcgen.core.Ability)1