mvd.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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.5: exchanged all "say" with bprintf or dprintf
  22. 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)
  23. 1.3: added "exec_script_on_system_after_recording" for config.lua. BE CAREFUL with those shellscripts!
  24. 1.2: Checks for (Action Quake 2 and sv_mvd_enable != 0). And added useful mvd defaults for scoreboard
  25. 1.1: Even works on aq2-tng public teamplay mode now
  26. 1.0: All working on aq2-tng matchmode
  27. 0.9: Last Debug
  28. config.lua example, with all options:
  29. ------------------------------
  30. plugins = {
  31. some_other_plugin = {
  32. blah = "blah"
  33. },
  34. mvd = {
  35. mvd_webby = "http://mvd2.quadaver.org",
  36. exec_script_on_system_after_recording = "/home/gameservers/quake2/plugins/woot.sh",
  37. needs_cvar_q2a_mvd_autorecord = false
  38. }
  39. }
  40. ------------------------------
  41. !! Notice the "," in front of mvd = {
  42. --]]
  43. local game = gi.cvar("game", "").string
  44. local sv_mvd_enable = gi.cvar("sv_mvd_enable", "").string
  45. if game ~= "action" or sv_mvd_enable == "0" or sv_mvd_enable == "" or sv_mvd_enable == nil then
  46. gi.dprintf("mvd.lua WARNING: This script only loads when game is 'action' and when sv_mvd_enable is '1' or '2'!\n")
  47. return 0
  48. end
  49. -- if we came to here, it's action!
  50. local version = "1.5hau"
  51. gi.AddCommandString("sets q2a_mvd "..version.."\n")
  52. local mvd_webby -- configure this one in the config.lua
  53. local exec_script_on_system_after_recording -- configure this one in the config.lua
  54. local needs_cvar_q2a_mvd_autorecord -- configure this one in the config.lua
  55. -- state vars
  56. local teams_ready = {}
  57. local mvd_records = false
  58. local started_on_round_state = ""
  59. local mvd_pathfile = ""
  60. local mvd_file = ""
  61. ---- small helper functions follow: ----
  62. function file_exists(fname)
  63. local f=io.open(fname,"r")
  64. if f~=nil then
  65. io.close(f)
  66. return true
  67. else
  68. return false
  69. end
  70. end
  71. function string_strip(badstring)
  72. goodstring = badstring
  73. goodstring = string.gsub(goodstring, "%W", "")
  74. return goodstring
  75. end
  76. function reset_ready_state()
  77. for k in pairs (teams_ready) do
  78. teams_ready[k] = nil
  79. end
  80. end
  81. function get_round_state()
  82. local teamplay = gi.cvar("teamplay", "").string
  83. if teamplay == "1" then
  84. local cvar_t1 = gi.cvar("t1", "").string
  85. local cvar_t2 = gi.cvar("t2", "").string
  86. local cvar_t2 = gi.cvar("t3", "").string
  87. local rstate = ""..t1.." "..t2.." "..t3..""
  88. return rstate
  89. end
  90. return 0
  91. end
  92. ---- big helper functions follow: ----
  93. function mvd_start_recording()
  94. -- if teamplay == 1 then save round state .. for later usage -
  95. -- this is to know if record stopping also can delete the newly recorded mvd, because the rounds did not happen.
  96. started_on_round_state = get_round_state()
  97. -- build filename
  98. local now = os.date("%Y-%m-%d_%H%M%S")
  99. local mapname = gi.cvar("mapname", "").string
  100. local filename = now
  101. for k,v in pairs(teams_ready) do
  102. filename = filename .. "_" .. v
  103. --gi.AddCommandString("say starting team: "..v.."\n")
  104. end
  105. filename = filename.."_"..mapname
  106. gi.AddCommandString("mvdrecord -z "..filename.."\n")
  107. mvd_file = filename..".mvd2.gz"
  108. mvd_pathfile = game.."/demos/"..mvd_file
  109. mvd_records = true
  110. local hostname = gi.cvar("hostname", "").string
  111. gi.bprintf(PRINT_HIGH, "'%s' MVD2 recording started: %s\n", hostname, mvd_file)
  112. end
  113. function mvd_stop_and_delete()
  114. local current_round_state = get_round_state()
  115. if current_round_state == started_on_round_state then
  116. if mvd_records == true then
  117. gi.AddCommandString("mvdstop\n")
  118. mvd_records = false
  119. -- if file exists in game dir, delete it
  120. --$game demos/lala.mvd2.gz
  121. if file_exists(mvd_pathfile) then
  122. if os.remove(mvd_pathfile) then
  123. gi.bprintf('mvd.lua mvd_stop_and_delete(): Deleted the MVD2: %s\n', mvd_file)
  124. else
  125. gi.dprintf('mvd.lua mvd_stop_and_delete(): Problems deleting MVD2: %s\n', mvd_file)
  126. end
  127. end
  128. mvd_pathfile = ""
  129. mvd_file = ""
  130. end
  131. else
  132. if mvd_records == true then
  133. gi.bprintf('Be quick to ready up again! MVD2 is still recording!\n')
  134. end
  135. end
  136. end
  137. function mvd_stop()
  138. if mvd_records == true then
  139. gi.AddCommandString("mvdstop\n")
  140. mvd_records = false
  141. -- if file exists in game dir, advertise it:
  142. if file_exists(mvd_pathfile) then
  143. --$game demos/lala.mvd2.gz
  144. if mvd_webby ~= nil then
  145. gi.bprintf(PRINT_HIGH, 'Download the MVD2(%s) - %s\n', mvd_file, mvd_webby)
  146. else
  147. gi.bprintf(PRINT_HIGH, 'Download the MVD2: %s\n', mvd_file)
  148. end
  149. if exec_script_on_system_after_recording ~= nil then
  150. gi.dprintf('mvd.lua mvd_stop(): os.execute '..exec_script_on_system_after_recording..' "'..game..'" "'..mvd_file..'"\n')
  151. local returnstuff = os.execute(exec_script_on_system_after_recording..' "'..game..'" "'..mvd_file..'"')
  152. gi.dprintf('mvd.lua mvd_stop(): os.execute returns: '..returnstuff..'\n')
  153. end
  154. end
  155. mvd_pathfile = ""
  156. mvd_file = ""
  157. end
  158. end
  159. ---- q2admin hook functions follow: ----
  160. function q2a_load(config)
  161. gi.dprintf("mvd.lua q2a_load(): "..version.." for Action Quake 2 loaded.\n")
  162. mvd_webby = config.mvd_webby
  163. exec_script_on_system_after_recording = config.exec_script_on_system_after_recording
  164. if mvd_webby == nil then
  165. gi.dprintf("mvd.lua q2a_load(): You may define 'mvd_webby' in the config.lua file.\n")
  166. -- safe default:
  167. mvd_webby = nil
  168. end
  169. if exec_script_on_system_after_recording == nil then
  170. gi.dprintf("mvd.lua q2a_load(): You may define 'exec_script_on_system_after_recording' in the config.lua file.\n")
  171. -- safe default:
  172. exec_script_on_system_after_recording = nil
  173. end
  174. if needs_cvar_q2a_mvd_autorecord == nil then
  175. gi.dprintf("mvd.lua q2a_load(): You may define 'needs_cvar_q2a_mvd_autorecord' in the config.lua file.\n")
  176. needs_cvar_q2a_mvd_autorecord = false
  177. end
  178. -- set some working defaults (workarounds) if game is action...
  179. if game == "action" then
  180. gi.AddCommandString('set sv_mvd_nomsg 0\n')
  181. gi.AddCommandString('set sv_mvd_begincmd "putaway; h_cycle;"\n')
  182. gi.AddCommandString('set sv_mvd_scorecmd "h_cycle;"\n')
  183. gi.AddCommandString('alias h_cycle "h_cycle_sb; h_cycle_sb; h_cycle_sb; h_cycle_sb; h_cycle_sb;"\n')
  184. gi.AddCommandString('alias h_cycle_sb "wait; help; wait 75; help; wait 100; putaway;"\n')
  185. gi.AddCommandString('set mvd_default_map wfall\n')
  186. end
  187. end
  188. function LevelChanged(level)
  189. gi.dprintf("mvd.lua LevelChanged(): it has been changed to %s\n", level)
  190. -- reset teams ready state
  191. reset_ready_state()
  192. -- if mvd is recording, stop mvd recording and advertise
  193. mvd_stop()
  194. end
  195. function LogMessage(msg)
  196. --debug of all incoming msg:
  197. --gi.AddCommandString("say "..msg.."\n")
  198. local team_ready = string.match(msg, "(.+) is ready to play!")
  199. if team_ready ~= nil then
  200. -- strip whitespace and bad chars
  201. team_ready = string_strip(team_ready)
  202. --gi.AddCommandString("say adding team: "..team_ready.."\n")
  203. table.insert(teams_ready,team_ready)
  204. return true
  205. end
  206. local team_notready = string.match(msg, "(.+) is no longer ready to play!")
  207. if team_notready ~= nil then
  208. -- strip whitespace and bad chars
  209. team_notready = string_strip(team_notready)
  210. for k,v in pairs(teams_ready) do
  211. if v == team_notready then
  212. table.remove(teams_ready, k)
  213. end
  214. end
  215. return true
  216. end
  217. local match = string.match(msg, "The round will begin in 20 seconds!")
  218. if match ~= nil then
  219. -- if mvd not recording then start mvd record
  220. if mvd_records == false then
  221. if needs_cvar_q2a_mvd_autorecord == true then
  222. local q2a_mvd_autorecord = gi.cvar("q2a_mvd_autorecord", "").string
  223. if q2a_mvd_autorecord ~= nil and q2a_mvd_autorecord ~= "0" then
  224. mvd_start_recording()
  225. end
  226. else
  227. mvd_start_recording()
  228. end
  229. end
  230. return true
  231. end
  232. local match = string.match(msg, "Both Teams Must Be Ready!")
  233. if match ~= nil then
  234. -- if mvd is currently recording and t1, t2 == 0, then stop mvd recording and delete file
  235. mvd_stop_and_delete()
  236. return true
  237. end
  238. -- this message can't be catched by LogMessage .. .:(
  239. --local match = string.match(msg, "Recording local MVD to (.+)")
  240. --if match ~= nil then
  241. -- mvd_pathfile = match
  242. -- mvd_records = true
  243. -- gi.AddCommandString("say MVD2 recording started: "..mvd_pathfile.."\n")
  244. -- return true
  245. --end
  246. local match = string.match(msg, "Scores and time was resetted by request of captains")
  247. if match ~= nil then
  248. -- reset teams ready state
  249. reset_ready_state()
  250. -- if mvd is currently recording and t1, t2 == 0, then stop mvd recording and delete file
  251. mvd_stop_and_delete()
  252. return true
  253. end
  254. local match = string.match(msg, "Match is over, waiting for next map, please vote a new one..")
  255. if match ~= nil then
  256. -- reset teams ready state
  257. reset_ready_state()
  258. -- if mvd is recording, stop mvd recording and advertise
  259. mvd_stop()
  260. return true
  261. end
  262. local match = string.match(msg, "Game ending at:.+")
  263. if match ~= nil then
  264. -- reset teams ready state
  265. reset_ready_state()
  266. -- if mvd is recording, stop mvd recording and advertise
  267. mvd_stop()
  268. return true
  269. end
  270. end -- of LogMessage
  271. -- The round will begin in 20 seconds!
  272. -- if mvd is not recording then start mvd record
  273. -- Both Teams Must Be Ready!
  274. -- if mvd is currently recording and t1, t2 == 0, then stop mvd recording and delete file
  275. -- Recording local MVD to demos/1352750130_Team#2_Team#1.mvd2.gz
  276. -- save full filename
  277. -- set mvd_records state to true
  278. -- Scores and time was resetted by request of captains
  279. -- if mvd is currently recording, then stop mvd recording and delete file
  280. -- Match is over, waiting for next map, please vote a new one..
  281. -- if mvd is currently recording, then stop mvd recording (and keep file) and advertise!
  282. --[[
  283. # vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
  284. --]]