include "printGeneral.beh"; include "printClassic.beh"; Sign {{ public int sign() { return 1; } }} Neg {{ public int sign() { return 0; } }} Derivative{{ /** Is this a Classic Derivative? */ public boolean isClassic(){ return type.isClassic(); } /** Is this a Secret Derivative? */ public boolean isSecret(){ return type.isSecret(); } /** Helper Constructor for unpurchased Derivatives */ public Derivative(String name, PlayerID own, Price price, Type type){ this(name, own, Option.none(), price, type, Option.none(), Option.none()); } /** Return a new Derivative with the given PlayerID as the Buyer */ public Derivative buy(PlayerID pid){ if(optbuyer.isSome())throw new RuntimeException("Derivative "+name+" Already Bought!"); return new Derivative(name, seller, Option.some(pid), price, type, optraw, optfinished); } /** Return a new Derivative with the given RawMaterial */ public Derivative deliver(RawMaterialInstance rmi){ if(optraw.isSome())throw new RuntimeException("Derivative "+name+" Already Delivered!"); return new Derivative(name, seller, optbuyer, price, type, Option.some(new RawMaterial(rmi)),optfinished); } /** Return a new Derivative with the given FinishedProduct */ public Derivative finish(FinishedProduct fin){ if(optfinished.isSome())throw new RuntimeException("Derivative "+name+" Already Finished!"); return new Derivative(name, seller, optbuyer, price, type, optraw, Option.some(fin)); } /** Return a new Derivative with the given PlayerID as the Seller, at the new price */ public Derivative reoffer(PlayerID newseller, Price newprice){ return new Derivative(name, newseller, optbuyer, newprice, type, optraw, optfinished); } /** Return a new Derivative with a truncated RawMaterial */ public Derivative truncateRM(int len){ if(!optraw.isSome() || optraw.inner().instance.cs.length() <= len)return this; RawMaterial nrm = new RawMaterial(new RawMaterialInstance(optraw.inner().instance.cs.reverse(len).reverse())); return new Derivative(name, seller, optbuyer, price, type, Option.some(nrm), optfinished); } }} Accounts{{ /** Get the account balance for the given Player */ public double getAccount(Player p){ return getAccount(p.id); } /** Get the account balance for the given PlayerID */ public double getAccount(PlayerID p){ return getAccountPair(p).b; } /** Get the account Pair for the given PlayerID */ public Pair getAccountPair(PlayerID p){ return accounts.find(new AccountPred(p)); } /** Predicate that helps lookup a Player's Account */ static class AccountPred extends List.Pred>{ PlayerID pid; AccountPred(PlayerID p){ pid = p; } public boolean huh(Pair pair){ return pair.a.equals(pid); } } /** Return a new Accounts that is adjusted for the given change */ public Accounts update(Pair change){ Pair acc = getAccountPair(change.a); return replace(new Pair(acc.a, acc.b+change.b)); } /** Return a new Accounts that replaces the given PlayerID account Pair */ public Accounts replace(Pair newacc){ return new Accounts(accounts.replace(new AccountPred(newacc.a), newacc)); } /** Return a new Accounts without the given PlayerID */ public Accounts remove(PlayerID pid){ return new Accounts(accounts.remove(new AccountPred(pid))); } }} Store{{ /** Does the given PlayerID have anything in the store */ public boolean contains(PlayerID pid){ return stores.contains(new StorePred(pid)); } /** Get the Store for the given Player */ public PlayerStore getStore(Player p){ return getStore(p.id); } /** Get the Store for the given PlayerID */ public PlayerStore getStore(PlayerID pid){ return getStorePair(pid).b; } /** Get the Store for the given PlayerID */ public Pair getStorePair(PlayerID pid){ return stores.find(new StorePred(pid)); } /** Predicate that helps lookup a Player's Store */ static class StorePred extends List.Pred>{ PlayerID pid; StorePred(PlayerID p){ pid = p; } public boolean huh(Pair pair){ return pair.a.equals(pid); } } /** Return a new Store with the new given PlayerID/PlayerStore Pair added */ public Store add(Pair newstr){ return new Store(stores.push(newstr)); } /** Return a new Store that replaces the given PlayerID/PlayerStore Pair */ public Store replace(Pair newstr){ return new Store(stores.replace(new StorePred(newstr.a), newstr)); } /** Return a new Store without the given PlayerID */ public Store remove(PlayerID pid){ return new Store(stores.remove(new StorePred(pid))); } }} PlayerStore{{ public PlayerStore pushSale(Derivative d){ return new PlayerStore(forSale.push(d), bought); } }} Players{{ static class ByID extends List.Pred{ PlayerID pid; ByID(PlayerID p){ this.pid = p; } ByID(Player p){ this(p.id); } public boolean huh(Player p) { return pid.equals(p.id); } } /** Return the number of Players */ public int length(){ return players.length(); } /** Lookup the Player based on its ID */ public Player forID(PlayerID pid){ return players.find(new ByID(pid)); } /** Remove the given Player based on its ID */ public Players remove(PlayerID pid){ return new Players(players.remove(new ByID(pid))); } /** Does the given ID exist in Players? */ public boolean contains(PlayerID pid){ return players.contains(new ByID(pid)); } }} Type{{ public Type(List inst){ this(new Classic(),inst); } /** Is this a Classic type? */ public boolean isClassic(){ return kind.isClassic(); } /** Is this a Secret Derivative? */ public boolean isSecret(){ return kind.isSecret(); } }} Kind{{ /** Is this a Classic kind? Not yet... */ public boolean isClassic(){ return false; } /** Is this a Secret kind? Not yet... */ public boolean isSecret(){ return false; } }} Classic{{ /** Is this a Classic kind? YES!! */ public boolean isClassic(){ return true; } }} Secret{{ /** Is this a Secret kind? YES!! */ public boolean isSecret(){ return true; } }} RawMaterialInstance{{ public RawMaterialInstance(List cs){ this(cs,Option.none()); } public RawMaterialInstance(List cs, SecretOfRawMaterial sec){ this(cs,Option.some(sec)); } public RawMaterialInstance(List cs, Assignment a){ this(cs, new SecretOfRawMaterial(Option.some(a), new Quality(0.0))); } public RawMaterialInstance addSecret(Assignment a){ return new RawMaterialInstance(cs,a); } public RawMaterialInstance addSecretQuality(double q){ return new RawMaterialInstance(cs, new SecretOfRawMaterial(secret.inner().assign, new Quality(q))); } }}