Advertisement
Doob

[OpenComputers] warehouse

Sep 17th, 2016
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.66 KB | None | 0 0
  1. local component = require("component")
  2. local ser = require("serialization")
  3. local event = require("event")
  4. local deb = component.debug
  5. local mod = component.modem
  6. local wName, plName = "warehouse.tbl", "up.tbl"
  7. local tWarehouse = {
  8.   -- [PlayerName] = {[ItemName:ItemMeta] = Amount}
  9. }
  10.  
  11. local tUP = {
  12.   -- [PlayerName] = Password
  13. }
  14.  
  15. local function sL(tbl, f)
  16.   local file = io.open(f, 'w')
  17.   file:write(ser.serialize(tbl))
  18.   file:close()
  19. end
  20.  
  21. local function lL(f)
  22.   local file = io.open(f, 'r')
  23.   if not file then
  24.     file = io.open(f, 'a')
  25.   else
  26.     return ser.unserialize(file:read('*a'))
  27.   end
  28.   file:close()
  29. end
  30.  
  31. local function wList(PlayerName) -- получение информации о хранилище
  32.   return tWarehouse[PlayerName]
  33. end
  34. ----------------------------------------------------------
  35. local function getPassword(PlayerName) -- получение пароля к хранилищу
  36.   local ps = ""
  37.   for i = 1, 16 do
  38.     ps = ps..string.char(math.random(33, 126))
  39.   end
  40.   tUP[PlayerName] = ps
  41.   deb.runCommand("give "..PlayerName.." minecraft:paper 1 0 {display:{Name:"..ps.."}}")
  42. end
  43.  
  44. local function DEL(PlayerName, ItemName, ItemMeta, Amount) -- отнимание предмета у игрока
  45.   local s, f = deb.runCommand("clear "..PlayerName.." "..ItemName.." "..ItemMeta.." "..Amount)
  46.   if s == 1 then
  47.     return tonumber(f:sub(37+#PlayerName):match("[0-9+]"))
  48.   else
  49.     return false
  50.   end
  51. end
  52.  
  53. local function ADD(PlayerName, ItemName, ItemMeta, Amount) -- добавление предмета игроку
  54.   if Amount <= 64 then
  55.     if deb.runCommand("give "..PlayerName.." "..ItemName.." "..Amount..""..ItemMeta) == 1 then
  56.       return true
  57.     else
  58.       return false
  59.     end
  60.   else
  61.     return false
  62.   end
  63. end
  64. ----------------------------------------------------------
  65. local actions = {
  66.   ["PtW"] = function(PlayerName, ItemName, ItemMeta, Amount) -- перемещение предмета из инвентаря в хранилище
  67.     local getA = DEL(PlayerName, ItemName, ItemMeta, Amount)
  68.     if getA then
  69.       if not tWarehouse[PlayerName] then
  70.         tWarehouse[PlayerName] = {}
  71.       end
  72.       if not tWarehouse[PlayerName][ItemName .."@".. ItemMeta] then
  73.         tWarehouse[PlayerName][ItemName .."@".. ItemMeta] = getA
  74.       else
  75.         tWarehouse[PlayerName][ItemName .."@".. ItemMeta] = tWarehouse[PlayerName][ItemName .."@".. ItemMeta] + getA
  76.       end
  77.     end
  78.   end,
  79.  
  80.   ["WtP"] = function(PlayerName, ItemName, ItemMeta, Amount) -- перемещение предмета из хранилища в инвентарь
  81.     if tWarehouse[PlayerName] then
  82.       local getA = tWarehouse[PlayerName][ItemName .."@".. ItemMeta]
  83.       if getA then
  84.         if getA >= Amount then
  85.           ADD(PlayerName, ItemName, ItemMeta, Amount)
  86.         else
  87.           ADD(PlayerName, ItemName, ItemMeta, getA)
  88.         end
  89.       end
  90.     end
  91.   end,
  92.  
  93.   ["PtP"] = function(PlayerS, PlayerT, ItemName, ItemMeta, Amount) -- перемещение предмета от одного игрока другому
  94.     local getA = DEL(PlayerS, ItemName, ItemMeta, Amount)
  95.     ADD(PlayerT, ItemName, ItemMeta, Amount)
  96.   end,
  97.  
  98.   ["WtW"] = function(PlayerS, PlayerT, ItemName, ItemMeta, Amount) -- перемещение предмета из хранилища одного игрока в хранилище другого
  99.     if tWarehouse[PlayerS] then
  100.       if not tWarehouse[PlayerT] then
  101.         tWarehouse[PlayerT] = {}
  102.       end
  103.       if not tWarehouse[PlayerT][ItemName .."@".. ItemMeta] then
  104.         tWarehouse[PlayerT][ItemName .."@".. ItemMeta] = 0
  105.       end
  106.       local getA = tWarehouse[PlayerS][ItemName .."@".. ItemMeta]
  107.       if getA then
  108.         if getA >= Amount then
  109.           tWarehouse[PlayerS][ItemName .."@".. ItemMeta] = tWarehouse[PlayerS][ItemName .."@".. ItemMeta] - Amount
  110.           tWarehouse[PlayerT][ItemName .."@".. ItemMeta] = tWarehouse[PlayerT][ItemName .."@".. ItemMeta] + Amount
  111.         else
  112.           tWarehouse[PlayerS][ItemName .."@".. ItemMeta] = nil
  113.           tWarehouse[PlayerT][ItemName .."@".. ItemMeta] = tWarehouse[PlayerT][ItemName .."@".. ItemMeta] + getA
  114.         end
  115.       end
  116.     end
  117.   end
  118. }
  119.  
  120. mod.open(999)
  121. lL(wName)
  122. lL(plName)
  123. -- password\0action\0player ...
  124. while true do
  125.   local tbl = {}
  126.   local e = {event.pull()}
  127.   if e[1] == "modem_message" then
  128.     for i in e[6]:gmatch("[^\0]+") do table.insert(tbl, i) end
  129.     if tUP[tbl[3]] == tbl[1] then
  130.       actions[tbl[2]](table.unpack(tbl, 3))
  131.     end
  132.   elseif e[1] == "touch" then
  133.     getPassword(e[6])
  134.   elseif e[1] == "key_down" then
  135.     sL(tWarehouse, wName)
  136.     sL(tUP, plName)
  137.     os.exit()
  138.   end
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement