Advertisement
Wyvern67

Untitled

May 13th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | None | 0 0
  1. local com
  2. function prompt()
  3.     local block
  4.     local data_table
  5.  
  6.     io.write("\n")
  7.     print_help()
  8.  
  9.     while true do
  10.         io.write("> ")
  11.         input = io.read("*l")
  12.  
  13.         if input == "help" then
  14.             print_help()
  15.         elseif input == "quit" or input == "q" or input == "exit" then
  16.             break
  17.         elseif input == "create" then
  18.             com:advertise_blockchain("test")
  19.         elseif input == "new" then
  20.             block = Block()
  21.             if not block then
  22.                 error("An error has occurred: the block has not been created\n")
  23.                 os.exit()
  24.             else
  25.                 print("Your block has been succefuly created.")
  26.                 print("Add data on this block before commiting it and advertising it.\n")
  27.             end
  28.         elseif input == "data" then
  29.             if not block then
  30.                 print("You don't have any new current block to add data.")
  31.                 print("Please, create a new block to add data.\n")
  32.             else
  33.                 print_add_help()
  34.                 while true do
  35.                     io.write("current_block> ")
  36.                     data_block = io.read("*l")
  37.  
  38.                     if data_block == "show" then
  39.                         -- print data of your current block
  40.                         data_table = block:get_data()
  41.  
  42.                         if table_is_empty(data_table) then
  43.                             print("There is no data on this block yet\n")
  44.                         else
  45.                             print("\nINDEX\tDATA")
  46.                             for key,value in pairs(data_table) do
  47.                                 print(key, value)
  48.                             end
  49.                             io.write("\n")
  50.                         end
  51.                     elseif data_block == "add" then
  52.                         -- write data and put them on your block
  53.                         print("\nWrite your current Block new data:")
  54.  
  55.                         -- prompt
  56.                         io.write("current_block:add> ")
  57.  
  58.                         -- write new data
  59.                         data_add = io.read("*l")
  60.                         print("New data: " .. data_add .. "\n")
  61.  
  62.                         -- checking if the data is satisfactory to the user
  63.                         print("Put this data on my block ?")
  64.                         print("\ty/yes -- put it")
  65.                         print("\tn/no -- quit without put any new data\n")
  66.                         data_add_check = io.read("*l")
  67.  
  68.                         if data_add_check == "y" or data_add_check == "yes" then
  69.                             -- set data on this block
  70.                             block:set_data(data_add)
  71.                         elseif data_add_check == "n" or data_add_check == "no" then
  72.                             break
  73.                         else
  74.                             print("Invalid command\n")
  75.                         end
  76.                     elseif data_block == "remove" then
  77.                         -- remove a particular data you put on your block
  78.                         if table_is_empty(data_table) then
  79.                             print("You don't have any data on your block\n")
  80.                         else
  81.                             print("\nWrite the index of the data you want remove (0 to remove all data):")
  82.                             -- prompt
  83.                             io.write("current_block:remove> ")
  84.                             local index_remove = io.read("*n")
  85.  
  86.                             local table_length = table_length(data_table)
  87.                             if index_remove <= table_length then
  88.                                 if index_remove == 0 then
  89.                                     print("Remove all the data of this block ?")
  90.                                     print("\ty/yes -- remove all")
  91.                                     print("\tn/no -- quit without remove")
  92.                                     local remove_all_check = io.read("*l")
  93.                                         if remove_all_check == "y" or remove_all_check == "yes" then
  94.                                             -- remove all data on this block
  95.                                             block:remove_all()
  96.                                         elseif remove_all_check == "n" or remove_all_check == "no" then
  97.                                             break
  98.                                         else
  99.                                             print("Invalid command\n")
  100.                                         end
  101.                                 else
  102.                                     -- remove the data at the specify index
  103.                                     block:remove(index_remove)
  104.                                     print("Data at index " .. index_remove .. " have been remove\n")
  105.                                 end
  106.                             else
  107.                                 print("Invalid index.\n")
  108.                                 print("Do \"show\" to see your data with their index\n")
  109.                             end
  110.                         end
  111.                     elseif data_block == "clear" then
  112.                         -- remove all data of your block
  113.                         print("clear")
  114.                     elseif data_block == "help_add" then
  115.                         -- display the add play
  116.                         print_add_help()
  117.                     elseif data_block == "help" then
  118.                         -- display the general help
  119.                         print_help()
  120.                     elseif data_block == "quit" or data_block == "q" or data_block == "exit" then
  121.                         -- quit the add command
  122.                         break
  123.                     else
  124.                         print("Invalid command\n")
  125.                 end
  126.             end
  127.         end
  128.         elseif input == "commit" then
  129.             print("commit")
  130.         elseif input == "advertise" then
  131.             print("advertise")
  132.         elseif input == "request" then
  133.             print("request")
  134.         elseif input == "share" then
  135.             print("share")
  136.         elseif input == "validate" then
  137.             print("validate")
  138.         elseif input == "deny" then
  139.             print("deny")
  140.         elseif input == "accept" then
  141.             print("accept")
  142.         else
  143.             print("Invalid command\n")
  144.         end
  145.     end
  146. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement