Jump to content

spectator problem in rankingboard


Fury

Recommended Posts

rankingboard_client.lua:

local sx,sy = guiGetScreenSize() 
 
RankingBoard = {}
RankingBoard.__index = RankingBoard
 
RankingBoard.instances = {}
 
local screenWidth, screenHeight = guiGetScreenSize()
local topDistance = 250
local bottomDistance = 0.3*screenHeight
local posLeftDistance = 30
local nameLeftDistance = 60
local labelHeight = 16
local maxPositions = math.floor((screenHeight - topDistance - bottomDistance)/labelHeight)
 
posLabel = {}
playerLabel = {}
alphaElem = {}
colorElem = {}
biggestLabelWidth = 0
 
function resetRankingboard ()
    for id, elem in pairs(playerLabel) do
        setElementData(elem,"spectated",false)
    end
    for i, val in ipairs(Spectators) do
        table.remove(Spectators, i)
    end
posLabel = {}
playerLabel = {}
Spectators = {}      
alphaElem = {}
colorElem = {}
biggestLabelWidth = 0
end
addEvent("onClientMapStopping",true)
addEventHandler ( "onClientMapStopping", getRootElement(), resetRankingboard )
 
function resetSpectators ()
    for i, val in ipairs(Spectators) do
        table.remove(Spectators, i)
    end
    for id, elem in pairs(playerLabel) do
        setElementData(elem,"spectated",false)
    end  
Spectators = {}      
end
addEvent("onClientPlayerWasted",true)
addEventHandler ( "onClientPlayerWasted", getLocalPlayer(), resetSpectators )
 
function RankingBoard.create(id)
    RankingBoard.instances[id] = setmetatable({ id = id, direction = 'down', labels = {}, position = 0 }, RankingBoard)
    posLabel = {}
    playerLabel = {}
end
 
function RankingBoard.call(id, fn, ...)
    RankingBoard[fn](RankingBoard.instances[id], ...)
end
 
function RankingBoard:setDirection(direction, plrs)
    self.direction = direction
    if direction == 'up' then
        self.highestPos = plrs--#g_Players
        self.position = self.highestPos + 1
    end
end
 
function RankingBoard:add(name, time)
    local position
    local y
    local doBoardScroll = false
    if self.direction == 'down' then
        self.position = self.position + 1
        if self.position > maxPositions then
            return
        end
        y = topDistance + (self.position-1)*labelHeight
    elseif self.direction == 'up' then
        self.position = self.position - 1
        local labelPosition = self.position
        if self.highestPos > maxPositions then
            labelPosition = labelPosition - (self.highestPos - maxPositions)
            if labelPosition < 1 then
                labelPosition = 0
                doBoardScroll = true
            end
        elseif labelPosition < 1 then
            return
        end
        y = topDistance + (labelPosition-1)*labelHeight
    end
    posLabel[name], posLabelShadow = createShadowedLabelFromSpare(posLeftDistance, y, 20, labelHeight, tostring(self.position) .. ')', 'right')
    if time then
        if not self.firsttime then
            self.firsttime = time
            time = '' .. msToTimeStr(time)
        else
            time = '#ffffff+' .. msToTimeStr(time - self.firsttime)
        end
    else
        time = ''
    end
    --time = "  "
    playerLabel[name], playerLabelShadow = createShadowedLabelFromSpare(nameLeftDistance, y, 250, labelHeight, name ..' #abcdef:: #ffffff'..time..'')
    local team = getPlayerTeam( getPlayerFromName( name ) )
    if team then
        Cr,Cg,Cb = getTeamColor(team)
    else
        Cr,Cg,Cb = 255,255,255
    end
    table.insert(self.labels, posLabel[name])
    table.insert(self.labels, posLabelShadow)
    table.insert(self.labels, playerLabel[name])
    table.insert(self.labels, playerLabelShadow)
    alphaElem[playerLabel[name]] = 0
    alphaElem[posLabel[name]] = 0
    colorElem[playerLabel[name]] = {30,30,30}
    playSoundFrontEnd(7)
        guiSetAlpha(posLabel[name], 0)
        guiSetAlpha(posLabelShadow, 0)
        guiSetAlpha(playerLabel[name], 0)
        guiSetAlpha(playerLabelShadow, 0)      
    if doBoardScroll then          
        local anim = Animation.createNamed('race.boardscroll', self)
        anim:addPhase({ from = 0, to = 1, time = 1250, fn = RankingBoard.scroll, firstLabel = posLabel[name] })
        anim:addPhase({ fn = RankingBoard.destroyLastLabel, firstLabel = posLabel[name] })
        anim:play()
    end
    for id, elem in pairs(playerLabel) do
            local widthRezise1 = dxGetTextWidth(string.gsub( guiGetText(elem), '#%x%x%x%x%x%x', '' ),1,"default-bold")
            if widthRezise1 > biggestLabelWidth then
                biggestLabelWidth = widthRezise1
            end
        end
