• 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"

From Kancolle Wiki
Jump to navigation Jump to search
Line 63: Line 63:
 
local rand = math.random()
 
local rand = math.random()
 
local rtnHash = (math.floor(time*265314426625821*rand)-randomizer*time) % 2^32+math.floor(math.random()*1381242451)*randomizer
 
local rtnHash = (math.floor(time*265314426625821*rand)-randomizer*time) % 2^32+math.floor(math.random()*1381242451)*randomizer
local step1 = math.floor(time*265314426625821*rand)
+
-- local step1 = math.floor(time*265314426625821*rand)
local step2 = randomizer*time
+
-- local step2 = randomizer*time
local step3 = step1-step2
+
-- local step3 = step1-step2
local step4 = step3 % 2^32
+
-- local step4 = step3 % 2^32
local step5 = math.floor(math.random()*1381242451)
+
-- local step5 = math.floor(math.random()*1381242451)
local step6 = step5*randomizer
+
-- local step6 = step5*randomizer
local step7 = step4 + step6
+
-- local step7 = step4 + step6
local rtnStr = tostring(step1) .. " | " .. tostring(step2) .. " | " .. tostring(step3) .. " | " .. tostring(step4) .. " | " .. tostring(step5) .. " | " .. tostring(step6).. " | " .. tostring(step7) .. " | " .. tostring(rtnHash)
+
-- local rtnStr = tostring(step1) .. " | " .. tostring(step2) .. " | " .. tostring(step3) .. " | " .. tostring(step4) .. " | " .. tostring(step5) .. " | " .. tostring(step6).. " | " .. tostring(step7) .. " | " .. tostring(rtnHash)
 +
local rtnStr = tostring(rtnHash)
 
return rtnStr
 
return rtnStr
 
-- return (math.floor(time*265314426625821*rand)-randomizer) % 2^32+math.floor(math.random()*1381242451)
 
-- return (math.floor(time*265314426625821*rand)-randomizer) % 2^32+math.floor(math.random()*1381242451)

Revision as of 05:31, 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 i = 1, #string do
		local letter = string:sub(i,i)
		hash = bit32.lshift(hash,3)
		hash = hash+string.byte(letter) % (2^31-1)
	end
	return hash
end

function p.timeHash(seed)
	local randomizer = 0
		
	if seed == nil then
		randomizer = math.random(0,9999999)
	elseif type(seed) == "table" then
		if table.empty(seed) then
			randomizer = math.random(0,9999999)
		else
			for k,v in pairs(seed) do
				if type(k) == "number" then
					randomizer = randomizer + k
				elseif type(k) == "string" then
					randomizer = randomizer + p.hashString(k)
				end
				if type(v) == "string" then
					randomizer = randomizer + p.hashString(v)
				elseif type(v) == "number" then
					randomizer = randomizer + v
				elseif type(v) == "table" then
					local tableString = p.dictConcat(v)
					randomizer = randomizer + p.hashString(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()
	local rtnHash = (math.floor(time*265314426625821*rand)-randomizer*time) % 2^32+math.floor(math.random()*1381242451)*randomizer
	-- local step1 = math.floor(time*265314426625821*rand)
	-- local step2 = randomizer*time
	-- local step3 = step1-step2
	-- local step4 = step3 % 2^32
	-- local step5 = math.floor(math.random()*1381242451)
	-- local step6 = step5*randomizer
	-- local step7 = step4 + step6
	-- local rtnStr = tostring(step1) .. " | " .. tostring(step2) .. " | " .. tostring(step3) .. " | " .. tostring(step4) .. " | " .. tostring(step5) .. " | " .. tostring(step6).. " | " .. tostring(step7) .. " | " .. tostring(rtnHash)
	local rtnStr = tostring(rtnHash)
	return rtnStr
	-- return (math.floor(time*265314426625821*rand)-randomizer) % 2^32+math.floor(math.random()*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