forked from Rikenbacker/gt_oc_infusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe_add.lua
294 lines (244 loc) · 6.84 KB
/
recipe_add.lua
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
local component = require("component")
local json = require('json')
local settings = {
recipesFileName = "recipes.json",
essentiaFileName = "essentia.json",
maxPatternsCount = 36
}
local Essentia = {}
function Essentia.new()
local essentia = {}
local obj = {}
local f = io.open(settings.essentiaFileName, "r")
if f~=nil then
essentia = json.decode(f:read("*all"))
io.close(f)
end
function obj.getLabel(name)
if (essentia[name]) == nil then
return name
else
return essentia[name].label
end
end
function obj.getAspect(name)
if (essentia[name]) == nil then
return name
else
return essentia[name].aspect
end
end
function obj.getCount()
return #essentia
end
function obj.getByPartName(partName)
local result = {}
for key, value in pairs(essentia) do
if type(value) == "table" then
if string.find(value.label, partName) ~= nil or string.find(value.aspect, partName) ~= nil then
table.insert(result, key)
end
end
end
return result
end
return obj
end
local Recipes = {}
function Recipes.new()
local recipes = {}
local obj = {}
local f = io.open(settings.recipesFileName, "r")
if f~=nil then
recipes = json.decode(f:read("*all"))
io.close(f)
end
function obj.getCount()
return #recipes
end
function obj.get(n)
return recipes[n]
end
function obj.findRecipe(inputItems)
for i = 1, #recipes do
if #recipes[i].input == #inputItems then
local fullMatch = true
for j = 1, #inputItems do
if inputItems[j].name ~= recipes[i].input[j].name or inputItems[j].size ~= recipes[i].input[j].size then
fullMatch = false
end
end
if fullMatch == true then
return recipes[i]
end
end
end
return nil
end
function obj.insert(name, inputs, essentias)
table.insert(recipes, {name = name, input = inputs, aspects = essentias})
local f = io.open(settings.recipesFileName, "w")
if f~=nil then
f:write(json.encode(recipes))
io.close(f)
end
end
return obj
end
local Tools = {}
function Tools.new()
local obj = {}
local interface = "me_interface"
local database = "database"
for address, type in component.list() do
if type == interface and obj[interface] == nil then
obj[interface] = component.proxy(address)
elseif type == database and obj[database] == nil then
obj[database] = component.proxy(address)
end
end
function obj.getInterface()
return obj[interface]
end
function obj.getDatabase()
return obj[database]
end
function obj.makeLabel(item)
return item.name .. "/" .. item.damage
end
function obj.loadRecipes(recipes)
local patterns = {}
-- Получаю список шаблонов и краткую информацию для дальнейшего заполнения
for patternSlot = 1, settings.maxPatternsCount do
local pattern = obj[interface].getInterfacePattern(patternSlot)
if pattern ~= nil then
-- Заполняю входные данные те что можно без использовани БД
local inputs = {}
for i = 1, #pattern.inputs do
if pattern.inputs[i].count ~= nil then
table.insert(inputs, {position = i, size = pattern.inputs[i].count})
end
end
-- Ищу певый заполненный выходной слот чтобы оттуда спереть наименование результата
local outputSlot = nil
for i = 1, #pattern.outputs do
if pattern.outputs[i].count ~= nil and outputSlot == nil then
outputSlot = i
end
end
table.insert(patterns, {slot = patternSlot, name = pattern.outputs[outputSlot].name, inputSlots = inputs})
end
end
print("Patterns: " .. #patterns)
-- Дозаполняю рецепты по данным через БД
for _, recipe in pairs(patterns) do
io.write(recipe.name)
local items = {}
for i = 1, #recipe.inputSlots do
obj[interface].storeInterfacePatternInput(recipe.slot, recipe.inputSlots[i].position, obj[database].address, 1)
table.insert(items, {name = obj.makeLabel(obj[database].get(1)), size = recipe.inputSlots[i].size})
end
recipe.readRecipe = items
recipe.foundRecipe = recipes.findRecipe(items)
if recipe.foundRecipe == nil then
print(": New")
else
print ": exists"
end
end
return patterns
end
return obj
end
function showListRecipes(recipes)
for i = 1, recipes.getCount() do
print(recipes.get(i).name)
end
end
function addNewRecipe(pattern, recipes, essentia)
local finished = false
local aspects = {}
while finished == false do
print(pattern.name .." now needed ".. #aspects .. " types of essentia.")
print("To add aspect to recipe type begining of the aspect name (like \"praec\" or \"victu\") and press enter. Or input empty string for finishing.")
io.flush()
local aspectName = io.read("*l")
if aspectName ~= "" then
local essentias = essentia.getByPartName(aspectName)
if #essentias == 0 then
print("Matches not found. Pleace try again.")
else
for i, id in pairs(essentias) do
print("("..i..") " .. essentia.getLabel(id))
end
print("(0) for choose another")
local num = io.read("*n")
io.read()
if num < 0 or num > #essentias then
print("Wrong number.")
elseif num ~= 0 then
local id = essentias[num]
print("How much ".. essentia.getLabel(id) .. "?")
local count = io.read("*n")
io.read()
table.insert(aspects, {name = id, size = count})
end
end
else -- aspectName ~= ""
print("Write new recipe? Y/n")
local answer = io.read("*l")
if answer == "Y" or answer == "y" then
recipes.insert(pattern.name, pattern.readRecipe, aspects)
end
finished = true
end
end
end
function scanPatterns(tools, recipes, essentia)
patterns = tools.loadRecipes(recipes)
if #patterns == 0 then
print("Patterns not fount.")
return
end
for _, pattern in pairs(patterns) do
if pattern.foundRecipe == nil then
print("New recipe found: " .. pattern.name .. ". Do you want to add it? Y/n")
result = io.read("*l")
if result == "Y" or result == "y" then
addNewRecipe(pattern, recipes, essentia)
end
end
end
end
function main()
local tools = Tools.new()
local recipes = Recipes.new()
local essentia = Essentia.new()
if tools.getInterface() ~= nil then
print("ME Interface found")
else
print("ERROR: ME Interface not found!")
return 1
end
if tools.getDatabase() ~= nil then
print("Database found")
else
print("ERROR: Database not found!")
return 1
end
print("Recipes loaded:", recipes.getCount())
print("")
local result = ""
while result ~= "0" do
print("1 - Show recipes list")
print("2 - Scan interface for new recipes")
print("0 - exit")
result = io.read("*l")
if result == "1" then
showListRecipes(recipes)
elseif result == "2" then
scanPatterns(tools, recipes, essentia)
end
end
end
main()