Module

World

From Dogcraft Wiki

(Created meta module for keeping track of current and past worlds and subworlds and performing the tasks of extracting the world/subworld name from a string, and getting the associated "parent" world for a given extracted subworld string. This module is meant for use with other modules (such as WorldPills or WorldSwitch))
 
m (added an optional start parameter to extractSubworld and extractWorld)
Line 1: Line 1:
local p = {}
local p = {}


-- table of all past and current world names
-- table of all past and current world names
Line 45: Line 44:


-- function which returns the first world name found in a string
-- function which returns the first world name found in a string
function p.extractWorld(input)
function p.extractWorld(input, start)
start = start or 1
input = mw.ustring.lower(input)
input = mw.ustring.lower(input)
worlds = p.worlds
worlds = p.worlds
for key,value in ipairs(worlds) do  
for key,value in ipairs(worlds) do  
if (mw.ustring.find(input, value, 1, true) ~= nil) then
if (mw.ustring.find(input, value, start, true) ~= nil) then
return value
return value
end
end
Line 61: Line 61:


-- function which returns the first subworld name found in a string
-- function which returns the first subworld name found in a string
function p.extractSubworld(input)
function p.extractSubworld(input, start)
start = start or 1
input = mw.ustring.lower(input)
input = mw.ustring.lower(input)
subworlds = p.subworlds
subworlds = p.subworlds

Revision as of 01:57, 29 December 2023

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

local p = {}

-- table of all past and current world names
p.worlds = {"survival 1", "survival 2", "survival 3", "survival 4", "survival 5", "survival 6", "creative", "patreon", "hub", "lobby", "amplified", "MCMMO", "skyblock"}


-- table of all past and current subworld names with associated "parent" worlds
p.subworldsWithWorlds = {
	["husky"]		= p.worlds[4],
	["beagle"]		= p.worlds[4],
	["akita"]		= p.worlds[5],
	["corgi"]		= p.worlds[5],
	["labrador"]	= p.worlds[5],
	["shepherd"]	= p.worlds[5],
	["sheltie"] 	= p.worlds[6],
	["retriever"]	= p.worlds[6],
	["streamtown"]	= p.worlds[6],
}


-- function to convert the subworldsWithWorlds table to a list of only subworld names
function p.subworlds()
	out = {}
	index = 1
	for subworld in pairs(p.subworldsWithWorlds) do
		out[index] = subworld
		index = index + 1
	end
	return out
end


-- table of only subworld names
p.subworlds = p.subworlds()


-- table of world names with associated subworlds (unused)
-- p.worldsWithSubworlds = {
-- 	["survival 4"] = {"husky", "beagle"},
-- 	["survival 5"] = {"akita", "corgi", "labrador", "shepherd"},
-- 	["survival 6"] = {"sheltie", "retriever", "streamtown"},
-- }


-- function which returns the first world name found in a string
function p.extractWorld(input, start)
	start = start or 1
	input = mw.ustring.lower(input)
	worlds = p.worlds
	for key,value in ipairs(worlds) do 
		if (mw.ustring.find(input, value, start, true) ~= nil) then
			return value
		end
	end
	if (mw.ustring.find(input, 'survival', 1, true) ~= nil) then
		return 'survival 1'
	end
	return ''
end
	

-- function which returns the first subworld name found in a string
function p.extractSubworld(input, start)
	start = start or 1
	input = mw.ustring.lower(input)
	subworlds = p.subworlds
	for key,value in ipairs(subworlds) do 
		if (mw.ustring.find(input, value, 1, true) ~= nil) then
			return value
		end
	end
	return ''--return 'not found'
end


-- function which returns the parent world for the first subworld found in a string
function p.getParentWorld(input)
	input = mw.ustring.lower(input)
	subworld = p.extractSubworld(input)
	return p.subworldsWithWorlds[subworld]
end

return p
This page was last modified on 29 December 2023, at 01:57. (3 months ago)