- Welcome to the Kancolle Wiki!
- If you have any questions regarding site content, account registration, etc., please visit the KanColle Wiki Discord
Difference between revisions of "Module:RemiLib"
Jump to navigation
Jump to search
Remi Scarlet (talk | contribs) |
Remi Scarlet (talk | contribs) |
||
Line 7: | Line 7: | ||
-- I fucking hate lua. | -- I fucking hate lua. | ||
− | + | -- Takes a table. Returns a bool of whether it's empty or not. | |
+ | function table.empty (self) | ||
+ | for _, _ in pairs(self) do | ||
+ | return false | ||
+ | end | ||
+ | return true | ||
+ | end | ||
-- takes a string and returns string with first letter capitalized | -- takes a string and returns string with first letter capitalized | ||
Line 30: | Line 36: | ||
if seed == nil then | if seed == nil then | ||
+ | mw.log("asdf") | ||
randomizer = math.random(0,9999999) | randomizer = math.random(0,9999999) | ||
elseif type(seed) == "table" then | elseif type(seed) == "table" then | ||
− | + | if table.empty(seed) then | |
− | + | randomizer = math.random(0,9999999) | |
− | + | else | |
− | + | mw.log("fdsa") | |
− | + | for k,v in pairs(seed) do | |
− | + | randomizer = randomizer + p.hashString(k) | |
− | + | if type(v) == "string" then | |
− | + | randomizer = randomizer + p.hashString(v) | |
− | + | end | |
+ | if type(v) == "number" then | ||
+ | randomizer = randomizer + v | ||
+ | end | ||
+ | end | ||
+ | end | ||
elseif type(seed) == "string" then | elseif type(seed) == "string" then | ||
randomizer = p.hashString(seed) | randomizer = p.hashString(seed) | ||
Line 48: | Line 60: | ||
local time = os.time() | local time = os.time() | ||
local rand = math.random(-1,1) | local rand = math.random(-1,1) | ||
− | return (math.floor(time*265314426625821*rand | + | mw.log(randomizer) |
+ | mw.log(rand) | ||
+ | mw.log(time) | ||
+ | mw.log("fdsa") | ||
+ | return (math.floor(time*265314426625821*rand)-randomizer) % 2^32+math.floor(math.random(-1,1)*1381242451) | ||
end | end | ||
Revision as of 04:55, 25 October 2015
Note
These are literally just random library functions that I use often. Please try to avoid touching it if possible (or at least contact me if you really want to change something).
You're obviously free to use these for your own modules if you so wish.
local p = {}
local bit32 = require( 'bit32' )
-- Module for random library functions for lua because lua sucks. Written by
-- Remi_Scarlet
-- I fucking hate lua.
-- Takes a table. Returns a bool of whether it's empty or not.
function table.empty (self)
for _, _ in pairs(self) do
return false
end
return true
end
-- takes a string and returns string with first letter capitalized
function p.capitalize(str)
return (str:gsub("^%l", string.upper))
end
function p.hashString(string)
local hash = 0
for letter in string do
hash = bit32.lshift(hash,1)
hash = hash+string.byte(letter) % (2^31-1)
end
end
function p.timeHash(seed)
local randomizer = 0
mw.log("Aaaaaaa")
mw.log(type(seed))
mw.log(seed)
if seed == nil then
mw.log("asdf")
randomizer = math.random(0,9999999)
elseif type(seed) == "table" then
if table.empty(seed) then
randomizer = math.random(0,9999999)
else
mw.log("fdsa")
for k,v in pairs(seed) do
randomizer = randomizer + p.hashString(k)
if type(v) == "string" then
randomizer = randomizer + p.hashString(v)
end
if type(v) == "number" then
randomizer = randomizer + v
end
end
end
elseif type(seed) == "string" then
randomizer = p.hashString(seed)
elseif type(seed) == "number" then
randomizer = seed
end
local time = os.time()
local rand = math.random(-1,1)
mw.log(randomizer)
mw.log(rand)
mw.log(time)
mw.log("fdsa")
return (math.floor(time*265314426625821*rand)-randomizer) % 2^32+math.floor(math.random(-1,1)*1381242451)
end
-- returns a string representation of a table.
-- cannot do recursive tables. Eg, only single dimensional tables will work
-- should a multi-dimensional table be given, it will simply put "table" as the value
function p.dictConcat(dict,sep)
if sep == nil then sep = " | " end
local final = ""
for k,v in pairs(dict) do
if k ~= nil then
if v ~= nil then
final = final .. k .. "=" .. tostring(v) .. sep
else
final = final .. k .. "= nil" .. sep
end
else
if v ~= nil then
final = final .. tostring(v) .. sep
else
final = final .. "nil" .. sep
end
end
end
return final
end
-- returns tables merged.
function p.mergeTable(t1,t2)
for k,v in pairs(t2) do
if type(v) == "table" then
if type(t1[k] or false) == "table" then
tableMerge(t1[k] or {}, t2[k] or {})
else
t1[k] = v
end
else
t1[k] = v
end
end
return t1
end
-- Assumes a1 and a2 are "arrays" with their keys being ints in increasing order from 1
-- returns array with a2's indexes being appended to a1 in order
function p.mergeArrays(a1,a2)
local len = #a1
for i,v in pairs(a2) do
a1[len+i] = v
end
return a1
end
p.lowercase = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}
p.uppercase = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
p.letters = p.mergeArrays(p.lowercase,p.uppercase)
-- checks if data is in the array as a key
function p.valid(data, array)
local valid = {}
if array ~= nil then
for i = 1, #array do
valid[array[i]] = true
end
if valid[data] then
return true
else
return false
end
end
end
-- returns number of elements and tables there are.
-- recurse parameter is a boolean on whether to recurse on tables
function p.getTableSize(table, recurse)
if recurse == nil then recurse = false end
local size = 0
for k,v in pairs(table) do
if type(v) == "table" and recurse then
size = size+getRowSize(v)
elseif v ~= "" and k ~= "label" then
size = size+1
end
end
return size
end
return p