Jump to content

Module:Affection

From example

Documentation for this module may be created at Module:Affection/doc

local p = {}

-- 好感度等级配置
local AFFECTION_LEVELS = {
  {min = 0, max = 19, name = "陌生", color = "#999999", icon = "💔"},
  {min = 20, max = 39, name = "认识", color = "#88ccff", icon = "💙"},
  {min = 40, max = 59, name = "友好", color = "#66dd88", icon = "💚"},
  {min = 60, max = 79, name = "亲密", color = "#ffaa44", icon = "🧡"},
  {min = 80, max = 99, name = "喜欢", color = "#ff6699", icon = "💖"},
  {min = 100, max = 999, name = "热恋", color = "#ff0066", icon = "❤️"}
}

-- 获取好感度等级信息
local function getAffectionLevel(value)
  for _, level in ipairs(AFFECTION_LEVELS) do
    if value >= level.min and value <= level.max then
      return level
    end
  end
  return AFFECTION_LEVELS[1] -- 默认返回最低等级
end

-- 生成好感度进度条
local function generateProgressBar(value, max)
  max = max or 100
  local percentage = math.min(100, (value / max) * 100)
  local level = getAffectionLevel(value)
  
  local html = mw.html.create('div')
    :addClass('affection-container')
    :css('margin', '10px 0')
  
  -- 好感度标签
  local label = html:tag('div')
    :css('display', 'flex')
    :css('justify-content', 'space-between')
    :css('margin-bottom', '5px')
    :css('font-size', '14px')
  
  label:tag('span')
    :css('font-weight', 'bold')
    :css('color', level.color)
    :wikitext(level.icon .. ' ' .. level.name)
  
  label:tag('span')
    :css('color', '#666')
    :wikitext(value .. ' / ' .. max)
  
  -- 进度条容器
  local barContainer = html:tag('div')
    :css('width', '100%')
    :css('height', '20px')
    :css('background-color', '#f0f0f0')
    :css('border-radius', '10px')
    :css('overflow', 'hidden')
    :css('box-shadow', 'inset 0 2px 4px rgba(0,0,0,0.1)')
  
  -- 进度条填充
  barContainer:tag('div')
    :css('width', percentage .. '%')
    :css('height', '100%')
    :css('background', 'linear-gradient(90deg, ' .. level.color .. ', ' .. level.color .. 'dd)')
    :css('transition', 'width 0.3s ease')
    :css('box-shadow', '0 2px 4px rgba(0,0,0,0.2)')
  
  return tostring(html)
end

-- 主要的显示函数
function p.show(frame)
  local value = tonumber(frame.args[1] or frame.args.value or 0)
  local max = tonumber(frame.args[2] or frame.args.max or 100)
  
  return generateProgressBar(value, max)
end

-- 获取等级名称
function p.getLevelName(frame)
  local value = tonumber(frame.args[1] or frame.args.value or 0)
  local level = getAffectionLevel(value)
  return level.name
end

-- 获取等级图标
function p.getLevelIcon(frame)
  local value = tonumber(frame.args[1] or frame.args.value or 0)
  local level = getAffectionLevel(value)
  return level.icon
end

-- 获取等级颜色
function p.getLevelColor(frame)
  local value = tonumber(frame.args[1] or frame.args.value or 0)
  local level = getAffectionLevel(value)
  return level.color
end

-- 计算好感度变化
function p.calculate(frame)
  local current = tonumber(frame.args.current or 0)
  local change = tonumber(frame.args.change or 0)
  local max = tonumber(frame.args.max or 100)
  
  local newValue = math.max(0, math.min(max, current + change))
  
  if frame.args.format == 'diff' then
    local sign = change >= 0 and '+' or ''
    local color = change >= 0 and '#00aa00' or '#aa0000'
    return '<span style="color:' .. color .. ';font-weight:bold;">' .. sign .. change .. '</span>'
  end
  
  return tostring(newValue)
end

-- 好感度对比显示(显示前后变化)
function p.showChange(frame)
  local oldValue = tonumber(frame.args.old or 0)
  local newValue = tonumber(frame.args.new or 0)
  local max = tonumber(frame.args.max or 100)
  
  local change = newValue - oldValue
  local oldLevel = getAffectionLevel(oldValue)
  local newLevel = getAffectionLevel(newValue)
  
  local html = mw.html.create('div')
    :addClass('affection-change')
  
  -- 旧值
  html:tag('div')
    :css('opacity', '0.6')
    :wikitext("'''之前:''' " .. oldLevel.icon .. ' ' .. oldLevel.name .. ' (' .. oldValue .. ')')
  
  -- 变化值
  local sign = change >= 0 and '+' or ''
  local color = change >= 0 and '#00aa00' or '#aa0000'
  html:tag('div')
    :css('color', color)
    :css('font-weight', 'bold')
    :css('font-size', '18px')
    :css('margin', '5px 0')
    :wikitext(sign .. change)
  
  -- 新值
  html:tag('div')
    :wikitext("'''现在:''' " .. newLevel.icon .. ' ' .. newLevel.name .. ' (' .. newValue .. ')')
  
  -- 新进度条
  html:wikitext(generateProgressBar(newValue, max))
  
  -- 等级提升提示
  if newLevel.name ~= oldLevel.name then
    local levelUpText = newLevel.min > oldLevel.min and '关系升温了!' or '关系冷淡了...'
    local levelUpColor = newLevel.min > oldLevel.min and '#ff6699' or '#6699ff'
    html:tag('div')
      :css('color', levelUpColor)
      :css('font-weight', 'bold')
      :css('text-align', 'center')
      :css('margin-top', '10px')
      :css('font-size', '16px')
      :wikitext('✨ ' .. levelUpText .. ' ✨')
  end
  
  return tostring(html)
end

return p