I wrote the following script, attempting to wrap my head around database usage in Mudlet.
function createDB()
db:create("my database", {
friends = {
name = "",
city = "",
_index = { "city" },
_unique = { "name" }
}
});
end
createDB()
local mydb = db:get_database("my database")
function purgeDB()
db:delete(mydb.friends, true)
cecho("<red>\nDatabase purged.")
end
function mergeDB()
db:merge_unique(mydb.friends, true)
cecho("<red>\nDuplicate merged.")
end
function countFriends()
echo(db:aggregate(mydb.friends.name, "count"))
end
function addFriend(playerName, playerCity)
db:add(mydb.friends, {name=playerName, city=playerCity})
cecho("<LimeGreen>\nFriend added: "..playerName)
end
function ashtanFriends()
local ashtanitefriends = db:fetch(mydb.friends, db:eq(mydb.friends.city, "Ashtan"))
display(ashtanitefriends)
end
function eleusisFriends()
local eleusianfriends = db:fetch(mydb.friends, db:eq(mydb.friends.city, "Eleusis"))
display(eleusianfriends)
end
function shallamFriends()
local shallamesefriends = db:fetch(mydb.friends, db:eq(mydb.friends.city, "Shallam"))
display(shallamesefriends)
end
I then made an alias with the pattern of ^friend (.+) (.+)$
which executed the following:
if matches[2] == "list" then
if matches[3] == "eleusis" then
eleusisFriends()
elseif matches[3] == "ashtan" then
ashtanFriends()
elseif matches[3] == "shallam" then
shallamFriends()
end
elseif matches[2] == "purge" and matches[3] == "all" then
purgeDB()
elseif matches[2] == "merge" and matches[3] == "all" then
mergeDB()
else
addFriend(matches[2], matches[3])
end
Usage would be something like:
friend Tanris ashtan
friend Darroth shallam
friend Zeon eleusis
friend list eleusis
friend purge all
I was attempting to make a 'merge' usage as you can see.. which I assumed just cleared duplicates in the database, then I realised I'm just creating a temporary array with this setup, it hasn't been committed to any real consistent database when mudlet closed all of this was lost. Can anyone elaborate on how I could finish this setup? Hopefully this also helped others trying to learn, 'coz I couldn't find any useful snippets out there that were this simple.
Comments