Scoreboard tags (MYSQL)

Well, went off trying to learn MYSQL query’s and managed to come up with something, however I cant seem to think of a practical way of doing this (one that actually works in this case).

Basically I have a file called stats.lua and in that is:

[lua]
require (“mysqloo”) // Include the modules

local DATABASE_HOST = “" // database host (can be an ip)
local DATABASE_PORT = 3306 // port to the database, you probably wont need to change this unless you get told to
local DATABASE_NAME = "
” // name of the database
local DATABASE_USERNAME = “" // username which you use to access it
local DATABASE_PASSWORD = "
” // password of the username
testtest = 1

function connectToDatabase()
databaseObject = mysqloo.connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT)
databaseObject.onConnected = function() print(“Database linked!”) end
databaseObject.onConnectionFailed = function() print(“Failed to connect to the database.”) end
databaseObject:connect()
end

connectToDatabase()

function checkQuery(query)
local playerInfo = query:getData()
if playerInfo[1] ~= nil then
return true
else
return false
end
end

function FirstJoinMysql( ply )
local query1 = databaseObject:query(“SELECT * FROM Players WHERE SteamID = '” … ply:SteamID() … “’”)
query1.onSuccess = function(q)
if not checkQuery(q) then
local query2 = databaseObject:query(“INSERT INTO Players(SteamID, Rank) VALUES (’” … ply:SteamID() … "’, " … 0 … “)”) // else create the bugger
query2.onSuccess = function(q) print(“Created you!”) end
query2.onError = function(q,e) print(“something went wrong”) end
query2:start()
else
print(“You are already created!”)
end
end
query1.onError = function(q,e) print(“something went wrong when checking”) end
query1:start()
end
hook.Add( “PlayerInitialSpawn”, “PlayerInitialSpawn”, FirstJoinMysql )
[/lua]

That’s great, also what I did was in dzplayerpanel.lua (Where the scoreboard is created and controlled) I have done this:

[lua]
function PANEL:Refresh()
local pl = self:GetPlayer()
if pl:IsValid() then
local tall = self:GetTall()

result = sql.Query("SELECT Rank FROM Players WHERE SteamID = '"..pl:SteamID().."'")
if (result) == 1 then
	self.m_RankLabel:SetText("VIP")
	self.m_RankLabel:SetTextColor(COLOR_YELOW)
elseif (result) == 2 then
	self.m_RankLabel:SetText("ADMIN")
	self.m_RankLabel:SetTextColor(COLOR_RED)
else
	self.m_RankLabel:SetText("GUEST")
	self.m_RankLabel:SetTextColor(COLOR_WHITE)
end


	self.m_RankLabel:SizeToContents()
	self.m_NameLabel:SetText(pl:Name())
	self.m_NameLabel:SizeToContents()
	if pl:Alive() then
		self.m_NameLabel:SetTextColor(team.GetColor(pl:Team()))
	else
		self.m_NameLabel:SetTextColor(COLOR_GRAY)
	end

	self.m_ScoreLabel:SetText(pl:Frags())
	self.m_ScoreLabel:SizeToContents()

	local ping = pl:Ping()
	local colPing
	if 200 < ping then
		colPing = COLOR_RED
	elseif 150 < ping then
		colPing = COLOR_YELLOW
	elseif 100 < ping then
		colPing = color_white
	elseif 60 < ping then
		colPing = COLOR_CYAN
	else
		colPing = COLOR_LIMEGREEN
	end
	self.m_PingLabel:SetText(ping)
	self.m_PingLabel:SizeToContents()
	self.m_PingLabel:SetTextColor(colPing)

	self:RefreshTeam()
end

self:InvalidateLayout()

end
[/lua]

Now in that I have this:

[lua]

result = sql.Query(“SELECT Rank FROM Players WHERE SteamID = '”…pl:SteamID()…"’")
if (result) == 1 then
self.m_RankLabel:SetText(“VIP”)
self.m_RankLabel:SetTextColor(COLOR_YELOW)
elseif (result) == 2 then
self.m_RankLabel:SetText(“ADMIN”)
self.m_RankLabel:SetTextColor(COLOR_RED)
else
self.m_RankLabel:SetText(“GUEST”)
self.m_RankLabel:SetTextColor(COLOR_WHITE)
end
[/lua]

Call me stupid, but that does not look right at all for me, I don’t know why. There is no errors at all but I am always made a Guest. (I have done [lua]include( “stats.lua” )
AddCSLuaFile( “stats.lua” )[/lua] at the top of the script.

Basically, what I want is that if the Rank from the Players MYSQL table is 1 then do w/e I said in that lua above. I tried different methods but I cant seem to get [lua]self.m_RankLabel[/lua] to work in any other file (gathering that it’s a client file), I also tried [lua]IncludeClientFile(“dzplayerpanel”)[/lua] but that didn’t work.

Again any help is highly appreciated.

You can’t use sql functions from the client. You have to send a umsg with the results.

I had a look on the wiki and to send to an individual player they used:

[lua]
local filter = RecipientFilter();
filter:AddPlayer( pl1 );
filter:AddPlayer( pl3 );

umsg.Start( id, filter );
umsg.End();
[/lua]

I don’t fully understand that as how would I send the players Rank to them. (Basically I need the ranks to be sent to the scoreboard which will determine who is and who isn’t VIP/Admin. It checks this individually on client side, I cant think of a better way of doing this.)

SERVER:



result = sql.Query("SELECT Rank FROM Players WHERE SteamID = '"..pl:SteamID().."'") 
local filter = RecipientFilter()
filter:AddPlayer( pl );
umsg.Start('RankSend',filter )
umsg.Short(result)
umsg.end


CLIENT:



usermessage.Hook('RankSend',function(data)
LocalPlayer().Rank = data:ReadShort()
end)