-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcookbookdata.lua
More file actions
254 lines (223 loc) · 6.91 KB
/
cookbookdata.lua
File metadata and controls
254 lines (223 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
local USE_SETTINGS_FILE = PLATFORM ~= "PS4" and PLATFORM ~= "NACL"
local MAX_RECIPES = 6
local cooking = require("cooking")
local CookbookData = Class(function(self)
self.preparedfoods = {}
self.newfoods = {}
self.filters = {}
--self.save_enabled = nil
end)
function CookbookData:GetKnownPreparedFoods()
return self.preparedfoods
end
function CookbookData:Save(force_save)
if force_save or (self.save_enabled and self.dirty) then
local str = json.encode({preparedfoods = self.preparedfoods, filters = self.filters})
TheSim:SetPersistentString("cookbook", str, false)
self.dirty = false
end
end
function CookbookData:Load()
self.preparedfoods = {}
self.filters = {}
local needs_save = false
local really_bad_state = false
TheSim:GetPersistentString("cookbook", function(load_success, data)
if load_success and data ~= nil then
local status, recipe_book = pcall( function() return json.decode(data) end )
if status and recipe_book then
if type(recipe_book.preparedfoods) == "table" then
self.preparedfoods = recipe_book.preparedfoods
if type(recipe_book.filters) == "table" then -- Optional data that does not matter.
self.filters = recipe_book.filters
end
else
really_bad_state = true
print("Failed to load preparedfoods table in cookbook!")
end
else
really_bad_state = true
print("Failed to load the cookbook!", status, recipe_book)
end
end
end)
if really_bad_state then
print("Trying to apply online cache of cookbook data..")
if self:ApplyOnlineProfileData() then
print("Was a success, using preparedfoods values of:")
dumptable(self.preparedfoods)
print("Also using old stored filters values of:")
dumptable(self.filters)
needs_save = true
else
print("Which also failed. This error is unrecoverable. Cookbook will be cleared.")
end
end
if needs_save then
print("Saving cookbook file as a fixup.")
self:Save(true)
end
end
local function DecodeCookbookEntry(value)
local data = {recipes = {}}
local recipes = string.split(value, "|")
for i = 1, #recipes-1 do
table.insert(data.recipes, string.split(recipes[i], ","))
end
data.has_eaten = recipes[#recipes] == "true"
return data
end
local function EncodeCookbookEntry(entry)
local str = ""
if entry.recipes ~= nil then
for i = 1, math.min(MAX_RECIPES, #entry.recipes) do
local r = entry.recipes[i]
str = str .. table.concat(r, ",") .. "|"
end
end
str = str .. (entry.has_eaten and "true" or "false")
return str
end
function CookbookData:ApplyOnlineProfileData()
if not self.synced and
(TheInventory:HasSupportForOfflineSkins() or not (TheFrontEnd ~= nil and TheFrontEnd:GetIsOfflineMode() or not TheNet:IsOnlineMode())) and
TheInventory:HasDownloadedInventory() then
self.preparedfoods = self.preparedfoods or {}
for k, v in pairs(TheInventory:GetLocalCookBook()) do
self.preparedfoods[k] = DecodeCookbookEntry(v)
end
self.synced = true
end
return self.synced
end
function CookbookData:IsNewFood(product)
return self.newfoods[product] == true
end
function CookbookData:ClearNewFlags()
self.newfoods = {}
end
function CookbookData:ClearFilters()
self.filters = {}
self.dirty = true
end
function CookbookData:SetFilter(category, value)
if self.filters[category] ~= value then
self.filters[category] = value
self.dirty = true
end
end
function CookbookData:GetFilter(category)
return self.filters[category]
end
function CookbookData:IsUnlocked(product)
return self.preparedfoods[product]
end
function CookbookData:IsValidEntry(product)
for cooker, recipes in pairs(cooking.cookbook_recipes) do
if recipes[product] ~= nil then
return true
end
end
return false
end
local function UnlockPreparedFood(self, product)
if self.preparedfoods[product] == nil then
self.preparedfoods[product] = {}
end
return self.preparedfoods[product]
end
function CookbookData:LearnFoodStats(product)
if product == nil then
print("Invalid cookbook recipe:", product)
return
elseif not self:IsValidEntry(product) then
--silent fail
return false
end
local updated = false
local preparedfood = UnlockPreparedFood(self, product)
if not preparedfood.has_eaten then
preparedfood.has_eaten = true
self.newfoods[product] = true
updated = true
end
if updated and self.save_enabled then
if not cooking.IsModCookerFood(product) and not TheNet:IsDedicated() then
TheInventory:SetCookBookValue(product, EncodeCookbookEntry(preparedfood))
end
self:Save(true)
end
return updated
end
local function IsKnownRecipe(known_recipes, ingredients)
for ri, known_recipe in ipairs(known_recipes) do
if #ingredients == #known_recipe then
local known = true
for i, ingredient in ipairs(ingredients) do
if ingredients[i] ~= known_recipe[i] then
known = false
break
end
end
if known then
return ri
end
end
end
end
function CookbookData:AddRecipe(product, ingredients)
if product == nil or ingredients == nil then
print("Invalid cookbook recipe:", product, unpack(ingredients or {"(empty)"}))
return
elseif not self:IsValidEntry(product) then
--silent fail
return false
end
ingredients = self:RemoveCookedFromName(ingredients)
table.sort(ingredients)
local updated = false
local preparedfood = UnlockPreparedFood(self, product)
if preparedfood.recipes == nil then
preparedfood.recipes = {ingredients}
self.newfoods[product] = true
updated = true
else
local recipes = preparedfood.recipes
local known_index = IsKnownRecipe(recipes, ingredients)
if known_index ~= nil then
if known_index > 2 then
table.remove(recipes, known_index)
table.insert(recipes, 1, ingredients)
updated = true
end
else
if #recipes >= MAX_RECIPES then
table.remove(recipes, #recipes)
end
table.insert(recipes, 1, ingredients)
self.newfoods[product] = true
updated = true
end
end
if updated and self.save_enabled then
if not cooking.IsModCookerFood(product) and not TheNet:IsDedicated() then
TheInventory:SetCookBookValue(product, EncodeCookbookEntry(preparedfood))
end
self:Save(true)
end
return updated
end
function CookbookData:RemoveCookedFromName(ingredients)
local ret = {}
for i, v in ipairs(ingredients) do
local str = v
str = string.gsub(str, "_cooked_", "")
str = string.gsub(str, "cooked_", "")
str = string.gsub(str, "quagmire_cooked", "quagmire_")
str = string.gsub(str, "_cooked", "")
str = string.gsub(str, "cooked", "")
table.insert(ret, str)
end
return ret
end
return CookbookData