置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:VG Awards

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. local p = {}
  2. local data = require('Module:VG Awards/data')
  3. local yesno = require('Module:Yesno')
  4. local getArgs = require('Module:Arguments').getArgs
  5. local function isWonOrNominated(val)
  6. val = type(val) == 'string' and val:lower() or val
  7. if val == 'won'
  8. or val == 'w'
  9. or val == '获奖'
  10. or val == '獲獎'
  11. or val == '得奖'
  12. or val == '得獎'
  13. then
  14. return true
  15. elseif val == 'nominated'
  16. or val == 'n'
  17. or val == '提名'
  18. or val == '未获奖'
  19. or val == '未獲獎'
  20. or val == '未得奖'
  21. or val == '未得獎'
  22. then
  23. return false
  24. else
  25. return yesno(val)
  26. end
  27. end
  28. local function getArgKeyTables(args)
  29. local awardsData, hasReferences, hasRows = {
  30. _years = {}
  31. }, false, false
  32. for k, v in pairs(args) do
  33. if string.match(k, '^[^_]+_[^_]+_.+$') then
  34. local pos1 = string.find(k, '_')
  35. local pos2 = string.find(k, '_', pos1 + 1)
  36. local year = string.sub(k, 1, pos1 - 1)
  37. local award = string.sub(k, pos1 + 1, pos2 - 1)
  38. local category = string.sub(k, pos2 + 1)
  39. if not awardsData[year] then
  40. awardsData[year] = {
  41. _awards = {},
  42. _row = 0
  43. }
  44. table.insert(awardsData['_years'], year)
  45. end
  46. if not awardsData[year][award] then
  47. awardsData[year][award] = {}
  48. table.insert(awardsData[year]['_awards'], award)
  49. end
  50. if category == 'Name' then
  51. awardsData[year][award]['_name'] = v
  52. elseif category == 'Ref' then
  53. awardsData[year][award]['_ref'] = v
  54. hasReferences = true
  55. else
  56. table.insert(awardsData[year][award], {category, isWonOrNominated(v)})
  57. awardsData[year]["_row"] = awardsData[year]["_row"] + 1
  58. hasRows = true
  59. end
  60. end
  61. end
  62. table.sort(awardsData['_years'], function (a, b)
  63. return tonumber(a) < tonumber(b)
  64. end)
  65. return awardsData, hasReferences, hasRows
  66. end
  67. local function renderHeadingRow(builder, hasReferences)
  68. local line = builder:tag('tr')
  69. line:tag('th')
  70. :wikitext(data.i18n.year)
  71. :done()
  72. :tag('th')
  73. :wikitext(data.i18n.award)
  74. :done()
  75. :tag('th')
  76. :wikitext(data.i18n.awardCategory)
  77. :done()
  78. :tag('th')
  79. :wikitext(data.i18n.result)
  80. if hasReferences then
  81. line:tag('th')
  82. :wikitext(data.i18n.references)
  83. end
  84. end
  85. local function renderYearAwards(builder, year, yearAwards, hasReferences, args)
  86. local isYearRendered = false
  87. table.sort(yearAwards['_awards'], function (a, b)
  88. if data.awards[a] then
  89. if data.awards[b] then
  90. return data.awards[a].sortkey < data.awards[b].sortkey
  91. else
  92. return true
  93. end
  94. else
  95. if data.awards[b] then
  96. return false
  97. else
  98. return a < b
  99. end
  100. end
  101. end)
  102. for _, v in ipairs(yearAwards['_awards']) do
  103. table.sort(yearAwards[v], function (a, b)
  104. if a[2] then
  105. if b[2] then
  106. return a[1] < b[1]
  107. else
  108. return true
  109. end
  110. else
  111. if b[2] then
  112. return false
  113. else
  114. return a[1] < b[1]
  115. end
  116. end
  117. end)
  118. local isAwardRendered = false
  119. for _2, v2 in ipairs(yearAwards[v]) do
  120. local line = builder:tag('tr')
  121. if not isYearRendered then
  122. line:tag('th')
  123. :attr('rowspan', yearAwards['_row'])
  124. :wikitext(year)
  125. isYearRendered = true
  126. end
  127. if not isAwardRendered then
  128. line:tag('th')
  129. :attr('rowspan', #yearAwards[v])
  130. :wikitext(yearAwards[v]['_name'] or (data.awards[v] and data.awards[v].name) or v)
  131. end
  132. line:tag('td')
  133. :wikitext(v2[1])
  134. :done()
  135. :tag('td')
  136. :css('background-color', (v2[2] and data.i18n.wonBackground) or data.i18n.nominatedBackground)
  137. :wikitext((v2[2] and data.i18n.won) or data.i18n.nominated)
  138. if not isAwardRendered then
  139. if hasReferences then
  140. line:tag('td')
  141. :attr('rowspan', #yearAwards[v])
  142. :wikitext(yearAwards[v]['_ref'])
  143. end
  144. isAwardRendered = true
  145. end
  146. end
  147. end
  148. end
  149. local function renderAwards(builder, awardsData, hasReferences, args)
  150. renderHeadingRow(builder, hasReferences)
  151. for _, v in ipairs(awardsData['_years']) do
  152. local yearAwards = awardsData[v]
  153. renderYearAwards(builder, v, yearAwards, hasReferences, args)
  154. end
  155. end
  156. local function renderMainTable(awardsData, hasReferences, args)
  157. local tbl = mw.html.create('table')
  158. :addClass('wikitable')
  159. :addClass('no-min-width-on-mobile')
  160. :css('text-align', 'center')
  161. :css('display', 'table')
  162. if args.align then
  163. tbl.addClass('float' .. args.align)
  164. end
  165. if args.width then
  166. tbl
  167. :css('width', args.width)
  168. else
  169. tbl
  170. :css('min-width', '30em')
  171. end
  172. if args.title and args.state and (args.state == 'autocollapse'
  173. or args.state == 'collapsed' or args.state == 'expanded') then
  174. tbl
  175. :addClass('collapsible')
  176. :addClass(args.state)
  177. end
  178. tbl:tag('caption')
  179. :wikitext(args.title or data.i18n.awardTitle)
  180. :css('display', 'table-caption')
  181. if args.subtitle then
  182. tbl:tag('tr'):tag('th')
  183. :attr('colspan', (hasReferences and 5) or 4)
  184. :wikitext(args.subtitle)
  185. end
  186. renderAwards(tbl, awardsData, hasReferences, args)
  187. local style = mw.getCurrentFrame():extensionTag('templatestyles', '', {src = 'Template:VG Reviews/styles.css'})
  188. return tostring(tbl) .. style
  189. end
  190. function p._main(frame, args)
  191. local awardsData, hasReferences, hasRows = getArgKeyTables(args)
  192. if hasRows ~= 0 then
  193. return renderMainTable(awardsData, hasReferences, args)
  194. elseif mw.title.getCurrentTitle().namespace == 0 then
  195. return data.i18n.emptyCategory
  196. end
  197. end
  198. function p.main(frame)
  199. return p._main(frame, getArgs(frame, {wrappers = "Template:VG Awards"}))
  200. end
  201. return p