Exemplo n.º 1
0
def loadPreset(name):

    loadDefaultPreset()

    #Other settings first
    for k in presetcfg.MOD_PRESETS[name].keys():
        if k != "WEAPONFILE":
            io.setvar(k, presetcfg.MOD_PRESETS[name][k])

    #Weapon bans
    try:
        weaponBans = presetcfg.MOD_PRESETS[name]["WEAPONFILE"]
        modName = io.getVar("GameOptions.GameInfo.ModName")
        io.setvar("GameServer.WeaponRestrictionsFile",
                  "cfg/presets/" + modName + "/" + weaponBans + ".wps")
    except KeyError:
        pass
def init():
	initPresets()

	io.startLobby(cfg.SERVER_PORT)

	# if we load this script with already some worms on board, we have to update our worm list now
	for w in io.getWormList():
		parseNewWorm( w, io.getWormName(w) )

	global GLOBAL_SETTINGS;
	for f in GLOBAL_SETTINGS.keys():
		io.setvar( f, GLOBAL_SETTINGS[f] )
	for f in cfg.GLOBAL_SETTINGS.keys():
		io.setvar( f, cfg.GLOBAL_SETTINGS[f] )
	
	if io.getVar("GameOptions.GameInfo.AllowEmptyGames") == "false" and cfg.MIN_PLAYERS < 2:
		io.messageLog("GameOptions.GameInfo.AllowEmptyGames is false - setting cfg.MIN_PLAYERS to 2", io.LOG_WARN)
		cfg.MIN_PLAYERS = 2
Exemplo n.º 3
0
def init():

    checkConfigLists()
    initPresets()

    io.startLobby(cfg.SERVER_PORT)

    # if we load this script with already some worms on board, we have to update our worm list now
    for w in io.getWormList():
        parseNewWorm(w, io.getWormName(w))

    for f in cfg.GLOBAL_SETTINGS.keys():
        io.setvar(f, cfg.GLOBAL_SETTINGS[f])

    if io.getVar("GameOptions.GameInfo.AllowEmptyGames"
                 ) == "false" and cfg.MIN_PLAYERS < 2:
        io.messageLog(
            "GameOptions.GameInfo.AllowEmptyGames is false - setting cfg.MIN_PLAYERS to 2",
            io.LOG_WARN)
        cfg.MIN_PLAYERS = 2
