Advertisement
jtams322

Untitled

Dec 20th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. file = fs.open("database", "r")
  2.  
  3. tLines = {}
  4. line = "a"
  5. while line ~= nil do
  6. line = file.readLine()
  7. table.insert(tLines, line)
  8. end
  9.  
  10. function check(table, value1, value2)
  11. for k,v in pairs(table) do
  12. print(v)
  13. if string.find(v, value1) and string.find(v, value2) then
  14. print("Value: " .. v)
  15. balance = string.sub(v, #value1+#value2+3, #v)
  16. print("balance")
  17. print(balance)
  18. return balance
  19. end
  20. end
  21. return false
  22. end
  23.  
  24. function checkUserEx(table, value1, value2)
  25. for k,v in pairs(table) do
  26. if string.find(v, value1) and string.find(v, value2) then
  27. return true
  28. end
  29. end
  30. return false
  31. end
  32.  
  33. function codeCheck(table, code)
  34. for k,v in pairs(table) do
  35. if string.find(v, code) then
  36. balance = string.sub(v, #code+2, #v)
  37. return balance
  38. end
  39. end
  40. return false
  41. end
  42.  
  43. function deleteLine(del)
  44. for k,v in pairs(tLines) do
  45. if string.find(v, del) then
  46. tLines[k] = "USED CODE"
  47. writeData()
  48. end
  49. end
  50. end
  51.  
  52. function changeLine(content1, content2, change)
  53. for k,v in pairs(tLines) do
  54. if string.find(v, content1) and string.find(v, content2) then
  55. print("found line")
  56. tLines[k] = change
  57. writeData()
  58. return true
  59. end
  60. end
  61. return false
  62. end
  63.  
  64. function writeData()
  65. file.close()
  66. file = fs.open("database", "w")
  67. for _,v in pairs(tLines) do
  68. file.writeLine(v)
  69. end
  70. file.close()
  71. os.reboot()
  72. end
  73.  
  74. rednet.open("back")
  75. while true do
  76. id, msg = rednet.receive()
  77. if msg[1] == "newUser" then
  78. if checkUserEx(tLines, msg[2], msg[3]) then
  79. rednet.send(id, false)
  80. os.reboot()
  81. else
  82. rednet.send(id, true)
  83. print("sent true")
  84. table.insert(tLines, msg[2] .. "-" .. msg[3] .. "-" .. "0")
  85. writeData()
  86. sleep(5)
  87. os.reboot()
  88. end
  89. end
  90. if msg[1] == "newCode" then
  91. table.insert(tLines, msg[2] .. "-" .. msg[3])
  92. writeData()
  93. os.reboot()
  94. end
  95. if msg[1] == "balance" then
  96. print("Balance")
  97. newLine = msg[2] .. "-" .. msg[3] .. "-" .. msg[4]
  98. if changeLine(msg[2], msg[3], newLine) then
  99. print("CHANGED")
  100. else
  101. print(msg[2] .. "-" .. msg[3])
  102. print(msg[2] .. "-" .. msg[3] .. "-" .. msg[4])
  103. end
  104. sleep(34)
  105. end
  106. if msg[1] == "code" then
  107. print("Code")
  108. codeBal = codeCheck(tLines,msg[2])
  109. if codeBal == false then
  110. rednet.send(id, false)
  111. else
  112. rednet.send(id, codeBal)
  113. deleteLine(msg[2])
  114. end
  115. end
  116. if msg[1] == "usercred" then
  117. if msg[2] == "USED" then
  118. rednet.send(id, false)
  119. os.reboot()
  120. end
  121. print("usercred")
  122. print(msg[2].."-"..msg[3])
  123. balance = check(tLines, msg[2], msg[3])
  124. if balance == false then
  125. rednet.send(id, false)
  126. os.reboot()
  127. else
  128. rednet.send(id, balance)
  129. os.reboot()
  130. end
  131. end
  132. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement