Cobra_Tomtrein-temp-

Untitled

Jul 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. local modem = peripheral.wrap("modem")
  2. modem.open(1234)
  3.  
  4.  
  5. local pass = "testme"
  6. local shift = 6
  7. local isServer = true
  8.  
  9. function makeTable(tbl,chr)
  10. local num = string.byte(chr)
  11. local cur_size = #tbl
  12. for i=1,8 do
  13. local n_val = 0
  14. if (num % 2) == 1 then
  15. n_val = 1
  16. num = num -1
  17. end
  18. num = num / 2
  19. tbl[cur_size+9-i] = n_val
  20. end
  21. return tbl
  22. end
  23.  
  24. function dequeue(bit_table)
  25. if #bit_table == 0 then
  26. if bit_table.txt == "" then
  27. return
  28. end
  29. local chr = bit_table.txt:sub(1,1)
  30. bit_table.txt = bit_table.txt:sub(2)
  31. makeTable(bit_table,chr)
  32. end
  33. local val = bit_table[1]
  34. for i=1,#bit_table do
  35. bit_table[i] = bit_table[i+1]
  36. end
  37. return val
  38. end
  39.  
  40. function dequeueChar(bit_table)
  41. local num = 0
  42. for i=1,8 do
  43. num = num * 2
  44. local tmp = dequeue(bit_table)
  45. if not tmp then return end
  46. num = num + tmp
  47. end
  48. return string.char(num)
  49. end
  50.  
  51. function makeTest(pass)
  52. local bit_table = {}
  53. for i=1,shift do
  54. bit_table[i] = math.random(0,1)
  55. end
  56. bit_table.txt = pass
  57. local msg = ""
  58. while true do
  59. local tmp = dequeueChar(bit_table)
  60. if not tmp then break end
  61. msg = msg..tmp
  62. end
  63. return msg
  64. end
  65.  
  66. function test(msg)
  67. local bit_table = {}
  68. bit_table.txt = msg
  69. for i=1,shift do
  70. dequeue(bit_table)
  71. end
  72. local tmp = pass
  73. for i=1,#tmp do
  74. if tmp:sub(i,i) ~= dequeueChar(bit_table) then
  75. return false
  76. end
  77. end
  78. return true
  79. end
  80.  
  81. if isServer then
  82. while true do
  83. local evt,sd,c1,c2,msg = os.pullEvent("modem_message")
  84. if test(msg) then
  85. redstone.setOutput(side,not redstone.getOutput(side))
  86. end
  87. end
  88. else
  89. modem.transmit(1234,1234,makeTest(read()))
  90. end
  91.  
  92.  
  93. --print(test(makeTest()))
Advertisement
Add Comment
Please, Sign In to add comment