001/**
002 * 
003 */
004package com.fs.starfarer.api.impl.campaign.shared;
005
006import java.util.ArrayList;
007import java.util.HashSet;
008import java.util.LinkedHashMap;
009import java.util.List;
010import java.util.Map;
011import java.util.Set;
012
013import com.fs.starfarer.api.Global;
014import com.fs.starfarer.api.campaign.SectorAPI;
015import com.fs.starfarer.api.campaign.econ.MonthlyReport;
016import com.fs.starfarer.api.impl.campaign.CoreScript;
017import com.fs.starfarer.api.util.TimeoutTracker;
018
019/**
020 * Assorted bits of shared data within a single campaign. NOT across campaigns.
021 * @author Alex
022 *
023 */
024public class SharedData {
025        
026        public static class UniqueEncounterData {
027                public List<String> interactedWith = new ArrayList<String>();
028                public List<String> historianBlurbsShown = new ArrayList<String>();
029                
030                public boolean wasInteractedWith(String id) {
031                        return interactedWith.contains(id);
032                }
033                
034                public void setWasInteractedWith(String id) {
035                        interactedWith.add(id);
036                }
037                
038                protected Object readResolve() {
039                        if (historianBlurbsShown == null) {
040                                historianBlurbsShown = new ArrayList<String>();
041                        }
042                        return this;
043                }
044                
045        }
046        
047        protected TimeoutTracker<String> marketsThatSentRelief = new TimeoutTracker<String>();
048        protected TimeoutTracker<String> marketsThatSentTradeFleet = new TimeoutTracker<String>();
049        //private TimeoutTracker<String> starSystemCustomsTimeout = new TimeoutTracker<String>();
050        
051        // faction, then star system
052        protected Map<String, TimeoutTracker<String>> starSystemCustomsTimeout = new LinkedHashMap<String, TimeoutTracker<String>>();
053        
054        //protected SectorActivityTracker activityTracker = new SectorActivityTracker();
055        protected PlayerActivityTracker playerActivityTracker = new PlayerActivityTracker();
056        
057        protected Set<String> marketsWithoutTradeFleetSpawn = new HashSet<String>();
058        //protected Set<String> marketsWithoutPatrolSpawn = new HashSet<String>();
059        
060        private PersonBountyEventData personBountyEventData = new PersonBountyEventData();
061
062        protected float playerPreLosingBattleFP = -1;
063        protected float playerPreLosingBattleCrew = -1;
064        protected long playerLosingBattleTimestamp = 0;
065        
066        protected MonthlyReport previousReport = new MonthlyReport();
067        protected MonthlyReport currentReport = new MonthlyReport();
068        
069        protected UniqueEncounterData uniqueEncounterData = new UniqueEncounterData();
070        
071        public SharedData() {
072        }
073        
074        public MonthlyReport getPreviousReport() {
075                if (previousReport == null) previousReport = new MonthlyReport();
076                return previousReport;
077        }
078        public MonthlyReport getCurrentReport() {
079                if (currentReport == null) currentReport = new MonthlyReport();
080                return currentReport;
081        }
082        public void setCurrentReport(MonthlyReport currentReport) {
083                this.currentReport = currentReport;
084        }
085        public void setPreviousReport(MonthlyReport previousReport) {
086                this.previousReport = previousReport;
087        }
088        
089        public void rollOverReport() {
090                previousReport = currentReport;
091                currentReport = new MonthlyReport();
092        }
093
094        public long getPlayerLosingBattleTimestamp() {
095                return playerLosingBattleTimestamp;
096        }
097
098        public void setPlayerLosingBattleTimestamp(long playerLosingBattleTimestamp) {
099                this.playerLosingBattleTimestamp = playerLosingBattleTimestamp;
100        }
101
102        public float getPlayerPreLosingBattleFP() {
103                return playerPreLosingBattleFP;
104        }
105
106        public void setPlayerPreLosingBattleFP(float playerPreLosingBattleFP) {
107                this.playerPreLosingBattleFP = playerPreLosingBattleFP;
108        }
109
110        public float getPlayerPreLosingBattleCrew() {
111                return playerPreLosingBattleCrew;
112        }
113
114        public void setPlayerPreLosingBattleCrew(float playerPreLosingBattleCrew) {
115                this.playerPreLosingBattleCrew = playerPreLosingBattleCrew;
116        }
117
118        public UniqueEncounterData getUniqueEncounterData() {
119                return uniqueEncounterData;
120        }
121
122        protected Object readResolve() {
123                if (starSystemCustomsTimeout == null) {
124                        starSystemCustomsTimeout = new LinkedHashMap<String, TimeoutTracker<String>>();
125                }
126                if (personBountyEventData == null) {
127                        personBountyEventData = new PersonBountyEventData();
128                }
129                if (uniqueEncounterData == null) {
130                        uniqueEncounterData = new UniqueEncounterData();
131                }
132                return this;
133        }
134
135        public PersonBountyEventData getPersonBountyEventData() {
136                return personBountyEventData;
137        }
138
139        public void advance(float amount) {
140                
141                SectorAPI sector = Global.getSector();
142                if (sector.isPaused()) {
143                        return;
144                }
145                
146                float days = sector.getClock().convertToDays(amount);
147                
148                marketsThatSentRelief.advance(days);
149                marketsThatSentTradeFleet.advance(days);
150                
151                for (TimeoutTracker<String> curr : starSystemCustomsTimeout.values()) {
152                        curr.advance(days);
153                }
154                
155                //activityTracker.advance(days);
156                playerActivityTracker.advance(days);
157        }
158        
159
160        
161//      public SectorActivityTracker getActivityTracker() {
162//              return activityTracker;
163//      }
164
165        public TimeoutTracker<String> getMarketsThatSentRelief() {
166                return marketsThatSentRelief;
167        }
168        public TimeoutTracker<String> getMarketsThatSentTradeFleet() {
169                return marketsThatSentTradeFleet;
170        }
171        
172        public PlayerActivityTracker getPlayerActivityTracker() {
173                return playerActivityTracker;
174        }
175
176        public static SharedData getData() {
177                Object data = Global.getSector().getPersistentData().get(CoreScript.SHARED_DATA_KEY);
178                if (data == null) {
179                        data = new SharedData();
180                        Global.getSector().getPersistentData().put(CoreScript.SHARED_DATA_KEY, data);
181                }
182                return (SharedData) data;
183        }
184
185        public Set<String> getMarketsWithoutTradeFleetSpawn() {
186                return marketsWithoutTradeFleetSpawn;
187        }
188
189
190        public void resetCustomsTimeouts() {
191                starSystemCustomsTimeout.clear();
192        }
193        
194        public TimeoutTracker<String> getStarSystemCustomsTimeout(String factionId) {
195                TimeoutTracker<String> tracker = starSystemCustomsTimeout.get(factionId);
196                if (tracker == null) {
197                        tracker = new TimeoutTracker<String>();
198                        starSystemCustomsTimeout.put(factionId, tracker);
199                }
200                return tracker;
201        }
202        
203}
204
205
206
207