Exemplo n.º 4
0
def parseNewWorm(wormID, name):
    global worms

    exists = False
    try:
        worm = worms[wormID]
        exists = True
    except KeyError:  #Worm doesn't exist.
        worm = Worm()

    worm.real_name = name  #NOTE: This is the name used by the server
    #Now prepare the name used by the script - used for ranking, for example
    name = name.replace("\t", " ").strip(
    )  # Do not allow tab in names, it will screw up our ranking tab-separated text-file database
    #Check if the player has quotation marks in his/her name
    quotemarks_in_nick = 0
    if "\"" in name:
        name = name.replace(
            "\"", "\'\'"
        )  #Double quotes (") cause problems with chat/private messages - convert to single quotes (')
        quotemarks_in_nick = 1

    worm.Name = name
    worm.iID = wormID
    worm.Ping = []

    wormIP = io.getWormIP(wormID).split(":")[
        0]  #Get worm IP address now - it will be needed later
    worm.Ip = wormIP

    worms[wormID] = worm

    #Set team for handler ##NOTE: OLX sets a team when the player joins - and it may not always be 0 (blue)!!! So get the team number now!
    #TEST: Log these initial teams
    if cfg.TEAMCHANGE_LOGGING:
        io.messageLog(
            "TEAMJOIN: Game reports team " + str(io.getWormTeam(wormID)) +
            " for joining worm " + str(name), io.LOG_INFO)
    worm.Team = io.getWormTeam(wormID)

    # io.messageLog("Curtime " + str(time.time()) + " IP " + str(wormIP) + " Kicked worms: " + str(kickedUsers), io.LOG_INFO)
    if wormIP in kickedUsers and kickedUsers[wormIP] > time.time():
        io.kickWorm(
            wormID, "You can join in " +
            str(int(kickedUsers[wormIP] - time.time()) / 60 + 1) + " minutes")
        return

    #Original ranking authentication based on skin color...
    #NOTE: This is a weak way of authentication
    if cfg.RANKING_AUTHENTICATION:
        if not name in ranking.auth:
            ranking.auth[name] = getWormSkin(wormID)
            try:
                rank_auth_file_path = io.getWriteFullFileName(
                    cfg.RANKING_AUTH_FILE)
                f = open(rank_auth_file_path, "a")
                try:
                    portalocker.lock(f, portalocker.LOCK_EX)
                except:
                    pass
                f.write(name + "\t" + str(ranking.auth[name][0]) + " " +
                        ranking.auth[name][1] + "\n")
                f.close()
            except IOError:
                io.messageLog(
                    "parseNewWorm: Unable to open ranking authentication file: "
                    + cfg.RANKING_AUTH_FILE + " from path: " +
                    rank_auth_file_path, io.LOG_ERROR)
        else:
            if ranking.auth[name] != getWormSkin(wormID):
                io.kickWorm(wormID,
                            "Player with name %s already registered" % name)
                return

    #Notify or kick players that have forbidden nicks
    if name in cfg.FORBIDDEN_NAMES:
        if cfg.NAME_CHECK_ACTION == 1:
            worm.Name = "random" + str(random.randint(
                0, cfg.NAME_CHECK_RANDOM))  #Assign random name
            io.privateMsg(wormID, cfg.NAME_CHECK_WARNMSG)
        elif cfg.NAME_CHECK_ACTION == 2:
            io.kickWorm(wormID, cfg.NAME_CHECK_KICKMSG)
            return

    #NEW: Kick players who have newline tags in their nicks - those can cause annoyance
    #NOTE: This may give false positives if there are nested tags, but it's an unlikely case
    if detectFormattingTags(name):
        io.kickWorm(
            wormID,
            "Please remove formatting tags ( <...> ) from your nickname - they cause problems"
        )
        return

    #Kick players with excessively long nicks.
    #NOTE: In 0.59, oversized name are truncated automatically so this becomes obsolete
    if len(name) > cfg.MAX_NAME_LENGTH:
        io.kickWorm(wormID, "name too long")
        return

    #Kick players with quotation marks in their name (only if configured!)
    #NOTE: This should not be needed anymore
    if quotemarks_in_nick == 1 and cfg.KICK_QUOTEMARKS == 1:
        io.kickWorm(
            wormID,
            "please remove quotation marks from your nickname - they screw up ranking."
        )
        return

    #If only one player per IP allowed, check if there is already a player from the IP
    #NOTE: This is a weak check and may cause more harm than good as it prevents people behind a shared IP from joining while not really preventing rank manipulation
    if cfg.ONE_PLAYER_PER_IP:
        for w in worms.keys():
            if worms[w].Ip == wormIP and w != wormID:
                io.kickWorm(wormID, "only one player per IP address allowed")
                return

    #Nag players to download the latest version. 0.58 rc3 may still be in use, and this is less harsh than kicking.
    #TODO: Do this in a better way?
    if cfg.VERSION_CHECK:
        ver = io.getWormVersion(wormID)
        newest_ver = io.getVar("GameOptions.State.NewestVersion")
        #Newest version, everything OK
        if distutils.version.LooseVersion(
                ver.lower()) == distutils.version.LooseVersion(
                    newest_ver.lower()):
            pass
        #Outdated 0.58 versions and older
        elif distutils.version.LooseVersion(
                ver.lower()) < distutils.version.LooseVersion(
                    newest_ver.lower()):
            io.privateMsg(
                wormID, "You're using an outdated version! Please download " +
                newest_ver.replace("/", " ").replace("_", " ") +
                " from http://openlierox.net/")
        #0.59 b10 and later are buggy
        elif distutils.version.LooseVersion(
                ver.lower()) >= distutils.version.LooseVersion(
                    "OpenLieroX/0.59_beta10".lower()):
            io.privateMsg(
                wormID,
                "You're using a buggy version of the game! You may want to download "
                + newest_ver.replace("/", " ").replace("_", " ") +
                " from http://openlierox.net/")
        #Other 0.59 versions are not as buggy but they lack the nice updates of 0.58 rc5
        else:
            io.privateMsg(
                wormID,
                "You seem to be using an experimental version of the game! You may want to download "
                + newest_ver.replace("/", " ").replace("_", " ") +
                " from http://openlierox.net/")

    #Assign team
    if io.getGameType() == 1:
        worms[
            wormID].votedTeams = 1  #Set vote status to 1 so that it will be TDM if it was TDM before this player joined
        io.privateMsg(
            wormID,
            "Game type is team deathmatch - say !noteams if you do not want teams for the next game"
        )
        #NOTE: The player is already assigned to a team by the game! - so assigning him to the smallest team here would lead to unbalancement
        # if the team status were, for example, [4, 4]. So check whether 1) teams are balanced (difference <=1) or not, and 2) the player is not in the smallest team
        #NOTE: Calling balanceTeams might look more elegant, but it might move other players as well, and it should not be called when we are not in lobby
        #NOTE: Be careful with RandomTeamForNewWorm option! It may cause an exception!
        team_status = getNumberWormsInAllTeams()  #Get team member counts
        team_status = team_status[
            0:cfg.MAX_TEAMS]  #Remove teams that are not used
        wormteam = io.getWormTeam(
            wormID)  #Could use worm.Team too, but this might be safer...
        if (max(team_status) - min(team_status)
            ) > 1 and wormteam != team_status.index(min(team_status)):
            setTeam(wormID, team_status.index(min(team_status)))

    #Update votes - this is needed if there are, let's say, 3 players who all vote for TDM but TDM requires 4 - now there are 4 and 3 of them vote so TDM should be selected!
    #No need to broadcast anything - TDM is broadcasted if the status changes.
    updateVotes()
