427 | function FarmStats:getStatisticData() |
428 | if not g_currentMission.missionDynamicInfo.isMultiplayer or not g_currentMission.missionDynamicInfo.isClient then |
429 | self:addStatistic("workedHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("workedHectares")), g_i18n:getArea(self:getTotalValue("workedHectares")), "%.2f") |
430 | self:addStatistic("cultivatedHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("cultivatedHectares")), g_i18n:getArea(self:getTotalValue("cultivatedHectares")), "%.2f") |
431 | self:addStatistic("plowedHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("plowedHectares")), g_i18n:getArea(self:getTotalValue("plowedHectares")), "%.2f") |
432 | self:addStatistic("sownHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("sownHectares")), g_i18n:getArea(self:getTotalValue("sownHectares")), "%.2f") |
433 | self:addStatistic("fertilizedHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("fertilizedHectares")), g_i18n:getArea(self:getTotalValue("fertilizedHectares")), "%.2f") |
434 | self:addStatistic("threshedHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("threshedHectares")), g_i18n:getArea(self:getTotalValue("threshedHectares")), "%.2f") |
435 | |
436 | self:addStatistic("workedTime", nil, Utils.formatTime(self:getSessionValue("workedTime")), Utils.formatTime(self:getTotalValue("workedTime")), "%s") |
437 | self:addStatistic("cultivatedTime", nil, Utils.formatTime(self:getSessionValue("cultivatedTime")), Utils.formatTime(self:getTotalValue("cultivatedTime")), "%s") |
438 | self:addStatistic("plowedTime", nil, Utils.formatTime(self:getSessionValue("plowedTime")), Utils.formatTime(self:getTotalValue("plowedTime")), "%s") |
439 | self:addStatistic("sownTime", nil, Utils.formatTime(self:getSessionValue("sownTime")), Utils.formatTime(self:getTotalValue("sownTime")), "%s") |
440 | self:addStatistic("fertilizedTime", nil, Utils.formatTime(self:getSessionValue("fertilizedTime")), Utils.formatTime(self:getTotalValue("fertilizedTime")), "%s") |
441 | self:addStatistic("threshedTime", nil, Utils.formatTime(self:getSessionValue("threshedTime")), Utils.formatTime(self:getTotalValue("threshedTime")), "%s") |
442 | |
443 | self:addStatistic("traveledDistance", g_i18n:getMeasuringUnit(), g_i18n:getDistance(self:getSessionValue("traveledDistance")), g_i18n:getDistance(self:getTotalValue("traveledDistance")), "%.2f") |
444 | self:addStatistic("fuelUsage", g_i18n:getText("unit_liter"), g_i18n:getFluid(self:getSessionValue("fuelUsage")), g_i18n:getFluid(self:getTotalValue("fuelUsage")), "%.2f") |
445 | self:addStatistic("seedUsage", g_i18n:getText("unit_liter"), g_i18n:getFluid(self:getSessionValue("seedUsage")), g_i18n:getFluid(self:getTotalValue("seedUsage")), "%.2f") |
446 | self:addStatistic("sprayUsage", g_i18n:getText("unit_liter"), g_i18n:getFluid(self:getSessionValue("sprayUsage")), g_i18n:getFluid(self:getTotalValue("sprayUsage")), "%.2f") |
447 | self:addStatistic("baleCount", nil, self:getSessionValue("baleCount"), self:getTotalValue("baleCount"), "%d") |
448 | |
449 | self:addStatistic("plantedTreeCount", nil, self:getSessionValue("plantedTreeCount"), self:getTotalValue("plantedTreeCount"), "%d") |
450 | self:addStatistic("cutTreeCount", nil, self:getSessionValue("cutTreeCount"), self:getTotalValue("cutTreeCount"), "%d") |
451 | |
452 | self:addStatistic("fieldJobMissionCount", nil, self:getSessionValue("fieldJobMissionCount"), self:getTotalValue("fieldJobMissionCount"), "%d") |
453 | self:addStatistic("transportMissionCount", nil, self:getSessionValue("transportMissionCount"), self:getTotalValue("transportMissionCount"), "%d") |
454 | |
455 | self:addStatistic("playTime", nil, Utils.formatTime(self:getSessionValue("playTime")), Utils.formatTime(self:getTotalValue("playTime")), "%s") |
456 | self:addStatistic("workersHired", nil, self:getSessionValue("workersHired"), nil, "%s") |
457 | |
458 | self:addStatistic("storageCapacity", nil, g_currentMission:getFarmSiloCapacity(), nil, "%s") |
459 | end |
460 | |
461 | return Utils.getNoNil(self.statisticData, {}) |
462 | end |
137 | function FarmStats:loadFromXMLFile(xmlFile, key) |
138 | local farmKey = key |
139 | local key = key .. ".statistics" |
140 | |
141 | self.statistics["traveledDistance"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".traveledDistance"), 0) |
142 | |
143 | self.statistics["fuelUsage"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".fuelUsage"), 0) |
144 | self.statistics["seedUsage"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".seedUsage"), 0) |
145 | self.statistics["sprayUsage"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".sprayUsage"), 0) |
146 | |
147 | self.statistics["workedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".workedHectares"), 0) |
148 | self.statistics["cultivatedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".cultivatedHectares"), 0) |
149 | self.statistics["sownHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".sownHectares"), 0) |
150 | self.statistics["fertilizedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".fertilizedHectares"), 0) |
151 | self.statistics["sprayedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".sprayedHectares"), 0) |
152 | self.statistics["threshedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".threshedHectares"), 0) |
153 | self.statistics["weededHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".weededHectares"), 0) |
154 | self.statistics["plowedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".plowedHectares"), 0) |
155 | |
156 | self.statistics["workedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".workedTime"), 0) |
157 | self.statistics["cultivatedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".cultivatedTime"), 0) |
158 | self.statistics["sownTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".sownTime"), 0) |
159 | self.statistics["fertilizedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".fertilizedTime"), 0) |
160 | self.statistics["threshedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".threshedTime"), 0) |
161 | self.statistics["weededTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".weededTime"), 0) |
162 | self.statistics["sprayedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".sprayedTime"), 0) |
163 | self.statistics["plowedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".plowedTime"), 0) |
164 | |
165 | self.statistics["baleCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".baleCount"), 0) |
166 | |
167 | self.statistics["breedCowsCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".breedCowsCount"), 0) |
168 | self.statistics["breedSheepCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".breedSheepCount"), 0) |
169 | self.statistics["breedPigsCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".breedPigsCount"), 0) |
170 | self.statistics["breedChickenCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".breedChickenCount"), 0) |
171 | self.statistics["breedHorsesCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".breedHorsesCount"), 0) |
172 | |
173 | self.statistics["fieldJobMissionCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".fieldJobMissionCount"), 0) |
174 | self.statistics["fieldJobMissionByNPC"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".fieldJobMissionByNPC"), 0) |
175 | self.statistics["transportMissionCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".transportMissionCount"), 0) |
176 | |
177 | self.statistics["plantedTreeCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".plantedTreeCount"), 0) |
178 | self.statistics["cutTreeCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".cutTreeCount"), 0) |
179 | self.statistics["woodTonsSold"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".woodTonsSold"), 0) |
180 | self.statistics["treeTypesCut"] = Utils.getNoNil(getXMLString(xmlFile, key .. ".treeTypesCut"), "000000") |
181 | |
182 | self.statistics["revenue"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".revenue"), 0) |
183 | self.statistics["expenses"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".expenses"), 0) |
184 | |
185 | self.statistics["playTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".playTime"), 0) |
186 | |
187 | -- Finances |
188 | local i = 0 |
189 | while true do |
190 | local financeKey = string.format("%s.finances.stats(%d)", farmKey, i) |
191 | local day = getXMLInt(xmlFile, financeKey .. "#day") |
192 | if day == nil then |
193 | break |
194 | end |
195 | |
196 | local finances = FinanceStats:new() |
197 | finances:loadFromXMLFile(xmlFile, financeKey) |
198 | |
199 | if day == 0 then |
200 | self.finances = finances |
201 | else |
202 | table.insert(self.financesHistory, finances) |
203 | end |
204 | |
205 | i = i + 1 |
206 | end |
207 | end |
30 | function FarmStats:new() |
31 | local self = {} |
32 | setmetatable(self, FarmStats_mt) |
33 | |
34 | -- General statistics |
35 | self.statistics = {} |
36 | for _, statName in pairs(FarmStats.STAT_NAMES) do |
37 | self.statistics[statName] = { session = 0, total = 0 } |
38 | end |
39 | |
40 | self.statistics.treeTypesCut = "000000" |
41 | |
42 | -- Finances |
43 | self.finances = FinanceStats:new() |
44 | self.financesHistory = {} -- over time, the finances of past days is put in here |
45 | |
46 | -- Major achievements |
47 | self.heroStats = {} |
48 | for _, heroStat in pairs(FarmStats.HERO_STAT_NAMES) do |
49 | self.heroStats[heroStat] = { id = nil, value = nil, accumValue = 0 } |
50 | end |
51 | self.heroStatsLoaded = false |
52 | self.moneyEarnedHeroAccum = 0 |
53 | self.nextHeroAccumUpdate = 0 |
54 | |
55 | if g_currentMission:getIsServer() then |
56 | g_currentMission:addUpdateable(self) |
57 | end |
58 | |
59 | -- this is increased whenever the finance stats are changed, so that other code can check if it has the latest state |
60 | self.financesVersionCounter = 0 |
61 | self.financesHistoryVersionCounter = 0 |
62 | self.financesHistoryVersionCounterLocal = 0 |
63 | |
64 | self.updatePlayTime = true |
65 | |
66 | return self |
67 | end |
77 | function FarmStats:saveToXMLFile(xmlFile, key) |
78 | |
79 | setXMLFloat(xmlFile, key .. ".statistics.traveledDistance", self.statistics.traveledDistance.total) |
80 | |
81 | setXMLFloat(xmlFile, key .. ".statistics.fuelUsage", self.statistics.fuelUsage.total) |
82 | setXMLFloat(xmlFile, key .. ".statistics.seedUsage", self.statistics.seedUsage.total) |
83 | setXMLFloat(xmlFile, key .. ".statistics.sprayUsage", self.statistics.sprayUsage.total) |
84 | |
85 | setXMLFloat(xmlFile, key .. ".statistics.workedHectares", self.statistics.workedHectares.total) |
86 | setXMLFloat(xmlFile, key .. ".statistics.cultivatedHectares", self.statistics.cultivatedHectares.total) |
87 | setXMLFloat(xmlFile, key .. ".statistics.sownHectares", self.statistics.sownHectares.total) |
88 | setXMLFloat(xmlFile, key .. ".statistics.fertilizedHectares", self.statistics.fertilizedHectares.total) |
89 | setXMLFloat(xmlFile, key .. ".statistics.threshedHectares", self.statistics.threshedHectares.total) |
90 | setXMLFloat(xmlFile, key .. ".statistics.plowedHectares", self.statistics.plowedHectares.total) |
91 | |
92 | setXMLFloat(xmlFile, key .. ".statistics.workedTime", self.statistics.workedTime.total) |
93 | setXMLFloat(xmlFile, key .. ".statistics.cultivatedTime", self.statistics.cultivatedTime.total) |
94 | setXMLFloat(xmlFile, key .. ".statistics.sownTime", self.statistics.sownTime.total) |
95 | setXMLFloat(xmlFile, key .. ".statistics.fertilizedTime", self.statistics.fertilizedTime.total) |
96 | setXMLFloat(xmlFile, key .. ".statistics.threshedTime", self.statistics.threshedTime.total) |
97 | setXMLFloat(xmlFile, key .. ".statistics.plowedTime", self.statistics.plowedTime.total) |
98 | |
99 | setXMLInt(xmlFile, key .. ".statistics.baleCount", self.statistics.baleCount.total) |
100 | |
101 | setXMLInt(xmlFile, key .. ".statistics.breedCowsCount", self.statistics.breedCowsCount.total) |
102 | setXMLInt(xmlFile, key .. ".statistics.breedSheepCount", self.statistics.breedSheepCount.total) |
103 | setXMLInt(xmlFile, key .. ".statistics.breedPigsCount", self.statistics.breedPigsCount.total) |
104 | setXMLInt(xmlFile, key .. ".statistics.breedChickenCount", self.statistics.breedChickenCount.total) |
105 | setXMLInt(xmlFile, key .. ".statistics.breedHorsesCount", self.statistics.breedHorsesCount.total) |
106 | |
107 | setXMLInt(xmlFile, key .. ".statistics.fieldJobMissionCount", self.statistics.fieldJobMissionCount.total) |
108 | setXMLInt(xmlFile, key .. ".statistics.fieldJobMissionByNPC", self.statistics.fieldJobMissionByNPC.total) |
109 | setXMLInt(xmlFile, key .. ".statistics.transportMissionCount", self.statistics.transportMissionCount.total) |
110 | |
111 | setXMLFloat(xmlFile, key .. ".statistics.revenue", self.statistics.revenue.total) |
112 | setXMLFloat(xmlFile, key .. ".statistics.expenses", self.statistics.expenses.total) |
113 | setXMLFloat(xmlFile, key .. ".statistics.playTime", self.statistics.playTime.total) |
114 | |
115 | setXMLInt(xmlFile, key .. ".statistics.plantedTreeCount", self.statistics.plantedTreeCount.total) |
116 | setXMLInt(xmlFile, key .. ".statistics.cutTreeCount", self.statistics.cutTreeCount.total) |
117 | setXMLFloat(xmlFile, key .. ".statistics.woodTonsSold", self.statistics.woodTonsSold.total) |
118 | setXMLString(xmlFile, key .. ".statistics.treeTypesCut", self.statistics.treeTypesCut) |
119 | |
120 | local toSave = { self.finances } |
121 | local numHistoricItems = #self.financesHistory |
122 | for n = 3, 0, -1 do |
123 | if numHistoricItems > n then |
124 | table.insert(toSave, self.financesHistory[numHistoricItems - n]) |
125 | end |
126 | end |
127 | |
128 | for i, finances in ipairs(toSave) do |
129 | local statsKey = string.format("%s.finances.stats(%d)", key, i-1) |
130 | setXMLInt(xmlFile, statsKey.."#day", i-1) |
131 | finances:saveToXMLFile(xmlFile, statsKey) |
132 | end |
133 | end |
211 | function FarmStats:update(dt) |
212 | if GS_PLATFORM_TYPE == GS_PLATFORM_TYPE_XBOXONE or GS_PLATFORM_TYPE == GS_PLATFORM_TYPE_GGP then |
213 | -- Check if the stats are now available. If so, update from the queue and empty said queue. |
214 | if not self.heroStatsLoaded and areStatsAvailable() then |
215 | self.heroStatsLoaded = true |
216 | for heroStatName, heroStat in pairs(self.heroStats) do |
217 | heroStat.id = statsGetIndex(heroStatName) |
218 | heroStat.value = statsGet(heroStat.id) |
219 | if heroStat.accumValue ~= 0 then |
220 | heroStat.value = heroStat.value + heroStat.accumValue |
221 | statsSet(heroStat.id, heroStat.value) |
222 | heroStat.accumValue = 0 |
223 | end |
224 | end |
225 | end |
226 | |
227 | -- Update qeued money earned stat to prevent event-rate limit hit |
228 | if g_time >= self.nextHeroAccumUpdate and self.moneyEarnedHeroAccum > 0 then |
229 | self:addValueToHeroStat("moneyEarned", self.moneyEarnedHeroAccum) |
230 | self.moneyEarnedHeroAccum = 0 |
231 | self.nextHeroAccumUpdate = g_time + 10 * 1000 |
232 | end |
233 | end |
234 | |
235 | self:updateStats("playTime", dt / (1000 * 60), self.updatePlayTime) |
236 | end |