end
 
function RankingBoard:scroll(param, phase)
    local firstLabelIndex = table.find(self.labels, phase.firstLabel)
    for i=firstLabelIndex,firstLabelIndex+3 do
        guiSetAlpha(self.labels[i], param)
    end
    local x, y
    for i=0,#self.labels/4-1 do
        for j=1,4 do
            x = (j <= 2 and posLeftDistance or nameLeftDistance)
            y = topDistance + ((maxPositions - i - 1) + param)*labelHeight
            if j % 2 == 0 then
                x = x + 1
                y = y + 1
            end
            guiSetPosition(self.labels[i*4+j], sx + x, y, false)
        end
    end
    for i=1,4 do
        guiSetAlpha(self.labels[i], 1 - param)
    end
end
 
function RankingBoard:destroyLastLabel(phase)
    for i=1,4 do
        destroyElementToSpare(self.labels[1])
        guiSetVisible(self.labels[1],false)
        table.remove(self.labels, 1)
    end
    local firstLabelIndex = table.find(self.labels, phase.firstLabel)
    for i=firstLabelIndex,firstLabelIndex+3 do
        guiSetAlpha(self.labels[i], 1)
    end
end
 
function RankingBoard:addMultiple(items)
    for i,item in ipairs(items) do
        self:add(item.name, item.time)
    end
end
 
 
function testing (c,name)
RankingBoard:add(name)
end
addCommandHandler("addt",testing)
 
function RankingBoard:clear()
    table.each(self.labels, destroyElementToSpare)
    self.labels = {}
end
 
function RankingBoard:destroy()
    self:clear()
    RankingBoard.instances[self.id] = nil
end
 
Spectators = {}
addEvent('addSpectator', true)
addEvent('removeSpectator', true)
 
addEventHandler('addSpectator', getRootElement(),
    function(Spectator)
        table.insert(Spectators, Spectator)
        for idS, spectatorer in pairs(Spectators) do
            for id, elem in pairs(playerLabel) do
                if id == getPlayerNametagText(spectatorer) then
                    setElementData(elem,"spectated",true)
                end
            end
        end
    end  
)
 
addEventHandler('removeSpectator', getRootElement(),
    function(spectator)
        for i, val in ipairs(Spectators) do
            if (val == spectator) then
                table.remove(Spectators, i)
                for id, elem in pairs(playerLabel) do
                    if id == getPlayerNametagText(val) then
                        setElementData(elem,"spectated",false)
                    end
                end
            end
        end
    end
)
 
--
-- Label cache
--
 
 
local spareElems = {}
local donePrecreate = false
 
function RankingBoard.precreateLabels(count)
    donePrecreate = false
    while #spareElems/4 < count do
        local label, shadow = createShadowedLabel(10, 1, 20, 10, 'a' )
        --guiSetAlpha(label,0)
        guiSetAlpha(shadow,0)
       
 
       
        guiSetVisible(label, false)
        guiSetVisible(shadow, false)
        destroyElementToSpare(label)
        destroyElementToSpare(shadow)
    end
    donePrecreate = true
end
 
function destroyElementToSpare(elem)
   
Link to comment
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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