Starsector API
Loading...
Searching...
No Matches
PlayerTradeProfitabilityData.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.shared;
2
3import java.util.ArrayList;
4import java.util.LinkedHashMap;
5import java.util.Map;
6
7import org.apache.log4j.Logger;
8
9import com.fs.starfarer.api.Global;
10import com.fs.starfarer.api.util.IntervalUtil;
11import com.fs.starfarer.api.util.Misc;
12
14 public static Logger log = Global.getLogger(PlayerTradeProfitabilityData.class);
15
16 public static class CommodityData {
17 private String commodityId;
18 private float totalPrice;
19 private float quantity;
20
21 public float getQuantity() {
22 return quantity;
23 }
24 public void setQuantity(float quantity) {
25 this.quantity = quantity;
26 }
27 public CommodityData(String commodityId) {
28 this.commodityId = commodityId;
29 }
30 public String getCommodityId() {
31 return commodityId;
32 }
33 public void setCommodityId(String commodityId) {
34 this.commodityId = commodityId;
35 }
36 public float getTotalPrice() {
37 return totalPrice;
38 }
39 public void setTotalPrice(float totalPrice) {
40 this.totalPrice = totalPrice;
41 }
42 }
43
44 private Map<String, CommodityData> dataBought = new LinkedHashMap<String, CommodityData>();
45 private IntervalUtil tracker;
46
47 private long accruedXP = 0;
48
50 tracker = Misc.createEconIntervalTracker();
51 }
52
53 public void reportNetBought(String commodityId, float quantity, float totalPrice) {
54 CommodityData data = getBoughtDataFor(commodityId);
55 data.setQuantity(data.getQuantity() + quantity);
56 data.setTotalPrice(data.getTotalPrice() + totalPrice);
57 }
58
59 public void reportNetSold(String commodityId, float quantity, float totalPrice) {
60 CommodityData data = getBoughtDataFor(commodityId);
61 if (data.getQuantity() < 1 || quantity < 1) return;
62 float avgBuyPrice = data.getTotalPrice() / data.getQuantity();
63
64 float net = quantity;
65 if (quantity > data.getQuantity()) net = data.getQuantity();
66 data.setQuantity(data.getQuantity() - net);
67
68 if (net < 1) return;
69
70 float paidForNet = avgBuyPrice * net;
71 data.setTotalPrice(Math.max(0, data.getTotalPrice() - paidForNet));
72
73 float receivedForNet = net * totalPrice / quantity;
74
75 float profit = receivedForNet - paidForNet;
76 if (profit <= 0) return;
77
78 float xpPerCredit = Global.getSettings().getFloat("economyPlayerXPPerCreditOfProfit");
79
80 long xp = (long) (profit * xpPerCredit);
81
82 accruedXP += xp;
83
84 log.info("Player accrued " + xp + " xp for selling " + commodityId + " (profit per unit: " + (int) (profit / net) + ")");
85 }
86
87
88
89 public void advance(float days) {
90 tracker.advance(days);
91 if (tracker.intervalElapsed()) {
92 float factor = Misc.getGenericRollingAverageFactor();
93 for (CommodityData cd : new ArrayList<CommodityData>(dataBought.values())) {
94 cd.setQuantity(cd.getQuantity() * factor);
95 cd.setTotalPrice(cd.getTotalPrice() * factor);
96 if (cd.getQuantity() < 1) dataBought.remove(cd.getCommodityId());
97 }
98 }
99 }
100
101 public CommodityData getBoughtDataFor(String commodityId) {
102 CommodityData cd = dataBought.get(commodityId);
103 if (cd == null) {
104 cd = new CommodityData(commodityId);
105 dataBought.put(commodityId, cd);
106 }
107 return cd;
108 }
109
110 public long getAccruedXP() {
111 return accruedXP;
112 }
113
114 public void setAccruedXP(long accruedXP) {
115 this.accruedXP = accruedXP;
116 }
117}
118
119
120
121
122
123
124
125
126
127
128
static SettingsAPI getSettings()
Definition Global.java:51
static Logger getLogger(Class c)
Definition Global.java:26
void reportNetBought(String commodityId, float quantity, float totalPrice)
void reportNetSold(String commodityId, float quantity, float totalPrice)