Exemplo n.º 5
0
def controlHandlerDefault():

    global worms, gameState, lobbyChangePresetTimeout, lobbyWaitBeforeGame, lobbyWaitAfterGame
    global lobbyWaitGeneral, lobbyEnoughPlayers, oldGameState, scriptPaused, sentStartGame
    global currentAutocyclePreset, currentAutocycleMap
    global vote_locked, shuffle_counter

    if scriptPaused:
        return

    curTime = time.time()

    if gameState == GAME_LOBBY:

        # Do not check ping in lobby - it's wrong

        if oldGameState != GAME_LOBBY:
            lobbyEnoughPlayers = False  # reset the state
            lobbyWaitGeneral = curTime + cfg.WAIT_BEFORE_SPAMMING_TOO_FEW_PLAYERS_MESSAGE
            lobbyWaitAfterGame = curTime
            if oldGameState == GAME_PLAYING:
                lobbyWaitAfterGame = curTime + cfg.WAIT_AFTER_GAME

            #Reset shuffle counter
            shuffle_counter = 0

            #Update votes when game ends
            updateResult = updateVotes(send_msg=("map", "mod", "teams"))
            #Autocycle if not voted
            #Advance autocycle counters first
            if not updateResult[0]:
                currentAutocycleMap += 1
                if currentAutocycleMap >= len(cfg.LEVELS):
                    currentAutocycleMap = 0
            if not updateResult[1]:
                currentAutocyclePreset += 1
                if currentAutocyclePreset >= len(cfg.PRESETS):
                    currentAutocyclePreset = 0
            ##Map
            if not updateResult[0]:
                io.setvar("GameOptions.GameInfo.LevelName",
                          cfg.LEVELS[currentAutocycleMap])
            ##Mod (preset)
            if not updateResult[1]:
                loadPreset(cfg.PRESETS[currentAutocyclePreset])

            #Check if teams are balanced
            ##This should be done because teams may become uneven if people leave during game and BALANCE_TEAMS_INGAME is not set
            if io.getGameType() == 1:
                io.chatMsg("Checking if teams are even...")
                balanceTeams(bmsg="game ended")

        if lobbyWaitAfterGame <= curTime:

            if not lobbyEnoughPlayers and lobbyWaitGeneral <= curTime:
                lobbyWaitGeneral = curTime + cfg.WAIT_BEFORE_SPAMMING_TOO_FEW_PLAYERS_MESSAGE
                io.chatMsg(cfg.TOO_FEW_PLAYERS_MESSAGE)

            if not lobbyEnoughPlayers and len(
                    worms
            ) >= cfg.MIN_PLAYERS:  # Enough players already - start game
                lobbyEnoughPlayers = True
                io.chatMsg(cfg.WAIT_BEFORE_GAME_MESSAGE)
                lobbyWaitBeforeGame = curTime + cfg.WAIT_BEFORE_GAME

            if lobbyEnoughPlayers and len(
                    worms
            ) < cfg.MIN_PLAYERS:  # Some players left when game not yet started
                lobbyEnoughPlayers = False
                io.chatMsg(cfg.TOO_FEW_PLAYERS_MESSAGE)

            if lobbyEnoughPlayers and not sentStartGame:

                if lobbyWaitBeforeGame <= curTime:  # Start the game

                    #Starting game...		##NOTE Removed team check from here - teams should be assigned before the round starts
                    if io.startGame():
                        if cfg.ALLOW_TEAM_CHANGE and len(
                                worms) >= cfg.MIN_PLAYERS_TEAMS:
                            io.chatMsg(cfg.TEAM_CHANGE_MESSAGE)
                        sentStartGame = True
                        vote_locked = True  #Lock vote-able settings when starting
                        clearVotes()  #Clear votes when starting
                    else:
                        io.messageLog("Game could not be started",
                                      io.LOG_ERROR)  #Log these errors!
                        io.chatMsg("Game could not be started")
                        oldGameState == GAME_PLAYING  # hack that it resets at next control handler call

    if gameState == GAME_WEAPONS:

        # if we allow empty games, ignore this check
        if len(worms) < cfg.MIN_PLAYERS and not io.getVar(
                "GameOptions.GameInfo.AllowEmptyGames"
        ):  # Some players left when game not yet started
            io.chatMsg("Too few players -> back to lobby")
            io.gotoLobby()
            sentStartGame = False

    if gameState == GAME_PLAYING:

        if cfg.PING_CHECK:
            checkMaxPing()

    #Unlock voting when the game is safely running
    if (gameState == GAME_WEAPONS or gameState
            == GAME_PLAYING) and oldGameState != GAME_LOBBY and vote_locked:
        vote_locked = False
