001package com.fs.starfarer.api.campaign;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import com.fs.starfarer.api.Global;
007import com.fs.starfarer.api.campaign.CampaignUIAPI.CoreUITradeMode;
008import com.fs.starfarer.api.campaign.CargoAPI.CargoItemType;
009import com.fs.starfarer.api.campaign.econ.MarketAPI;
010import com.fs.starfarer.api.campaign.econ.SubmarketAPI;
011import com.fs.starfarer.api.fleet.FleetMemberAPI;
012import com.fs.starfarer.api.impl.campaign.shared.PlayerTradeDataForSubmarket;
013
014public class PlayerMarketTransaction {
015
016        public static class ShipSaleInfo {
017                private FleetMemberAPI member;
018                private float price;
019                public ShipSaleInfo(FleetMemberAPI member, float price) {
020                        this.member = member;
021                        this.price = price;
022                }
023                public FleetMemberAPI getMember() {
024                        return member;
025                }
026                public void setMember(FleetMemberAPI member) {
027                        this.member = member;
028                }
029                public float getPrice() {
030                        return price;
031                }
032                public void setPrice(float price) {
033                        this.price = price;
034                }
035        }
036        
037        public static enum LineItemType {
038                BOUGHT,
039                SOLD,
040                UPDATE,
041        }
042        public static class TransactionLineItem {
043                private String id;
044                private LineItemType itemType;
045                private CargoItemType cargoType;
046                private SubmarketAPI submarket;
047                private float quantity;
048                private float price;
049                private float tariff;
050                private float demandPrice;
051                private float demandTariff;
052                private long timestamp;
053                public TransactionLineItem(String id, LineItemType itemType,
054                                CargoItemType cargoType, SubmarketAPI submarket,
055                                float quantity, float price, float tariff, long timestamp) {
056                        this.id = id;
057                        this.itemType = itemType;
058                        this.cargoType = cargoType;
059                        this.submarket = submarket;
060                        this.quantity = quantity;
061                        this.price = price;
062                        this.tariff = tariff;
063                        this.timestamp = timestamp;
064                }
065                /**
066                 * Only set for an "UPDATE", which assumes that the price/tariff pair is for the supply prices.
067                 * @return
068                 */
069                public float getDemandPrice() {
070                        return demandPrice;
071                }
072                public void setDemandPrice(float price2) {
073                        this.demandPrice = price2;
074                }
075                public float getDemandTariff() {
076                        return demandTariff;
077                }
078                public void setDemandTariff(float demandTariff) {
079                        this.demandTariff = demandTariff;
080                }
081                public float getTariff() {
082                        return tariff;
083                }
084                public void setTariff(float tariff) {
085                        this.tariff = tariff;
086                }
087                public SubmarketAPI getSubmarket() {
088                        return submarket;
089                }
090                public void setSubmarket(SubmarketAPI submarket) {
091                        this.submarket = submarket;
092                }
093                public String getId() {
094                        return id;
095                }
096                public void setId(String id) {
097                        this.id = id;
098                }
099                public LineItemType getItemType() {
100                        return itemType;
101                }
102                public void setItemType(LineItemType itemType) {
103                        this.itemType = itemType;
104                }
105                public CargoItemType getCargoType() {
106                        return cargoType;
107                }
108                public void setCargoType(CargoItemType cargoType) {
109                        this.cargoType = cargoType;
110                }
111                public float getQuantity() {
112                        return quantity;
113                }
114                public void setQuantity(float quantity) {
115                        this.quantity = quantity;
116                }
117                public float getPrice() {
118                        return price;
119                }
120                public void setPrice(float price) {
121                        this.price = price;
122                }
123                public long getTimestamp() {
124                        return timestamp;
125                }
126                public void setTimestamp(long timestamp) {
127                        this.timestamp = timestamp;
128                }
129        }
130        
131        private float creditValue;
132        private CargoAPI bought = Global.getFactory().createCargo(true);
133        private CargoAPI sold = Global.getFactory().createCargo(true);
134        
135        private List<TransactionLineItem> lineItems = new ArrayList<TransactionLineItem>();
136        
137        private MarketAPI market;
138        private SubmarketAPI submarket;
139        
140        private List<ShipSaleInfo> shipsBought = new ArrayList<ShipSaleInfo>();
141        private List<ShipSaleInfo> shipsSold = new ArrayList<ShipSaleInfo>();
142        private final CoreUITradeMode tradeMode;
143        
144        public PlayerMarketTransaction(MarketAPI market, SubmarketAPI submarket, CoreUITradeMode tradeMode) {
145                this.market = market;
146                this.submarket = submarket;
147                this.tradeMode = tradeMode;
148        }
149        
150        
151        public CoreUITradeMode getTradeMode() {
152                return tradeMode;
153        }
154        
155        /**
156         * Only set for commodities (and possibly weapons) in non-free transactions.
157         * @return
158         */
159        public List<TransactionLineItem> getLineItems() {
160                return lineItems;
161        }
162
163        public float getBaseTradeValueImpact() {
164                float total = 0;
165                for (CargoStackAPI stack : bought.getStacksCopy()) {
166                        float qty = stack.getSize();
167//                      if (qty < 1) continue;
168//                      float val = (float) stack.getBaseValuePerUnit() * qty * playerImpactMult;
169                        float val = PlayerTradeDataForSubmarket.computeImpactOfHavingAlreadyBought(market, 
170                                                                stack.getType(), stack.getData(), stack.getBaseValuePerUnit(), qty);
171                        total += val;
172                }
173                
174                for (CargoStackAPI stack : sold.getStacksCopy()) {
175                        float qty = stack.getSize();
176//                      if (qty < 1) continue;
177//                      float val = (float) stack.getBaseValuePerUnit() * qty * playerImpactMult;
178//                      if (!market.hasCondition(Conditions.FREE_PORT) &&
179//                                      stack.isCommodityStack() && market.isIllegal(stack.getCommodityId())) {
180//                              val *= illegalImpactMult;
181//                      }
182                        float val = PlayerTradeDataForSubmarket.computeImpactOfHavingAlreadySold(market, 
183                                        stack.getType(), stack.getData(), stack.getBaseValuePerUnit(), qty);                    
184                        total += val;
185                }
186                
187                return total;
188        }
189        
190        
191        /**
192         * Positive if the player received money, negative if they paid money.
193         * Only the net, so it's possible to have a very small value for a very large transaction.
194         * @return
195         */
196        public float getCreditValue() {
197                return creditValue;
198        }
199        public void setCreditValue(float creditValue) {
200                this.creditValue = creditValue;
201        }
202        public CargoAPI getBought() {
203                return bought;
204        }
205        public void setBought(CargoAPI bought) {
206                this.bought = bought;
207        }
208        public CargoAPI getSold() {
209                return sold;
210        }
211        public void setSold(CargoAPI sold) {
212                this.sold = sold;
213        }
214        public MarketAPI getMarket() {
215                return market;
216        }
217        public SubmarketAPI getSubmarket() {
218                return submarket;
219        }
220
221        public float getQuantityBought(String commodityId) {
222//              float total = 0f;
223//              for (CargoStackAPI stack : getBought().getStacksCopy()) {
224//                      String id = stack.getCommodityId();
225//                      if (id != null && id.equals(commodityId)) total += stack.getSize();
226//              }
227//              return total;
228                return getBought().getQuantity(CargoItemType.RESOURCES, commodityId);
229        }
230        public float getQuantitySold(String commodityId) {
231//              float total = 0f;
232//              for (CargoStackAPI stack : getSold().getStacksCopy()) {
233//                      String id = stack.getCommodityId();
234//                      if (id != null && id.equals(commodityId)) total += stack.getSize();
235//              }
236                return getSold().getQuantity(CargoItemType.RESOURCES, commodityId);
237        }
238
239        public List<ShipSaleInfo> getShipsBought() {
240                return shipsBought;
241        }
242
243        public List<ShipSaleInfo> getShipsSold() {
244                return shipsSold;
245        }
246
247        
248}
249
250
251
252
253