remove local f == factions

This commit is contained in:
Luke aka SwissalpS 2024-09-24 20:05:29 +02:00
parent 4caea5e17e
commit 7131896a0b

164
mtt.lua

@ -2,14 +2,14 @@
-- https://github.com/OgelGames/fakelib.git -- https://github.com/OgelGames/fakelib.git
local pd local pd
if table.packer then if table.pack then
pd = function(...) print(dump(table.pack(...))) end pd = function(...) print(dump(table.pack(...))) end
else else
pd = function(...) for _, v in ipairs({ ... }) do print(dump(v)) end end pd = function(...) for _, v in ipairs({ ... }) do print(dump(v)) end end
end end
local f, fcc, S = factions, factions.handle_command, factions.S local fcc, S = factions.handle_command, factions.S
f.mode_unique_faction = false factions.mode_unique_faction = false
f.max_members_list = 11 factions.max_members_list = 11
-- factions chat command checker -- factions chat command checker
-- b1: expected return bool -- b1: expected return bool
@ -22,20 +22,20 @@ local function fccc(b1, s1, n, c)
end end
local function resetDB() local function resetDB()
for k in pairs(f.get_facts()) do for k in pairs(factions.get_facts()) do
f.disband_faction(k) factions.disband_faction(k)
end end
end end
local function makeFactions() local function makeFactions()
return f.register_faction('Endorian', 'Endor', 'eEe') return factions.register_faction('Endorian', 'Endor', 'eEe')
and f.register_faction('Alberian', 'Albert', 'a') and factions.register_faction('Alberian', 'Albert', 'a')
and f.register_faction('Gandalfian', 'Gandalf', 'GgG♥💩☺') and factions.register_faction('Gandalfian', 'Gandalf', 'GgG♥💩☺')
end end
local function dbChecks(callback) local function dbChecks(callback)
-- basic db integrity tests -- basic db integrity tests
local facts = f.get_facts() local facts = factions.get_facts()
assert('table' == type(facts)) assert('table' == type(facts))
assert('table' == type(facts.Alberian)) assert('table' == type(facts.Alberian))
assert('Albert' == facts.Alberian.owner) assert('Albert' == facts.Alberian.owner)
@ -70,13 +70,13 @@ mtt.register('join & setup players', function(callback)
-- make Albert a faction-admin -- make Albert a faction-admin
local player_privs = minetest.get_player_privs('Albert') local player_privs = minetest.get_player_privs('Albert')
player_privs[f.priv] = true player_privs[factions.priv] = true
minetest.set_player_privs('Albert', player_privs) minetest.set_player_privs('Albert', player_privs)
-- make sure the others aren't -- make sure the others aren't
for _, name in ipairs({ 'Endor', 'Gandalf', 'HanSolo' }) do for _, name in ipairs({ 'Endor', 'Gandalf', 'HanSolo' }) do
player_privs = minetest.get_player_privs(name) player_privs = minetest.get_player_privs(name)
player_privs[f.priv] = nil player_privs[factions.priv] = nil
minetest.set_player_privs(name, player_privs) minetest.set_player_privs(name, player_privs)
end end
@ -100,15 +100,15 @@ end)
mtt.register('basic db checks', dbChecks) mtt.register('basic db checks', dbChecks)
mtt.register('backend functions: player_is_in_faction', function(callback) mtt.register('backend functions: player_is_in_faction', function(callback)
assert(false == f.player_is_in_faction( assert(false == factions.player_is_in_faction(
'notExistingFaction', 'notExistingPlayer')) 'notExistingFaction', 'notExistingPlayer'))
assert(false == f.player_is_in_faction( assert(false == factions.player_is_in_faction(
'notExistingFaction', 'Gandalf')) 'notExistingFaction', 'Gandalf'))
assert(false == f.player_is_in_faction( assert(false == factions.player_is_in_faction(
'Gandalfian', 'notExistingPlayer')) 'Gandalfian', 'notExistingPlayer'))
assert(nil == f.player_is_in_faction( assert(nil == factions.player_is_in_faction(
'Gandalfian', 'Albert')) 'Gandalfian', 'Albert'))
assert(true == f.player_is_in_faction( assert(true == factions.player_is_in_faction(
'Gandalfian', 'Gandalf')) 'Gandalfian', 'Gandalf'))
callback() callback()
@ -116,84 +116,84 @@ end)
mtt.register('backend functions: get_player_faction', function(callback) mtt.register('backend functions: get_player_faction', function(callback)
-- (depricated) --> check log output for messages -- (depricated) --> check log output for messages
assert(false == f.get_player_faction('notExistingPlayer')) assert(false == factions.get_player_faction('notExistingPlayer'))
assert(nil == f.get_player_faction('HanSolo')) assert(nil == factions.get_player_faction('HanSolo'))
assert('Alberian' == f.get_player_faction('Albert')) assert('Alberian' == factions.get_player_faction('Albert'))
callback() callback()
end) end)
mtt.register('backend functions: get_player_factions', function(callback) mtt.register('backend functions: get_player_factions', function(callback)
if pcall(f.get_player_factions, nil) then if pcall(factions.get_player_factions, nil) then
callback('did not fail with nil as player argument -> bad') callback('did not fail with nil as player argument -> bad')
end end
if pcall(f.get_player_factions, 42) then if pcall(factions.get_player_factions, 42) then
callback('did not fail with number as player argument -> bad') callback('did not fail with number as player argument -> bad')
end end
assert(false == f.get_player_factions('notExistingPlayer')) assert(false == factions.get_player_factions('notExistingPlayer'))
assert(false == f.get_player_factions('HanSolo')) assert(false == factions.get_player_factions('HanSolo'))
assert('Alberian' == f.get_player_factions('Albert')[1]) assert('Alberian' == factions.get_player_factions('Albert')[1])
callback() callback()
end) end)
mtt.register('backend functions: get_owned_factions', function(callback) mtt.register('backend functions: get_owned_factions', function(callback)
assert(false == f.get_owned_factions(nil)) assert(false == factions.get_owned_factions(nil))
assert(false == f.get_owned_factions(42)) assert(false == factions.get_owned_factions(42))
assert(false == f.get_owned_factions('notExistingPlayer')) assert(false == factions.get_owned_factions('notExistingPlayer'))
assert(false == f.get_owned_factions('HanSolo')) assert(false == factions.get_owned_factions('HanSolo'))
local t = f.get_owned_factions('Albert') local t = factions.get_owned_factions('Albert')
assert(1 == #t and 'Alberian' == t[1]) assert(1 == #t and 'Alberian' == t[1])
callback() callback()
end) end)
mtt.register('backend functions: get_administered_factions', function(callback) mtt.register('backend functions: get_administered_factions', function(callback)
if pcall(f.get_administered_factions) then if pcall(factions.get_administered_factions) then
callback('calling get_administered_factions with nil did not raise error') callback('calling get_administered_factions with nil did not raise error')
end end
-- a bit strange that number as player name 'works' -- a bit strange that number as player name 'works'
assert(false == f.get_administered_factions(42)) assert(false == factions.get_administered_factions(42))
assert(false == f.get_administered_factions('notExistingPlayer')) assert(false == factions.get_administered_factions('notExistingPlayer'))
assert(false == f.get_administered_factions('HanSolo')) assert(false == factions.get_administered_factions('HanSolo'))
local t = f.get_administered_factions('Gandalf') local t = factions.get_administered_factions('Gandalf')
assert(1 == #t and 'Gandalfian' == t[1]) assert(1 == #t and 'Gandalfian' == t[1])
assert(3 == #f.get_administered_factions('Albert')) assert(3 == #factions.get_administered_factions('Albert'))
callback() callback()
end) end)
mtt.register('backend functions: get_owner', function(callback) mtt.register('backend functions: get_owner', function(callback)
assert(false == f.get_owner('notExistingFaction')) assert(false == factions.get_owner('notExistingFaction'))
assert('Gandalf' == f.get_owner('Gandalfian')) assert('Gandalf' == factions.get_owner('Gandalfian'))
callback() callback()
end) end)
mtt.register('backend functions: chown', function(callback) mtt.register('backend functions: chown', function(callback)
assert(false == f.chown('notExistingFaction', 'Gandalf')) assert(false == factions.chown('notExistingFaction', 'Gandalf'))
assert(true == f.chown('Endorian', 'Gandalf')) assert(true == factions.chown('Endorian', 'Gandalf'))
-- revert the 'illegal' use -- revert the 'illegal' use
f.chown('Endorian', 'Endor') factions.chown('Endorian', 'Endor')
callback() callback()
end) end)
mtt.register('backend functions: register_faction', function(callback) mtt.register('backend functions: register_faction', function(callback)
-- (partly tested in setup) -- (partly tested in setup)
assert(false == f.register_faction('Endorian', 'Endor', 'rodnE')) assert(false == factions.register_faction('Endorian', 'Endor', 'rodnE'))
assert(false == f.register_faction()) assert(false == factions.register_faction())
-- empty password -- empty password
assert(f.register_faction('foo', 'bar', '')) assert(factions.register_faction('foo', 'bar', ''))
callback() callback()
end) end)
mtt.register('backend functions: disband_faction', function(callback) mtt.register('backend functions: disband_faction', function(callback)
-- (partly tested in setup) -- (partly tested in setup)
assert(f.disband_faction('foo')) assert(factions.disband_faction('foo'))
assert(false == f.disband_faction()) assert(false == factions.disband_faction())
assert(false == f.disband_faction('notExistingFaction')) assert(false == factions.disband_faction('notExistingFaction'))
callback() callback()
end) end)
@ -205,46 +205,46 @@ mtt.register('backend functions: hash_password', function(callback)
end) end)
mtt.register('backend functions: valid_password', function(callback) mtt.register('backend functions: valid_password', function(callback)
assert(false == f.valid_password()) assert(false == factions.valid_password())
assert(false == f.valid_password('Endorian')) assert(false == factions.valid_password('Endorian'))
assert(false == f.valid_password('Endorian', 'foobar')) assert(false == factions.valid_password('Endorian', 'foobar'))
assert(true == f.valid_password('Endorian', 'eEe')) assert(true == factions.valid_password('Endorian', 'eEe'))
callback() callback()
end) end)
mtt.register('backend functions: get_password (depricated)', function(callback) mtt.register('backend functions: get_password (depricated)', function(callback)
assert(nil == f.get_password()) assert(nil == factions.get_password())
assert(nil == f.get_password('Endorian')) assert(nil == factions.get_password('Endorian'))
callback() callback()
end) end)
mtt.register('backend functions: set_password', function(callback) mtt.register('backend functions: set_password', function(callback)
assert(false == f.set_password('notExistingFaction', 'foobar')) assert(false == factions.set_password('notExistingFaction', 'foobar'))
assert(false == f.set_password('Endorian')) assert(false == factions.set_password('Endorian'))
assert(true == f.set_password('Endorian', 'EeE')) assert(true == factions.set_password('Endorian', 'EeE'))
assert(f.valid_password('Endorian', 'EeE')) assert(factions.valid_password('Endorian', 'EeE'))
-- revert that again -- revert that again
f.set_password('Endorian', 'eEe') factions.set_password('Endorian', 'eEe')
callback() callback()
end) end)
mtt.register('backend functions: join_faction', function(callback) mtt.register('backend functions: join_faction', function(callback)
assert(false == f.join_faction()) assert(false == factions.join_faction())
assert(false == f.join_faction('Endorian')) assert(false == factions.join_faction('Endorian'))
assert(false == f.join_faction('Endorian', 'notExistingPlayer')) assert(false == factions.join_faction('Endorian', 'notExistingPlayer'))
assert(true == f.join_faction('Endorian', 'Gandalf')) assert(true == factions.join_faction('Endorian', 'Gandalf'))
callback() callback()
end) end)
mtt.register('backend functions: leave_faction', function(callback) mtt.register('backend functions: leave_faction', function(callback)
assert(false == f.leave_faction()) assert(false == factions.leave_faction())
assert(false == f.leave_faction('Endorian')) assert(false == factions.leave_faction('Endorian'))
assert(false == f.leave_faction('Endorian', 'notExistingPlayer')) assert(false == factions.leave_faction('Endorian', 'notExistingPlayer'))
assert(true == f.leave_faction('Endorian', 'Gandalf')) assert(true == factions.leave_faction('Endorian', 'Gandalf'))
callback() callback()
end) end)
@ -263,10 +263,10 @@ mtt.register('frontend functions: create', function(callback)
assert(fccc(false, S("Missing password."), 'Gandalf', 'create foobar')) assert(fccc(false, S("Missing password."), 'Gandalf', 'create foobar'))
assert(fccc(false, S("Faction @1 already exists.", 'Gandalfian'), assert(fccc(false, S("Faction @1 already exists.", 'Gandalfian'),
'Gandalf', 'create Gandalfian foobar')) 'Gandalf', 'create Gandalfian foobar'))
f.mode_unique_faction = true factions.mode_unique_faction = true
assert(fccc(false, S("You are already in a faction."), assert(fccc(false, S("You are already in a faction."),
'Gandalf', 'create Gandalfian2 foobar')) 'Gandalf', 'create Gandalfian2 foobar'))
f.mode_unique_faction = false factions.mode_unique_faction = false
-- correct creation (also with capitals in sub-command) -- correct creation (also with capitals in sub-command)
assert(fccc(true, S("Registered @1.", 'Gandalfian2'), assert(fccc(true, S("Registered @1.", 'Gandalfian2'),
'Gandalf', 'cREate Gandalfian2 foobar')) 'Gandalf', 'cREate Gandalfian2 foobar'))
@ -317,7 +317,7 @@ mtt.register('frontend functions: info', function(callback)
makeFactions() makeFactions()
assert(fccc(false, S("Faction @1 doesn't exist.", 'foobar'), assert(fccc(false, S("Faction @1 doesn't exist.", 'foobar'),
'Endor', 'info foobar')) 'Endor', 'info foobar'))
f.join_faction('Endorian', 'Gandalf') factions.join_faction('Endorian', 'Gandalf')
assert(fccc(false, S("You are in multiple factions, you have to choose one of them: @1.", assert(fccc(false, S("You are in multiple factions, you have to choose one of them: @1.",
'Endorian, Gandalfian'), 'Gandalf', 'info') 'Endorian, Gandalfian'), 'Gandalf', 'info')
or fccc(false, S("You are in multiple factions, you have to choose one of them: @1.", or fccc(false, S("You are in multiple factions, you have to choose one of them: @1.",
@ -326,11 +326,11 @@ mtt.register('frontend functions: info', function(callback)
-- so just dumping result for optical check. -- so just dumping result for optical check.
pd('Endor executes: /factions info', fcc('Endor', 'info')) pd('Endor executes: /factions info', fcc('Endor', 'info'))
assert(fcc('Endor', 'info')) assert(fcc('Endor', 'info'))
f.max_members_list = 1 factions.max_members_list = 1
pd('max_members_list == 1 and Endor executes: /factions info', pd('max_members_list == 1 and Endor executes: /factions info',
fcc('Endor', 'info')) fcc('Endor', 'info'))
assert(fcc('Endor', 'info')) assert(fcc('Endor', 'info'))
f.max_members_list = 11 factions.max_members_list = 11
pd('Endor executes: /factions info Gandalfian', fcc('Endor', 'info Gandalfian')) pd('Endor executes: /factions info Gandalfian', fcc('Endor', 'info Gandalfian'))
assert(fcc('Endor', 'info Gandalfian')) assert(fcc('Endor', 'info Gandalfian'))
@ -359,10 +359,10 @@ mtt.register('frontend functions: player_info', function(callback)
end) end)
mtt.register('frontend functions: join', function(callback) mtt.register('frontend functions: join', function(callback)
f.mode_unique_faction = true factions.mode_unique_faction = true
assert(fccc(false, S("You are already in a faction."), assert(fccc(false, S("You are already in a faction."),
'Endor', 'join')) 'Endor', 'join'))
f.mode_unique_faction = false factions.mode_unique_faction = false
assert(fccc(false, S("Missing faction name."), assert(fccc(false, S("Missing faction name."),
'Endor', 'join')) 'Endor', 'join'))
assert(fccc(false, S("Faction @1 doesn't exist.", 'notExistingFaction'), assert(fccc(false, S("Faction @1 doesn't exist.", 'notExistingFaction'),
@ -437,15 +437,15 @@ mtt.register('frontend functions: passwd', function(callback)
'Endor', 'passwd foobar Gandalfian')) 'Endor', 'passwd foobar Gandalfian'))
assert(fccc(true, S("Password has been updated."), assert(fccc(true, S("Password has been updated."),
'Endor', 'passwd foobar')) 'Endor', 'passwd foobar'))
assert(f.get_facts().Endorian.password256 == assert(factions.get_facts().Endorian.password256 ==
'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2') 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2')
assert(fccc(true, S("Password has been updated."), assert(fccc(true, S("Password has been updated."),
'Gandalf', 'passwd foobar Gandalfian')) 'Gandalf', 'passwd foobar Gandalfian'))
assert(f.get_facts().Gandalfian.password256 == assert(factions.get_facts().Gandalfian.password256 ==
'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2') 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2')
assert(fccc(true, S("Password has been updated."), assert(fccc(true, S("Password has been updated."),
'Albert', 'passwd barf Gandalfian')) 'Albert', 'passwd barf Gandalfian'))
assert(f.get_facts().Gandalfian.password256 == assert(factions.get_facts().Gandalfian.password256 ==
'8a6e40cfcd99060eb1efdfeb689fe26606e221b4fd487bb224ab79a82648ccd9') '8a6e40cfcd99060eb1efdfeb689fe26606e221b4fd487bb224ab79a82648ccd9')
callback() callback()
@ -467,15 +467,15 @@ mtt.register('frontend functions: chown', function(callback)
'Gandalf', 'chown notExistingPlayer foobar')) 'Gandalf', 'chown notExistingPlayer foobar'))
assert(fccc(false, S("@1 isn't in faction @2.", 'Endor', 'Gandalfian'), assert(fccc(false, S("@1 isn't in faction @2.", 'Endor', 'Gandalfian'),
'Gandalf', 'chown Endor foobar')) 'Gandalf', 'chown Endor foobar'))
f.join_faction('Gandalfian', 'Endor') factions.join_faction('Gandalfian', 'Endor')
assert(fccc(false, S("Permission denied: Wrong password."), assert(fccc(false, S("Permission denied: Wrong password."),
'Gandalf', 'chown Endor foobar')) 'Gandalf', 'chown Endor foobar'))
assert(fccc(true, S("Ownership has been transferred to @1.", 'Endor'), assert(fccc(true, S("Ownership has been transferred to @1.", 'Endor'),
'Gandalf', 'chown Endor barf')) 'Gandalf', 'chown Endor barf'))
assert('Endor' == f.get_owner('Gandalfian')) assert('Endor' == factions.get_owner('Gandalfian'))
assert(fccc(true, S("Ownership has been transferred to @1.", 'Gandalf'), assert(fccc(true, S("Ownership has been transferred to @1.", 'Gandalf'),
'Albert', 'chown Gandalf foobar Gandalfian')) 'Albert', 'chown Gandalf foobar Gandalfian'))
assert('Gandalf' == f.get_owner('Gandalfian')) assert('Gandalf' == factions.get_owner('Gandalfian'))
callback() callback()
end) end)
@ -493,10 +493,10 @@ mtt.register('frontend functions: invite', function(callback)
'Albert', 'invite notExistingPlayer Gandalfian')) 'Albert', 'invite notExistingPlayer Gandalfian'))
assert(fccc(false, S("Player @1 is already in faction @2.", 'Endor', 'Gandalfian'), assert(fccc(false, S("Player @1 is already in faction @2.", 'Endor', 'Gandalfian'),
'Albert', 'invite Endor Gandalfian')) 'Albert', 'invite Endor Gandalfian'))
f.mode_unique_faction = true factions.mode_unique_faction = true
assert(fccc(false, S("Player @1 is already in faction @2.", 'Gandalf', 'Gandalfian'), assert(fccc(false, S("Player @1 is already in faction @2.", 'Gandalf', 'Gandalfian'),
'Albert', 'invite Gandalf Endorian')) 'Albert', 'invite Gandalf Endorian'))
f.mode_unique_faction = false factions.mode_unique_faction = false
assert(fccc(true, S("@1 is now a member of faction @2.", 'Gandalf', 'Endorian'), assert(fccc(true, S("@1 is now a member of faction @2.", 'Gandalf', 'Endorian'),
'Albert', 'invite Gandalf Endorian')) 'Albert', 'invite Gandalf Endorian'))
@ -504,7 +504,7 @@ mtt.register('frontend functions: invite', function(callback)
end) end)
mtt.register('final db checks', function(callback) mtt.register('final db checks', function(callback)
pd(f.get_facts()) pd(factions.get_facts())
callback() callback()
end) end)