| Line 1: |
Line 1: |
| | + | -- [[Category:Net modules]] |
| | + | |
| | local p = {} | | local p = {} |
| | | | |
| Line 7: |
Line 9: |
| | -- 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 |
| | | | |
| | + | function table.getKeys(self) |
| | + | local keys = {} |
| | + | for k,_ in pairs(self) do |
| | + | table.insert(keys,k) |
| | + | end |
| | + | return keys |
| | + | end |
| | + | |
| | + | function table.getVals(self) |
| | + | local vals = {} |
| | + | for _,v in pairs(self) do |
| | + | table.insert(vals,v) |
| | + | end |
| | + | return vals |
| | + | end |
| | | | |
| | -- takes a string and returns string with first letter capitalized | | -- takes a string and returns string with first letter capitalized |
| Line 14: |
Line 38: |
| | end | | end |
| | | | |
| − | function p.hashString(string) | + | function p.hashString(str) |
| | local hash = 0 | | local hash = 0 |
| − | for letter in string do | + | if str == nil or str == "" then |
| − | hash = bit32.lshift(hash,1)
| + | return 0 |
| − | hash = hash+string.byte(letter) % (2^31-1) | |
| | end | | end |
| | + | if type(str) == "string" and str ~= "" then |
| | + | for i = 1, #str do |
| | + | local letter = str:sub(i,i) |
| | + | hash = bit32.lshift(hash,3) |
| | + | hash = hash+str.byte(letter) % (2^31-1) |
| | + | end |
| | + | end |
| | + | return hash |
| | end | | end |
| | | | |
| | + | |
| | + | -- Hashes a seed if given, if not just returns a hash based on current time. |
| | function p.timeHash(seed) | | function p.timeHash(seed) |
| | + | -- lmfao this function wasn't a hash at all because it would never |
| | + | -- return the same thing twice. |
| | + | math.randomseed(p.hashString("Amatsukaze-chan is best")) |
| | + | |
| | local randomizer = 0 | | local randomizer = 0 |
| − |
| |
| − | mw.log(type(seed))
| |
| | | | |
| | if seed == nil then | | if seed == nil then |
| | randomizer = math.random(0,9999999) | | randomizer = math.random(0,9999999) |
| | elseif type(seed) == "table" then | | elseif type(seed) == "table" then |
| − | for k,v in pairs(dict) do | + | if table.empty(seed) then |
| − | randomizer = randomizer + p.hashString(k)
| + | randomizer = math.random(0,9999999) |
| − | if type(v) == "string" then
| + | else |
| − | randomizer = randomizer + p.hashString(v)
| + | for k,v in pairs(seed) do |
| − | end
| + | if type(k) == "number" then |
| − | if type(v) == "number" then
| + | randomizer = randomizer + k |
| − | randomizer = randomizer + v | + | 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 |
| | end | | end |
| Line 45: |
Line 90: |
| | end | | end |
| | local time = os.time() | | local time = os.time() |
| − | local rand = math.random(-1,1) | + | local rand = math.random() |
| − | return (math.floor(time*265314426625821*rand*randomizer)-randomizer) % 2^32+math.floor(math.random(-1,1)*1381242451) | + | local rtnHash = (math.floor(time*5501*rand)-math.floor(randomizer*time) + math.floor(math.random()*5501*randomizer)) % 2^32 |
| | + | return rtnHash |
| | end | | end |
| | | | |