🌟 | 現在、 鉄壁ヘッドショットには対応済みです。 鉄壁HSは通常HSと同じダメージになります。LMG及びDMR、チャージライフル、ハンマーポイント弾を除き、すべてのダメージ値が一致していることを確認しています。 |
「モジュール:Utility/Library」の版間の差分
ナビゲーションに移動
検索に移動
(一部分に同じ要素をもつ配列をテキスト化する関数を標準化) |
(関数getQuantityStringをmw.loadDataによって読み込まれたテーブルでも正しく対応できるようなhackを追加) |
||
(同じ利用者による、間の17版が非表示) | |||
2行目: | 2行目: | ||
function aw.shallowCopy(obj) | function aw.shallowCopy(obj) | ||
local | local ret | ||
if type(obj) == 'table' then | |||
ret = {} | |||
for key, value in pairs(obj) do | for key, value in pairs(obj) do | ||
ret[key] = value | |||
end | end | ||
else | else | ||
ret = obj | |||
end | end | ||
return | return ret | ||
end | |||
function aw.mergeTable(tbl1, tbl2, overwrite) | |||
local ret | |||
if overwrite then | |||
ret = tbl1 | |||
else | |||
ret = aw.shallowCopy(tbl1) | |||
end | |||
for key, value in pairs(tbl2) do | |||
if type(value) == 'table' and type(tbl1[key] or false) == 'table' then | |||
ret[key] = aw.mergeTable(ret[key], tbl2[key], overwrite) | |||
else | |||
ret[key] = value | |||
end | |||
end | |||
return ret | |||
end | end | ||
function aw.getAsBoolean(strOrNumOrBool, defaultValue) | function aw.getAsBoolean(strOrNumOrBool, defaultValue) | ||
local typename = type(strOrNumOrBool) | local typename = type(strOrNumOrBool) | ||
if typename == 'boolean' then | if typename == 'boolean' then | ||
26行目: | 41行目: | ||
local str = string.lower(strOrNumOrBool) | local str = string.lower(strOrNumOrBool) | ||
return str == 'true' or str == 't' or str == '1' | return str == 'true' or str == 't' or str == '1' | ||
elseif defaultValue == nil then | |||
return false | |||
else | else | ||
return defaultValue | return defaultValue | ||
52行目: | 69行目: | ||
function aw.round(num) | function aw.round(num) | ||
return math.floor(tonumber(string.format("%. | return math.floor(tonumber(string.format("%.6f", num)) + 0.5) | ||
end | |||
function aw.roundx(num, digits) | |||
local shift = math.pow(10, digits) | |||
return aw.round(shift * num) / shift | |||
end | end | ||
function aw.roundover(num) | function aw.roundover(num) | ||
return math.ceil(tonumber(string.format("%. | return math.ceil(tonumber(string.format("%.6f", num)) - 0.5) | ||
end | |||
function aw.clamp(num, low, high) | |||
return math.min(math.max(num, low), high) | |||
end | |||
-- 型チェック | |||
function aw.isNumber(num) | |||
return type(num) == 'number' | |||
end | |||
function aw.isNumberAndGreaterThanZero(num) | |||
return aw.isNumber(num) and num > 0 | |||
end | end | ||
function aw. | function aw.isNumberAndGreaterThanOrEqualToZero(num) | ||
if | return aw.isNumber(num) and num >= 0 | ||
return aw.round(num) | end | ||
function aw.isNumberAndGreaterThanX(num, x) | |||
return aw.isNumber(num) and num > x | |||
end | |||
function aw.isNumberAndGreaterThanOrEqualToX(num, x) | |||
return aw.isNumber(num) and num >= x | |||
end | |||
-- 文字列ライブラリー | |||
function aw.stringstarts(str, target) | |||
return string.sub(str, 1, string.len(target)) == target | |||
end | |||
function aw.stringends(str, target) | |||
return target == '' or string.sub(str, -string.len(target)) == target | |||
end | |||
function aw.getEmphasizableTextFunc(mainFormat, prefixFormat, suffixFormat) | |||
prefixFormat = prefixFormat or '%s' | |||
suffixFormat = suffixFormat or '%s' | |||
local psFormat = string.format('%s%s%s', prefixFormat, mainFormat, suffixFormat) | |||
local pFormat = string.format('%s%s', prefixFormat, mainFormat) | |||
local sFormat = string.format('%s%s', mainFormat, suffixFormat) | |||
return function(longname, shortname) | |||
local shortableName = string.gsub( | |||
longname, | |||
string.format('(.*)%s(.*)', string.gsub(shortname, '-', '%%-')), | |||
function(prefix, suffix) | |||
if prefix ~= '' then | |||
if suffix ~= '' then | |||
return string.format(psFormat, prefix, shortname, suffix) | |||
else | |||
return string.format(pFormat, prefix, shortname) | |||
end | |||
else | |||
if suffix ~= '' then | |||
return string.format(sFormat, shortname, suffix) | |||
else | |||
return string.format(mainFormat, shortname) | |||
end | |||
end | |||
end) | |||
return shortableName | |||
end | |||
end | |||
function aw.getQuantityString(tbl, num) | |||
local count = #tbl | |||
if count == 0 then | |||
if tbl[4] then | |||
count = 4 | |||
elseif tbl[3] then | |||
count = 3 | |||
elseif tbl[2] then | |||
count = 2 | |||
elseif tbl[1] then | |||
count = 1 | |||
else | |||
count = 0 | |||
end | |||
end | |||
-- { '1st', '2nd', '3rd', '%dth' } | |||
if count >= 4 then | |||
-- e.g. 3rd | |||
if num == 3 then | |||
return tbl[3] | |||
-- e.g. 2nd | |||
elseif num == 2 then | |||
return tbl[2] | |||
-- e.g. 2st | |||
elseif num == 1 then | |||
return tbl[1] | |||
-- e.g. %dth | |||
else | |||
return string.format(tbl[4], num) | |||
end | |||
-- { 'once', 'twice', '%d times' } | |||
elseif count == 3 then | |||
-- e.g. twice | |||
if num == 2 then | |||
return tbl[2] | |||
-- e.g. once | |||
elseif num == 1 then | |||
return tbl[1] | |||
-- e.g. %d times | |||
else | |||
return string.format(tbl[3], num) | |||
end | |||
-- { '1 round', '%d rounds' } | |||
elseif count == 2 then | |||
-- e.g. 1 round | |||
if num == 1 then | |||
return tbl[1] | |||
-- e.g. %d rounds | |||
else | |||
return string.format(tbl[2], num) | |||
end | |||
-- { '%dm' } | |||
else | else | ||
return aw. | return string.format(tbl[1], num) | ||
end | |||
end | |||
-- 数字にカンマをつける関数 | |||
function aw.comma(amount) | |||
local formatted = amount | |||
while true do | |||
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') | |||
if k == 0 then | |||
break | |||
end | |||
end | end | ||
return formatted | |||
end | end | ||
2021年9月11日 (土) 20:02時点における最新版
このモジュールについての説明文ページを モジュール:Utility/Library/doc に作成できます
local aw = {} function aw.shallowCopy(obj) local ret if type(obj) == 'table' then ret = {} for key, value in pairs(obj) do ret[key] = value end else ret = obj end return ret end function aw.mergeTable(tbl1, tbl2, overwrite) local ret if overwrite then ret = tbl1 else ret = aw.shallowCopy(tbl1) end for key, value in pairs(tbl2) do if type(value) == 'table' and type(tbl1[key] or false) == 'table' then ret[key] = aw.mergeTable(ret[key], tbl2[key], overwrite) else ret[key] = value end end return ret end function aw.getAsBoolean(strOrNumOrBool, defaultValue) local typename = type(strOrNumOrBool) if typename == 'boolean' then return strOrNumOrBool elseif typename == 'number' then return strOrNumOrBool ~= 0 elseif typename == 'string' then local str = string.lower(strOrNumOrBool) return str == 'true' or str == 't' or str == '1' elseif defaultValue == nil then return false else return defaultValue end end function aw.getAsNumber(strOrNum, defaultValue) defaultValue = defaultValue or 0 local typename = type(strOrNum) if typename == 'number' then return strOrNum elseif typename == 'string' then return tonumber(strOrNum) else return defaultValue end end function aw.escapeHtml(html) local result, _ = html :gsub('&', '&') :gsub('"', '"') return result end function aw.round(num) return math.floor(tonumber(string.format("%.6f", num)) + 0.5) end function aw.roundx(num, digits) local shift = math.pow(10, digits) return aw.round(shift * num) / shift end function aw.roundover(num) return math.ceil(tonumber(string.format("%.6f", num)) - 0.5) end function aw.clamp(num, low, high) return math.min(math.max(num, low), high) end -- 型チェック function aw.isNumber(num) return type(num) == 'number' end function aw.isNumberAndGreaterThanZero(num) return aw.isNumber(num) and num > 0 end function aw.isNumberAndGreaterThanOrEqualToZero(num) return aw.isNumber(num) and num >= 0 end function aw.isNumberAndGreaterThanX(num, x) return aw.isNumber(num) and num > x end function aw.isNumberAndGreaterThanOrEqualToX(num, x) return aw.isNumber(num) and num >= x end -- 文字列ライブラリー function aw.stringstarts(str, target) return string.sub(str, 1, string.len(target)) == target end function aw.stringends(str, target) return target == '' or string.sub(str, -string.len(target)) == target end function aw.getEmphasizableTextFunc(mainFormat, prefixFormat, suffixFormat) prefixFormat = prefixFormat or '%s' suffixFormat = suffixFormat or '%s' local psFormat = string.format('%s%s%s', prefixFormat, mainFormat, suffixFormat) local pFormat = string.format('%s%s', prefixFormat, mainFormat) local sFormat = string.format('%s%s', mainFormat, suffixFormat) return function(longname, shortname) local shortableName = string.gsub( longname, string.format('(.*)%s(.*)', string.gsub(shortname, '-', '%%-')), function(prefix, suffix) if prefix ~= '' then if suffix ~= '' then return string.format(psFormat, prefix, shortname, suffix) else return string.format(pFormat, prefix, shortname) end else if suffix ~= '' then return string.format(sFormat, shortname, suffix) else return string.format(mainFormat, shortname) end end end) return shortableName end end function aw.getQuantityString(tbl, num) local count = #tbl if count == 0 then if tbl[4] then count = 4 elseif tbl[3] then count = 3 elseif tbl[2] then count = 2 elseif tbl[1] then count = 1 else count = 0 end end -- { '1st', '2nd', '3rd', '%dth' } if count >= 4 then -- e.g. 3rd if num == 3 then return tbl[3] -- e.g. 2nd elseif num == 2 then return tbl[2] -- e.g. 2st elseif num == 1 then return tbl[1] -- e.g. %dth else return string.format(tbl[4], num) end -- { 'once', 'twice', '%d times' } elseif count == 3 then -- e.g. twice if num == 2 then return tbl[2] -- e.g. once elseif num == 1 then return tbl[1] -- e.g. %d times else return string.format(tbl[3], num) end -- { '1 round', '%d rounds' } elseif count == 2 then -- e.g. 1 round if num == 1 then return tbl[1] -- e.g. %d rounds else return string.format(tbl[2], num) end -- { '%dm' } else return string.format(tbl[1], num) end end -- 数字にカンマをつける関数 function aw.comma(amount) local formatted = amount while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if k == 0 then break end end return formatted end -- 一部分に同じ要素をもつ配列をテキスト化する関数 local function toRepeatingString(cache, count) if count > 1 then return cache .. '×' .. count else return cache end end function aw.stringifyRepeatingArray(array, separator) local cache = array[1] local count = 0 local output = nil for _, damage in ipairs(array) do if cache == damage then count = count + 1 else local text = toRepeatingString(cache, count) if output ~= nil then output = output .. separator .. text else output = text end cache = damage count = 1 end end local text = toRepeatingString(cache, count) if output ~= nil then output = output .. separator .. text else output = text end return output end return aw