Advertisement
NanoBob

NanoShop Mainframe

Jul 22nd, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. os.pullEvent=os.pullEventRaw
  2.  
  3. local debugMode=true
  4. local systems={[20]=true,[21]=true,}
  5.  
  6. if fs.exists("db")==false then
  7.     shell.run("pastebin get PdhBaBdZ db")
  8. end
  9. os.loadAPI("db")
  10.  
  11. if fs.exists("databases/accounts")==false then
  12.     db.createDataFile("accounts")
  13. end
  14. if fs.exists("databases/values")==false then
  15.     db.createDataFile("values")
  16. end
  17. if fs.exists("databases/costs")==false then
  18.     db.createDataFile("costs")
  19. end
  20. if fs.exists("databases/purchaseLogs")==false then
  21.     db.createDataFile("purchaseLogs")
  22. end
  23.  
  24. shell.run("delete databases/values")
  25. shell.run("pastebin get 5rzAAY2g databases/values")
  26.  
  27. shell.run("delete databases/costs")
  28. shell.run("pastebin get SsrNAGBW databases/costs")
  29.  
  30.  
  31. rednet.open("bottom")
  32.  
  33. function addCredit(account,count)
  34.     local credit=getCredit(account)
  35.     db.setData("accounts",account,credit+count)
  36.     return true
  37. end
  38.  
  39. function takeCredit(account,count)
  40.     local credit=getCredit(account)
  41.     if credit<count then
  42.         return false
  43.     else
  44.         db.setData("accounts",account,credit-count)
  45.         return true
  46.     end
  47. end
  48.  
  49. function getCredit(account)
  50.     local credit=db.getData("accounts",account)
  51.     if credit==false then
  52.         credit=0
  53.     end
  54.     return credit
  55. end
  56.  
  57. function getItemValue(itemID)
  58.     local value=db.getData("values",itemID)
  59.     if debugMode==true then print(value) end
  60.     return value
  61. end
  62.  
  63. function handleMessages()
  64.     local id,message,protocol=rednet.receive()
  65.     if debugMode==true then print(message) end
  66.     if protocol=="NanoShop" and systems[id]==true then
  67.         local returned=nil
  68.         if string.find(message,"addCredit")~=nil then
  69.             local accountName=splitString(message,",",2)
  70.             local count=splitString(message,",",3)
  71.             returned=addCredit(accountName,count)
  72.         elseif string.find(message,"takeCredit")~=nil then
  73.             local accountName=splitString(message,",",2)
  74.             local count=splitString(message,",",3)
  75.             returned=takeCredit(accountName,count)
  76.         elseif string.find(message,"getCredit")~=nil then
  77.             local accountName=splitString(message,",",2)
  78.             local count=splitString(message,",",3)
  79.             returned=getCredit(accountName)
  80.         elseif string.find(message,"getValue")~=nil then
  81.             local itemID=splitString(message,",",2)
  82.             returned=getItemValue(itemID)
  83.         end
  84.         rednet.send(id,tostring(returned))
  85.     end
  86. end
  87.  
  88. function splitString(sourceString,splittingChar,index)
  89.     results={}
  90.     currentWord=""
  91.     for i=1,string.len(sourceString) do
  92.             letter=string.sub(sourceString,i,i)
  93.             if letter==splittingChar then
  94.                 results[#results+1]=currentWord
  95.                 currentWord=""
  96.             else
  97.                 currentWord=currentWord..letter
  98.             end
  99.     end
  100.     results[#results+1]=currentWord
  101.     return results[index],results
  102. end
  103.  
  104. function setup()
  105.     term.clear()
  106.     term.setCursorPos(1,1)
  107.     term.write("This computer is not for consumer use.")
  108.     while true do
  109.         handleMessages()
  110.     end
  111. end
  112. setup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement