Jump to content

[QUESTION] Would this code work the right way?


koragg

Recommended Posts

So I have this script I made edited which counts a player's winstreaks. And if another player wins it shows message that the current winner has broken the previous guy's winstreak.

Well, yes but if the previous guy leaves the server then there is no text when someone else beats his streak (as the previous guy is not online anymore and there is no way to get his data).

Would the below code show the message even if the previous streak's owner has left the server?

local opponentStreak
local opponentName
-------------------------------------------------------------------------------------------------------------------------
function onQuit()
	if getElementData(source, "Streak") then
		opponentStreak = getElementData(source, "Streak")
		opponentName = getPlayerName(source)
	end
end
addEventHandler("onPlayerQuit", root, onQuit)
-------------------------------------------------------------------------------------------------------------------------
addEvent("onPlayerRaceWinStreak")
function winstreak()
    local players = getElementsByType("player")
    local playeraccount = getPlayerAccount(source) 
	
    local minPlayers = 5
	if #players < minPlayers then
		setElementData(source, "Streak", nil)
        return
    end
           
    local wins = getElementData(source, "Streak") or 0
    wins = wins + 1
	if wins > 1 then
		if isGuestAccount(playeraccount) then
			exports.messages:outputGameMessage(getPlayerName(source):gsub("#%x%x%x%x%x%x", "").." did a WinStreak of "..wins, getRootElement(), 2, 30, 144, 255)
		else
			local knightc = 0
			local pointsc = 0
            if wins == 2 then
				knightc = wins
				pointsc = wins * wins
            elseif wins == 3 then
				knightc = wins
				pointsc = wins * wins
		    elseif wins == 4 then
				knightc = wins
				pointsc = wins * wins
			elseif wins == 5 then
				knightc = wins
				pointsc = wins * wins
            elseif wins == 6 then
				knightc = wins
				pointsc = wins * wins
			elseif wins == 7 then
				knightc = wins
				pointsc = wins * wins
			elseif wins == 8 then
				knightc = wins
				pointsc = wins * wins
			elseif wins == 9 then
				knightc = wins
				pointsc = wins * wins
			elseif wins > 9 then
				knightc = wins
				pointsc = wins * wins
			end
			local kc = tonumber(getAccountData(playeraccount, "knightcoins") or 0) + knightc
			local pts = tonumber(getAccountData(playeraccount, "points") or 0) + pointsc
			exports.messages:outputGameMessage("#FFFFFF"..addTeamColor(source).." earned "..knightc.." KnightCoins and "..pointsc.." Points for his WinStreak of "..wins.." [("..comma_value(kc).."), ("..comma_value(pts)..")]", getRootElement(), 2, 30, 144, 255)   
			local mesaj = "You earned "..knightc.." KnightCoins and "..pointsc.." Points for your WinStreak of "..wins.." [("..comma_value(kc).."), ("..comma_value(pts)..")]"
			triggerClientEvent(source ,"serverMEsajlarAl", source, mesaj)
			setAccountData(playeraccount, "knightcoins", kc)
			setElementData(source ,"data.knightcoins", kc, true)
			setAccountData(playeraccount, "points", pts)
			setElementData(source ,"data.points", pts, true)
			triggerClientEvent(source, "addNotification", root, "#FEFE22+"..knightc.." #00FFFFKnightCoins #00FF00("..comma_value(kc)..")", 1)
			local oldEarningsData = getAccountData(playeraccount, "totalEarnings") or 0
			local newEarningsData = oldEarningsData + knightc
			setAccountData(playeraccount, "totalEarnings", newEarningsData)
			setElementData(source, "totalEarnings", newEarningsData)
		end
	end
	
	local winstreaks = getElementData(source, "Streak" ) or 0
    if winstreaks < wins then
		setElementData(source , "winstreak", wins, true)
    end       
	setElementData(source, "Streak", wins)
    -- clear opponents streak
    for _, player in ipairs(players) do
        if player ~= source then
            if getElementData(player, "Streak") then
                if getElementData(player, "Streak") > 1 then
					exports.messages:outputGameMessage(getPlayerName(source):gsub("#%x%x%x%x%x%x", "").." broke "..getPlayerName(player):gsub("#%x%x%x%x%x%x", "").."'s WinStreak of "..getElementData(player, "Streak"), getRootElement(), 2, 30, 144, 255)
				end
                setElementData(player, "Streak", nil)
            end
        end
	end
	if opponentStreak and opponentName then
		if opponentStreak > 1 then
			exports.messages:outputGameMessage(getPlayerName(source):gsub("#%x%x%x%x%x%x", "").." broke "..opponentName:gsub("#%x%x%x%x%x%x", "").."'s WinStreak of "..opponentStreak, getRootElement(), 2, 30, 144, 255)
		end
		opponentStreak = nil
		opponentName = nil
	end
    return
end
addEventHandler("onPlayerRaceWinStreak", root, winstreak)

Basically I save the guy's nickname and winstreaks count to some global variables when he quits and then display those IF the player is no longer online and another one has won 1+ times.