Exemplo n.º 6
0
MIN_PLAYERS = 1
MIN_PLAYERS_TEAMS = 6  # Players will be split in two teams automatically if there is enough players
MAX_TEAMS = 2  # Only blue and red teams
TOO_FEW_PLAYERS_MESSAGE = "Game will start with minimum %i players. Team Deathmatch if there's %i or more players" % (
    MIN_PLAYERS, MIN_PLAYERS_TEAMS)
WAIT_BEFORE_SPAMMING_TOO_FEW_PLAYERS_MESSAGE = 30  # Seconds to wait before another "Game will start with %i players" message
FILL_WITH_BOTS_TO = 0  # Fill server with bots if noone playing, set to 2 to get 1 bot with a single human player

WAIT_AFTER_GAME = 0  # Seconds to wait in lobby after round finished
WAIT_BEFORE_GAME = 0  # Seconds to wait in lobby before next round, will give some message
WAIT_BEFORE_GAME_MESSAGE = "Game will start in %i seconds" % WAIT_BEFORE_GAME

import dedicated_control_io as io  # control handler

GAME_LIVES = -2
GAME_MAX_KILLS = int(io.getVar("GameOptions.GameInfo.KillLimit"))
GAME_MAX_TIME = float(io.getVar("GameOptions.GameInfo.TimeLimit"))
WEAPON_SELECTION_TIME = int(
    io.getVar("GameOptions.Server.WeaponSelectionMaxTime"))

