×

Langue

Fermer
Atelier 801
  • Forums
  • Dev Tracker
  • Connexion
    • English Français
      Português do Brasil Español
      Türkçe Polski
      Magyar Română
      العربية Skandinavisk
      Nederlands Deutsch
      Bahasa Indonesia Русский
      中文 Filipino
      Lietuvių kalba 日本語
      Suomi עברית
      Italiano Česky
      Hrvatski Slovensky
      Български Latviešu
      Estonian
  • Langue
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • [Function] Handling New Names (Name#0000) in Lua
[Function] Handling New Names (Name#0000) in Lua
Shamousey
« Consul »
1520385300000
    • Shamousey#0095
    • Profil
    • Derniers messages
    • Tribu
#1
  7
I've seen some people are concerned about the new naming system which will change everyone's username to append a unique ID to the end of it, and what this means for modules.

I wrote up a couple of quick functions that I plan to implement into #utility so that instead of having to type the full name in a command (eg. "!kill Shamousey#0000") you can still type the short name if there is only one player with that name in the room (eg "!kill Shamousey").

The main function, "user_in_list" will return one of the following values:
  • If only one user with the given username is found in the list, return true
  • If no users with the given username is found in the list, return false
  • If multiple users with the given username is found in the list, return a list of these names


If you want to implement these functions into your own code to handle this kind of logic, here they are:

Code Lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
--- Returns the split name and hashtag from an Atelier 801 username
--
-- @param username The original username string.
--
-- @return (Pair) The user's name (before hashtag) and hashtag.
--
function username_and_tags(username)
local name, hashtag

if not username then
error("Username argument not supplied.")
end

if username:sub(1,1) == '+' then
name = username
else
local tagLocation = username:find('#')
if tagLocation then
name = username:sub(1, tagLocation - 1)
hashtag = username:sub(tagLocation, -1)
else
name = username
end
end
return name, hashtag
end

--- Checks if a given full or partial Atelier 801 username is within the given list.
--
-- @param username The username string.
--
-- @param[opt] list The list to check within. If no list supplied,
-- tfm.get.room.playerList is used.
--
-- @param[opt] values If true, the values inside the list will be checked
-- instead of the keys.
--
-- @return Returns true if a single name is found.
-- Returns false if no names are found.
-- Returns a list of names if multiple are found.
--
function user_in_list(username, list, values)
local name, hashtag = username_and_tags(username)
local list = list or tfm.get.room.playerList
local values = values or false
local playersFound = {}

for iteration_key, iteration_value in pairs(list) do
local valueToCheck = values and iteration_value or iteration_key
local nameToCheck, hashtagToCheck = username_and_tags(valueToCheck)

if name == nameToCheck and (not hashtag or hashtag == hashtagToCheck) then
table.insert(playersFound, valueToCheck)
end
end

if #playersFound == 0 then
return false
end

if #playersFound == 1 then
return true
end

return playersFound
end

Dernière modification le 1520385540000
Shamousey
« Consul »
1520385360000
    • Shamousey#0095
    • Profil
    • Derniers messages
    • Tribu
#2
  6
To test and prove what they do, here are some assertions:

Code Lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
local playerListAssociative = {
["Shamousey#0000"] = true,
["Shamousey#1234"] = true,
["Shamousey#9999"] = true,
["Tigrounette#0000"] = true,
["+Goondad"] = true,
}

local playerListFlat = {
[1] = "Shamousey#0000",
[2] = "Shamousey#1234",
[3] = "Shamousey#9999",
[4] = "Tigrounette#0000",
[5] = "+Goondad",
}

do
-- An error is thrown if no username is passed.
assert(false == pcall(function() user_in_list() end))

-- Fully qualified usernames return true.
assert(true == user_in_list("Shamousey#0000", playerListAssociative))
assert(true == user_in_list("Tigrounette#0000", playerListAssociative))
assert(true == user_in_list("+Goondad", playerListAssociative))
assert(true == user_in_list("Tigrounette#0000", playerListAssociative))

-- Usernames without the hashtag that exist will return either true or a list.
assert(true == user_in_list("Tigrounette", playerListAssociative))
assert(3 == #user_in_list("Shamousey", playerListAssociative))

-- Usernames that can not be found at all return false.
assert(false == user_in_list("+Goond", playerListAssociative))
assert(false == user_in_list("+Goondad#0000", playerListAssociative))
assert(false == user_in_list("Melibellule#0000", playerListAssociative))
assert(false == user_in_list("Melibellule", playerListAssociative))
assert(false == user_in_list("Shamo", playerListAssociative))

-- The same methods also work when checking for a value instead of a key
assert(true == user_in_list("Shamousey#0000", playerListFlat, true))
assert(true == user_in_list("+Goondad", playerListFlat, true))
assert(true == user_in_list("Tigrounette", playerListFlat, true))
assert(3 == #user_in_list("Shamousey", playerListFlat, true))
assert(false == user_in_list("+Goond", playerListFlat, true))
assert(false == user_in_list("+Goondad#0000", playerListFlat, true))

print("All tests passed.")
end
Mercedestamy
« Censeur »
1520385960000
    • Mercedestamy#0000
    • Profil
    • Derniers messages
    • Tribu
#3
  2
wo, it's cool and pretty
Kimsterjay
« Consul »
1520419920000
    • Kimsterjay#0000
    • Profil
    • Derniers messages
    • Tribu
#4
  3
Urghh..
Caphira
« Censeur »
1520420580000
    • Caphira#0000
    • Profil
    • Derniers messages
#5
  0
Nice adaption!
Jack_sparrow
« Censeur »
1520422500000
    • Jack_sparrow#8381
    • Profil
    • Derniers messages
    • Tribu
#6
  1
Thanks!!
Yatsuki
« Censeur »
1520422680000
    • Yatsuki#9574
    • Profil
    • Derniers messages
#7
  1
thank you
Massi
« Consul »
1520422740000
    • Massi#0095
    • Profil
    • Derniers messages
    • Tribu
#8
  1
Nice, thank you !!!
Bolodefchoco
« Sénateur »
1520431560000
    • Bolodefchoco#0095
    • Profil
    • Derniers messages
    • Tribu
#9
  3
tragic update tho

nice
Dryqz
« Citoyen »
1520515800000
    • Dryqz#6558
    • Profil
    • Derniers messages
#10
  1
grax<3
Muutluerkek
« Citoyen »
1520885820000
    • Muutluerkek#0000
    • Profil
    • Derniers messages
    • Tribu
#11
  3
not cool new name system :/
Sebafrancuz
« Consul »
1520925360000
    • Sebafrancuz#0000
    • Profil
    • Derniers messages
    • Tribu
#12
  1
Useful function, thank you Shamousey!
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • [Function] Handling New Names (Name#0000) in Lua
© Atelier801 2018

Equipe Conditions Générales d'Utilisation Politique de Confidentialité Contact

Version 1.27