use of pcgen.cdom.helper.StatLock in project pcgen by PCGen.
the class StatLockFacetTest method testGetLock.
@Test
public void testGetLock() {
Object source1 = new Object();
Object source2 = new Object();
PCStat stat = new PCStat();
stat.setName("Stat" + n++);
StatLock t1 = new StatLock(CDOMDirectSingleRef.getRef(stat), FormulaFactory.getFormulaFor(1));
PCStat stat1 = new PCStat();
stat1.setName("Stat" + n++);
StatLock t2 = new StatLock(CDOMDirectSingleRef.getRef(stat1), FormulaFactory.getFormulaFor(4));
PCStat stat3 = new PCStat();
stat3.setName("Stat" + n++);
StatLock t3 = new StatLock(CDOMDirectSingleRef.getRef(stat3), FormulaFactory.getFormulaFor(2));
assertNull(facet.getLockedStat(id, stat));
getFacet().add(id, t1, source1);
assertEquals(1, facet.getLockedStat(id, stat));
assertNull(facet.getLockedStat(id, stat1));
getFacet().add(id, t2, source1);
assertEquals(4, facet.getLockedStat(id, stat1));
getFacet().add(id, t3, source1);
assertEquals(2, facet.getLockedStat(id, stat3));
}
use of pcgen.cdom.helper.StatLock in project pcgen by PCGen.
the class DefineStatLst method parseToken.
@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
if (obj instanceof Ungranted) {
return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
}
ParsingSeparator sep = new ParsingSeparator(value, '|');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
if (!sep.hasNext()) {
return new ParseResult.Fail(getTokenName() + " may not be empty", context);
}
String firstItem = sep.next();
DefineStatSubToken subToken;
try {
subToken = DefineStatSubToken.valueOf(firstItem);
} catch (IllegalArgumentException e1) {
return new ParseResult.Fail("Found unexpected sub tag " + firstItem + " in " + getTokenName() + Constants.COLON + value + ". Must be one of " + StringUtils.join(DefineStatSubToken.values(), ", ") + Constants.DOT, context);
}
if (!sep.hasNext()) {
return new ParseResult.Fail(getTokenName() + Constants.COLON + subToken + "| must be followed by a stat.", context);
}
String statKey = sep.next();
CDOMSingleRef<PCStat> stat = context.getReferenceContext().getCDOMReference(PCSTAT_CLASS, statKey);
Formula f = null;
if (subToken == DefineStatSubToken.LOCK || subToken == DefineStatSubToken.MINVALUE || subToken == DefineStatSubToken.MAXVALUE) {
if (!sep.hasNext()) {
return new ParseResult.Fail(getTokenName() + Constants.COLON + subToken + "| must be followed by both a stat and a value.", context);
}
String formula = sep.next();
f = FormulaFactory.getFormulaFor(formula);
if (!f.isValid()) {
return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + f.toString(), context);
}
}
if (sep.hasNext()) {
return new ParseResult.Fail(getTokenName() + Constants.COLON + value + " has too many pipe separated item.", context);
}
switch(subToken) {
case LOCK:
context.getObjectContext().addToList(obj, ListKey.STAT_LOCKS, new StatLock(stat, f));
break;
case UNLOCK:
context.getObjectContext().addToList(obj, ListKey.UNLOCKED_STATS, stat);
break;
case NONSTAT:
context.getObjectContext().addToList(obj, ListKey.NONSTAT_STATS, stat);
break;
case STAT:
context.getObjectContext().addToList(obj, ListKey.NONSTAT_TO_STAT_STATS, stat);
break;
case MINVALUE:
context.getObjectContext().addToList(obj, ListKey.STAT_MINVALUE, new StatLock(stat, f));
break;
case MAXVALUE:
context.getObjectContext().addToList(obj, ListKey.STAT_MAXVALUE, new StatLock(stat, f));
break;
}
return ParseResult.SUCCESS;
}
use of pcgen.cdom.helper.StatLock in project pcgen by PCGen.
the class StatListTest method setUp.
/**
* @see pcgen.AbstractCharacterTestCase#setUp()
*/
@Override
protected void setUp() throws Exception {
super.setUp();
PlayerCharacter pc = getCharacter();
LoadContext context = Globals.getContext();
locker = new PCTemplate();
locker.setName("locker");
CDOMDirectSingleRef<PCStat> strRef = CDOMDirectSingleRef.getRef(str);
locker.addToListFor(ListKey.STAT_LOCKS, new StatLock(strRef, FormulaFactory.getFormulaFor(12)));
unlocker = new PCTemplate();
unlocker.setName("unlocker");
unlocker.addToListFor(ListKey.UNLOCKED_STATS, strRef);
bonus = TestHelper.makeAbility("Bonus", AbilityCategory.FEAT, "General.Fighter");
BonusObj aBonus = Bonus.newBonus(context, "STAT|STR|7|TYPE=Enhancement");
if (aBonus != null) {
bonus.addToListFor(ListKey.BONUS, aBonus);
}
lockedBonus = TestHelper.makeAbility("LockedBonus", AbilityCategory.FEAT, "General.Fighter");
aBonus = Bonus.newBonus(context, "LOCKEDSTAT|STR|3|TYPE=Morale");
if (aBonus != null) {
lockedBonus.addToListFor(ListKey.BONUS, aBonus);
}
setPCStat(pc, str, 6);
}
use of pcgen.cdom.helper.StatLock in project pcgen by PCGen.
the class StatIntegrationTest method causeLock.
private static void causeLock(CDOMObject r, PCStat stat, int i) {
StatLock sl = new StatLock(CDOMDirectSingleRef.getRef(stat), FormulaFactory.getFormulaFor(i));
r.addToListFor(ListKey.STAT_LOCKS, sl);
}
use of pcgen.cdom.helper.StatLock in project pcgen by PCGen.
the class StatLockFacet method getLockedStat.
/**
* Returns the numerical value for the given PCStat which has been locked
* for the Player Character identified by the given CharID. Returns null if
* no StatLock exists on the Player Character for the given PCStat.
*
* @param id
* The CharID identifying the Player Character for which the
* locked stat value is to be returned
* @param stat
* The PCStat for which the numerical lock value is to be
* returned
* @return The numerical value for the given PCStat which has been locked
* for the Player Character identified by the given CharID; null if
* no StatLock exists on the Player Character for the given PCStat
*/
public Number getLockedStat(CharID id, PCStat stat) {
Number max = Double.NEGATIVE_INFINITY;
boolean hit = false;
Map<StatLock, Set<Object>> componentMap = getCachedMap(id);
if (componentMap != null) {
for (Iterator<Map.Entry<StatLock, Set<Object>>> it = componentMap.entrySet().iterator(); it.hasNext(); ) {
Entry<StatLock, Set<Object>> me = it.next();
Set<Object> set = me.getValue();
StatLock lock = me.getKey();
if (lock.getLockedStat().equals(stat)) {
for (Object source : set) {
String sourceString = (source instanceof CDOMObject) ? ((CDOMObject) source).getQualifiedKey() : "";
Number val = formulaResolvingFacet.resolve(id, lock.getLockValue(), sourceString);
if (val.doubleValue() > max.doubleValue()) {
hit = true;
max = val;
}
}
}
}
}
return hit ? max : null;
}
Aggregations