# Note: This is unfair and I don't thing it is such a good idea. (At least for the average player, only
# pro-gamers perhaps want that.)
# A user with a high ping doesn't give any disadvantages to other players (or at least that should not be the case and I wonder if it is).
MAX_PING = 2000  # Max ping to auto-kick player

RECORD_VIDEO = 1  # If we should record video on our ded server. Warning: it eats CPU!
TIME_TO_KILL_VIDEORECORDER = 60  # Wait one minute before killing recorder - it may encode rather slowly!

# TODO: We should use the OLX chatcommand system.
# TODO: Register dedscript commands in OLX chatcommand system.
# Users can enter some commands too
Exemplo n.º 7
0
MIN_PLAYERS = 1
MIN_PLAYERS_TEAMS = 6 # Players will be split in two teams automatically if there is enough players
MAX_TEAMS = 2 # Only blue and red teams
TOO_FEW_PLAYERS_MESSAGE = "Game will start with minimum %i players. Team Deathmatch if there's %i or more players" % (MIN_PLAYERS, MIN_PLAYERS_TEAMS)
WAIT_BEFORE_SPAMMING_TOO_FEW_PLAYERS_MESSAGE = 30 # Seconds to wait before another "Game will start with %i players" message
FILL_WITH_BOTS_TO = 0 # Fill server with bots if noone playing, set to 2 to get 1 bot with a single human player

WAIT_AFTER_GAME = 0 # Seconds to wait in lobby after round finished
WAIT_BEFORE_GAME = 0 # Seconds to wait in lobby before next round, will give some message
WAIT_BEFORE_GAME_MESSAGE = "Game will start in %i seconds" % WAIT_BEFORE_GAME

import dedicated_control_io as io # control handler

GAME_LIVES = -2
GAME_MAX_KILLS = int(io.getVar("GameOptions.GameInfo.KillLimit"))
GAME_MAX_TIME = float(io.getVar("GameOptions.GameInfo.TimeLimit"))
WEAPON_SELECTION_TIME = int(io.getVar("GameOptions.GameInfo.WeaponSelectionMaxTime"))

# Note: This is unfair and I don't thing it is such a good idea. (At least for the average player, only 
# pro-gamers perhaps want that.)
# A user with a high ping doesn't give any disadvantages to other players (or at least that should not be the case and I wonder if it is).
MAX_PING = 2000 # Max ping to auto-kick player

RECORD_VIDEO = 1 # If we should record video on our ded server. Warning: it eats CPU!
TIME_TO_KILL_VIDEORECORDER = 60 # Wait one minute before killing recorder - it may encode rather slowly!

