DBQuery, Check Name, if the same than reject command?
5 replies, posted
I'm trying to make an Organization's system for my rp gamemode.
But with my lua knowhow, I'm not sure how to make it so, when a player creates an Organization, it checks for any other organizations that have the same name.
And if so, rejects it.
The table is Cyderrp_organization and the field for the organization, is 'org'.
If anyone can help, it'd be greatly appreciated.
[lua]local function Organization(ply, args)
local len = string.len(args)
local low = string.lower(args)
if len > 30 then
Notify(ply, 1, 4, string.format("Your Organization name is too long."))
return ""
elseif len < 3 then
Notify(ply, 1, 4, string.format("Your Organization name is too short."))
return ""
end
if string.find(args, "\160") or string.find(args, " ") == 1 then --No system spaces in your name bro!
Notify(ply, 1, 4, string.format(LANGUAGE.unable, "RPname", ""))
return ""
end
if low == "ooc" or low == "shared" or low == "world" or low == "n/a" or low == "world prop" then
Notify(ply, 1, 4, string.format(LANGUAGE.unable, "RPname", ""))
return ""
end
local allowed = { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',
'z', 'x', 'c', 'v', 'b', 'n', 'm', ' ' }
for k in string.gmatch(args, ".") do
if not table.HasValue(allowed, string.lower(k)) then
Notify(ply, 1, 4, string.format(LANGUAGE.unable, "RPname", k))
return ""
end
end
ply:SetOrganization(args)
return ""
end
AddChatCommand("-bigbang", RPName)[/lua]
Yes, it's the copied Name function, and yes, DarkRP. But i'm doing a mass over-haul.
How do you store these organizations? Show us the SetOrganization function please.
unrelated to your question, but for checking allowed characters, do this
[lua]
if string.gmatch(args,"%A") then
Notify(ply, 1, 4, string.format(LANGUAGE.unable, "RPname", k))
return ""
end
[/lua]
%A matches any characters that are NOT a-zA-Z. However, you could use %a to match ONLY a-zA-z (notice the case difference)
so using the above tidbit of code, you could remove the string.lower at the beginning too :p
Thanks, and I'll post my SetOrganization function.
[lua]function DB.StoreOrganization(ply, org)
if not org or string.len(org) < 2 then return end
ply:SetCyderRPVar("organization", org)
DB.QueryValue("SELECT org FROM Cyderrp_organization WHERE steam = " .. sql.SQLStr(ply:SteamID()) .. ";", function(r)
if r then
DB.Query("UPDATE Cyderrp_organization SET org = " .. sql.SQLStr(org) .. " WHERE steam = " .. sql.SQLStr(ply:SteamID()) .. ";")
else
DB.Query("INSERT INTO Cyderrp_organization VALUES(" .. sql.SQLStr(ply:SteamID()) .. ", " .. sql.SQLStr(org) .. ");")
end
end)
end
local organizationlist --Make sure the DB doesn't get checked for ALL Organization when someone InitialSpawns
function DB.RetrieveOrganization(callback)
if organizationlist then
return callback(organizationlist)
end
DB.Query("SELECT * FROM Cyderrp_organization;", function(r)
if r then
organizationlist = r
callback(organizationlist)
else
organizationlist = {}
callback(organizationlist)
end
end)
end
function DB.RetrieveOrganization(ply, callback)
for k,v in pairs(organizationlist or {}) do
if v.steam == ply:SteamID() then return callback(v.org) end -- First check the cache for RP names
end
DB.QueryValue("SELECT org FROM Cyderrp_organization WHERE steam = " .. sql.SQLStr(ply:SteamID()) .. ";", callback)
end
[/lua]
These are all the functions in data.lua that have to do with the database.
you could do...
[lua]
local org = DB.Query("SELECT org FROM Cyderrp_organization WHERE steam = " .. sql.SQLStr(ply:SteamID()) .. ";")
if org == args then
--stuff
else
--stuff
end
[/lua]
Args = what the player typed correct?
Sorry, you need to Log In to post a reply to this thread.