ULX

Author Topic: Auto-Force Spectate on team switch?  (Read 1473 times)

0 Members and 1 Guest are viewing this topic.

Offline sirrfuchs

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Auto-Force Spectate on team switch?
« on: November 26, 2016, 06:18:04 PM »
Hey there! I run a TDM Conquest Server, and I'm running into a few problems here and there, I'm very limited in time and investments into coding currently (70 hr/w Job / Family and Kids).


One of the problems that I have right now is that currently we have two(2)teams, and when one team is losing, people from that team will switch to the other team (winning team)to avoid the loss.

I know there's probably add-ons or things that can combat this, but currently our developer is working on bigger issues at the moment, so I've come here for some helpful advice / help!

What I want to happen is that when someone switches from one team to another after 3 minutes have passed to be forced into spectator for (x) amount of time (Configurable), and the punishment to gradually increase for insert.player.


A few pieces of information.

It's a custom-coded game mode, Each (Match) has a 30 minute timer, unless it's Conquest where as we use a (Ticket reducing) system to concur who the winner is.

We do use ULX, and I'm hoping that we can integrate something from ULX to combat our issues above.

I'm unsure of what other information is pertinent for this, so if you do need any other information, please let me know below!


I do have several ways of communication, so please let me know your preferred method!


We've already a system in place to auto-balance each team as seen below:

Code: [Select]

SetGlobalInt( "Unbalanced", 0 )
local teamPrevent = 0
hook.Add( "Think", "TeamBalance", function()
if CurTime() > teamPrevent then
teamPrevent = CurTime() + 4
local amtB = #team.GetPlayers( 2 )
local amtR = #team.GetPlayers( 1 )
if amtB - amtR >= 3 then
if GetGlobalInt( "Unbalanced" ) ~= 2 then
SetGlobalInt( "Unbalanced", 2 )
end
elseif amtR - amtB >= 3 then
if GetGlobalInt( "Unbalanced" ) ~= 1 then
SetGlobalInt( "Unbalanced", 1 )
end
elseif amtB - amtR < 3 and amtR - amtB < 3 then
if GetGlobalInt( "Unbalanced" ) ~= 0 then
SetGlobalInt( "Unbalanced", 0 )
end
end
end
end )

function team.GetSortedPlayers( tea )
local tab = team.GetPlayers( tea )
table.sort( tab, function( a, b ) return a:Frags() > b:Frags() end )
return tab
end

timer.Create( "TeamBalance", 30, 0, function()
if GetGlobalInt( "Unbalanced" ) > 0 then
local more
local less
if GetGlobalInt( "Unbalanced" ) == 1 then
more = 1
less = 2
elseif GetGlobalInt( "Unbalanced" ) == 2 then
more = 2
less = 1
end
local tmore = team.GetSortedPlayers( more )
local tless = team.GetSortedPlayers( less )
local diff = math.floor( ( #tmore - #tless ) / 2 )
local temp = {}
for i = 1, diff do
local ply = tmore[ #tmore - ( i - 1 ) ]
ply:ConCommand( "tdm_setteam " ..  less )
table.insert( temp, ply:Nick() )
end
local exp = table.concat( temp, ", " )
ULib.tsayColor( nil, false, Color( 255, 255, 255 ), "Player(s) " .. exp .. " moved for team balance." )
end
end )


 

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Auto-Force Spectate on team switch?
« Reply #1 on: November 26, 2016, 06:38:58 PM »
First, when using the [code] tags for lua, try to use [code=lua] to give some syntax highlighting and line numbers, it makes it easier to read.

Secondly, I'm not familiar with the gamemode you are using, is there a pre-built in Spectator system, or is it going to need to be coded from scratch? I'd be glad to try and help you, but I need to know if there is a specific command to move someone/yourself to spectator while in the game, or if it needs to be made.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Auto-Force Spectate on team switch?
« Reply #2 on: November 26, 2016, 10:39:52 PM »
Sir's use of code tags is fine.
Your opinion is your own.
Not sure our SMF lua knows Gmod lua anyway.

Sir,
Many of us here too are limited in time.
However, I hope the logic/pseudocode below might help you, or someone helping you, get started.
No, the below not 100% Gmod or lua; it's just a representation. You'll need to fill in many blanks on your own.
Code: [Select]
-- set a variable to current time
local RoundStartTime = GetCurTime()

local function roundtimestart()
    -- New round started, set new time
    RoundStartTime = GetCurTime()
end

-- use the hook that triggers at a round start.
hook.add( GMOD_ROUND_START_HOOKNAME, "get_my_round_start", roundtimestart )


local function punishteamswitcher( player )
    -- Check if current time minus round start time is greater than 3 minutes
    if GetCurTime() - RoundStartTime > 3_minutes_in_seconds?_I_forget_what_GetCurTime_does then
        -- Do you have a teamswitch command?
        do_your_change_team(player, team_spectate)
    end
end

-- use the hook that triggers at a team change
hook.add (GMOD_TEAM_SWITCH_HOOKNAME, "switch_punisher", punishteamswitcher )
« Last Edit: November 26, 2016, 10:44:17 PM by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming