Swshop update

- Toggle lamp based on keepalive packets from the node
- Add functionality to split profits of a bought item
(Will document later)
This commit is contained in:
Anavrins
2020-05-06 03:03:49 -04:00
parent 59de8e7f63
commit bc3a48f30f

View File

@@ -45,7 +45,7 @@ local function getItemDetails(item)
t = textutils.unserialize(t)
for key, v in pairs(t) do
if v.name == item then
return key, tonumber(v.price)
return key, v
end
end
end
@@ -89,7 +89,9 @@ local function handleTransaction(transaction)
logTransaction(t, { refund = amount, reason = reason })
end
t.itemId, t.price = getItemDetails(metadata.name)
local id, item = getItemDetails(metadata.name)
t.itemId, t.price = id, tonumber(item.price)
if not t.itemId or not t.price then
print('invalid item')
logTransaction(t, { reason = 'invalid item' })
@@ -104,6 +106,15 @@ local function handleTransaction(transaction)
return refundTransaction(value, "error=Please pay the price listed on-screen.")
end
if item.shares then
local amount = t.value - (t.value % t.price)
for _, share in pairs(item.shares) do
local cut = math.floor(amount * share.rate) -- Don't include fractional Krist
print("Sent shares of "..cut.." to "..share.address)
await(k.makeTransaction, privatekey, share.address, cut, share.meta)
end
end
local count = math.floor(value / t.price)
local uid = math.random()
print(string.format('requesting %d of %s', count, t.itemId))
@@ -151,6 +162,12 @@ local function connect()
local transaction = data.transaction
handleTransaction(transaction)
end)
await(ws.subscribe, "keepalive", function(data)
local state = rs.getOutput(rsSide)
rs.setOutput(rsSide, not state)
end)
assert(success, "Failed to subscribe to event")
end