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

Module:Sandbox/Hcmzn:Background

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. -- 本模块为个人测试用
  2. local data = mw.loadData("模块:战舰少女:特殊数据")
  3. local enemies = mw.loadData("模块:战舰少女:敌舰属性列表")
  4. local alias = mw.loadData("模块:战舰少女:别名")
  5. local ships = data.ships
  6. local equipts = data.equipts
  7. local skills = data.skills
  8. local tr = data.tr
  9. local equiptDisplay = data.equiptDisplay
  10. local byIndex = data.byIndex
  11. local p = {}
  12. -- Tables --
  13. local shipTypeAbbrName = { CV='航母', CVL='轻母', AV='装母', BB='战列', BBV='航战', BC='战巡', CA='重巡', CL='轻巡', CAV='航巡', BM='重炮', DD='驱逐', ASDG='导驱', SS='潜艇', SC='炮潜', AP='补给' }
  14. local shipTypeFullName = {
  15. CV='航空母舰', CVL='轻型航空母舰', BB='战列舰', BBV='航空战列舰', BC='战列巡洋舰',
  16. CA='重巡洋舰', CL='轻巡洋舰', CAV='航空巡洋舰', BM='浅水重炮舰', DD='驱逐舰', ASDG='导弹驱逐舰', SS='潜水艇', SC='重炮潜艇', AP='补给舰'
  17. }
  18. local color = { 'black', 'green', 'blue', 'purple', 'orange', 'red' }
  19. local rangeName = { '无', '短', '中', '长', '超长' }
  20. -- 6星字体颜色的调色板, 每行一个颜色(格式{r,g,b}), 从最左到最右均匀分布, 支持任意种颜色
  21. local palette = {
  22. { 0xf3, 0xc7, 0x41 },
  23. { 0xe2, 0x1d, 0x3a },
  24. { 0xbb, 0x44, 0xb8 },
  25. { 0x3c, 0x7a, 0xf7 }
  26. }
  27. local function rainbow(str, bold)
  28. local n = mw.ustring.len(str)
  29. local m = #palette - 1
  30. local hasDot = false
  31. if mw.ustring.sub(str, n - 1, n - 1) == '·' then
  32. hasDot = true
  33. str = mw.ustring.gsub(str, '·改', '改')
  34. n = n - 1
  35. end
  36. local ret = ''
  37. local dbg = ''
  38. for i = 1,n do
  39. local p = i - 0.5
  40. local r = 1
  41. while p * m > n * r do
  42. r = r + 1
  43. end
  44. local l = r - 1
  45. local pl = n * l / m
  46. local pr = n * r / m
  47. local wl = (pr - p) / (pr - pl)
  48. local wr = (p - pl) / (pr - pl)
  49. local cr = math.floor(palette[l + 1][1] * wl + palette[r + 1][1] * wr + 0.5)
  50. local cg = math.floor(palette[l + 1][2] * wl + palette[r + 1][2] * wr + 0.5)
  51. local cb = math.floor(palette[l + 1][3] * wl + palette[r + 1][3] * wr + 0.5)
  52. local color = string.format('#%02x%02x%02x', cr, cg, cb)
  53. local ch = mw.ustring.sub(str, i, i)
  54. if hasDot and i == n then ch = "·" .. ch end
  55. if bold then ch = "'''" .. ch .. "'''" end
  56. ret = ret .. "<span style='color:" .. color .. ";'>" .. ch .. "</span>"
  57. end
  58. return ret
  59. end
  60. function addColor(str, rarity, bold)
  61. if rarity == 6 then
  62. return rainbow(str, bold)
  63. end
  64. if rarity < 1 then rarity = 1 end
  65. if bold then str = "'''" .. str .. "'''" end
  66. return '<span style="color:' .. color[rarity] .. ';">' .. str .. '</span>'
  67. end
  68. -- ## Helper functions ##
  69. local function formatFloat(x)
  70. if x == 0 then return 0 end
  71. x = math.floor(x * 100 + 0.5) / 100
  72. return string.format('%.2f', x)
  73. end
  74. local function getImage(aliasName, index, prefix, size)
  75. local ret = prefix .. index .. '.png'
  76. if aliasName ~= nil and aliasName ~= '' then
  77. ret = aliasName .. '.png'
  78. end
  79. if size ~= nil then
  80. ret = ret .. '|' .. size
  81. end
  82. return '[[File:' .. ret .. ']]'
  83. end
  84. local function notContains(str, ch)
  85. if str == nil then return true end
  86. return string.find(str, ch) == nil
  87. end
  88. -- ## Ship functions ##
  89. local function getBaseName(name)
  90. if alias.shipBaseName[name] ~= nil then
  91. return alias.shipBaseName[name]
  92. end
  93. name = string.gsub(name, '·改', '')
  94. name = string.gsub(name, '(特殊)', '')
  95. if mw.ustring.sub(name, 1, 2) == '航母' then return '敌航空母舰' end
  96. if mw.ustring.sub(name, 1, 2) == '轻母' then return '敌轻型航母' end
  97. if mw.ustring.sub(name, 1, 2) == '战列' then return '敌战列舰' end
  98. if mw.ustring.sub(name, 1, 2) == '战巡' then return '敌战列巡洋舰' end
  99. if mw.ustring.sub(name, 1, 2) == '旗舰' then return '敌航空战列舰' end
  100. if mw.ustring.sub(name, 1, 2) == '重巡' then return '敌重巡洋舰' end
  101. if mw.ustring.sub(name, 1, 2) == '雷巡' then return '敌重雷装巡洋舰' end
  102. if mw.ustring.sub(name, 1, 2) == '轻巡' then return '敌轻巡洋舰' end
  103. if mw.ustring.sub(name, 1, 2) == '驱逐' then return '敌驱逐舰' end
  104. if mw.ustring.sub(name, 1, 2) == '潜艇' then return '潜艇' end
  105. if mw.ustring.sub(name, 1, 2) == '补给' then return '敌补给舰' end
  106. return name
  107. end
  108. local function formatShipTitle(shipName, removeStyles, frame)
  109. local isBold = notContains(removeStyles, 'b')
  110. local hasColor = notContains(removeStyles, 'c')
  111. local hasLink = notContains(removeStyles, 'l')
  112. local ret = shipName
  113. local ship = ships[shipName]
  114. if ship == nil then ship = enemies[fixEnemyName(shipName)] end -- backward compatibility
  115. if hasColor then
  116. ret = addColor(ret, ship.rarity, isBold)
  117. elseif isBold then
  118. ret = "'''" .. ret .. "'''"
  119. end
  120. if hasLink then
  121. ret = '[[战舰少女:' .. getBaseName(shipName) .. '|' .. ret .. ']]'
  122. end
  123. return ret
  124. end
  125. -- Get an attribute of a ship.
  126. -- Return in 'BASE(MAX)' format if the attribute can grow with level or strengthen.
  127. local function getAttr(shipName, attrName)
  128. local ship = ships[shipName]
  129. local canGrow = false
  130. for i,v in pairs({'火力', '装甲', '鱼雷', '对空', '回避', '对潜', '索敌'}) do
  131. if attrName == v then
  132. canGrow = true
  133. break
  134. end
  135. end
  136. if canGrow then
  137. local base = ship[tr['基础' .. attrName]]
  138. local max_ = ship[tr[attrName .. '上限']]
  139. if base == max_ then
  140. return base
  141. else
  142. return base .. '(' .. max_ .. ')'
  143. end
  144. elseif attrName == '搭载' then
  145. return ship.cap1 + ship.cap2 + ship.cap3 + ship.cap4
  146. else
  147. local ret = ship[tr[attrName]]
  148. if ret == nil then ret = '' end -- some ships lack attributes about modification
  149. return ret
  150. end
  151. end
  152. -- Get a list of attributes, splited by '||' (in favor of wiki table).
  153. local function getAttrList(shipName, attrNameList)
  154. local ret = ''
  155. for i,attrName in pairs(attrNameList) do
  156. ret = ret .. getAttr(shipName, attrName) .. '||'
  157. end
  158. return string.sub(ret, 1, -3)
  159. end
  160. local function getShipNormalImage(shipName, size)
  161. return getImage(alias.shipNormalImage[shipName], ships[shipName].index, 'L_NORMAL_', size)
  162. end
  163. local function getShipBrokenImage(shipName, size)
  164. return getImage(alias.shipBrokenImage[shipName], ships[shipName].index, 'L_BROKEN_', size)
  165. end
  166. local function getShipDialogue(header, shipName, dialogueType, replace)
  167. local ret = '|-\n|style="text-align:center"|' .. header .. '||'
  168. if replace ~= '' and replace ~= nil then
  169. return ret .. replace .. '\n'
  170. else
  171. if (ships[shipName] == nil) then return '\n警告:错误的船名"' .. shipName .. '"\n\n' end
  172. local d = ships[shipName][dialogueType]
  173. if d == nil or d == '' then
  174. return ''
  175. else
  176. return ret .. ships[shipName][dialogueType] .. '\n'
  177. end
  178. end
  179. end
  180. -- ## Equipment functions ##
  181. local function formatEquiptListAttr(equiptName, attrName)
  182. local equipt = equipts[equiptName]
  183. local attr = equipt[tr[attrName]]
  184. if attr == nil then attr = 0 end
  185. local ret = 'data-sort-value="' .. attr .. '"|'
  186. if attrName == '射程' then
  187. ret = ret .. rangeName[attr + 1]
  188. elseif attrName == '耗铝' then
  189. ret = attr
  190. elseif attr == 0 then
  191. ret = ret .. '&zwnj;'
  192. elseif attrName == '对空补正' then
  193. ret = ret .. attr .. '%'
  194. elseif attr > 0 then
  195. ret = ret .. attrName .. '<wbr>+' .. attr
  196. else
  197. ret = ret .. attrName .. '<wbr>' .. attr
  198. end
  199. return ret
  200. end
  201. local function formatEquiptButtonAttr(name, val)
  202. if name == '射程' then
  203. return name .. ':' .. rangeName[val + 1] .. '<br>'
  204. elseif name == '对空补正' then
  205. return name .. ':' .. val .. '%<br>'
  206. elseif name == '耗铝' then
  207. return ''
  208. elseif val > 0 then
  209. return name .. '+' .. val .. '<br>'
  210. else
  211. return name .. val .. '<br>'
  212. end
  213. end
  214. local function getEquiptIcon(equipt, size)
  215. return getImage(alias.equiptIcon[equipt.index], equipt.index, 'Equip_L_', size)
  216. end
  217. local function fixEquiptName(name)
  218. name = string.gsub(name, 'Ⅲ', 'III')
  219. name = string.gsub(name, 'Ⅵ', 'VI')
  220. name = string.gsub(name, '日本', 'J国')
  221. name = string.gsub(name, '德国', 'G国')
  222. name = string.gsub(name, '英国', 'E国')
  223. name = string.gsub(name, '美国', 'U国')
  224. name = string.gsub(name, '意大利', 'I国')
  225. name = string.gsub(name, '法国', 'F国')
  226. name = string.gsub(name, '德国', 'G国')
  227. name = string.gsub(name, '苏联', 'S国')
  228. name = string.gsub(name, '中国', 'C国')
  229. local ret = alias.equiptName[name]
  230. if ret == nil then ret = name end
  231. return ret
  232. end
  233. local function onShip(equiptName)
  234. local ret = ''
  235. for i = 1,1300 do
  236. local shipName = byIndex[i]
  237. if shipName ~= nil then
  238. local eq = {ships[shipName].eq1, ships[shipName].eq2, ships[shipName].eq3, ships[shipName].eq4}
  239. for k = 1,4 do
  240. if eq[k] == equiptName then
  241. if i > 1000 and i < 2000 then
  242. local shipBaseName = byIndex[i - 1000]
  243. ret = ret .. '[[战舰少女:' .. shipBaseName .. '|' .. shipName .. ']]、'
  244. else
  245. ret = ret .. '[[战舰少女:' .. shipName .. '|' .. shipName .. ']]、'
  246. end
  247. break
  248. end
  249. end
  250. end
  251. end
  252. ret = string.sub(ret, 1, -4)
  253. if ret ~= '' and ret ~= nil then
  254. ret = ret .. '自带'
  255. end
  256. return ret
  257. end
  258. -- ## Ship ##
  259. p['舰娘'] = function(frame)
  260. local shipName = frame.args[1]
  261. local removeStyles = frame.args[2]
  262. return formatShipTitle(shipName, removeStyles, frame)
  263. end
  264. p['属性'] = function(frame)
  265. local ship = ships[frame.args[1]]
  266. if ship == nil then return '' end
  267. local attr = frame.args[2]
  268. if ship[attr] == nil then return '' end
  269. return ship[attr]
  270. end
  271. -- {{#invoke:战舰少女|舰娘属性|船名|属性名}} -> 属性
  272. p['舰娘属性'] = function(frame)
  273. local shipName = frame.args[1]
  274. local attrName = frame.args[2]
  275. return getAttr(shipName, attrName)
  276. end
  277. -- {{#invoke:战舰少女|图鉴编号|船名}} -> 图鉴编号
  278. p['图鉴编号'] = function(frame)
  279. return ships[frame.args[1]].index
  280. end
  281. -- {{#invoke:战舰少女|稀有度|船名}} -> 稀有度
  282. p['稀有度'] = function(frame)
  283. local shipName = frame.args[1]
  284. local colorful = frame.args[2]
  285. local r = ships[shipName].rarity
  286. if colorful then
  287. return "<span style='color:" .. color[r] .. ";'>'''" .. r .. "'''</span>"
  288. else
  289. return r
  290. end
  291. end
  292. -- {{#invoke:战舰少女|改造属性|船名|属性名}} -> "改前属性 → 改后属性"
  293. p['改造属性'] = function(frame)
  294. local baseShipName = frame.args[1]
  295. local attrName = frame.args[2]
  296. local modShipName = byIndex[ships[baseShipName].index + 1000]
  297. local baseAttr = getAttr(baseShipName, attrName)
  298. local modAttr = getAttr(modShipName, attrName)
  299. if baseAttr == modAttr then
  300. return baseAttr
  301. else
  302. return baseAttr .. ' → ' .. modAttr
  303. end
  304. end
  305. -- {{#invoke:战舰少女|舰娘属性列表|舰种|属性1,属性2,...}}
  306. p['舰娘属性列表'] = function(frame)
  307. local shipType = frame.args[1]
  308. local attrNameList = mw.text.split(frame.args[2], ',', true)
  309. -- table title
  310. ret = '|-\n!colspan=' .. (#attrNameList + 3) .. '|' .. shipTypeAbbrName[shipType] .. '属性列表\n'
  311. -- table header
  312. ret = ret .. '|-\n!图鉴<br>编号!!头像!!名称'
  313. for i,attrName in pairs(attrNameList) do
  314. ret = ret .. '!!'
  315. if mw.ustring.len(attrName) > 2 then -- break long headers into two lines
  316. ret = ret .. mw.ustring.sub(attrName, 1, 2)
  317. local tmp = mw.ustring.sub(attrName, 3, -1)
  318. if tmp ~= '上限' then
  319. ret = ret .. '<br>' .. tmp
  320. end
  321. else
  322. ret = ret .. attrName
  323. end
  324. end
  325. ret = ret .. '\n'
  326. -- table content
  327. for i = 1,1300 do -- keys of table will be sorted as string by default (11>2)
  328. local shipName = byIndex[i]
  329. if shipName ~= nil and ships[shipName].type == shipType then
  330. ret = ret .. '|-\n|' .. i
  331. ret = ret .. '||' .. getShipProfile(shipName, '150px')
  332. ret = ret .. '||' .. formatShipTitle(shipName)
  333. ret = ret .. '||' .. getAttrList(shipName, attrNameList) ..'\n'
  334. end
  335. end
  336. return ret
  337. end
  338. p['舰娘立绘'] = function(frame)
  339. local shipName = frame.args[1]
  340. local size = frame.args[2]
  341. return getShipNormalImage(shipName, size)
  342. end
  343. p['大破立绘'] = function(frame)
  344. local shipName = frame.args[1]
  345. local size = frame.args[2]
  346. return getShipBrokenImage(shipName, size)
  347. end
  348. function getShipProfile(shipName, size)
  349. local ret = alias.profile[shipName]
  350. if ret == nil then
  351. local tmp = ships[shipName]
  352. if tmp == nil then return shipName end
  353. ret = ships[shipName].index
  354. end
  355. ret = '[[File:Warship_Girls_icon_' .. ret .. '.png|link=战舰少女:' .. getBaseName(shipName)
  356. if size ~= nil and size ~= '' then ret = ret .. '|' .. size end
  357. return ret .. ']]'
  358. end
  359. p['头像'] = function(frame)
  360. return getShipProfile(frame.args[1], frame.args[2])
  361. end
  362. p['台词'] = function(frame)
  363. local shipName = frame.args[1]
  364. local dialogue = frame.args[2]
  365. local force = frame.args[3]
  366. if force ~= nil and force ~= '' then return force end
  367. if ships[shipName] == nil then return "none"
  368. else return ships[shipName][dialogue] end
  369. end
  370. function skillDesc(skillName)
  371. local desc = skills[skillName]
  372. if desc == nil then return '' end
  373. desc = desc:gsub('_G%[_', '<span style="color:green;">')
  374. desc = desc:gsub('_R%[_', '<span style="color:red;">')
  375. desc = desc:gsub('_%]_', '</span>')
  376. return desc
  377. end
  378. p['技能描述'] = function(frame)
  379. local shipName = frame.args[1]
  380. return skillDesc(ships[shipName].skill)
  381. end
  382. p['技能描述2'] = function(frame)
  383. local shipName = frame.args[1]
  384. return skillDesc(ships[shipName].skill2)
  385. end
  386. -- ## Equipment ##
  387. p['装备'] = function(frame)
  388. local name = frame.args[1]
  389. name = fixEquiptName(name)
  390. local equipt = equipts[name]
  391. if (equipt == nil) then return "'''" .. name .. "'''" end
  392. local icon = getEquiptIcon(equipt, '25px')
  393. local title = addColor(name, equipt.rarity, true)
  394. return icon .. '[[战舰少女/装备#' .. name .. '|' .. title .. ']]'
  395. end
  396. p['装备列表行'] = function(frame)
  397. local equiptType = frame.args[1]
  398. local equiptName = frame.args[2]
  399. local remarks = frame.args[3]
  400. local onShip = onShip(equiptName)
  401. equiptName = fixEquiptName(equiptName)
  402. local equipt = equipts[equiptName]
  403. local ret = 'id="' .. equiptName .. '"|'
  404. ret = ret .. equipt['index'] .. '||' .. getEquiptIcon(equipt, '70px')
  405. ret = ret .. '||' .. addColor(equiptName, equipt.rarity, true)
  406. for i, attrName in pairs(equiptDisplay[equiptType]) do
  407. ret = ret .. '||' .. formatEquiptListAttr(equiptName, attrName)
  408. end
  409. ret = ret .. '||'
  410. if remarks == '' or remarks == nil then
  411. ret = ret .. onShip
  412. elseif string.find(remarks, '<自带>') ~= nil then
  413. local z = string.find(remarks, '<自带>')
  414. remarks = string.sub(remarks, 1, z-1) .. onShip .. string.sub(remarks, z+8, -1)
  415. ret = ret .. remarks
  416. else
  417. ret = ret .. remarks
  418. end
  419. return ret
  420. end
  421. local attrOrder = { '火力', '鱼雷', '装甲', '对空', '轰炸', '索敌', '对潜', '命中', '回避', '幸运', '射程', '对空补正', '耗铝' }
  422. p['装备按钮'] = function(frame)
  423. local equiptName = frame.args[1]
  424. if frame.args[2] ~= nil then
  425. equiptName = ships[frame.args[1]]['eq' .. frame.args[2]]
  426. end
  427. local equipt = equipts[equiptName]
  428. if equipt == nil then return equiptName end
  429. btn = getEquiptIcon(equipt, '25px') .. addColor(equiptName, equipt.rarity, true)
  430. ctnt = ''
  431. for i, attrName in pairs(attrOrder) do
  432. local attrVal = equipt[tr[attrName]]
  433. if attrVal ~= 0 and attrVal ~= nil then
  434. ctnt = ctnt .. formatEquiptButtonAttr(attrName, attrVal)
  435. end
  436. end
  437. if equipt['special'] ~= nil then
  438. ctnt = ctnt .. equipt['special']
  439. end
  440. return frame:expandTemplate{ title = 'Toggle', args = { button=btn, content=ctnt } }
  441. end
  442. -- ## Enemies ##
  443. function fixEnemyName(name)
  444. name = name:gsub('IV', 'Ⅳ')
  445. name = name:gsub('V', 'Ⅴ')
  446. name = name:gsub('III', 'Ⅲ')
  447. name = name:gsub('II', 'Ⅱ')
  448. name = name:gsub('I', 'Ⅰ')
  449. return name
  450. end
  451. p['敌舰列表'] = function(frame)
  452. local colors = { 'black', 'green', 'blue', 'purple', 'orange', 'red' }
  453. local ret = ''
  454. local rec = 0
  455. local aa = 0
  456. local speed = { 0, 0, 0 }
  457. local n = { 0, 0, 0 }
  458. for i = 1,6 do
  459. name = frame.args[i]
  460. if name == nil then
  461. ret = ret .. '||'
  462. else
  463. name = fixEnemyName(name)
  464. ret = ret .. formatShipTitle(name, 'b') .. '||'
  465. local ship = enemies[name]
  466. rec = rec + ship.rec
  467. aa = aa + ship.aa
  468. speed[ship.class] = speed[ship.class] + ship.speed
  469. n[ship.class] = n[ship.class] + 1
  470. end
  471. end
  472. local s = 0
  473. if n[1] == 0 and n[2] == 0 then
  474. s = speed[3] / n[3]
  475. else
  476. s = math.min(speed[1] / n[1], speed[2] / n[2])
  477. end
  478. return ret .. rec .. '||' .. math.floor(s) .. '||' .. formatFloat(aa)
  479. end
  480. p['彩色'] = function(frame)
  481. local str = frame.args[1]
  482. local noBold = frame.args[2]
  483. return rainbow(str, noBold == nil)
  484. end
  485. p['自带'] = function(frame)
  486. local equiptName = frame.args[1]
  487. return onShip(equiptName)
  488. end
  489. p['debug'] = function()
  490. local frame = { }
  491. frame.args = { '胡德·改' }
  492. return p['技能描述2'](frame)
  493. end
  494. return p