Skip to content

Commit

Permalink
Merge pull request #357 from erik-vos/master
Browse files Browse the repository at this point in the history
1837 Coal company exchange ready for testing
  • Loading branch information
neutronc authored Mar 19, 2021
2 parents 98839bc + 6ca9c63 commit b84f6ba
Show file tree
Hide file tree
Showing 80 changed files with 7,719 additions and 5,706 deletions.
18 changes: 17 additions & 1 deletion src/main/java/net/sf/rails/game/CompanyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class CompanyManager extends RailsManager implements Configurable {
// TODO Redundant, current usage can be replaced.
private final Map<String, Map<String, Company>> mCompaniesByTypeAndName = new HashMap<>();

/** A map of lists of companies per type, kept in sequence */
private final Map<String, List<PublicCompany>> mlCompaniesByType = new HashMap<>();

/** A list of all company types */
private final List<CompanyType> lCompanyTypes = new ArrayList<>();

Expand Down Expand Up @@ -83,7 +86,7 @@ public void configureFromXML(Tag tag) throws ConfigurationException {
= new HashMap<>();

//NEW//
Map<String, Tag> typeTags = new HashMap<String, Tag>();
Map<String, Tag> typeTags = new HashMap<>();

for (Tag compTypeTag : tag.getChildren(CompanyType.ELEMENT_ID)) {
// Extract the attributes of the Component
Expand Down Expand Up @@ -150,14 +153,23 @@ public void configureFromXML(Tag tag) throws ConfigurationException {
((PublicCompany)company).setIndex (numberOfPublicCompanies++);
mPublicCompanies.put(name, (PublicCompany) company);
lPublicCompanies.add((PublicCompany) company);

/* Lists in original order of public companies by Type */
if (!mlCompaniesByType.containsKey(type)) {
mlCompaniesByType.put(type, new ArrayList<>());
}
mlCompaniesByType.get(type).add ((PublicCompany)company);
}

/* By type and name */
if (!mCompaniesByTypeAndName.containsKey(type))
mCompaniesByTypeAndName.put(type,
new HashMap<String, Company>());
(mCompaniesByTypeAndName.get(type)).put(
name, company);



String alias = company.getAlias();
if (alias != null) createAlias (alias, name);

Expand Down Expand Up @@ -296,6 +308,10 @@ public List<Company> getCompaniesByType (String type) {
return new ArrayList<>(mCompaniesByTypeAndName.get(type).values());
}

public List<PublicCompany> getPublicCompaniesByType (String type) {
return new ArrayList<>(mlCompaniesByType.get(type));
}

public void closeAllPrivates() {
for (PrivateCompany priv : lPrivateCompanies) {
if (priv.isCloseable()) // check if private is closeable
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/net/sf/rails/game/CompanyType.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public static CompanyType create(CompanyManager parent, String id, String classN
return new CompanyType(parent, id, className);
}

/**
* @see net.sf.rails.common.parser.Configurable#configureFromXML(org.w3c.dom.Element)
*/
public void configureFromXML(Tag tag) throws ConfigurationException {
//No longer needed.
}
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/net/sf/rails/game/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ protected enum GameEnd {

protected PossibleActions possibleActions = PossibleActions.create();

protected final ArrayListState<PossibleAction> executedActions = new ArrayListState<>(this, "executedActions");
protected final ArrayListState<PossibleAction> executedActions
= new ArrayListState<>(this, "executedActions");

/**
* Special properties that can be used by other players or companies
Expand Down Expand Up @@ -540,7 +541,7 @@ public void nextRound(Round round) {
// Create a new OperatingRound (never more than one Stock Round)
// OperatingRound.resetRelativeORNumber();

relativeORNumber.set(1);
relativeORNumber.set(0);
startOperatingRound(true);
} else {
startStockRound();
Expand All @@ -559,15 +560,15 @@ public void nextRound(Round round) {

// Create a new OperatingRound (never more than one Stock Round)
// OperatingRound.resetRelativeORNumber();
relativeORNumber.set(1);
relativeORNumber.set(0);
startOperatingRound(true);

} else if (round instanceof OperatingRound) {
if (gameOverPending.value() && gameEndWhen == GameEnd.AFTER_THIS_OR) {

finishGame();

} else if (relativeORNumber.add(1) <= numOfORs.value()) {
} else if (relativeORNumber.value() < numOfORs.value()) {
// There will be another OR
startOperatingRound(true);
} else if (getRoot().getCompanyManager().getNextUnfinishedStartPacket() != null) {
Expand Down Expand Up @@ -602,7 +603,7 @@ protected void createStartRound(StartPacket startPacket) {
String startRoundClassName = startPacket.getRoundClassName();
startRoundNumber.add(1);
StartRound startRound = createRound(startRoundClassName,
"startRound_" + startRoundNumber.value());
"IR_" + startRoundNumber.value());
startRound.start();
}

Expand All @@ -617,8 +618,8 @@ protected boolean runIfStartPacketIsNotCompletelySold() {
}

protected void startStockRound() {
StockRound sr = createRound(stockRoundClass, "SR_" + stockRoundNumber.value());
stockRoundNumber.add(1);
StockRound sr = createRound(stockRoundClass, "SR_" + stockRoundNumber.value());

// For debugging only: check where the certs are.
if (log.isDebugEnabled() && stockRoundNumber.value() == 1) {
Expand All @@ -636,12 +637,19 @@ protected void startOperatingRound(boolean operate) {
log.debug("Operating round started with operate-flag={}", operate);
String orId;
if (operate) {
orId = "OR_" + absoluteORNumber.value();
absoluteORNumber.add(1);
if (showCompositeORNumber) {
relativeORNumber.add (1);
orId = "OR_" + stockRoundNumber.value() + "." + relativeORNumber.value();
} else {
orId = "OR_" + absoluteORNumber.value();
}
} else {
orId = "OR_Start_" + startRoundNumber.value();
relativeORNumber.add(1);
orId = "OR_0." + relativeORNumber.value();
}
OperatingRound or = createRound(operatingRoundClass, orId);
if (operate) absoluteORNumber.add(1);
//if (operate) absoluteORNumber.add(1);
or.start();
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/sf/rails/game/OperatingRound.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.sf.rails.game.state.Observer;
import net.sf.rails.game.state.*;
import net.sf.rails.util.SequenceUtil;
import net.sf.rails.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rails.game.action.*;
Expand Down Expand Up @@ -102,6 +103,7 @@ public OperatingRound(GameManager parent, String id) {
this.guiHints.setVisibilityHint(GuiDef.Panel.STOCK_MARKET, false);
this.guiHints.setVisibilityHint(GuiDef.Panel.STATUS, true);
this.guiHints.setActivePanel(GuiDef.Panel.MAP);
log.info ("--- Starting OR type round: {} ---", getId());
}

public void start() {
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/net/sf/rails/game/PublicCompany.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ public class PublicCompany extends RailsAbstractItem implements Company, RailsMo
/**
* Relation to a later to be founded National/Regional Major Company
*/
private String relatedPublicCompany = null;
private String relatedPublicCompanyName = null;
private PublicCompany relatedPublicCompany = null;

private String foundingStartCompany = null;

Expand Down Expand Up @@ -485,7 +486,7 @@ public void configureFromXML(Tag tag) throws ConfigurationException {

floatPerc = tag.getAttributeAsInteger("floatPerc", floatPerc);

relatedPublicCompany = tag.getAttributeAsString("relatedCompany", relatedPublicCompany);
relatedPublicCompanyName = tag.getAttributeAsString("relatedCompany", relatedPublicCompanyName);

foundingStartCompany = tag.getAttributeAsString("foundingCompany", foundingStartCompany);

Expand Down Expand Up @@ -746,6 +747,10 @@ public void finishConfiguration(RailsRoot root)
currentPrice.setPrice(parPrice.getPrice());
}

if (Util.hasValue(relatedPublicCompanyName)) {
relatedPublicCompany = getRoot().getCompanyManager().getPublicCompany(relatedPublicCompanyName);
}

int certIndex = 0;
if (certificateTags != null) {
int shareTotal = 0;
Expand Down Expand Up @@ -1172,7 +1177,7 @@ public void setOperated() {

/**
* Reinitialize a company, i.e. close it and make the shares available for a new company start.
* IMplemented rules are now as in 18EU.
* Implemented rules are now as in 18EU.
* TODO Will see later if this is generic enough.
*/
protected void reinitialise() {
Expand Down Expand Up @@ -2301,17 +2306,16 @@ public int compareTo(PublicCompany other) {
return this.getId().compareTo(other.getId());
}

public void setRelatedNationalCompany(String companyName) {
this.relatedPublicCompany = companyName;
public String getRelatedPublicCompanyName() {
return relatedPublicCompanyName;
}

public String getRelatedNationalCompany() {
public PublicCompany getRelatedPublicCompany() {
return relatedPublicCompany;
}


public boolean isRelatedToNational(String nationalInFounding) {
return this.getRelatedNationalCompany().equals(nationalInFounding);
return this.getRelatedPublicCompanyName().equals(nationalInFounding);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/sf/rails/game/ReleaseRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ private void releaseCompanyShares(PublicCompany company) {
List<PublicCertificate> certsToMove = new ArrayList<>();
for (PublicCertificate cert : unavailable.getCertificates(company)) {
if (cert.isInitiallyAvailable()) {
Util.breakIf(cert.getCompany().getId(), "U1,U3");
certsToMove.add(cert);
share = cert.getShare();
totalShare += share;
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/net/sf/rails/game/Round.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.sf.rails.game.model.PortfolioModel;
import net.sf.rails.game.round.RoundFacade;
import net.sf.rails.game.state.BooleanState;
import net.sf.rails.game.state.Creatable;
import net.sf.rails.game.state.Currency;
import net.sf.rails.game.state.Portfolio;

Expand Down Expand Up @@ -126,11 +125,21 @@ protected int getPhaseNumber () {
* Set the operating companies in their current acting order
*/
// What is the reason of that to have that here? => move to OR?
// called only internally
// EV: No, also used in 1837/CoalExchangeRound, which is an SR type
public List<PublicCompany> setOperatingCompanies() {
return setOperatingCompanies(null, null);
}

public List<PublicCompany> setOperatingCompanies (String type) {
List<PublicCompany> selectedCompanies = new ArrayList<>();
for (PublicCompany comp : setOperatingCompanies()) {
if (type.equals(comp.getType().getId())) {
selectedCompanies.add (comp);
}
}
return selectedCompanies;
}

// What is the reason of that to have that here => move to OR?
// this is still required for 18EU StockRound as due to the merger there are companies that have to discard trains
// called only internally
Expand All @@ -145,20 +154,20 @@ public List<PublicCompany> setOperatingCompanies(List<PublicCompany> oldOperatin
boolean reorder = gameManager.isDynamicOperatingOrder()
&& oldOperatingCompanies != null && lastOperatingCompany != null;

int lastOperatingCompanyndex;
int lastOperatingCompanyIndex;
if (reorder) {
newOperatingCompanies = oldOperatingCompanies;
lastOperatingCompanyndex = oldOperatingCompanies.indexOf(lastOperatingCompany);
lastOperatingCompanyIndex = oldOperatingCompanies.indexOf(lastOperatingCompany);
} else {
newOperatingCompanies = companyManager.getAllPublicCompanies();
lastOperatingCompanyndex = -1;
lastOperatingCompanyIndex = -1;
}

for (PublicCompany company : newOperatingCompanies) {
if (!reorder && !canCompanyOperateThisRound(company)) continue;

if (reorder
&& oldOperatingCompanies.indexOf(company) <= lastOperatingCompanyndex) {
&& oldOperatingCompanies.indexOf(company) <= lastOperatingCompanyIndex) {
// Companies that have operated this round get lowest keys
key = oldOperatingCompanies.indexOf(company);
} else if (company.hasStockPrice()) {
Expand All @@ -175,7 +184,7 @@ public List<PublicCompany> setOperatingCompanies(List<PublicCompany> oldOperatin
operatingCompanies.put(key, company);
}

return new ArrayList<PublicCompany>(operatingCompanies.values());
return new ArrayList<>(operatingCompanies.values());
}

/**
Expand Down Expand Up @@ -222,7 +231,7 @@ protected void floatCompany(PublicCompany company) {

// Move cash and shares where required
int soldPercentage = company.getSoldPercentage();
int cash = 0;
int cash;
int capitalisationMode = company.getCapitalisation();
if (company.hasStockPrice()) {
int capFactor = 0;
Expand All @@ -247,7 +256,7 @@ protected void floatCompany(PublicCompany company) {
cash = company.getFixedPrice();
}

// Substract initial token cost (e.g. 1851, 18EU)
// Subtract initial token cost (e.g. 1851, 18EU)
cash -= company.getBaseTokensBuyCost();

company.setFloated(); // After calculating cash (for 1851: price goes
Expand Down Expand Up @@ -297,4 +306,9 @@ public boolean wasInterrupted() {
return wasInterrupted.value();
}

/** Stub to allow lower subclasses to provide their own window title */
public String getOwnWindowTitle() {
return null;
}

}
3 changes: 2 additions & 1 deletion src/main/java/net/sf/rails/game/StartRound.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected StartRound(GameManager parent, String id,
guiHints.setVisibilityHint(GuiDef.Panel.STOCK_MARKET, false);
guiHints.setVisibilityHint(GuiDef.Panel.MAP, true);
guiHints.setActivePanel(GuiDef.Panel.START_ROUND);
log.info("Starting initial round type: {}", getId());
}

// For backwards compatibility
Expand All @@ -102,7 +103,7 @@ public void start() {
// init current with priority player
startPlayer = playerManager.setCurrentToPriorityPlayer();

ReportBuffer.add(this, LocalText.getText("StartOfInitialRound"));
ReportBuffer.add(this, LocalText.getText("StartOfInitialRound", getStartRoundNumber()));
ReportBuffer.add(this, LocalText.getText("HasPriority",
startPlayer.getId()));
}
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/net/sf/rails/game/TrainCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,4 @@ public void discard () {
this.moveTo(discardTo);
}

/* obsolete
public void discard(BankPortfolio discardTo, boolean dualTrainBecomesUndecidedInPool) {
this.moveTo(discardTo);
if(trainCardType.isDual() && discardTo == Bank.getPool(this)) {
if (dualTrainBecomesUndecidedInPool) {
setActualTrain(null);
}
}
String discardText = LocalText.getText("CompanyDiscardsTrain", getOwner().getId(), this.toText(), discardTo.getId());
ReportBuffer.add(this, discardText);
}*/

}
7 changes: 4 additions & 3 deletions src/main/java/net/sf/rails/game/financial/StockRound.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public StockRound(GameManager parent, String id) {
guiHints.setVisibilityHint(GuiDef.Panel.MAP, true);
guiHints.setVisibilityHint(GuiDef.Panel.STOCK_MARKET, true);
guiHints.setActivePanel(GuiDef.Panel.STATUS);
log.info ("--- Starting SR type round: {} ---", getId());
}

/**
Expand Down Expand Up @@ -1423,7 +1424,7 @@ public boolean sellShares(SellShares action)
adjustSharePrice(company, currentPlayer, numberSold, soldBefore);

if (!company.isClosed()) {
log.info("certsToSell={}", certsToSell);
log.debug("certsToSell={}", certsToSell);
executeShareTransfer(company, certsToSell,
dumpedPlayer, presidentShareNumbersToSell);
}
Expand Down Expand Up @@ -1469,7 +1470,7 @@ protected final boolean executeShareTransferTo(PublicCompany company,
}

// Transfer the sold certificates
log.info ("Certs to pool: {}", certsToSell);
log.debug ("Certs to pool: {}", certsToSell);
Portfolio.moveAll(certsToSell, bankTo);

return swapped;
Expand Down Expand Up @@ -1842,7 +1843,7 @@ protected void setPriority(String string) {
// NationalFormationRound, 1835PrussianFormationRound: setPossibleActions, start

// not overridden
@Deprecated
// @Deprecated // Why???
public void setCurrentPlayer(Player player) {
getRoot().getPlayerManager().setCurrentPlayer(player);
currentPlayer = player;
Expand Down
Loading

0 comments on commit b84f6ba

Please sign in to comment.