ULX

Author Topic: Change to a defined map when server is empty  (Read 2746 times)

0 Members and 1 Guest are viewing this topic.

Offline MajorPain931

  • Newbie
  • *
  • Posts: 5
  • Karma: -1
Change to a defined map when server is empty
« on: September 26, 2014, 03:56:03 AM »
Hey guys,

I have a little problem, I can't manage to solve. The thing is, I have a server with mapvoting at the end. But when no one's at the server mapvoting results in random maps. Some of them are not that popular (But I want to keep them to vote) which results in less people joining. So I want to make my server automatically change map to a certain rather popular map when no one's ingame and round's over. How can I accomplish this?
I looked around the Hooks, but I didn't find one, which fit my purposes, making it change the map all the time when no one's online would work, but results in high useless cpu usage... I'd like to avoid it.  So, a simple script, which checks if players are online (Not the problem, I can do this) but just at the end of the map...
Can some1 help me? I just need a hint, no done script.

Regards MP

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Change to a defined map when server is empty
« Reply #1 on: September 26, 2014, 07:38:28 AM »
Use the Player disconnect hook
Check server, no one on? Start a 2(?) minute timer
Playerconnect(or spawn) hook - check for and cancel timer if it exists.
Times up? Check for players again  (just to be sure we're not changing and someone is on).
If on, do nothing
If no one is on, change map.

You may need to adjust your timer length if it takes a while for folks to connect between map changes.
Could even do a 10-15 minute just for an 'all clear'
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MajorPain931

  • Newbie
  • *
  • Posts: 5
  • Karma: -1
Re: Change to a defined map when server is empty
« Reply #2 on: September 27, 2014, 12:46:35 AM »
Nice, I will give it a try and tell you, if it works (With code, should some1 need same script, too) :)

Thanks

Offline MajorPain931

  • Newbie
  • *
  • Posts: 5
  • Karma: -1
Re: Change to a defined map when server is empty
« Reply #3 on: September 27, 2014, 02:33:40 AM »
Okay, here, what I've written.
I didn't find any command which returns number of players connected (Is there any?), so I had to do it that way.
When map is changed and it's not the defined one, server changes map properly. And when some1 joins timer stops.
But the disconnecting thingy doesn't work... The player disconnects but the function doesn't get called at all? Used "print( "Function got called" )" before the checks to determine it's getting called, but it's not.
But that's not a big deal, since the map just needs about 30 minutes till it's over even if no one is online.
Maybe some1 can refine my code or give me some hints? That's the first real code I wrote in lua and for GMod...


Code:
Code: [Select]
local changeMap = function()
        if (table.Count(player.GetAll()) == 0) then
                RunConsoleCommand( "changelevel", "ph_restaurant" )
        end
end

local startTimer = function (i)
        if (table.Count(player.GetAll()) == i ) then
                if(!timer.Exists("mapChanger")) then
                        if (game.GetMap() != "ph_restaurant") then
                                timer.Create( "mapChanger", 180, 1, changeMap)
                        end
                end
        end
end

local stopTimer = function ()
        if (timer.Exists("mapChanger")) then
                timer.Remove("mapChanger")
        end
end

hook.Add("Initialize", "StartTimerInit", startTimer(0))
hook.Add("PlayerConnect", "StopTimerConnect", stopTimer)
hook.Add("PlayerDisconnected", "StartTimerDC", startTimer(1))

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Change to a defined map when server is empty
« Reply #4 on: September 28, 2014, 08:46:23 AM »
table.Count ... player.getall is only way I know to count players.
I found http://wiki.garrysmod.com/page/Player/GetCount too, but you'd have to specify the "player" type entity name, and I don't know/remember that (or where to find it)
It's also Sandbox and derived only.

Where exactly are you placing the "got called" debug message.
You're passing 1 to the function on disconnect. If that was the last player, (meaning 0(count...getall) != 1(i), it's never going to start that timer.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MajorPain931

  • Newbie
  • *
  • Posts: 5
  • Karma: -1
Re: Change to a defined map when server is empty
« Reply #5 on: September 28, 2014, 10:40:56 AM »
Got it now. Had some strange behaviour with the disconnected hook at first.
The hook got called but getPlayers still returned the disconnecting player in the table. That's why I used "== 1".
But now this doesn't happen anymore, when it gets called, the table is empty. Don't know why it was different at the beginning.
Thanks for helping me, was working fine today and yesterday :)