//call this from player accepting the trade public void acceptTrade(Trades trade) { Player offered = trade.getPlayerOffering(); for (int i = 0; i < trade.getResourcesOffered().Count; i++) { offered.changeResource((Enums.ResourceType)i, -trade.getResourcesOffered()[i]); offered.changeResource((Enums.ResourceType)i, trade.getResourcesWanted()[i]); this.changeResource((Enums.ResourceType)i, trade.getResourcesOffered()[i]); this.changeResource((Enums.ResourceType)i, -trade.getResourcesWanted()[i]); } for (int i = 0; i < trade.getCommoditiesOffered().Count; i++) { offered.changeCommodity((Enums.CommodityType)i, -trade.getCommoditiesOffered()[i]); offered.changeCommodity((Enums.CommodityType)i, trade.getCommoditiesWanted()[i]); this.changeCommodity((Enums.CommodityType)i, trade.getCommoditiesOffered()[i]); this.changeCommodity((Enums.CommodityType)i, -trade.getCommoditiesWanted()[i]); } offered.changeGoldCount(trade.getGoldWanted() - trade.getGoldOffered()); this.changeGoldCount(trade.getGoldOffered() - trade.getGoldWanted()); trade.CmdDestroy(trade.netId);//destroy the trade }
// Make sure a given trade is valid public bool isValidBankTrade(int[] resRatios, int[] comRatios, Trades trade) { int totalAvailable = 0; int totalWanted = 0; // Extract the information from the trade SyncListInt resOffered = trade.getResourcesOffered(); SyncListInt resWanted = trade.getResourcesWanted(); SyncListInt comOffered = trade.getCommoditiesOffered(); SyncListInt comWanted = trade.getCommoditiesWanted(); // Find the total offered amount for (int i = 0; i < resOffered.Count; i++) { totalAvailable += resOffered [i] / resRatios [i]; } for (int i = 0; i < comOffered.Count; i++) { totalAvailable += comOffered [i] / comRatios [i]; } totalAvailable += trade.getGoldOffered() / 2; // Find the total requested amount for (int i = 0; i < resWanted.Count; i++) { if (resWanted [i] > resources [i]) { return(false); } totalWanted += resWanted [i]; } for (int i = 0; i < comWanted.Count; i++) { if (comWanted [i] > commodities [i]) { return(false); } totalWanted += comWanted [i]; } // Return true if the requested amount is valid if (totalWanted != 0 && totalWanted <= totalAvailable) { return(true); } else { return(false); } }
// Make a trade with the bank public bool tradeWithBank(int[] resRatios, int[] comRatios, Trades trade) { if (!isValidBankTrade(resRatios, comRatios, trade)) { return(false); } // Extract the information from the trade SyncListInt resOffered = trade.getResourcesOffered(); SyncListInt resWanted = trade.getResourcesWanted(); SyncListInt comOffered = trade.getCommoditiesOffered(); SyncListInt comWanted = trade.getCommoditiesWanted(); Player trader = trade.getPlayerOffering(); int tradeId = trader.getID(); int gold = trade.getGoldOffered(); // Update all relevent fields for (int i = 0; i < resOffered.Count; i++) { trader.changeResource((Enums.ResourceType)i, resOffered[i]); depositResource((Enums.ResourceType)i, resOffered[i], trader.isServer); } for (int i = 0; i < comOffered.Count; i++) { trader.changeCommodity((Enums.CommodityType)i, comOffered[i]); depositCommodity((Enums.CommodityType)i, comOffered[i], trader.isServer); } for (int i = 0; i < resWanted.Count; i++) { trader.changeResource((Enums.ResourceType)i, resWanted[i]); withdrawResource((Enums.ResourceType)i, resWanted[i], trader.isServer); } for (int i = 0; i < comWanted.Count; i++) { trader.changeCommodity((Enums.CommodityType)i, comWanted[i]); withdrawCommodity((Enums.CommodityType)i, comWanted[i], trader.isServer); } trader.changeGoldCount(gold); trade.CmdDestroy(trade.netId); return(true); }