|
From: <ev...@us...> - 2010-09-10 21:57:25
|
Revision: 1419
http://rails.svn.sourceforge.net/rails/?rev=1419&view=rev
Author: evos
Date: 2010-09-10 21:57:19 +0000 (Fri, 10 Sep 2010)
Log Message:
-----------
Further refactoring of (very old) share selling code to simplify structure and unravel cash and certificate movements.
There now is a central pay() method for cash transfer.
Modified Paths:
--------------
trunk/18xx/rails/game/Round.java
trunk/18xx/rails/game/ShareSellingRound.java
trunk/18xx/rails/game/StartRound.java
trunk/18xx/rails/game/StockMarket.java
trunk/18xx/rails/game/StockRound.java
trunk/18xx/rails/game/TreasuryShareRound.java
Modified: trunk/18xx/rails/game/Round.java
===================================================================
--- trunk/18xx/rails/game/Round.java 2010-09-10 16:30:21 UTC (rev 1418)
+++ trunk/18xx/rails/game/Round.java 2010-09-10 21:57:19 UTC (rev 1419)
@@ -113,7 +113,7 @@
protected int getNumberOfPlayers() {
return gameManager.getNumberOfPlayers();
}
-
+
protected int getNumberOfActivePlayers () {
int number = 0;
for (Player player : getPlayers()) {
@@ -268,7 +268,7 @@
int minorNo = 0;
for (PublicCompanyI company : companyManager.getAllPublicCompanies()) {
if (!canCompanyOperateThisRound(company)) continue;
-
+
// Key must put companies in reverse operating order, because sort
// is ascending.
if (company.hasStockPrice()) {
@@ -291,7 +291,7 @@
protected boolean canCompanyOperateThisRound (PublicCompanyI company) {
return company.hasFloated() && !company.isClosed();
}
-
+
/**
* Check if a company must be floated, and if so, do it. <p>This method is
* included here because it is used in various types of Round.
@@ -423,15 +423,32 @@
return getClass().getName().replaceAll(".*\\.", "");
}
- protected void executeTradeCertificate(Certificate cert, Portfolio newHolder, int price) {
+ protected void transferCertificate(Certificate cert, Portfolio newHolder) {
- Portfolio oldHolder = (Portfolio) cert.getHolder();
cert.moveTo(newHolder);
+ }
- if (price != 0) {
- new CashMove(newHolder.getOwner(), oldHolder.getOwner(), price);
+ // Note: all transferred shares must come from the same old shareholder.
+ protected void transferCertificates(List<? extends Certificate> certs,
+ Portfolio newHolder) {
+
+ for (Certificate cert : certs) {
+ if (cert != null) {
+ cert.moveTo(newHolder);
+ }
+ }
+ }
+
+ protected void pay (CashHolder from, CashHolder to, int amount) {
+ if (to != null && amount != 0) {
+ new CashMove (from, to, amount);
}
+ }
+ protected void pay (Portfolio from, Portfolio to, int amount) {
+ if (to != null && amount != 0) {
+ new CashMove (from.getOwner(), to.getOwner(), amount);
+ }
}
public GameManagerI getGameManager() {
Modified: trunk/18xx/rails/game/ShareSellingRound.java
===================================================================
--- trunk/18xx/rails/game/ShareSellingRound.java 2010-09-10 16:30:21 UTC (rev 1418)
+++ trunk/18xx/rails/game/ShareSellingRound.java 2010-09-10 21:57:19 UTC (rev 1419)
@@ -366,6 +366,7 @@
price = sellPrice.getPrice();
sellPrices.put(companyName, sellPrice);
}
+ int cashAmount = numberSold * price * shareUnits;
moveStack.start(true).linkToPreviousMoveSet();
@@ -375,48 +376,17 @@
company.getShareUnit(),
numberSold * company.getShareUnit(),
companyName,
- Bank.format(numberSold * price) ));
+ Bank.format(cashAmount) ));
boolean soldBefore = sellPrices.containsKey(companyName);
+
+ pay (bank, company, cashAmount);
adjustSharePrice (company, numberSold, soldBefore);
if (!company.isClosed()) {
- // Check if the presidency has changed
- if (presCert != null && dumpedPlayer != null && presSharesToSell > 0) {
- ReportBuffer.add(LocalText.getText("IS_NOW_PRES_OF",
- dumpedPlayer.getName(),
- companyName));
- // First swap the certificates
- Portfolio dumpedPortfolio = dumpedPlayer.getPortfolio();
- List<PublicCertificateI> swapped =
- portfolio.swapPresidentCertificate(company, dumpedPortfolio);
- for (int i = 0; i < presSharesToSell; i++) {
- certsToSell.add(swapped.get(i));
- }
- }
- // Transfer the sold certificates
- for (PublicCertificateI cert2 : certsToSell) {
- if (cert2 != null) {
- executeTradeCertificate (cert2, pool, cert2.getShares() * price);
- }
- }
-
- // Check if we still have the presidency
- if (currentPlayer == company.getPresident()) {
- Player otherPlayer;
- for (int i = currentIndex + 1; i < currentIndex + numberOfPlayers; i++) {
- otherPlayer = gameManager.getPlayerByIndex(i);
- if (otherPlayer.getPortfolio().getShare(company) > portfolio.getShare(company)) {
- portfolio.swapPresidentCertificate(company,
- otherPlayer.getPortfolio());
- ReportBuffer.add(LocalText.getText("IS_NOW_PRES_OF",
- otherPlayer.getName(),
- company.getName()));
- break;
- }
- }
- }
+ executeShareTransfer (company, certsToSell,
+ dumpedPlayer, presSharesToSell);
}
cashToRaise.add(-numberSold * price);
Modified: trunk/18xx/rails/game/StartRound.java
===================================================================
--- trunk/18xx/rails/game/StartRound.java 2010-09-10 16:30:21 UTC (rev 1418)
+++ trunk/18xx/rails/game/StartRound.java 2010-09-10 21:57:19 UTC (rev 1419)
@@ -257,14 +257,15 @@
player.getName(),
primary.getName(),
Bank.format(price) ));
- executeTradeCertificate (primary, player.getPortfolio(), price);
+ pay (player, bank, price);
+ transferCertificate (primary, player.getPortfolio());
checksOnBuying(primary, sharePrice);
if (item.hasSecondary()) {
Certificate extra = item.getSecondary();
ReportBuffer.add(LocalText.getText("ALSO_GETS",
player.getName(),
extra.getName() ));
- executeTradeCertificate (extra, player.getPortfolio(), 0);
+ transferCertificate (extra, player.getPortfolio());
checksOnBuying(extra, sharePrice);
}
item.setSold(player, price);
Modified: trunk/18xx/rails/game/StockMarket.java
===================================================================
--- trunk/18xx/rails/game/StockMarket.java 2010-09-10 16:30:21 UTC (rev 1418)
+++ trunk/18xx/rails/game/StockMarket.java 2010-09-10 21:57:19 UTC (rev 1419)
@@ -248,6 +248,7 @@
if (newrow > row) {
newsquare = getStockSpace(newrow, col);
}
+ /*
if (newsquare != oldsquare && newsquare.closesCompany()) {
company.setClosed();
oldsquare.removeToken(company);
@@ -257,6 +258,10 @@
} else {
prepareMove(company, oldsquare, newsquare);
}
+ */
+ if (newsquare != oldsquare) {
+ prepareMove(company, oldsquare, newsquare);
+ }
}
protected void moveRightOrUp(PublicCompanyI company) {
Modified: trunk/18xx/rails/game/StockRound.java
===================================================================
--- trunk/18xx/rails/game/StockRound.java 2010-09-10 16:30:21 UTC (rev 1418)
+++ trunk/18xx/rails/game/StockRound.java 2010-09-10 21:57:19 UTC (rev 1419)
@@ -1045,6 +1045,7 @@
// Selling price
int price = getCurrentSellPrice (company);
+ int cashAmount = numberSold * price * shareUnits;
// Save original price as it may be reused in subsequent sale actions in the same turn
boolean soldBefore = sellPrices.containsKey(companyName);
@@ -1059,7 +1060,7 @@
playerName,
company.getShareUnit() * shareUnits,
companyName,
- Bank.format(numberSold * price * shareUnits) ));
+ Bank.format(cashAmount) ));
} else {
ReportBuffer.add(LocalText.getText("SELL_SHARES_LOG",
playerName,
@@ -1067,51 +1068,16 @@
company.getShareUnit() * shareUnits,
numberSold * company.getShareUnit() * shareUnits,
companyName,
- Bank.format(numberSold * price * shareUnits) ));
+ Bank.format(cashAmount) ));
}
+ pay (bank, currentPlayer, cashAmount);
adjustSharePrice (company, numberSold, soldBefore);
if (!company.isClosed()) {
- // Check if the presidency has changed
- if (presCert != null && dumpedPlayer != null && presSharesToSell > 0) {
- ReportBuffer.add(LocalText.getText("IS_NOW_PRES_OF",
- dumpedPlayer.getName(),
- companyName ));
- // First swap the certificates
- Portfolio dumpedPortfolio = dumpedPlayer.getPortfolio();
- List<PublicCertificateI> swapped =
- portfolio.swapPresidentCertificate(company, dumpedPortfolio);
- for (int i = 0; i < presSharesToSell; i++) {
- certsToSell.add(swapped.get(i));
- }
- }
-
- // Transfer the sold certificates
- Iterator<PublicCertificateI> it = certsToSell.iterator();
- while (it.hasNext()) {
- cert = it.next();
- if (cert != null) {
- executeTradeCertificate(cert, pool, cert.getShares() * price);
- }
- }
-
- // Check if we still have the presidency
- if (currentPlayer == company.getPresident()) {
- Player otherPlayer;
- for (int i = currentIndex + 1; i < currentIndex + numberOfPlayers; i++) {
- otherPlayer = gameManager.getPlayerByIndex(i);
- if (otherPlayer.getPortfolio().getShare(company) > portfolio.getShare(company)) {
- portfolio.swapPresidentCertificate(company,
- otherPlayer.getPortfolio());
- ReportBuffer.add(LocalText.getText("IS_NOW_PRES_OF",
- otherPlayer.getName(),
- company.getName() ));
- break;
- }
- }
- }
+ executeShareTransfer (company, certsToSell,
+ dumpedPlayer, presSharesToSell);
}
// Remember that the player has sold this company this round.
@@ -1125,6 +1091,46 @@
return true;
}
+ protected void executeShareTransfer (PublicCompanyI company,
+ List<PublicCertificateI> certsToSell,
+ Player dumpedPlayer, int presSharesToSell) {
+
+ Portfolio portfolio = currentPlayer.getPortfolio();
+
+ // Check if the presidency has changed
+ if (dumpedPlayer != null && presSharesToSell > 0) {
+ ReportBuffer.add(LocalText.getText("IS_NOW_PRES_OF",
+ dumpedPlayer.getName(),
+ company.getName() ));
+ // First swap the certificates
+ Portfolio dumpedPortfolio = dumpedPlayer.getPortfolio();
+ List<PublicCertificateI> swapped =
+ portfolio.swapPresidentCertificate(company, dumpedPortfolio);
+ for (int i = 0; i < presSharesToSell; i++) {
+ certsToSell.add(swapped.get(i));
+ }
+ }
+
+ transferCertificates (certsToSell, pool);
+
+ // Check if we still have the presidency
+ if (currentPlayer == company.getPresident()) {
+ Player otherPlayer;
+ int currentIndex = getCurrentPlayerIndex();
+ for (int i = currentIndex + 1; i < currentIndex + numberOfPlayers; i++) {
+ otherPlayer = gameManager.getPlayerByIndex(i);
+ if (otherPlayer.getPortfolio().getShare(company) > portfolio.getShare(company)) {
+ portfolio.swapPresidentCertificate(company,
+ otherPlayer.getPortfolio());
+ ReportBuffer.add(LocalText.getText("IS_NOW_PRES_OF",
+ otherPlayer.getName(),
+ company.getName() ));
+ break;
+ }
+ }
+ }
+ }
+
protected int getCurrentSellPrice (PublicCompanyI company) {
String companyName = company.getName();
@@ -1143,9 +1149,22 @@
protected void adjustSharePrice (PublicCompanyI company, int numberSold, boolean soldBefore) {
- if (company.canSharePriceVary()) {
- stockMarket.sell(company, numberSold);
+ if (!company.canSharePriceVary()) return;
+
+ stockMarket.sell(company, numberSold);
+
+ StockSpaceI newSpace = company.getCurrentSpace();
+
+ if (newSpace.closesCompany() && company.canClose()) {
+ company.setClosed();
+ ReportBuffer.add(LocalText.getText("CompanyClosesAt",
+ company.getName(),
+ newSpace.getName()));
+ return;
}
+
+ // Company is still open
+
}
public boolean useSpecialProperty(UseSpecialProperty action) {
Modified: trunk/18xx/rails/game/TreasuryShareRound.java
===================================================================
--- trunk/18xx/rails/game/TreasuryShareRound.java 2010-09-10 16:30:21 UTC (rev 1418)
+++ trunk/18xx/rails/game/TreasuryShareRound.java 2010-09-10 21:57:19 UTC (rev 1419)
@@ -338,6 +338,8 @@
return false;
}
+ int cashAmount = shares * price;
+
// All seems OK, now buy the shares.
if (number == 1) {
ReportBuffer.add(LocalText.getText("BUY_SHARE_LOG",
@@ -345,7 +347,7 @@
shareUnit,
companyName,
from.getName(),
- Bank.format(shares * price) ));
+ Bank.format(cashAmount) ));
} else {
ReportBuffer.add(LocalText.getText("BUY_SHARES_LOG",
companyName,
@@ -354,14 +356,16 @@
number * shareUnit,
companyName,
from.getName(),
- Bank.format(shares * price) ));
+ Bank.format(cashAmount) ));
}
moveStack.start(true);
+
+ pay (company, bank, cashAmount);
PublicCertificateI cert2;
for (int i = 0; i < number; i++) {
cert2 = from.findCertificate(company, sharePerCert/shareUnit, false);
- executeTradeCertificate(cert2, portfolio, cert2.getShares() * price);
+ transferCertificate(cert2, portfolio);
}
hasBought.set(true);
@@ -481,20 +485,25 @@
moveStack.start(true);
+ int cashAmount = numberSold * price;
ReportBuffer.add(LocalText.getText("SELL_SHARES_LOG",
companyName,
numberSold,
company.getShareUnit(),
(numberSold * company.getShareUnit()),
companyName,
- Bank.format(numberSold * price) ));
+ Bank.format(cashAmount) ));
+ pay (bank, company, cashAmount);
// Transfer the sold certificates
+ transferCertificates (certsToSell, pool);
+ /*
for (PublicCertificateI cert2 : certsToSell) {
if (cert2 != null) {
- executeTradeCertificate (cert2, pool, cert2.getShares() * price);
+ transferCertificate (cert2, pool, cert2.getShares() * price);
}
}
+ */
stockMarket.sell(company, numberSold);
hasSold.set(true);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|