Skip to content

Commit cb87b6a

Browse files
committed
refactor: ♻️ Refactored Inventory
1 parent 3f4c7d6 commit cb87b6a

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

tests/test_spec.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---@diagnostic disable: need-check-nil, missing-parameter, param-type-mismatch
12
---@class are
23
---@field same function
34
---@field equal function
@@ -37,7 +38,7 @@ local turtleEmulator = require("../turtleEmulator")
3738
describe("Disabled Movement", function()
3839
local turtle
3940
setup(function()
40-
turtle = turtleEmulator:createTurtle(turtleEmulator)
41+
turtle = turtleEmulator:createTurtle()
4142
turtle.canMoveToCheck = function()
4243
return false
4344
end
@@ -66,7 +67,7 @@ end)
6667
describe("Enabled Movement", function()
6768
local turtle
6869
setup(function()
69-
turtle = turtleEmulator:createTurtle(turtleEmulator)
70+
turtle = turtleEmulator:createTurtle()
7071
turtle["canMoveToCheck"] = function()
7172
return true
7273
end
@@ -95,7 +96,7 @@ end)
9596
describe("Track Movement", function()
9697
local turtle
9798
setup(function()
98-
turtle = turtleEmulator:createTurtle(turtleEmulator)
99+
turtle = turtleEmulator:createTurtle()
99100
turtle["canMoveToCheck"] = function()
100101
return true
101102
end
@@ -142,7 +143,7 @@ end)
142143
describe("Track Facing Direction", function()
143144
local turtle
144145
setup(function()
145-
turtle = turtleEmulator:createTurtle(turtleEmulator)
146+
turtle = turtleEmulator:createTurtle()
146147
turtle["canMoveToCheck"] = function()
147148
return true
148149
end
@@ -262,7 +263,6 @@ describe("ProxyTests", function()
262263

263264
it("Create own key Value", function()
264265
local functionGotCalled = false
265-
turtle.canPrint = true
266266
function turtle:thisIsATest(RandomString)
267267
assert.are_not.equal(self, turtle, "The self reference is the Proxy instead of the turtle object")
268268
assert.are.equal("RandomString", RandomString, "Parameter 2 not correct, might be a self reference?")
@@ -271,7 +271,6 @@ describe("ProxyTests", function()
271271

272272
turtle:thisIsATest("RandomString")
273273
assert.are.equal(true, functionGotCalled, "Function was not called")
274-
turtle.canPrint = false
275274
end)
276275

277276
it("Change Metatable", function()
@@ -343,6 +342,7 @@ describe("InventoryTests", function()
343342
{ name = "minecraft:Wood", count = 32, maxcount = 64 },
344343
4)
345344
assert.is_true(succ)
345+
assert.are.equal(64, turtle.getItemDetail(4).count)
346346
assert.are.equal(64, turtle.inventory[4].count)
347347
end)
348348
it("Adding more items than maxcount without a specific slot", function()
@@ -351,8 +351,10 @@ describe("InventoryTests", function()
351351
local succ = turtle:addItemToInventory(
352352
{ name = "minecraft:Wood", count = 128, maxcount = 64 })
353353
assert.is_true(succ)
354-
assert.are.equal(64, turtle.inventory[3].count)
355-
assert.are.equal(64, turtle.inventory[5].count)
354+
assert.are.equal(64, turtle.getItemCount(3))
355+
assert.are.equal(1, turtle.getItemCount(4))
356+
assert.are.equal(64, turtle.getItemCount(5))
357+
assert.are.equal(0, turtle.getItemCount(6))
356358
end)
357359
end)
358360
describe("Remaining Inventory Tests", function()

turtleMock.lua

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ end
2424
---@alias height integer
2525
---@alias position {x: north, y: east, z: height}
2626

27-
---@alias item {name: string, count: integer}
28-
---@alias turtleSlot {name: string, count: integer, maxcount: integer, durabilty: integer, fuelgain: integer, placeAble: boolean, equipable: boolean}
29-
---@alias inventory { [integer]: turtleSlot }
27+
---@alias item {name: string, durabilty: integer, equipable: boolean, fuelgain: integer, placeAble: boolean, maxcount: number, wildcardInfo: any, count: integer}
28+
---@alias inventory { [integer]: item }
29+
30+
-- ---@alias equipslots {left : }
3031

3132

3233

@@ -96,6 +97,9 @@ local function down(self)
9697
self.position.y = self.position.y - 1
9798
return true
9899
end
100+
local function slotNotEmpty(slot)
101+
return slot ~= nil
102+
end
99103

100104
---@return TurtleProxy
101105
function turtleMock.createMock()
@@ -110,7 +114,7 @@ function turtleMock.createMock()
110114
---@type number
111115
fuelLevel = 0,
112116
---@type boolean
113-
canPrint = true,
117+
canPrint = false,
114118
---@type inventory
115119
inventory = {},
116120
---@type integer
@@ -210,7 +214,7 @@ end
210214
function turtleMock:getItemCount(slot)
211215
slot = slot or self.selectedSlot
212216
assert((slot >= 1 and slot <= 16) or slot == nil, "Slot number " .. slot .. " out of range")
213-
return self.inventory[slot] ~= nil and self.inventory[slot].count or 0
217+
return slotNotEmpty(self.inventory[slot]) and self.inventory[slot].count or 0
214218
end
215219

216220
--- gets the space in the selected slot or the specified slot
@@ -219,7 +223,7 @@ end
219223
function turtleMock:getItemSpace(slot)
220224
slot = slot or self.selectedSlot
221225
assert((slot >= 1 and slot <= 16) or slot == nil, "Slot number " .. slot .. " out of range")
222-
return self.inventory[slot] ~= nil and self.inventory[slot].maxcount - self.inventory[slot].count or
226+
return slotNotEmpty(self.inventory[slot]) and self.inventory[slot].maxcount - self.inventory[slot].count or
223227
self.defaultMaxSlotSize
224228
end
225229

@@ -233,23 +237,24 @@ end
233237
function turtleMock:getItemDetail(slot)
234238
slot = slot or self.selectedSlot
235239
assert((slot >= 1 and slot <= 16) or slot == nil, "Slot number " .. slot .. " out of range")
236-
local item = self.inventory[slot]
237-
return item ~= nil and { name = item.name, count = item.count } or nil
240+
---@type item
241+
local iSlot = self.inventory[slot]
242+
return iSlot ~= nil and { name = iSlot.name, count = iSlot.count } or nil
238243
end
239244

240245
--- Compare the item in the selected slot to the item in the specified slot.
241246
---@param slot integer
242247
---@return boolean equal true if the items are equal
243248
function turtleMock:compareTo(slot)
244249
assert((slot >= 1 and slot <= 16), "Slot number " .. slot .. " out of range")
245-
local item = self.inventory[self.selectedSlot]
246-
local compareItem = self.inventory[slot]
247-
if item == nil and compareItem == nil then
250+
local iSlot = self.inventory[self.selectedSlot]
251+
local compareSlot = self.inventory[slot]
252+
if iSlot == nil and compareSlot == nil then
248253
return true
249-
elseif item == nil or compareItem == nil then
254+
elseif iSlot == nil or compareSlot == nil then
250255
return false
251256
end
252-
return item.name == compareItem.name
257+
return iSlot.name == compareSlot.name
253258
end
254259

255260
--- Transfers items between the selected slot and the specified slot.
@@ -271,7 +276,7 @@ function turtleMock:transferTo(slot, count)
271276
return true
272277
end
273278
self.inventory[slot] = deepCopy(self.inventory[self.selectedSlot])
274-
self.inventory[slot].count = math.min(self.inventory[slot].maxcount, count)
279+
self.inventory[slot].count = math.min(self.inventory[slot] and self.inventory[slot].maxcount or 0 , count)
275280
local transferTo = math.min(currentSlot.count, count)
276281
currentSlot.count = currentSlot.count - transferTo
277282
if currentSlot.count < 1 then
@@ -298,7 +303,7 @@ end
298303

299304
--- Finds the first slot containing the specified item or no Item, starting with the selected slot and looping around.
300305
---@param turtle TurtleMock
301-
---@param item turtleSlot
306+
---@param item item
302307
---@param startingSlot number
303308
local function findFittingSlot(turtle, item, startingSlot)
304309
for i = startingSlot, 16 do
@@ -323,10 +328,11 @@ end
323328
---
324329
--- <b>note</b>: This function will only work for tests and does not work on the CraftOS-Turtle
325330
---@param turtle TurtleMock
326-
---@param item turtleSlot
331+
---@param item item
327332
---@param slot number | nil
328333
local function pickUpItem(turtle, item, slot)
329334
assert(item.count > 0, "Count must be greater than 0")
335+
turtle:print("Item: ", item.count)
330336
if slot == nil then
331337
while item.count > 0 do
332338
local fittingSlot = findFittingSlot(turtle, item, turtle.selectedSlot)
@@ -335,19 +341,18 @@ local function pickUpItem(turtle, item, slot)
335341
end
336342
local space = turtle:getItemSpace(fittingSlot)
337343
local toTransfer = math.min(space, item.count)
344+
345+
local currentCount = turtle:getItemCount(fittingSlot)
346+
turtle.inventory[fittingSlot] = deepCopy(item)
338347
if (turtle.inventory[fittingSlot] == nil) then
339-
turtle.inventory[fittingSlot] = {
340-
name = item.name,
341-
count = toTransfer,
342-
maxcount = turtle
343-
.defaultMaxSlotSize
344-
}
348+
turtle.inventory[fittingSlot].maxcount = item.maxcount or turtle.defaultMaxSlotSize
345349
end
350+
turtle.inventory[fittingSlot].count = currentCount + toTransfer
346351
item.count = item.count - toTransfer
347352
end
348353
else
349354
assert((slot >= 1 and slot <= 16), "Slot number " .. slot .. " out of range")
350-
if turtle.inventory[slot] ~= nil and turtle.inventory[slot].name ~= item.name then
355+
if slotNotEmpty(turtle.inventory[slot] ) and turtle.inventory[slot].name ~= item.name then
351356
return false, "Can't pick up item, slot is not empty"
352357
end
353358
if turtle:getItemSpace(slot) < item.count then
@@ -362,10 +367,12 @@ local function pickUpItem(turtle, item, slot)
362367
return true
363368
end
364369

370+
371+
365372
--- for Testing purposes:
366373

367374
--- adds an item to the inventory
368-
---@param item turtleSlot
375+
---@param item item
369376
---@param slot number | nil
370377
function turtleMock:addItemToInventory(item, slot)
371378
local succ, errorReason = pickUpItem(self, item, slot)
@@ -379,6 +386,10 @@ function turtleMock:getFuelLevel()
379386
return self.fuelLevel
380387
end
381388

389+
function turtleMock:getFuelLimit()
390+
return self.fuelLimit
391+
end
392+
382393
local function functionNotFoundError(key)
383394
return error("Function / Key: '" .. key .. "' not found")
384395
end
@@ -389,7 +400,9 @@ end
389400
---@param ... any
390401
---@return nil
391402
function turtleMock:print(...)
392-
return self.canPrint == true and print(...)
403+
if (self.canPrint == true) then
404+
print(...)
405+
end
393406
end
394407

395408
local mt = {

0 commit comments

Comments
 (0)