Edited by koragg
I can't test alone ... that's why i ask here.
Link to comment

Yes, that should work, but make sure you only use the opponentStreak and opponentName when the player is no longer online, because it will only update those values when the last winner leaves, not when there's a new (different) winner. I'm just looking at this part of the code:

			if wins == 2 then
				knightc = wins
				pointsc = wins * wins
            elseif wins == 3 then
				knightc = wins
				pointsc = wins * wins
		    elseif wins == 4 then
				knightc = wins
				pointsc = wins * wins
			elseif wins == 5 then
				knightc = wins
				pointsc = wins * wins
            elseif wins == 6 then
				knightc = wins
				pointsc = wins * wins
			elseif wins == 7 then
				knightc = wins
				pointsc = wins * wins
			elseif wins == 8 then
				knightc = wins
				pointsc = wins * wins
			elseif wins == 9 then
				knightc = wins
				pointsc = wins * wins
			elseif wins > 9 then
				knightc = wins
				pointsc = wins * wins
			end

Why do you repeat the same thing over and over again? Aren't all the elseif's the same?

Link to comment
45 minutes ago, pa3ck said:

Yes, that should work, but make sure you only use the opponentStreak and opponentName when the player is no longer online, because it will only update those values when the last winner leaves, not when there's a new (different) winner.

Isn't this responsible for the time when the player is online (new (different) winner) ?

for _, player in ipairs(players) do
	if player ~= source then
    	if getElementData(player, "Streak") then
        	if getElementData(player, "Streak") > 1 then
			exports.messages:outputGameMessage(getPlayerName(source):gsub("#%x%x%x%x%x%x", "").." broke "..getPlayerName(player):gsub("#%x%x%x%x%x%x", "").."'s WinStreak of "..getElementData(player, "Streak"), getRootElement(), 2, 30, 144, 255)
		end
            setElementData(player, "Streak", nil)
        end
    end
end

It searches for the "Streak" data in all online players and if it's higher than 1 it shows text. And only one player can have "Streak" higher than 1 as it is a winstreak (goes up by 1 only if you win, if someone else wins it goes to nil). If the line "if player ~= source then" is missing the player argument (he left) then it jumps to the "oponentName" part which is identical as this^ just with exchanged values for the opponent as he's left now.

Quote

Why do you repeat the same thing over and over again? Aren't all the elseif's the same?

Ignore it xD That was one of my very first scripts I put on my server 2 years ago, haven't really touched it since then. Gonna optimise it a lot soon (if you see how it checks what suffix to give after each number, you'll lol).

Edited by koragg
Link to comment
13 minutes ago, koragg said:

Isn't this responsible for the time when the player is online (new (different) winner) ?


for _, player in ipairs(players) do
	if player ~= source then
    	if getElementData(player, "Streak") then
        	if getElementData(player, "Streak") > 1 then
			exports.messages:outputGameMessage(getPlayerName(source):gsub("#%x%x%x%x%x%x", "").." broke "..getPlayerName(player):gsub("#%x%x%x%x%x%x", "").."'s WinStreak of "..getElementData(player, "Streak"), getRootElement(), 2, 30, 144, 255)
		end
            setElementData(player, "Streak", nil)
        end
    end
end

 

Yes, that pretty much does what you said, but as far as I understand your code, whatever this code does, the

if opponentStreak and opponentName then

part might still be true on rare occasions. It would be better to put them in an else block, if the one with the loop found someone, do not run this code as well, it's just one of those things that might never happen, but when it does, you have no idea what caused it.

 

13 minutes ago, koragg said:

Ignore it xD That was one of my very first scripts I put on my server 2 years ago, haven't really touched it since then. Gonna optimise it a lot soon (if you see how it checks what suffix to give after each number, you'll lol).

I know right? I still have a couple of years old code as well, when I just started pretty much everything (LUA was my first language), it's soo awkward to look at.. :)

Edited by pa3ck
  • Like 1
Link to comment

You can do something like this:

	local strkFnd = false 
	
	for _, player in ipairs(players) do
        if player ~= source then
            if getElementData(player, "Streak") then
                if getElementData(player, "Streak") > 1 then
					exports.messages:outputGameMessage(getPlayerName(source):gsub("#%x%x%x%x%x%x", "").." broke "..getPlayerName(player):gsub("#%x%x%x%x%x%x", "").."'s WinStreak of "..getElementData(player, "Streak"), getRootElement(), 2, 30, 144, 255)
					strkFnd = true
					opponentStreak = nil -- make sure it's cleared
					opponentName = nil
				end
                setElementData(player, "Streak", nil)
            end
        end
	end
	if opponentStreak and opponentName and not strkFnd then
		if opponentStreak > 1 then
			exports.messages:outputGameMessage(getPlayerName(source):gsub("#%x%x%x%x%x%x", "").." broke "..opponentName:gsub("#%x%x%x%x%x%x", "").."'s WinStreak of "..opponentStreak, getRootElement(), 2, 30, 144, 255)
		end
		opponentStreak = nil
		opponentName = nil
	end

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...