# TODO: We should use the OLX chatcommand system.
# TODO: Register dedscript commands in OLX chatcommand system.
# Users can enter some commands too
USER_PREFIX = ADMIN_PREFIX # Change to have custom user command prefix instead of "!"
def controlHandlerDefault():

    global worms, gameState, lobbyChangePresetTimeout, lobbyWaitBeforeGame, lobbyWaitAfterGame
    global lobbyWaitGeneral, lobbyEnoughPlayers, oldGameState, scriptPaused, sentStartGame
    global presetCicler, modCicler, mapCicler, LT_Cicler
    global videoRecorder, videoRecorderSignalTime

    if scriptPaused:
        return

    curTime = time.time()
    cmds.recheckVote(False)

    if gameState == GAME_LOBBY:

        # Do not check ping in lobby - it's wrong

        if oldGameState != GAME_LOBBY:
            mapCicler.check()
            modCicler.check()
            LT_Cicler.check()
            presetCicler.check()
            lobbyEnoughPlayers = False  # reset the state
            lobbyWaitGeneral = curTime + cfg.WAIT_BEFORE_SPAMMING_TOO_FEW_PLAYERS_MESSAGE
            lobbyWaitAfterGame = curTime
            if oldGameState == GAME_PLAYING:
                lobbyWaitAfterGame = curTime + cfg.WAIT_AFTER_GAME
            if videoRecorder:
                os.kill(
                    videoRecorder.pid, signal.SIGINT
                )  # videoRecorder.send_signal(signal.SIGINT) # This is available only on Python 2.6
                videoRecorderSignalTime = time.time()
                io.chatMsg("Waiting for video recorder to finish")

        canStart = True
        if videoRecorder and videoRecorder.returncode == None:
            canStart = False
            videoRecorder.poll()
            if time.time(
            ) - videoRecorderSignalTime > cfg.TIME_TO_KILL_VIDEORECORDER:
                io.chatMsg("Video recorder stalled, killing")
                os.kill(videoRecorder.pid, signal.SIGKILL)
                videoRecorder.poll()
            if videoRecorder.returncode != None:
                io.chatMsg("Video recorder encoding took " +
                           str(int(time.time() - videoRecorderSignalTime)) +
                           " secs")
                canStart = True
                videoRecorder = None

        if lobbyWaitAfterGame <= curTime:

            if not lobbyEnoughPlayers and lobbyWaitGeneral <= curTime:
                lobbyWaitGeneral = curTime + cfg.WAIT_BEFORE_SPAMMING_TOO_FEW_PLAYERS_MESSAGE
                io.chatMsg(cfg.TOO_FEW_PLAYERS_MESSAGE)

            if not lobbyEnoughPlayers and len(
                    worms
            ) >= cfg.MIN_PLAYERS:  # Enough players already - start game
                lobbyEnoughPlayers = True
                io.chatMsg(cfg.WAIT_BEFORE_GAME_MESSAGE)
                lobbyWaitBeforeGame = curTime + cfg.WAIT_BEFORE_GAME

            if lobbyEnoughPlayers and len(
                    worms
            ) < cfg.MIN_PLAYERS:  # Some players left when game not yet started
                lobbyEnoughPlayers = False
                io.chatMsg(cfg.TOO_FEW_PLAYERS_MESSAGE)

            if lobbyEnoughPlayers and not sentStartGame:

                if lobbyWaitBeforeGame <= curTime and canStart:  # Start the game

                    if io.getGameType() in ["Death Match", "Team Death Match"]:
                        if len(worms
                               ) >= cfg.MIN_PLAYERS_TEAMS:  # Split in teams
                            setvar("GameOptions.GameInfo.GameType",
                                   "Team Death Match")
                            if not cfg.ALLOW_TEAM_CHANGE:
                                counter = 0
                                for w in worms.values():
                                    if w.iID != -1:
                                        io.setWormTeam(w.iID,
                                                       counter % cfg.MAX_TEAMS)
                                        counter += 1
                        else:
                            io.setvar("GameOptions.GameInfo.GameType",
                                      "Death Match")

                    if io.startGame():
                        if cfg.ALLOW_TEAM_CHANGE and len(
                                worms) >= cfg.MIN_PLAYERS_TEAMS:
                            io.chatMsg(cfg.TEAM_CHANGE_MESSAGE)
                        sentStartGame = True
                        if cfg.RECORD_VIDEO:
                            try:
                                #io.messageLog("Running dedicated-video-record.sh, curdir " + os.path.abspath(os.path.curdir) ,io.LOG_INFO)
                                videoRecorder = subprocess.Popen(
                                    [
                                        "./dedicated-video-record.sh",
                                        "./dedicated-video-record.sh"
                                    ],
                                    stdin=open("/dev/null", "r"),
                                    stdout=open("../../../dedicatedVideo.log",
                                                "w"),
                                    stderr=subprocess.STDOUT,
                                    cwd="..")
                            except:
                                io.messageLog(formatExceptionInfo(),
                                              io.LOG_ERROR)
                    else:
                        io.chatMsg("Game could not be started")
                        oldGameState == GAME_PLAYING  # hack that it resets at next control handler call

    if gameState == GAME_WEAPONS:

        #checkMaxPing()

        # if we allow empty games, ignore this check
        if len(worms) < cfg.MIN_PLAYERS and not io.getVar(
                "GameOptions.GameInfo.AllowEmptyGames"
        ):  # Some players left when game not yet started
            io.chatMsg("Too less players -> back to lobby")
            io.gotoLobby()
            sentStartGame = False

    if gameState == GAME_PLAYING:

        checkMaxPing()
def SetWeaponBans(name="Standard 100lt"):
    modName = io.getVar("GameOptions.GameInfo.ModName")
    io.setvar("GameOptions.GameInfo.WeaponRestrictionsFile", name + ".wps")
Exemplo n.º 10
0
def SetWeaponBans(name="Standard 100lt"):
    modName = io.getVar("GameOptions.GameInfo.ModName")
    io.setvar("GameServer.WeaponRestrictionsFile",
              "cfg/presets/" + modName + "/" + name + ".wps")
Exemplo n.º 11
0
MIN_PLAYERS = 1
MIN_PLAYERS_TEAMS = 6 # Players will be split in two teams automatically if there is enough players
MAX_TEAMS = 2 # Only blue and red teams
TOO_FEW_PLAYERS_MESSAGE = "Game will start with minimum %i players. Team Deathmatch if there's %i or more players" % (MIN_PLAYERS, MIN_PLAYERS_TEAMS)
WAIT_BEFORE_SPAMMING_TOO_FEW_PLAYERS_MESSAGE = 30 # Seconds to wait before another "Game will start with %i players" message
FILL_WITH_BOTS_TO = 0 # Fill server with bots if noone playing, set to 2 to get 1 bot with a single human player

WAIT_AFTER_GAME = 0 # Seconds to wait in lobby after round finished
WAIT_BEFORE_GAME = 0 # Seconds to wait in lobby before next round, will give some message
WAIT_BEFORE_GAME_MESSAGE = "Game will start in %i seconds" % WAIT_BEFORE_GAME

import dedicated_control_io as io # control handler

GAME_LIVES = -2
GAME_MAX_KILLS = int(io.getVar("GameOptions.GameInfo.KillLimit"))
GAME_MAX_TIME = float(io.getVar("GameOptions.GameInfo.TimeLimit"))
WEAPON_SELECTION_TIME = int(io.getVar("GameOptions.Server.WeaponSelectionMaxTime"))

# Note: This is unfair and I don't thing it is such a good idea. (At least for the average player, only 
# pro-gamers perhaps want that.)
# A user with a high ping doesn't give any disadvantages to other players (or at least that should not be the case and I wonder if it is).
MAX_PING = 2000 # Max ping to auto-kick player

RECORD_VIDEO = 1 # If we should record video on our ded server. Warning: it eats CPU!
TIME_TO_KILL_VIDEORECORDER = 60 # Wait one minute before killing recorder - it may encode rather slowly!

# TODO: We should use the OLX chatcommand system.
# TODO: Register dedscript commands in OLX chatcommand system.
# Users can enter some commands too
USER_PREFIX = ADMIN_PREFIX # Change to have custom user command prefix instead of "!"
def controlHandlerDefault():

	global worms, gameState, lobbyChangePresetTimeout, lobbyWaitBeforeGame, lobbyWaitAfterGame
	global lobbyWaitGeneral, lobbyEnoughPlayers, oldGameState, scriptPaused, sentStartGame
	global presetCicler, modCicler, mapCicler, LT_Cicler
	global videoRecorder, videoRecorderSignalTime
	
	if scriptPaused:
		return

	curTime = time.time()
	cmds.recheckVote(False)
	
	if gameState == GAME_LOBBY:

		# Do not check ping in lobby - it's wrong

		if oldGameState != GAME_LOBBY:
			mapCicler.check()
			modCicler.check()
			LT_Cicler.check()
			presetCicler.check()
			lobbyEnoughPlayers = False # reset the state
			lobbyWaitGeneral = curTime + cfg.WAIT_BEFORE_SPAMMING_TOO_FEW_PLAYERS_MESSAGE
			lobbyWaitAfterGame = curTime
			if oldGameState == GAME_PLAYING:
				lobbyWaitAfterGame = curTime + cfg.WAIT_AFTER_GAME
			if videoRecorder:
				os.kill(videoRecorder.pid, signal.SIGINT)  # videoRecorder.send_signal(signal.SIGINT) # This is available only on Python 2.6
				videoRecorderSignalTime = time.time()
				io.chatMsg("Waiting for video recorder to finish")

		canStart = True
		if videoRecorder and videoRecorder.returncode == None:
			canStart = False
			videoRecorder.poll()
			if time.time() - videoRecorderSignalTime > cfg.TIME_TO_KILL_VIDEORECORDER:
				io.chatMsg("Video recorder stalled, killing")
				os.kill(videoRecorder.pid, signal.SIGKILL)
				videoRecorder.poll()
			if videoRecorder.returncode != None:
				io.chatMsg("Video recorder encoding took " + str(int(time.time() - videoRecorderSignalTime)) + " secs")
				canStart = True
				videoRecorder = None

		if lobbyWaitAfterGame <= curTime:

			if not lobbyEnoughPlayers and lobbyWaitGeneral <= curTime:
				lobbyWaitGeneral = curTime + cfg.WAIT_BEFORE_SPAMMING_TOO_FEW_PLAYERS_MESSAGE
				io.chatMsg(cfg.TOO_FEW_PLAYERS_MESSAGE)

			if not lobbyEnoughPlayers and len(worms) >= cfg.MIN_PLAYERS: # Enough players already - start game
				lobbyEnoughPlayers = True
				io.chatMsg(cfg.WAIT_BEFORE_GAME_MESSAGE)
				lobbyWaitBeforeGame = curTime + cfg.WAIT_BEFORE_GAME

			if lobbyEnoughPlayers and len(worms) < cfg.MIN_PLAYERS: # Some players left when game not yet started
				lobbyEnoughPlayers = False
				io.chatMsg(cfg.TOO_FEW_PLAYERS_MESSAGE)

			if lobbyEnoughPlayers and not sentStartGame:

				if lobbyWaitBeforeGame <= curTime and canStart: # Start the game

					if io.getGameType() in ["Death Match","Team Death Match"]:
						if len(worms) >= cfg.MIN_PLAYERS_TEAMS: # Split in teams
							setvar("GameOptions.GameInfo.GameType", "Team Death Match")
							if not cfg.ALLOW_TEAM_CHANGE:
								counter = 0
								for w in worms.values():
									if w.iID != -1:
										io.setWormTeam( w.iID, counter % cfg.MAX_TEAMS )
										counter += 1
						else:
							io.setvar("GameOptions.GameInfo.GameType", "Death Match")

					if io.startGame():
						if cfg.ALLOW_TEAM_CHANGE and len(worms) >= cfg.MIN_PLAYERS_TEAMS:
							io.chatMsg(cfg.TEAM_CHANGE_MESSAGE)
						sentStartGame = True
						if cfg.RECORD_VIDEO:
							try:
								#io.messageLog("Running dedicated-video-record.sh, curdir " + os.path.abspath(os.path.curdir) ,io.LOG_INFO)
								videoRecorder = subprocess.Popen( ["./dedicated-video-record.sh", "./dedicated-video-record.sh"],
												stdin=open("/dev/null","r"), stdout=open("../../../dedicatedVideo.log","w"),
												stderr=subprocess.STDOUT, cwd=".." )
							except:
								io.messageLog(formatExceptionInfo(),io.LOG_ERROR)
					else:
						io.chatMsg("Game could not be started")
						oldGameState == GAME_PLAYING # hack that it resets at next control handler call
					
	if gameState == GAME_WEAPONS:

		#checkMaxPing()

		# if we allow empty games, ignore this check
		if len(worms) < cfg.MIN_PLAYERS and not io.getVar("GameOptions.GameInfo.AllowEmptyGames"): # Some players left when game not yet started
			io.chatMsg("Too less players -> back to lobby")
			io.gotoLobby()
			sentStartGame = False

	if gameState == GAME_PLAYING:

		checkMaxPing()
def SetWeaponBans(name = "Standard 100lt"):
	modName = io.getVar("GameOptions.GameInfo.ModName")
	io.setvar( "GameOptions.GameInfo.WeaponRestrictionsFile", name + ".wps" )