Advertisement
sci4me

CC Krist Slot Machine

Oct 30th, 2016
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.98 KB | None | 0 0
  1. os.loadAPI("json")
  2.  
  3. local payment_address = "kyrfkvj8zf"
  4.  
  5. local op_account = {
  6.     key = "29f9dd35355280c902cfb1db1ca60bbe85b450a252fccc1b61ec24185ec5748a-000",
  7.     address = "kjleqwua7g" -- old: 6vmsG8mGC&xH5+y> new:
  8. }
  9.  
  10. local monitor = peripheral.wrap("top")
  11. monitor.setTextScale(0.5)
  12.  
  13. local markedTransactions = {}
  14.  
  15. local investments = {}
  16. local transactionID = -1
  17.  
  18. local player = nil
  19. local bet = 0
  20.  
  21. local playProgress = 0
  22. local slotA = 0
  23. local slotB = 0
  24. local slotC = 0
  25.  
  26. local pot = 0
  27.  
  28. local function saveMarkedTransactions()
  29.     local fh = fs.open("markedTransactions.dat", "w")
  30.     for k, v in pairs(markedTransactions) do
  31.         fh.writeLine(tostring(k))
  32.     end
  33.     fh.flush()
  34.     fh.close()
  35. end
  36.  
  37. local function updateInvestments()
  38.     local buffer = http.get("http://krist.ceriat.net/addresses/" .. op_account.address .. "/transactions")
  39.     local data = json.decode(buffer.readAll())
  40.     buffer.close()
  41.  
  42.     if not data.ok then
  43.         error(json.encodePretty(data))
  44.     end
  45.  
  46.     for i = 1, data.count - 1 do
  47.         local transaction = data.transactions[i]
  48.        
  49.         if transaction.id > transactionID and not markedTransactions[transaction.id] and transaction.from ~= op_account.address then
  50.             markedTransactions[transaction.id] = true
  51.             saveMarkedTransactions()
  52.  
  53.             transactionID = transaction.id
  54.  
  55.             if transaction.value > 0 then
  56.                 local newValue
  57.                 if investments[transaction.from] then
  58.                     newValue = investments[transaction.from] + transaction.value
  59.                 else    
  60.                     newValue = transaction.value
  61.                 end
  62.                 investments[transaction.from] = newValue
  63.             end
  64.         end
  65.     end
  66. end
  67.  
  68. local function sendKrist(address, amount)
  69.     local buffer = http.post("http://krist.ceriat.net/transactions/", "privatekey=" .. op_account.key .. "&to=" .. address .. "&amount=" .. tostring(amount))
  70.     local data = json.decode(buffer.readAll())
  71.     buffer.close()
  72.  
  73.     if data.ok then
  74.         return true
  75.     else
  76.         return false, data
  77.     end
  78. end
  79.  
  80. local function displayNonPlaying()
  81.     monitor.setBackgroundColor(colors.yellow)
  82.     for y = 1, 11 do
  83.         monitor.write("               ")
  84.         monitor.setCursorPos(1, y)
  85.     end
  86.  
  87.     monitor.setCursorPos(1, 2)
  88.     monitor.write("  Send KST to:")
  89.     monitor.setCursorPos(1, 3)
  90.     monitor.write("   kslot.kst")
  91.     monitor.setCursorPos(1, 4)
  92.     monitor.write("    to bet!")
  93.  
  94.     monitor.setCursorPos(1, 6)
  95.     monitor.write("   Jackpot:")
  96.     monitor.setCursorPos(9 - tostring(pot):len(), 7)
  97.     monitor.write(tostring(pot))
  98. end
  99.  
  100. local function writeSlot(x, y, id)
  101.     monitor.setCursorPos(x, y)
  102.  
  103.     monitor.setBackgroundColor(colors.red)
  104.     if id == 0 then
  105.         monitor.write("\1")
  106.     elseif id == 1 then
  107.         monitor.write("\2")
  108.     elseif id == 2 then
  109.         monitor.write("\3")
  110.     elseif id == 3 then
  111.         monitor.write("\4")
  112.     end
  113. end
  114.  
  115. local function displayPlaying()
  116.     monitor.setBackgroundColor(colors.yellow)
  117.     for y = 1, 11 do
  118.         monitor.write("               ")
  119.         monitor.setCursorPos(1, y)
  120.     end
  121.  
  122.     monitor.setCursorPos(1, 1)
  123.     monitor.write("     Player:")  
  124.     monitor.setCursorPos(1, 2)
  125.     monitor.write("   " .. player)
  126.     monitor.setCursorPos(6 - tostring(bet):len(), 3)
  127.     monitor.write("Bet: " .. bet)
  128.     monitor.setCursorPos(1, 8)
  129.     monitor.write("   Jackpot:")
  130.     monitor.setCursorPos(9 - tostring(pot):len(), 9)
  131.     monitor.write(tostring(pot))
  132.  
  133.     writeSlot(6, 6, slotA)
  134.     writeSlot(8, 6, slotB)
  135.     writeSlot(10, 6, slotC)
  136. end
  137.  
  138. local function jackpot()
  139.     monitor.setBackgroundColor(colors.yellow)
  140.     for y = 1, 11 do
  141.         monitor.write("               ")
  142.         monitor.setCursorPos(1, y)
  143.     end
  144.  
  145.     monitor.setCursorPos(1, 2)
  146.     monitor.write("    Jackpot!")
  147.     monitor.setCursorPos(1, 3)
  148.     monitor.write(player .. " wins")
  149.     monitor.setCursorPos(7 - tostring(pot):len(), 4)
  150.     monitor.write(tostring(pot) .. " KST!")
  151.  
  152.     sleep(1)
  153.  
  154.     local ok, err = sendKrist(player, pot)
  155.     if not ok then print(json.encodePretty(err)) end
  156.  
  157.     pot = 0
  158. end
  159.  
  160. local function loss()
  161.     monitor.setBackgroundColor(colors.yellow)
  162.     for y = 1, 11 do
  163.         monitor.write("               ")
  164.         monitor.setCursorPos(1, y)
  165.     end
  166.  
  167.     monitor.setCursorPos(1, 4)
  168.     monitor.write("     Loss!")
  169.  
  170.     local cut = math.floor(0.1 * bet)
  171.     pot = pot - cut
  172.     sendKrist(payment_address, cut)
  173.  
  174.     sleep(1)
  175. end
  176.  
  177. local function play()
  178.     investments[player] = 0
  179.     pot = pot + bet
  180.  
  181.     playProgress = 0
  182.     while playProgress < 15 do
  183.         playProgress = playProgress + 1
  184.         slotA = math.random(1, 3)
  185.         slotB = math.random(1, 3)
  186.         slotC = math.random(1, 3)
  187.  
  188.         displayPlaying()
  189.  
  190.         sleep(0.1)
  191.     end
  192.  
  193.     sleep(1)
  194.  
  195.     if slotA == slotB and slotA == slotC then
  196.         jackpot()
  197.     else
  198.         loss()
  199.     end
  200.  
  201.     player = nil
  202.     bet = 0
  203. end
  204.  
  205. local function needsToPlay()
  206.     player = nil    
  207.     bet = 0
  208.  
  209.     for k, v in pairs(investments) do
  210.         if v > 0 then
  211.             player = k
  212.             bet = v
  213.             break
  214.         end
  215.     end
  216.  
  217.     return player and bet
  218. end
  219.  
  220. local buffer = http.get("http://krist.ceriat.net/addresses/" .. op_account.address)
  221. local data = json.decode(buffer.readAll())
  222. buffer.close()
  223.  
  224. if not data.ok then
  225.     error(json.encodePretty(data))
  226. end
  227.  
  228. pot = data.address.balance
  229.  
  230. if fs.exists("markedTransactions.dat") then
  231.     local fh = fs.open("markedTransactions.dat", "r")
  232.     local line = fh.readLine()
  233.     while line do
  234.         markedTransactions[tostring(line)] = true
  235.         line = fh.readLine()
  236.     end
  237.     fh.close()
  238. end
  239.  
  240. while true do
  241.     updateInvestments()
  242.  
  243.     if needsToPlay() then
  244.         play()
  245.     else
  246.         displayNonPlaying()
  247.     end
  248.  
  249.     sleep(1)
  250. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement