Restart the round

Please post any questions about developing your plugin here. Please use the search function before posting!
petros789
Junior Member
Posts: 6
Joined: Sun Feb 08, 2015 9:40 pm

Restart the round

Postby petros789 » Sat Nov 21, 2015 1:18 am

Hello, how would I go about restarting the round on command?
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Sat Nov 21, 2015 1:20 am

You need to execute the "mp_restartgame <delay>" command on the server. This code restarts the round 1 second after typing "!restart":

Syntax: Select all

from engines.server import engine_server
from events import Event

@Event('player_say')
def on_player_say(event):
if event['text'] == '!restart':
engine_server.server_command('mp_restartgame 1\n')
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Postby Doldol » Sat Nov 21, 2015 1:23 am

What do you want exactly?

You could try these console command:

Code: Select all

"mp_restartgame" = "0"
 game
- If non-zero, game will restart in the specified number of seconds

"mp_restartgame_immediate" = "0"
game
- If non-zero, game will restart immediately


Or Mahi could beat me to it, still note the second command I listed. =]
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Sat Nov 21, 2015 12:40 pm

Or, if you want to preserve players and team scores, you might try executing endround cheat command, but you need to strip CHEAT flag from it first.

Syntax: Select all

from cvars import cvar
from cvars.flags import ConVarFlags
from engines.server import engine_server
from events import Event


ENDROUND_TEXT = "endround"
CVAR_ENDROUND = cvar.find_command_base(ENDROUND_TEXT)


def restart_round():
"""Strip the CHEAT flag, execute 'endround' and put the flag back."""
CVAR_ENDROUND.remove_flags(ConVarFlags.CHEAT)
engine_server.server_command('{0};'.format(ENDROUND_TEXT))
engine_server.server_execute()
CVAR_ENDROUND.add_flags(ConVarFlags.CHEAT)


@Event('player_say')
def on_player_say(game_event):
"""Detect !restart chat command"""
if game_event.get_string('text') == "!restart":
restart_round()



Or, if you want to end a round with a certain condition, you might want to check out this example by Doldol
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Sat Nov 21, 2015 1:01 pm

iPlayer wrote:I tried putting the flag back right after executing endround, but it didn't work for me, so I decided to put the flag back when a new round starts.
I guess this is caused by server_command adding the command to the parser which then executes waiting commands once a frame. You need to do something like this:

Syntax: Select all

engine_server.server_command('blah')
engine_server.server_execute() # Force any commands waiting to be executed now...
...
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Sat Nov 21, 2015 1:05 pm

Syntax: Select all

engine_server.server_execute()

That works, thanks. I edited my previous post.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Restart the round

Postby iPlayer » Fri Sep 16, 2016 10:09 pm

How safe it is to execute engine_server.server_execute() multiple times per frame? Won't it crash the server?
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re:

Postby satoon101 » Fri Sep 16, 2016 10:36 pm

Mahi wrote:You need to execute the "mp_restartgame <delay>" command on the server. This code restarts the round 1 second after typing "!restart":

Syntax: Select all

from engines.server import engine_server
from events import Event

@Event('player_say')
def on_player_say(event):
if event['text'] == '!restart':
engine_server.server_command('mp_restartgame 1\n')

Just wanted to add a note that the \n or ; is no longer necessary, as we handle that internally:
https://github.com/Source-Python-Dev-Te ... ines.h#L54
Image
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Restart the round

Postby satoon101 » Fri Sep 16, 2016 11:09 pm

iPlayer wrote:How safe it is to execute engine_server.server_execute() multiple times per frame? Won't it crash the server?

I 'believe' it is safe. It just executes all commands in the current parser. So, if there are no commands in the parser, it just shouldn't do anything. You could always test that to verify.

Syntax: Select all

for x in range(100000):
engine_server.server_execute()
Image
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Restart the round

Postby iPlayer » Fri Sep 16, 2016 11:15 pm

The reason I asked is because of "insertcmd" from EventScripts. It was followed by warnings that it's unsafe to change the flow of the command queue.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Restart the round

Postby satoon101 » Sat Sep 17, 2016 2:26 am

InsertCommand is completely different from ServerExecute. With InsertCommand, you are inserting the command into the parser at the beginning, whereas ServerCommand inserts at the end. ServerExecute just executes the current command parser instead of waiting till the appropriate time during the current tick to do so.
Image
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Restart the round

Postby Ayuto » Sat Sep 17, 2016 6:41 am

I think the reason for this warning was, because there were some random crashes with ES on Linux in combination with psyco. And it was assumed that functions like insertcmd, es.remove and es.fire were causing the crashes, because queuing these functions was working fine.

Edit:
I should also mention that we are planning to update the commands functions, so there is only an execute_server_command() function to execute a command immediately using the Dispatch() method and a queue_server_command() function to queue a command. I already have the code for that locally, but just haven't commited it yet.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re:

Postby iPlayer » Thu Sep 22, 2016 2:09 am

iPlayer wrote:

Syntax: Select all

def restart_round():
"""Strip the CHEAT flag, execute 'endround' and put the flag back."""
CVAR_ENDROUND.remove_flags(ConVarFlags.CHEAT)
engine_server.server_command('{0};'.format(ENDROUND_TEXT))
engine_server.server_execute()
CVAR_ENDROUND.add_flags(ConVarFlags.CHEAT)


Today the code above doesn't seem to work for some reason. Server still reports
Can't use cheat command endround in multiplayer, unless the server has sv_cheats set to 1.
even though there's engine_server.server_execute() right before putting CHEAT flag back on.

But! The following works fine:

Syntax: Select all

from commands import Command

...

def restart_round():
CVAR_ENDROUND.remove_flags(ConVarFlags.CHEAT)
CVAR_ENDROUND.dispatch(Command())
CVAR_ENDROUND.add_flags(ConVarFlags.CHEAT)
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Restart the round

Postby L'In20Cible » Thu Sep 22, 2016 2:31 am

You should avoid playing with cheat commands. Just use an  info_map_parameter entity.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Restart the round

Postby iPlayer » Thu Sep 22, 2016 2:43 am

Well, you could've posted example from our Cookbook (by Doldol).
Also, why should I avoid them?

This

necavi, even if I unset 'cheat' flag for ent_fire and then immediately set it back, how long does this "immediately" last? Can I strip the flag, use ent_fire and set the flag back in one tick? If so, I guess this is indeed safe. But if I have to bring the flag back in the next tick, this leaves a window for an attacker.


I have always done so in the same tick with no issue, yes, that was the entire premise of this plugin, basically: https://github.com/necavi/Merx
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Restart the round

Postby L'In20Cible » Thu Sep 22, 2016 2:54 am

iPlayer wrote:Well, you could've posted example from our Cookbook (by Doldol).
I redirected where I knew an example was.
iPlayer wrote:Also, why should I avoid them?
Cause they are... cheats for a reason. Also, for the simple fact that you assume a state of sv_cheats. What if a server that run your codes wants cheats enabled on their server? Exactly, you will mess his settings.

iPlayer wrote:This

necavi, even if I unset 'cheat' flag for ent_fire and then immediately set it back, how long does this "immediately" last? Can I strip the flag, use ent_fire and set the flag back in one tick? If so, I guess this is indeed safe. But if I have to bring the flag back in the next tick, this leaves a window for an attacker.


I have always done so in the same tick with no issue, yes, that was the entire premise of this plugin, basically: https://github.com/necavi/Merx

I didn't say it was not working. I just believe they should be avoided when there are alternatives.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Restart the round

Postby iPlayer » Thu Sep 22, 2016 3:00 am

Cause they are... cheats for a reason.

So what? If I can achieve the same result without cheats, but this approach is way more convenient?

Also, for the simple fact that you assume a state of sv_cheats. What if a server that run your codes wants cheats enabled on their server? Exactly, you will mess his settings.

I don't quite get your point. I don't manipulate sv_cheats in any way here. I just strip a CHEAT flag from a particular command and then put it back on.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Restart the round

Postby satoon101 » Thu Sep 22, 2016 3:07 am

I don't understand how it's "way more convenient" than this:

Syntax: Select all

def restart_round():
Entity.find_or_create('info_map_parameters').fire_win_condition(9)
Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Restart the round

Postby L'In20Cible » Thu Sep 22, 2016 3:29 am

iPlayer wrote:I don't quite get your point. I don't manipulate sv_cheats in any way here. I just strip a CHEAT flag from a particular command and then put it back on.

My bad I assumed you did, but my point remains the same. What if I want that flag removed on my server? You will put it back in and mess my settings.

satoon101 wrote:I don't understand how it's "way more convenient" than this:

Syntax: Select all

def restart_round():
Entity.find_or_create('info_map_parameters').fire_win_condition(9)

Exactly what I think too.

However, I just pointed out that there was better (in my opinion) alternatives but you really seems to be on the defensive and sticky on that method so next time I will just avoid proposing them. :wink:
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Restart the round

Postby iPlayer » Thu Sep 22, 2016 3:30 am

I don't feel like creating a new entity and firing its input just for the sake of restarting the round.

I suppose Invincible misread my code. I didn't set sv_cheats to 1, execute my command and then reset sv_cheats to 0. If that were the case, then I could indeed mess server settings.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 18 guests