Restart the round
Posted: Sat Nov 21, 2015 1:18 am
Hello, how would I go about restarting the round on command?
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')
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
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()
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: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.
Syntax: Select all
engine_server.server_command('blah')
engine_server.server_execute() # Force any commands waiting to be executed now...
...
Syntax: Select all
engine_server.server_execute()
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')
iPlayer wrote:How safe it is to execute engine_server.server_execute() multiple times per frame? Won't it crash the server?
Syntax: Select all
for x in range(100000):
engine_server.server_execute()
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)
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)
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 redirected where I knew an example was.iPlayer wrote:Well, you could've posted example from our Cookbook (by Doldol).
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:Also, why should I avoid them?
iPlayer wrote:Thisnecavi, 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
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.
Syntax: Select all
def restart_round():
Entity.find_or_create('info_map_parameters').fire_win_condition(9)
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.
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)