"Favorites" On map

Share your scripts and packages with other Mudlet users.
Post Reply
User avatar
chris
Posts: 493
Joined: Fri Jun 17, 2011 5:39 am

"Favorites" On map

Post by chris »

This will add a right click menu with nested submenus with favorites. The favorites are stored as room information in the following format:

favorite name|||favorite id|||room id|||parent id

Top menu items have no parent and you can use nil or -1. To add something to a room you can use this:
lastId is the room you are currently in.
Code: [show] | [select all] lua
Alias name: ^addFav\s(.*)$
--Adds a favorite in your current room.
--If you enter a string like: addFav Shops:Cancer's Shop it will add a new favorite
--under the Shops label
local favs = searchRoomUserData("favorite", "")
local entry = matches[2]
local parent = nil
local name = entry
if (string.find(entry,":")) then
	parent = string.match(entry,"[^:]+")
	name = string.match(entry,":(.+)")
end
local highestFav = 0
for i,v in pairs(favs) do
	local sStart = string.find(v,"|||")
	if sStart then
		local favId = tonumber(string.match(string.sub(v,sStart+3,-1),"(%d+)"))
		if favId >= highestFav then
			highestFav = favId+1
		end
		local fName = string.sub(v, 1, sStart-1)
		if parent and (fName == parent) then
			parent = favId
		end
	end
end
if not parent then
	parent = -1
end
if not tonumber(parent) then
	return
end
echo("adding "..name.."under "..parent)
--display(name.."|||"..highestFav.."|||"..lastId.."|||"..parent)
setRoomUserData(lastId, "favorite", name.."|||"..highestFav.."|||"..lastId.."|||"..parent)
setupFavorites()
Here is the corresponding function which populates the menu on the mapper right click.
Code: [show] | [select all] lua
function setupFavorites()
    favTable = searchRoomUserData("favorite", "")
    favSorted = {}
    unsorted = {}
    addMapMenu("Favorites")
    for i,v in pairs(favTable) do
    --repetitive but straightforward implementation
        local entry = string.split(v,'|||')
        local name, fav, room, parent = entry[1],entry[2],entry[3],entry[4]
        if parent then
            favSorted[parent] = favSorted[parent] or {}
            favSorted[parent][fav]={name,room}
        else
    --		unsorted
        end
    --add parents to their respective list so the player can actually click on them
    end
    if favSorted['-1'] then
    for i,v in pairs(favSorted['-1']) do
        if favSorted[i] then
            --designate -1 so we can keep the parent in order
            favSorted[i]['-1']={v[1], v[2]}
        end
    end
	end
	 nestItem=0
    for i,v in pairs(favSorted) do
		if v["-1"] then
				local k=v["-1"]
				local nest = k[1]..tostring(k[2])..tostring(nestItem)
				addMapMenu(nest, "Favorites", k[1])
				for j,t in pairs(v) do
					if j~="-1" then
						local entry=t[1]..tostring(t[2])..tostring(j)
						addMapEvent(entry, "onFavorite", nest, t[1], t[2])
					end
				end	
				nestItem=nestItem+1			
        else
--				Geyser.display(v)
				for j,k in pairs(v) do
					addMapEvent(k[1]..tostring(k[2]), "onFavorite", "Favorites", k[1], k[2])
			--	   favorites[k[1]..tostring(k[2])]=favorites["Favorites"]:addChild({name=k[1], width=100, height=25, flyOut=true, layoutDir="RV"})
			--		favorites[k[1]..tostring(k[2])]:setClickCallback("onFavorite", k[2])
				end
			end

    end
end

function onFavorite(...)
	gotoRoom(tonumber(arg[7]))
end

setupFavorites()

User avatar
Vadi
Posts: 4878
Joined: Sat Mar 14, 2009 3:13 pm

Re: "Favorites" On map

Post by Vadi »

Great idea!

Did you mean setRoomUserData instead of addRoomUserData though? Also you make use of "favourite" and "favourites" here.

User avatar
Vadi
Posts: 4878
Joined: Sat Mar 14, 2009 3:13 pm

Re: "Favorites" On map

Post by Vadi »

%d on matching returns a string, so you also want 'if tonumber(favId) >= highestFav then', I think.

User avatar
chris
Posts: 493
Joined: Fri Jun 17, 2011 5:39 am

Re: "Favorites" On map

Post by chris »

Fixed it and made it more accepting of names (%a won't match - or other chars for instnace)

Post Reply