Advertisement
BillBodkin

BodCoin

Oct 16th, 2021 (edited)
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | None | 0 0
  1. local bodCoinServerID = 138
  2.  
  3. function SendToServer(data)
  4.     if modemName == nil then
  5.         error("'modemName' is not defined")
  6.     end
  7.     rednet.open(modemName)
  8.     while true do
  9.         rednet.send(bodCoinServerID, data , "BodCoin")
  10.         while true do
  11.             local id, msg = rednet.receive("BodCoinResp", 2)
  12.             if not id then
  13.                 --print("Timed out")
  14.                 break
  15.             end
  16.             if id == bodCoinServerID then
  17.                 return msg
  18.             end
  19.             sleep(0)
  20.         end
  21.     end
  22. end
  23.  
  24. function GetLogin()
  25.     local file = fs.open("BodCoinLogin", "r")
  26.     if file == nil then
  27.         error("the file 'BodCoinLogin' is missing, please create and insert login details")
  28.     end
  29.     local toRet = textutils.unserialise(file.readAll())
  30.     file.close()
  31.     return toRet
  32. end
  33.  
  34. local moduleItems = {}
  35.  
  36. function GetBalance()
  37.     local getBalanceResp = SendToServer({
  38.         ["login"] = GetLogin(),
  39.         ["action"] = "getBalance"
  40.     })
  41.     if getBalanceResp["status"] == "success" then
  42.         return getBalanceResp["balance"]
  43.     end
  44.     return nil, getBalanceResp["msg"]
  45. end
  46.  
  47. moduleItems["GetBalance"] = GetBalance
  48.  
  49. function CheckAccountExists(username)
  50.     local accExistsResp = SendToServer({
  51.         ["login"] = GetLogin(),
  52.         ["action"] = "checkAccountExists",
  53.         ["username"] = username
  54.     })
  55.     if accExistsResp["status"] == "success" then
  56.         return accExistsResp["exists"]
  57.     end
  58.     return nil, accExistsResp["msg"]
  59. end
  60.  
  61. moduleItems["CheckAccountExists"] = CheckAccountExists
  62.  
  63. function SendCoins(toUsername, amount)
  64.     local sendCoinsResp =  SendToServer({
  65.         ["login"] = GetLogin(),
  66.         ["action"] = "send",
  67.         ["to"] = toUsername,
  68.         ["amount"] = amount
  69.     })
  70.     if sendCoinsResp["status"] == "success" then
  71.         return true
  72.     end
  73.     return false, sendCoinsResp["msg"]
  74. end
  75.  
  76. moduleItems["SendCoins"] = SendCoins
  77.  
  78. function RequestCoins(fromUsername, amount)
  79.     local requestCoinsResp = SendToServer({
  80.         ["login"] = GetLogin(),
  81.         ["action"] = "request",
  82.         ["from"] = fromUsername,
  83.         ["amount"] = amount
  84.     })
  85.     if requestCoinsResp["status"] == "success" then
  86.         return requestCoinsResp["requestID"]
  87.     end
  88.     return nil, requestCoinsResp["msg"]
  89. end
  90.  
  91. moduleItems["RequestCoins"] = RequestCoins
  92.  
  93. function GetRequestStatus(requestID)
  94.     local getRequestStatusResp = SendToServer({
  95.         ["login"] = GetLogin(),
  96.         ["action"] = "getRequestStatus",
  97.         ["requestID"] = requestID
  98.     })
  99.     if getRequestStatusResp["status"] == "success" then
  100.         return getRequestStatusResp["requestStatus"]
  101.     end
  102.     return nil, getRequestStatusResp["msg"]
  103. end
  104.  
  105. moduleItems["GetRequestStatus"] = GetRequestStatus
  106.  
  107. function RequestAndWaitForCoins(fromUsername, amount, timeout)
  108.     if timeout == nil then
  109.         timeout = 60
  110.     end
  111.     local requestID, requestRespError = RequestCoins(fromUsername, amount)
  112.     if requestID == nil then
  113.         error("RequestCoins failed - " .. requestRespError)
  114.     end
  115.    
  116.     for t = 1, timeout do
  117.         sleep(1)
  118.         local requestStatus, requestStatusError = GetRequestStatus(requestID)
  119.         if requestStatus == nil then
  120.             error("Failed to check request status - " .. requestStatusError)
  121.         elseif requestStatus == "allowed" then
  122.             return true
  123.         elseif requestStatus ~= "pending" then
  124.             return false, requestStatus
  125.         end
  126.     end
  127.     return false, "timeout"
  128. end
  129.  
  130. moduleItems["RequestAndWaitForCoins"] = RequestAndWaitForCoins
  131.  
  132. return moduleItems
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement