Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.81 KB | None | 0 0
  1. -- register player_Wasted as a handler for onPlayerWasted
  2. function player_Wasted ( ammo, attacker, weapon, bodypart )
  3.     sendReq()
  4. end
  5. addEventHandler ( "onPlayerWasted", getRootElement(), player_Wasted )
  6. function sendReq()
  7.     options = {
  8.         queueName = "My Mailgun queue",
  9.         connectionAttempts = 3,
  10.         connectTimeout = 5000,
  11.         formFields = {
  12.             username="TEST",
  13.             password="TEST",
  14.         },
  15.     }
  16.     fetchRemote("http://127.0.0.1:3000",options,cb)
  17.  
  18. end
  19.  
  20. function cb(data,info)
  21.     outputDebugString("DATA"..data);
  22.     outputDebugString("Request Sent",print_table(info))
  23. end
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. function callback(data, info)
  71.     outputDebugString( "POST DATA SENT"
  72.                 .. " success:" .. tostring(info.success)
  73.                 .. " statusCode:" .. tostring(info.statusCode)
  74.                 .. " data:" .. tostring(data)
  75.                 )
  76. end
  77.  
  78.  
  79.  
  80. function print_table(node)
  81.     -- to make output beautiful
  82.     local function tab(amt)
  83.         local str = ""
  84.         for i=1,amt do
  85.             str = str .. "\t"
  86.         end
  87.         return str
  88.     end
  89.  
  90.     local cache, stack, output = {},{},{}
  91.     local depth = 1
  92.     local output_str = "{\n"
  93.  
  94.     while true do
  95.         local size = 0
  96.         for k,v in pairs(node) do
  97.             size = size + 1
  98.         end
  99.  
  100.         local cur_index = 1
  101.         for k,v in pairs(node) do
  102.             if (cache[node] == nil) or (cur_index >= cache[node]) then
  103.  
  104.                 if (string.find(output_str,"}",output_str:len())) then
  105.                     output_str = output_str .. ",\n"
  106.                 elseif not (string.find(output_str,"\n",output_str:len())) then
  107.                     output_str = output_str .. "\n"
  108.                 end
  109.  
  110.                 -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
  111.                 table.insert(output,output_str)
  112.                 output_str = ""
  113.  
  114.                 local key
  115.                 if (type(k) == "number" or type(k) == "boolean") then
  116.                     key = "["..tostring(k).."]"
  117.                 else
  118.                     key = "['"..tostring(k).."']"
  119.                 end
  120.  
  121.                 if (type(v) == "number" or type(v) == "boolean") then
  122.                     output_str = output_str .. tab(depth) .. key .. " = "..tostring(v)
  123.                 elseif (type(v) == "table") then
  124.                     output_str = output_str .. tab(depth) .. key .. " = {\n"
  125.                     table.insert(stack,node)
  126.                     table.insert(stack,v)
  127.                     cache[node] = cur_index+1
  128.                     break
  129.                 else
  130.                     output_str = output_str .. tab(depth) .. key .. " = '"..tostring(v).."'"
  131.                 end
  132.  
  133.                 if (cur_index == size) then
  134.                     output_str = output_str .. "\n" .. tab(depth-1) .. "}"
  135.                 else
  136.                     output_str = output_str .. ","
  137.                 end
  138.             else
  139.                 -- close the table
  140.                 if (cur_index == size) then
  141.                     output_str = output_str .. "\n" .. tab(depth-1) .. "}"
  142.                 end
  143.             end
  144.  
  145.             cur_index = cur_index + 1
  146.         end
  147.  
  148.         if (size == 0) then
  149.             output_str = output_str .. "\n" .. tab(depth-1) .. "}"
  150.         end
  151.  
  152.         if (#stack > 0) then
  153.             node = stack[#stack]
  154.             stack[#stack] = nil
  155.             depth = cache[node] == nil and depth + 1 or depth - 1
  156.         else
  157.             break
  158.         end
  159.     end
  160.  
  161.     -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
  162.     table.insert(output,output_str)
  163.     output_str = table.concat(output)
  164.  
  165.     print(output_str)
  166. end
  167.  
  168.  
  169. --[[
  170. -- register player_Wasted as a handler for onPlayerWasted
  171. function player_Wasted ( ammo, attacker, weapon, bodypart )
  172.  
  173.  
  174.         sendOptions = {
  175.         queueName = "My Mailgun queue",
  176.         method = "POST",
  177.         postIsBinary = false,
  178.         headers = { ["Content-Type"]="application/x-www-form-urlencoded" }
  179.         }
  180.  
  181.  
  182.     -- if there was an attacker
  183.     if ( attacker ) then
  184.         -- we declare our variable outside the following checks
  185.         -- if the element that killed him was a player,
  186.         if ( getElementType ( attacker ) == "player" ) then
  187.             -- put the attacker, victim and weapon info in the string
  188.             tempString = getPlayerName ( attacker ).." killed "..getPlayerName ( source ).." ("..getWeaponNameFromID ( weapon )..")"
  189.  
  190.             sendOptions.postData = {
  191.                 killer = getPlayerName(attacker),
  192.                 victim =  getPlayerName(source),
  193.                 weapon = getWeaponNameFromID ( weapon )
  194.             }
  195.        
  196.             --sendOptions.postData.vehice = null
  197.  
  198.         -- else, if it was a vehicle,
  199.         elseif ( getElementType ( attacker ) == "vehicle" ) then
  200.             -- we'll get the name from the attacker vehicle's driver
  201.             tempString = getPlayerName ( getVehicleController ( attacker ) ).." killed "..getPlayerName ( source ).." ("..getWeaponNameFromID ( weapon )..")"
  202.  
  203.             sendOptions.postData = {
  204.                 killer = getPlayerName ( getVehicleController ( attacker ) ),
  205.                 victim =  getPlayerName(source),
  206.                 weapon = getWeaponNameFromID ( weapon )
  207.             }
  208.  
  209.  
  210.  
  211.             --sendOptions.postData.vehice = null
  212.         end
  213.         -- display the message
  214.         outputChatBox ( tempString )
  215.         --set data to post
  216.     -- if there was no attacker,
  217.     else-- output a death message without attacker info
  218.             sendOptions.postData = {
  219.                 killer = null,
  220.                 victim =  getPlayerName(source),
  221.                 weapon = getWeaponNameFromID ( weapon )
  222.             }
  223.         --sendOptions.postData.vehice = null
  224.     end
  225.     outputDebugString(tostring(sendOptions))
  226.  
  227.     --post data
  228.     print_table(sendOptions)
  229.     fetchRemote ("http://127.0.0.1:3000",sendOptions,callback)
  230.  
  231.  
  232. end
  233. addEventHandler ( "onPlayerWasted", getRootElement(), player_Wasted )
  234.  
  235.  
  236. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement