mvd.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. --[[
  2. LUA MVD script for the extended hifi-q2admin (https://github.com/hifi/q2admin/)
  3. ------------------------------
  4. Copyright (C) 2012 Paul Klumpp
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ------------------------------
  16. Function:
  17. Automatically starts recording of Q2Pro Multi View Demos within the Action Quake2 - TNG Mod when match starts.
  18. It also deletes the mvd-file if match countdown failed.
  19. Fully working initial version by Paul Klumpp, 2012-11-12
  20. Please record your big changes here and increase version number:
  21. 1.6: ability to give cvars as parameters to the exec_script
  22. 1.5: exchanged all "say" with bprintf or dprintf
  23. 1.4: added round_state check, added whether a cvar "q2a_mvd_autorecord" needs to be set so the recording happens runs (for lrcon usage)
  24. 1.3: added "exec_script_on_system_after_recording" for config.lua. BE CAREFUL with those shellscripts!
  25. 1.2: Checks for (Action Quake 2 and sv_mvd_enable != 0). And added useful mvd defaults for scoreboard
  26. 1.1: Even works on aq2-tng public teamplay mode now
  27. 1.0: All working on aq2-tng matchmode
  28. 0.9: Last Debug
  29. config.lua example, with all options:
  30. ------------------------------
  31. plugins = {
  32. some_other_plugin = {
  33. blah = "blah"
  34. },
  35. mvd = {
  36. mvd_webby = 'http://mvd2.quadaver.org',
  37. exec_script_on_system_after_recording = '/home/gameservers/quake2/plugins/woot.sh',
  38. exec_script_cvars_as_parameters = {q2a_mvd_file, game, hostname},
  39. needs_cvar_q2a_mvd_autorecord = false
  40. }
  41. }
  42. ------------------------------
  43. !! Notice the "," in front of mvd = {
  44. --]]
  45. local game = gi.cvar("game", "").string
  46. local sv_mvd_enable = gi.cvar("sv_mvd_enable", "").string
  47. if game ~= "action" or sv_mvd_enable == "0" or sv_mvd_enable == "" or sv_mvd_enable == nil then
  48. gi.dprintf("mvd.lua WARNING: This script only loads when game is 'action' and when sv_mvd_enable is '1' or '2'!\n")
  49. return 0
  50. end
  51. -- if we came to here, it's action!
  52. local version = "1.6hau"
  53. gi.AddCommandString("sets q2a_mvd "..version.."\n")
  54. local mvd_webby -- configure this one in the config.lua
  55. local exec_script_on_system_after_recording -- configure this one in the config.lua
  56. local exec_script_cvars_as_parameters -- configure this one in the config.lua
  57. local needs_cvar_q2a_mvd_autorecord -- configure this one in the config.lua
  58. -- state vars
  59. local teams_ready = {}
  60. local mvd_records = false
  61. local started_on_round_state = ""
  62. local mvd_pathfile = ""
  63. local mvd_file = ""
  64. gi.cvar_set("q2a_mvd_file", mvd_file)
  65. ---- small helper functions follow: ----
  66. function file_exists(fname)
  67. local f=io.open(fname,"r")
  68. if f~=nil then
  69. io.close(f)
  70. return true
  71. else
  72. return false
  73. end
  74. end
  75. function string_strip(badstring)
  76. goodstring = badstring
  77. goodstring = string.gsub(goodstring, "%W", "")
  78. return goodstring
  79. end
  80. function reset_ready_state()
  81. for k in pairs (teams_ready) do
  82. teams_ready[k] = nil
  83. end
  84. end
  85. function get_round_state()
  86. local teamplay = gi.cvar("teamplay", "").string
  87. if teamplay == "1" then
  88. local cvar_t1 = gi.cvar("t1", "").string
  89. local cvar_t2 = gi.cvar("t2", "").string
  90. local cvar_t3 = gi.cvar("t3", "").string
  91. local rstate = ""..cvar_t1.." "..cvar_t2.." "..cvar_t3..""
  92. return rstate
  93. end
  94. return 0
  95. end
  96. ---- big helper functions follow: ----
  97. function mvd_os_exec(script)
  98. gi.dprintf('mvd.lua mvd_os_exec(): '..script..'\n')
  99. local returnstuff = os.execute(script)
  100. gi.dprintf('mvd.lua mvd_os_exec(): returns: '..returnstuff..'\n')
  101. return returnstuff
  102. end
  103. function mvd_start_recording()
  104. -- if teamplay == 1 then save round state .. for later usage -
  105. -- this is to know if record stopping also can delete the newly recorded mvd, because the rounds did not happen.
  106. started_on_round_state = get_round_state()
  107. -- build filename
  108. local now = os.date("%Y-%m-%d_%H%M%S")
  109. local mapname = gi.cvar("mapname", "").string
  110. local filename = now
  111. for k,v in pairs(teams_ready) do
  112. filename = filename .. "_" .. v
  113. --gi.AddCommandString("say starting team: "..v.."\n")
  114. end
  115. filename = filename.."_"..mapname
  116. gi.AddCommandString("mvdrecord -z "..filename.."\n")
  117. mvd_file = filename..".mvd2.gz"
  118. gi.cvar_set("q2a_mvd_file", mvd_file)
  119. mvd_pathfile = game.."/demos/"..mvd_file
  120. mvd_records = true
  121. local hostname = gi.cvar("hostname", "").string
  122. gi.bprintf(PRINT_CHAT, "MVD: '%s' MVD2 recording started: %s\n", hostname, mvd_file)
  123. end
  124. function mvd_stop_and_delete()
  125. local current_round_state = get_round_state()
  126. if mvd_records == true then
  127. if current_round_state == started_on_round_state then
  128. gi.AddCommandString("mvdstop\n")
  129. mvd_records = false
  130. -- if file exists in game dir, delete it
  131. --$game demos/lala.mvd2.gz
  132. if file_exists(mvd_pathfile) then
  133. if os.remove(mvd_pathfile) then
  134. gi.bprintf(PRINT_HIGH, 'Deleted the MVD2: %s\n', mvd_file)
  135. else
  136. gi.dprintf('mvd.lua mvd_stop_and_delete(): Problems deleting MVD2: %s\n', mvd_file)
  137. end
  138. end
  139. mvd_pathfile = ""
  140. mvd_file = ""
  141. gi.cvar_set("q2a_mvd_file", mvd_file)
  142. else
  143. gi.bprintf(PRINT_CHAT, 'MVD: Be quick to ready up again! MVD2 is still recording!\n')
  144. end
  145. end
  146. end
  147. function mvd_test()
  148. mvd_file = "lala.mvd.tgz"
  149. gi.cvar_set("q2a_mvd_file", mvd_file)
  150. if exec_script_on_system_after_recording ~= nil then
  151. if exec_script_cvars_as_parameters ~= nil then
  152. gi.dprintf('drin(): %s\n', exec_script_cvars_as_parameters)
  153. local exec_str = ""
  154. for k in ipairs(exec_script_cvars_as_parameters) do
  155. gi.dprintf('lala: %s\n', k)
  156. kstr = string_strip(gi.cvar(k, "").string)
  157. exec_str = exec_str..' "'..kstr..'"'
  158. end
  159. mvd_os_exec(exec_script_on_system_after_recording..exec_str)
  160. else
  161. mvd_os_exec(exec_script_on_system_after_recording..' "'..game..'" "'..mvd_file..'"')
  162. end
  163. end
  164. end
  165. function mvd_stop()
  166. if mvd_records == true then
  167. gi.dprintf('mvd.lua mvd_stop(): stopping MVD recording...\n')
  168. gi.AddCommandString("mvdstop\n")
  169. mvd_records = false
  170. -- if file exists in game dir, advertise it:
  171. if file_exists(mvd_pathfile) then
  172. --$game demos/lala.mvd2.gz
  173. if mvd_webby ~= nil then
  174. gi.bprintf(PRINT_CHAT, 'MVD: Download the MVD2(%s) - %s\n', mvd_file, mvd_webby)
  175. else
  176. gi.bprintf(PRINT_CHAT, 'MVD: Download the MVD2: %s\n', mvd_file)
  177. end
  178. if exec_script_on_system_after_recording ~= nil then
  179. mvd_os_exec(exec_script_on_system_after_recording..' "'..game..'" "'..mvd_file..'"')
  180. end
  181. end
  182. mvd_pathfile = ""
  183. mvd_file = ""
  184. gi.cvar_set("q2a_mvd_file", mvd_file)
  185. end
  186. end
  187. ---- q2admin hook functions follow: ----
  188. function q2a_load(config)
  189. gi.dprintf("mvd.lua q2a_load(): "..version.." for Action Quake 2 loaded.\n")
  190. mvd_webby = config.mvd_webby
  191. exec_script_on_system_after_recording = config.exec_script_on_system_after_recording
  192. exec_script_cvars_as_parameters = config.exec_script_cvars_as_parameters
  193. needs_cvar_q2a_mvd_autorecord = config.needs_cvar_q2a_mvd_autorecord
  194. if mvd_webby == nil then
  195. gi.dprintf("mvd.lua q2a_load(): You may define 'mvd_webby' in the config.lua file.\n")
  196. -- safe default:
  197. mvd_webby = nil
  198. end
  199. if exec_script_on_system_after_recording == nil then
  200. gi.dprintf("mvd.lua q2a_load(): You may define 'exec_script_on_system_after_recording' in the config.lua file.\n")
  201. -- safe default:
  202. exec_script_on_system_after_recording = nil
  203. end
  204. if exec_script_cvars_as_parameters == nil then
  205. gi.dprintf("mvd.lua q2a_load(): You may define 'exec_script_cvars_as_parameters' in the config.lua file.\n")
  206. -- safe default:
  207. exec_script_cvars_as_parameters = nil
  208. end
  209. if needs_cvar_q2a_mvd_autorecord == nil then
  210. gi.dprintf("mvd.lua q2a_load(): You may define 'needs_cvar_q2a_mvd_autorecord' in the config.lua file.\n")
  211. -- safe default:
  212. needs_cvar_q2a_mvd_autorecord = false
  213. end
  214. -- set some working defaults (workarounds) if game is action...
  215. if game == "action" then
  216. gi.AddCommandString('set sv_mvd_nomsg 0\n')
  217. gi.AddCommandString('set sv_mvd_begincmd "putaway; h_cycle;"\n')
  218. gi.AddCommandString('set sv_mvd_scorecmd "h_cycle;"\n')
  219. gi.AddCommandString('alias h_cycle "h_cycle_sb; h_cycle_sb; h_cycle_sb; h_cycle_sb; h_cycle_sb;"\n')
  220. gi.AddCommandString('alias h_cycle_sb "wait; help; wait 75; help; wait 100; putaway;"\n')
  221. gi.AddCommandString('set mvd_default_map wfall\n')
  222. end
  223. end
  224. function LevelChanged(level)
  225. gi.dprintf("mvd.lua LevelChanged(): it has been changed to %s\n", level)
  226. -- reset teams ready state
  227. reset_ready_state()
  228. -- if mvd is recording, stop mvd recording and advertise
  229. mvd_stop()
  230. end
  231. function LogMessage(msg)
  232. --debug of all incoming msg:
  233. --gi.AddCommandString("say "..msg.."\n")
  234. local team_ready = string.match(msg, "(.+) is ready to play!")
  235. if team_ready ~= nil then
  236. -- strip whitespace and bad chars
  237. team_ready = string_strip(team_ready)
  238. --gi.AddCommandString("say adding team: "..team_ready.."\n")
  239. table.insert(teams_ready,team_ready)
  240. return true
  241. end
  242. local team_notready = string.match(msg, "(.+) is no longer ready to play!")
  243. if team_notready ~= nil then
  244. -- strip whitespace and bad chars
  245. team_notready = string_strip(team_notready)
  246. for k,v in pairs(teams_ready) do
  247. if v == team_notready then
  248. table.remove(teams_ready, k)
  249. end
  250. end
  251. return true
  252. end
  253. local match = string.match(msg, "The round will begin in 20 seconds!")
  254. if match ~= nil then
  255. -- if mvd not recording then start mvd record
  256. if mvd_records == false then
  257. if needs_cvar_q2a_mvd_autorecord == true then
  258. local q2a_mvd_autorecord = gi.cvar("q2a_mvd_autorecord", "0").string
  259. if q2a_mvd_autorecord ~= "" and q2a_mvd_autorecord ~= "0" then
  260. mvd_start_recording()
  261. end
  262. else
  263. mvd_start_recording()
  264. end
  265. end
  266. return true
  267. end
  268. local match = string.match(msg, "Both Teams Must Be Ready!")
  269. if match ~= nil then
  270. -- if mvd is currently recording and t1, t2 == 0, then stop mvd recording and delete file
  271. mvd_stop_and_delete()
  272. return true
  273. end
  274. -- this message can't be catched by LogMessage .. .:(
  275. --local match = string.match(msg, "Recording local MVD to (.+)")
  276. --if match ~= nil then
  277. -- mvd_pathfile = match
  278. -- mvd_records = true
  279. -- gi.AddCommandString("say MVD2 recording started: "..mvd_pathfile.."\n")
  280. -- return true
  281. --end
  282. local match = string.match(msg, "Scores and time was resetted by request of captains")
  283. if match ~= nil then
  284. -- reset teams ready state
  285. reset_ready_state()
  286. -- if mvd is currently recording and t1, t2 == 0, then stop mvd recording and delete file
  287. mvd_stop_and_delete()
  288. return true
  289. end
  290. local match = string.match(msg, "Match is over, waiting for next map, please vote a new one..")
  291. if match ~= nil then
  292. -- reset teams ready state
  293. reset_ready_state()
  294. -- if mvd is recording, stop mvd recording and advertise
  295. mvd_stop()
  296. return true
  297. end
  298. local match = string.match(msg, "Game ending at:.+")
  299. if match ~= nil then
  300. -- reset teams ready state
  301. reset_ready_state()
  302. -- if mvd is recording, stop mvd recording and advertise
  303. mvd_stop()
  304. return true
  305. end
  306. end -- of LogMessage
  307. -- The round will begin in 20 seconds!
  308. -- if mvd is not recording then start mvd record
  309. -- Both Teams Must Be Ready!
  310. -- if mvd is currently recording and t1, t2 == 0, then stop mvd recording and delete file
  311. -- Recording local MVD to demos/1352750130_Team#2_Team#1.mvd2.gz
  312. -- save full filename
  313. -- set mvd_records state to true
  314. -- Scores and time was resetted by request of captains
  315. -- if mvd is currently recording, then stop mvd recording and delete file
  316. -- Match is over, waiting for next map, please vote a new one..
  317. -- if mvd is currently recording, then stop mvd recording (and keep file) and advertise!
  318. --[[
  319. # vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
  320. # kate: space-indent on; indent-width 4; mixedindent off;
  321. --]]