software182

MenuServer.lua

Dec 24th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1. -- Menu Server for PES 2020: assign a menu based on the competition mode
  2. -- Custom content is used, not LiveCPK/game: server\menu-server is the root
  3. -- Author: Zlac & Hawke, 2020 (edited by 1002MB)
  4. -- Version: 1.0
  5. -- Originally posted on Evo-Web
  6. -- Massive thanks to Zlac... I 100% could not have done this without him, thanks mate
  7.  
  8. -- local new_menu_path
  9. local menuroot = ".\\server\\menu-server"
  10. local random_num
  11.  
  12. local competition_assignment_map = {}
  13. local exhib_same_league_tid
  14.  
  15.  
  16. -- remove trailing and leading whitespace from string
  17. local function trim(s)
  18. return s:gsub("^%s*(.-)%s*$", "%1")
  19. end
  20.  
  21. local function get_common_lib(ctx)
  22. return ctx.common_lib or _empty
  23. end
  24.  
  25. local function split(s, inSplitPattern)
  26. local outResults = {}
  27. -- chop off the trailing comment, if present
  28. local theCommentStart = string.find( s, "#", 1 )
  29. local data = s
  30. if theCommentStart ~= nil then
  31. data = string.sub(s, 1, theCommentStart-1)
  32. end
  33.  
  34. -- now do the splits by main separator (inSplitPattern)
  35. local theStart = 1
  36. local theSplitStart, theSplitEnd = string.find( data, inSplitPattern, theStart )
  37. while theSplitStart do
  38. outResults[#outResults+1] = trim(string.sub( data, theStart, theSplitStart-1 ))
  39. theStart = theSplitEnd + 1
  40. theSplitStart, theSplitEnd = string.find( data, inSplitPattern, theStart )
  41. end
  42. outResults[#outResults+1] = trim(string.sub( data, theStart ))
  43. return outResults
  44. end
  45.  
  46.  
  47.  
  48. local function load_map_txt(filename)
  49. local delim = ","
  50. local data = assert(io.lines(menuroot .. "\\" .. filename))
  51. log(filename .. " found in " .. menuroot .. "\\" .. filename)
  52.  
  53. for line in data do
  54. line = trim(string.gsub(line, "^\239\187\191", "")) -- removes UTF BOM bytes at the beginning of the first line in .txt file and leading/trailing whitespaces in every line
  55. local fields = split(line, delim)
  56. if #fields > 1 then
  57. for i=1,#fields do
  58. fields[i] = trim(fields[i])
  59. end
  60. if fields[1] ~= nil and fields[1] ~= "" then
  61. if filename == "map_competitions.txt" and fields[2] ~= nil then
  62. if competition_assignment_map[tonumber(fields[1])] ~= nil then
  63. table.insert(competition_assignment_map[tonumber(fields[1])], {fields[2]})
  64. else
  65. competition_assignment_map[tonumber(fields[1])] = { {fields[2]} }
  66. end
  67. log(string.format(" ==> %s menu assignment:: %s: %s ", filename, fields[1], fields[2]))
  68. end
  69. end
  70. end
  71. end
  72. end
  73.  
  74. local function has_value(tab, val)
  75. for index, value in ipairs(tab) do
  76. if value == val then
  77. return true
  78. end
  79. end
  80. return false
  81. end
  82.  
  83. function dump_table(o)
  84. if type(o) == 'table' then
  85. local s = '{ '
  86. for k,v in pairs(o) do
  87. if type(k) ~= 'number' then k = '"'..k..'"' end
  88. s = s .. '['..k..'] = ' .. dump_table(v) .. ','
  89. end
  90. return s .. '} '
  91. else
  92. return tostring(o)
  93. end
  94. end
  95.  
  96.  
  97. local function is_edit_mode(ctx)
  98. -- sorta works now, but probably needs to be more robust
  99. -- if all three values are known, we should not be in edit mode
  100. return not(ctx.tournament_id and ctx.home_team and ctx.away_team)
  101. end
  102.  
  103.  
  104. local function is_it_menu_file(filename)
  105. filename = string.lower(filename)
  106. if
  107. string.match(filename, "Asset\\model\\bg\\common") or
  108. string.match(filename, "common\\demo") or
  109. string.match(filename, "common\\menu") or
  110. string.match(filename, "common\\script\\flow") or
  111. string.match(filename, "common\\sound")
  112. then
  113. return true
  114. else
  115. return false
  116. end
  117. end
  118.  
  119.  
  120. local function get_new_menu_path(ctx)
  121. local tid = tonumber(ctx.tournament_id)
  122. local menu_path
  123.  
  124. if exhib_same_league_tid then
  125. if competition_assignment_map[exhib_same_league_tid] ~= nil and random_num ~= nil then
  126. menu_path = competition_assignment_map[exhib_same_league_tid][random_num][1] -- use the menu assigned for this competition
  127. end
  128. elseif tid then
  129. if competition_assignment_map[tid] ~= nil and random_num ~= nil then
  130. menu_path = competition_assignment_map[tid][random_num][1] -- use the menu assigned for this competition
  131. end
  132. end
  133. return menu_path
  134. end
  135.  
  136.  
  137. local function teams_selected(ctx, home_team_id, away_team_id)
  138. --whenever game selects new home_team, pick a random index of a menu to be used in selected competition (e.g. if multiple menus are assigned to one competition_ID)
  139. local tid = tonumber(ctx.tournament_id)
  140. random_num = nil
  141. exhib_same_league_tid = nil
  142.  
  143. if tid == 65535 then
  144. -- are both teams in exhibition mode from the same playable league?
  145. log("Checking if both teams in exhibition belong to the same league ... ")
  146. exhib_same_league_tid = get_common_lib(ctx).tid_same_league(home_team_id, away_team_id)
  147.  
  148. if exhib_same_league_tid ~= nil then
  149. log("... they do!")
  150. log("... mapped TournamentID for exhibition mode: " .. exhib_same_league_tid )
  151. if competition_assignment_map[exhib_same_league_tid] ~= nil then
  152. log(string.format("... competition with ID %s has %s menu(s) assigned.", exhib_same_league_tid, #competition_assignment_map[exhib_same_league_tid]))
  153. if #competition_assignment_map[exhib_same_league_tid] == 1 then
  154. -- if there's only one menu assigned to competition, set "random" number to 1
  155. -- do the same if replay mode is active - tid is not reliably available, so assume there's only 1 menu available
  156. random_num = 1
  157. else
  158. -- if there are more menus, select one index rendomly
  159. -- random_num = math.random(1, #competition_assignment_map[tid])
  160. random_num = math.random(#competition_assignment_map[exhib_same_league_tid])
  161. end
  162. log("Selecting random menu for competition ID " .. tostring(exhib_same_league_tid) .. " in exhibition mode (both teams belong to the same league): Menu no. " .. tostring(random_num) .. " (from " .. tostring(#competition_assignment_map[exhib_same_league_tid]) .. " menu(s) available)")
  163. else
  164. log(string.format("... competition with ID %s has no menus assigned.", exhib_same_league_tid))
  165. -- since the mapped competition does not have its own menus, revert to default exhibition mode random selection
  166. if competition_assignment_map[tid] ~= nil then
  167. if #competition_assignment_map[tid] == 1 then
  168. random_num = 1
  169. else
  170. random_num = math.random(#competition_assignment_map[tid])
  171. end
  172. log("Selecting random menu for exhibition mode: Menu no. " .. tostring(random_num) .. " (from " .. tostring(#competition_assignment_map[tid]) .. " menu(s) available)")
  173. end
  174. end
  175. else
  176. log("... they don't!")
  177. -- if not from the same league, make sure we still generate random_num for exhibition mode ...
  178. if competition_assignment_map[tid] ~= nil then
  179. if #competition_assignment_map[tid] == 1 then
  180. random_num = 1
  181. else
  182. random_num = math.random(#competition_assignment_map[tid])
  183. end
  184. log("Selecting menu for exhibition mode: Menu no. " .. tostring(random_num) .. " (from " .. tostring(#competition_assignment_map[tid]) .. " menu(s) available)")
  185. end
  186. end
  187. --
  188. elseif competition_assignment_map[tid] ~= nil then
  189. -- if #competition_assignment_map[tid] == 1 or ctx.is_replay_gallery == true then
  190. if #competition_assignment_map[tid] == 1 then
  191. random_num = 1
  192. else
  193. random_num = math.random(#competition_assignment_map[tid])
  194. end
  195. log("Selecting random menu for competition ID " .. tostring(tid) .. ": menu no. " .. tostring(random_num) .. " (from " .. tostring(#competition_assignment_map[tid]) .. " menu(s) available)")
  196. end
  197. end
  198.  
  199.  
  200. local function make_key(ctx, filename)
  201. -- if ctx.is_edit_mode == nil then -- do something only if edit mode is NOT active
  202. -- local menu_fname = which_menu(filename)
  203. -- if is_it_menu_file(filename) then
  204. if not is_edit_mode(ctx) and is_it_menu_file(filename) then
  205. -- log(string.format("Into make_key (menu file) ... (%s)", filename))
  206. local new_menu_path = get_new_menu_path(ctx)
  207. if new_menu_path then
  208. -- log("Into make_key (menu file): " .. new_menu_path .. "\\" .. filename)
  209. return new_menu_path .. "\\" .. filename
  210. end
  211. end
  212.  
  213. -- end
  214. end
  215.  
  216.  
  217. local function nil2str(value)
  218. if value ~= nil then
  219. return value
  220. else
  221. return "N/A"
  222. end
  223. end
  224.  
  225.  
  226. local function get_filepath(ctx, filename, key)
  227. -- log(string.format("Into get_filepath ... (%s, %s)", filename, key))
  228. -- if ctx.is_edit_mode == nil then -- do something only if edit mode is NOT active
  229. if key and filename and filename ~= key then -- filename ~= key shoud ensure that it really is a file from custom repository, with modified path prefix???
  230. -- log(string.format("menu file assignment for competition ID %s - %s\\%s", nil2str(ctx.tournament_id), menuroot, key))
  231. -- log(string.format("%s\\%s", menuroot, key))
  232. return string.format("%s\\%s", menuroot, key)
  233. end
  234. -- end
  235. end
  236.  
  237.  
  238. local function init(ctx)
  239. if menuroot:sub(1,1)=='.' then
  240. menuroot = ctx.sider_dir .. menuroot
  241. end
  242. load_map_txt("map_competitions.txt")
  243. math.randomseed(os.time())
  244.  
  245. ctx.register("livecpk_make_key", make_key)
  246. ctx.register("livecpk_get_filepath", get_filepath)
  247. ctx.register("set_teams", teams_selected)
  248. end
  249.  
  250. return { init = init }
Advertisement
Add Comment
Please, Sign In to add comment