Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1.  
  2. JSON = (loadfile "JSON.lua")() -- one-time load of the routines
  3. dofile('private_api_keys.lua')
  4.  
  5. print(PRIVATE_HYPEMAN_BOT_CLIENT_ID)
  6. print(PRIVATE_HYPEMAN_CHANNEL_ID)
  7.  
  8. local BOT_CLIENT_ID = 'Bot 111'
  9. local CHANNEL_ID = '333'
  10.  
  11. local CQ_BOT_ID = 'Bot 222'
  12. local CQ_BOT_CHANNEL_ID = '444'
  13.  
  14. local privmsgid = PRIVATE_COMMAND_ID
  15.  
  16. local PORT = 10081
  17. local HOST = '0.0.0.0'
  18. local ServerName = 'Rob Rulez!!!'
  19.  
  20. local dgram = require('dgram')
  21. local discordia = require('discordia')
  22.  
  23. local client = discordia.Client()
  24. local ch = nil
  25.  
  26. local cqbot = discordia.Client()
  27. local cqch = nil
  28.  
  29.  
  30. function tableHasKey(table,key)
  31. return table[key] ~= nil
  32. end
  33.  
  34. client:on('ready', function()
  35. print('Logged in as '.. client.user.username)
  36. ch = client:getChannel(CHANNEL_ID)
  37. ch:send('CAG is headed up to the CIC to monitor operations.')
  38. end)
  39.  
  40. cqbot:on('ready', function()
  41. print('Logged in as '.. cqbot.user.username)
  42. cqch = cqbot:getChannel(CQ_BOT_CHANNEL_ID)
  43. cqch:send('Paddles is headed up to the LSO-platform.')
  44. end)
  45.  
  46. --local function has_value (tab, val)
  47. -- for index, value in ipairs(tab) do
  48. -- if value == val then
  49. -- return true
  50. -- end
  51. -- end
  52. --
  53. -- return false
  54. --end
  55.  
  56. client:on('messageCreate', function(message)
  57.  
  58. if message.content == '!connect' and message.author.id == privmsgid and message.channel.guild == nil then
  59. print('Message was !connect received')
  60. message.channel:send('connecting to voice comms.')
  61. ConnectVoice()
  62. return
  63. end
  64.  
  65. if message.content == '!disconnect' and message.author.id == privmsgid and message.channel.guild == nil then
  66. message.channel:send('disconnecting from voice comms.')
  67. DisconnectVoice()
  68. return
  69. end
  70.  
  71. if message.author.id == privmsgid and message.channel.guild == nil then
  72. print('Creating Message '..message.content)
  73. CreateVoiceMp3(message.content)
  74. end
  75.  
  76. if id == CHANNEL_ID then
  77. local content = message.content
  78. if content == '!info' or content == '!about' or content == '!hypeman' then
  79. message.channel:send('HypeMan is an experimental Discord bot to announce Digital Combat Simulator (DCS) game events to Discord. See https://aggressors.ca/hypeman for more information')
  80. end
  81. end
  82. end)
  83.  
  84. client:run(BOT_CLIENT_ID)
  85. cqbot:run(CQ_BOT_ID)
  86.  
  87. local s2 = dgram.createSocket('udp4')
  88.  
  89. p('PORT',PORT)
  90. s2:bind(PORT,HOST)
  91.  
  92. -- local s1 = dgram.createSocket('udp4')
  93. -- s1:bind(PORT,HOST)
  94. -- s1:send('HELLO', PORT+1, '127.0.0.1')
  95.  
  96. local function starts_with(str, start)
  97. return str:sub(1, #start) == start
  98. end
  99.  
  100. -- * *Name*: The player name.
  101. -- * *Pass*: A running number counting the passes of the player
  102. -- * *Points Final*: The final points (i.e. when the player has landed). This is the average over all previous bolters or waveoffs, if any.
  103. -- * *Points Pass*: The points of each pass including bolters and waveoffs.
  104. -- * *Grade*: LSO grade.
  105. -- * *Details*: Detailed analysis of deviations within the groove.
  106. -- * *Wire*: Trapped wire, if any.
  107. -- * *Tgroove*: Time in the groove in seconds (not applicable during Case III).
  108. -- * *Case*: The recovery case operations in progress during the pass.
  109. -- * *Wind*: Wind on deck in knots during approach.
  110. -- * *Modex*: Tail number of the player.
  111. -- * *Airframe*: Aircraft type used in the recovery.
  112. -- * *Carrier Type*: Type name of the carrier.
  113. -- * *Carrier Name*: Name/alias of the carrier.
  114. -- * *Theatre*: DCS map.
  115. -- * *Mission Time*: Mission time at the end of the approach.
  116. -- * *Mission Date*: Mission date in yyyy/mm/dd format.
  117. -- * *OS Date*: Real life date from os.date(). Needs **os** to be desanitized.
  118.  
  119. local function getCaseString(mygrade)
  120.  
  121. local caseNumber = mygrade.case
  122.  
  123. if caseNumber == 1 then
  124. return '(CASE I)'
  125. elseif caseNumber == 2 then
  126. return '(CASE II)'
  127. elseif caseNumber == 3 then
  128. return '(CASE III)'
  129. else
  130. return ''
  131. end
  132. end
  133.  
  134. local function getWireString(mygrade)
  135. local mywire = mygrade.wire
  136.  
  137. if mywire == nil then
  138. return 'no wire'
  139. end
  140.  
  141. if mywire == 1 then
  142. return '1-wire'
  143. elseif mywire == 2 then
  144. return '2-wire'
  145. elseif mywire == 3 then
  146. return '3-wire'
  147. elseif mywire == 4 then
  148. return '4-wire'
  149. else
  150. return 'no wire'
  151. end
  152. end
  153.  
  154. --mygrade = {}
  155. --mygrade.grade = '_OK_'
  156. --mygrade.points = 3.0
  157. --mygrade.finalscore = 2.5
  158. --mygrade.details = 'LOL HIM XAR'
  159. --mygrade.wire=3
  160. --mygrade.Tgroove = 16.6
  161. --mygrade.case = 1
  162. --mygrade.wind= 25
  163. --mygrade.modex = 300
  164. --mygrade.airframe = 'F/A-18C hornet'
  165. --mygrade.carriertype = 'CVN99'
  166. --mygrade.carriername = 'HMCS Don Cherry'
  167. --mygrade.theatre = 'Persian Gulf'
  168. --mygrade.mitime = '01:02:03+1'
  169. --mygrade.midate= '1999/03/24'
  170. --mygrade.osdate = '2019/09/10 01:02:03'
  171.  
  172. -- this function was called wrap in quotes because it originally wrapped
  173. -- any value in quotes. But now it joins values with a , but the name is kept.
  174. local function wiq(str)
  175. return str..', '
  176. end
  177.  
  178. local function getCsvString(mygrade)
  179. -- This function generates the CSV row that gets sent to google sheet.
  180. -- Every value gets wiq'd (wrapped in quotes (a single quote) )
  181. -- The google sheet upload is handled by a python script that uploads the CSV string
  182. local my_string = wiq(mygrade.name)
  183. my_string = my_string .. wiq( mygrade.grade)
  184. my_string = my_string .. wiq( mygrade.points)
  185. my_string = my_string .. wiq( mygrade.finalscore)
  186. my_string = my_string .. wiq( mygrade.details)
  187. my_string = my_string .. wiq( mygrade.wire)
  188. my_string = my_string .. wiq( mygrade.Tgroove)
  189. my_string = my_string .. wiq( mygrade.case)
  190. my_string = my_string .. wiq( mygrade.wind)
  191. my_string = my_string .. wiq( mygrade.modex)
  192. my_string = my_string .. wiq( mygrade.airframe)
  193. my_string = my_string .. wiq( mygrade.carriertype)
  194. my_string = my_string .. wiq( mygrade.carriername)
  195. my_string = my_string .. wiq( mygrade.theatre)
  196. my_string = my_string .. wiq( mygrade.mitime)
  197. my_string = my_string .. wiq(mygrade.midate)
  198. my_string = my_string .. ' Server: ' .. ServerName
  199. --my_string = my_string .. wiq( mygrade.osdate)
  200. return my_string
  201. end
  202.  
  203. local function getGradeString(mygrade)
  204. -- This is the function that formats the grade string that will get sent to Discord reporting the grade.
  205. -- Example: Rob, OK, 3.0 PT, H_LUL_X _SLO_H_LUL_IM SLOLOLULIC LOAR, 3-wire, groove time 17.0 seconds, (CASE I)
  206. print ('LSO Grade, '.. mygrade.name .. ' trapped, sending to Discord')
  207. local msg_string = mygrade.name .. ', ' .. mygrade.grade .. ', ' .. mygrade.points .. ' PT, ' .. mygrade.details .. ', ' .. getWireString(mygrade) .. ', groove time ' .. mygrade.Tgroove .. ' seconds' .. ', ' .. mygrade.airframe .. ', ' .. getCaseString(mygrade)
  208. return msg_string
  209. end
  210.  
  211. local function f(msg)
  212.  
  213. local lua_table = JSON:decode(msg)
  214.  
  215. if lua_table['messageType'] ~= nil then
  216. local msg_id = lua_table['messageType']
  217.  
  218. if msg_id == 1 then
  219. -- print the message
  220. if ch ~= nil then
  221. local msg_string = lua_table['messageString']
  222.  
  223. if msg_string ~= nil then
  224. ch:send(msg_string)
  225. end
  226. end
  227.  
  228. elseif msg_id == 2 then
  229. -- AIRBOSS GRADE IS messageType = 2
  230.  
  231. if cqch ~= nil then
  232. -- cqch:send('MessageType = 2')
  233. local msg_string = getGradeString(lua_table)
  234. print(msg_string)
  235. --cqch:send(msg)
  236. cqch:send(msg_string)
  237. local msg2 = getCsvString(lua_table)
  238. local execString = "\".\\gsheet_upload.bat " .. "\"" .. msg2 .. "\"\""
  239. print(execString)
  240. io.popen(execString,'w')
  241. end
  242.  
  243. elseif msg_id == 3 then
  244. -- PILOT FLIGHT TIME MESSAGE?
  245.  
  246. elseif msg_id == 4 then
  247. -- WEAPON RELEASE MESSAGE?
  248.  
  249. elseif msg_id == 5 then
  250. -- HIT OR DAMAGE MESSAGE?
  251. end
  252. end
  253.  
  254.  
  255. end
  256.  
  257. --local msg2 = "TG, (OK), 3.0 PT, F(LOLUR)X F(LOLUR)IM (F)IC , 1-wire, groove time 22.0 seconds, (CASE I)"
  258. --local execString = "\".\\gsheet_upload.bat " .. "\"" .. msg2 .. "\"\""
  259. --print(execString)
  260. --pcall(io.popen(execString,'w'))
  261.  
  262. s2:on('message', function(msg, rinfo)
  263. -- local t = os.date()
  264. p('Message received: ')
  265. p(msg)
  266.  
  267. if ch ~= nil then
  268. botSendMessage = coroutine.wrap (f)
  269. botSendMessage(msg)
  270. end
  271. -- nilling the message here. Somehow the message was getting appended to and growing? not sure how or why that happened
  272. -- msg = nil
  273. -- Seemed to only be due to time acceleration in DCS? I don't know.
  274. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement