Dev Simple Server Helper

Discussion in 'Hobbies' started by PixeL, Jan 13, 2017.

  1. PixeL

    PixeL Man märker andras fel och glömmer sina egna Banned VIP Silver

    Hey everyone, I don't really contribute to the forum as in posting threads and such so I thought I'd share my latest little thing.
    Note: It's nothing really fancy, but it's helped me a ton.
    Basically, it's a python file that allows you to type which server you'd like to run.. Since I run multiple servers for testing, be it Deathrun, prophunt, darkrp, or others..
    Basically it allows me to type the name of the one I want to run. I know I could create batch files for each, but that's boring.. :/

    Btw, I'm not a "professional" at python. Just started learning it a while ago.

    Here's the code if you'd like to take a look:
    Code:
    import subprocess
    import time
    import signal
    import sys
    from optparse import OptionParser
    from datetime import datetime
    
    parser = OptionParser()
    parser.add_option("-s", "--server", dest="server",
                      help="pick a server", metavar="SERVER")
    
    (options, args) = parser.parse_args()
    
    if not options.server:
        name = raw_input("Which server would you like to start?: ")
    else:
        name = options.server
    
    def signal_handler(signal, frame):
        print("Exiting due to CTRL+C")
        sys.exit(0)
    
    def getInfo(name):
        mapName = "err"
        gamemode = "err"
    
        if name == "deathrun":
            mapName = "deathrun_cavern_b3"
            gamemode = "deathrun"
        elif name == "sandbox" or name == "darkrp":
            mapName = "gm_construct"
            gamemode = name
        elif name == "prophunt":
            mapName = "ph_school"
            gamemode = "prop_hunt"
    
        return (mapName, gamemode)
    
    def startServer():
        (m, g) = getInfo(name)
    
        if m == "err" or g == "err":
            print("Could not find gamemode")
            return
    
        print("Starting server at {" + str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) + "}...")
    
        cmdLine = r"srcds.exe -norestart -console -condebug -game garrysmod -nohltv -maxplayers 32 +map " + m + " +gamemode " + g + " +exec ph_server.cfg -authkey GOES HERE+host_workshop_collection {}"
        proc = subprocess.Popen(cmdLine)
    
        proc.wait() # Waits for the process to close, aka crash/exit/BREAK ;(
    
        print("Server stopped at {" + str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) + "}. Restarting...")
    
        signal.signal(signal.SIGINT, signal_handler)
    
        print("Press CTRL + C during the countdown to exit...")
    
        for x in range(5, 0, -1):
            print "Restarting server in %d seconds..." % (x)
            time.sleep(1)
    
        startServer() # Gotta love recursion ;)
    
    startServer()
    
    
    
    
     
    • Like Like x 2
    • Funny Funny x 1
    • Useful Useful x 1
    • Creative Creative x 1
  2. Amr

    Amr Benevolently Committed VIP Iron

    How long exactly did you study python?
     
  3. CorallocinB

    CorallocinB Animeme lord VIP Silver Emerald

    • Funny Funny x 3
  4. PixeL

    PixeL Man märker andras fel och glömmer sina egna Banned VIP Silver

    Well @Amr , I've been messing around with it for maybe a couple of weeks. The only reason it was such a short amount of time is due to the fact that I know a decent amount of other languages, so the transition wasn't very hard. I just needed to know the basics and I was easily able to understand enough to make moderate things.
     
    • Like Like x 1
  5. Python~

    Python~ Young Bard VIP Silver Emerald

    A couple things;

    1. Please don't mess around with me. I'm waiting til marriage :/
    2. I only know English, so even if you can speak other languages I wouldn't be able to understand very well
    3. I'm not "basic". And I'd like to think I'm at least a little unique :(
     
    Last edited: Jan 14, 2017
    • Funny Funny x 6
  6. Amr

    Amr Benevolently Committed VIP Iron

    Aa, python is quite easy to understand but in my opinion its options are limited. I enjoy c++ more
     
  7. PixeL

    PixeL Man märker andras fel och glömmer sina egna Banned VIP Silver

    Yeah, I've messed with that as well.