train = optimizer.minimize(Cross_entropy) with tf.variable_scope('validation') as scope: correct_prediction = tf.equal(tf.argmax(predict_label, axis=1), tf.argmax(inputsy, axis=1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) saver = tf.train.Saver(max_to_keep=3) # ---------------------------------------- train --------------------------------------------- # with tf.Session(config=tf.ConfigProto( log_device_placement=False, \ allow_soft_placement=True, \ gpu_options=tf.GPUOptions(allow_growth=True))) as sess: sess.run(tf.initialize_all_variables()) myClients = clients(args.num_of_clients, datasetname, args.batchsize, args.epoch, sess, train, inputsx, inputsy, is_IID=args.IID) vars = tf.trainable_variables() global_vars = sess.run(vars) num_in_comm = int(max(args.num_of_clients * args.cfraction, 1)) for i in range(args.num_comm): print("communicate round {}".format(i)) order = np.arange(args.num_of_clients) np.random.shuffle(order) clients_in_comm = ['client{}'.format(i) for i in order[0:num_in_comm]] sum_vars = None for client in tqdm(clients_in_comm): local_vars = myClients.ClientUpdate(client, global_vars) if sum_vars is None: sum_vars = local_vars
def clients_function(): clients.clients(database, root)
def __init__(self): if os.path.isfile(os.path.join('', '.monitor.lock')): print 'Instance of monitor already running...' sys.exit(0) else: f = open(os.path.join('', '.monitor.lock'), 'w') f.write('running!') f.close() #are we running? hah! self.running = True #set up the loggers self.logger = logging.getLogger('chatlog') self.logger.setLevel(logging.INFO) ch = logging.FileHandler('chatlog.txt') ch.setLevel(logging.INFO) formatter = logging.Formatter("%(asctime)s - %(message)s") ch.setFormatter(formatter) self.logger.addHandler(ch) self.log = logging.getLogger('gamelog') self.log.setLevel(logging.INFO) ch = logging.FileHandler('logfile.txt') ch.setLevel(logging.INFO) formatter = logging.Formatter(None) ch.setFormatter(formatter) self.log.addHandler(ch) #read config options config = ConfigParser.ConfigParser() config.readfp(open(os.path.abspath('.') + '//config.cfg')) self.host = config.get('server', 'ip') self.port = int(config.get('server', 'port')) self.pw = config.get('server', 'pass') self.dbhost = config.get('mysql', 'host') self.dbuser = config.get('mysql', 'user') self.dbpasswd = config.get('mysql', 'passwd') self.dbname = config.get('mysql', 'db') self.webip = config.get('web', 'ip') self.webport = config.get('web', 'port') self.gmail_user = config.get('email', 'user') self.gmail_pwd = config.get('email', 'pass') self.mail_to = config.get('email', 'send_to') #try: # self.ip = urllib.urlopen('http://whatismyip.org/').read() #except: # pass self.sqlerror = False self.PBMessages = ( (re.compile(r'^PunkBuster Server: Running PB Scheduled Task \(slot #(?P<slot>\d+)\)\s+(?P<task>.*)$'), 'PBScheduledTask'), (re.compile(r'^PunkBuster Server: Lost Connection \(slot #(?P<slot>\d+)\) (?P<ip>[^:]+):(?P<port>\d+) (?P<pbuid>[^\s]+)\(-\)\s(?P<name>.+)$'), 'PBLostConnection'), (re.compile(r'^PunkBuster Server: Master Query Sent to \((?P<pbmaster>[^\s]+)\) (?P<ip>[^:]+)$'), 'PBMasterQuerySent'), (re.compile(r'^PunkBuster Server: Player GUID Computed (?P<pbid>[0-9a-fA-F]+)\(-\) \(slot #(?P<slot>\d+)\) (?P<ip>[^:]+):(?P<port>\d+)\s(?P<name>.+)$'), 'PBPlayerGuid'), (re.compile(r'^PunkBuster Server: New Connection \(slot #(?P<slot>\d+)\) (?P<ip>[^:]+):(?P<port>\d+) \[(?P<something>[^\s]+)\]\s"(?P<name>.+)".*$'), 'PBNewConnection') ) self.mapnames = ['Panama Canal', 'Valparaiso', 'Laguna Alta', 'Isla Inocentes', 'Atacama Desert', 'Africa Harbor', 'White Pass', 'Nelson Bay', 'Laguna Presa', 'Port Valdez'] self.ALLmaps = {'Panama Canal':'MP_001', 'Valparioso':'MP_002', 'Laguna Alta':'MP_003', 'Isla Inocentes':'MP_004', 'Atacama Desert':'MP_005', 'Africa Harbor':'MP_006', 'White Pass':'******', 'Nelson Bay':'MP_008', 'Laguna Presa':'MP_009', 'Port Valdez':'MP_012'} self.CONQUEST = {'Panama Canal':'MP_003', 'Atacama Desert':'MP_005', 'Africa Harbor':'MP_006CQ', 'White Pass':'******', 'Laguna Presa':'MP_009CQ'} self.RUSH = {'Valparaiso':'MP_002', 'Isla Inocentes':'MP_004', 'Neslon Bay':'MP_008', 'Port Valdez':'MP_012GR', 'Laguna Presa':'MP_009GR'} self.SQDM = {'Isla Inocentes':'MP_004SDM', 'Africa Harbor':'MP_006SDM', 'White Pass':'******', 'Laguna Presa':'MP_009SDM'} self.SQRUSH = {'Panama Canal':'MP_001SR', 'Valparaiso':'MP_002SR', 'Atacama Desert':'MP_005SR', 'Port Valdez':'MP_012SR'} self.commands = {'!rules':'Show the server rules to player', '!help': 'Show player general help. Takes optional argument command for specific help', '!stats':'Show all players kills, deaths, and ratio for player', '!chuck':'Show all players a random Chuck Norris message', '!punish':'Required argument [player]. Kills [player] and displays attention getting message', '!map':'Required argument [map]. Changes map to [map] with immediate effect.', '!restart':'Restarts current map', '!kick':'Required arguments [player],[time],and [reason] Kicks [player] for [time] for [reason]', '!ban':'Required arguments [player] and [reason]. Bans [player] for [reason]', '!gametype':'Required argument [gametype]. Changes server to [gametype] with immediate effect.'} self.command = re.compile(r'^(?P<cid>\'[^\']{2,}\'|[0-9]+|[^\s]{2,}|@[0-9]+)\s?(?P<parms>.*)$', re.I) self.players = clients.clients() self.queue = Queue.Queue() self.chat = [] self.kills = [] self.map = '' self.round = ['0', '0'] self.scores = [0,0,0] self.pcount = 0 self.serverrank = '' self.serverperc = '' self.gametype = 'None' self.rc = rcon.RCon() self.eventmon = rcon.RCon() self.badwords = [] with open('badwords.txt', 'r') as f: for line in f: if line: self.badwords.append(line.strip('\n')) self.banwords = [] with open('banlist.txt', 'r') as f: for line in f: if line: self.banwords.append(line.strip('\n')) eq = threading.Thread(target=self.event_queue) eq.name = 'events' eq.start() #hw = threading.Thread(target=self.host_watch) #hw.name = 'host_watch' #hw.start() self.do_first_run() threading.Thread(target=self.status).start() self.main_loop()
def __init__(self): #a simple lock to prevent multiple instances running since a 3rd party (cron) will be running us. if os.path.isfile(os.path.join('') + '.monitor.lock'): os.remove(os.path.join('') + '.monitor.lock') os.sys.exit() else: open(os.path.join('') + '.monitor.lock', 'w') #the local copy of the game log must meet some specific criteria to be actively parsed by us. if it is a dated #copy or nonexistent, then we play catch up before we start parsing. this is mostly a 'initial-run' check/fix. #this _should_ also catch up if monitor has not run in some time (20 minutes-ish) if os.path.isfile(logLoc + logfile) and (time.time() - os.path.getmtime(logLoc + logfile) > 1200): self.ftp = ftptail.ftptail() self.ftp.updateLog() self.where = os.path.getsize(logLoc + logfile) Pickle.dump(self.where, open(os.path.join('pickles/', 'where'), 'w')) #remove any left behind player pickles with an out of date log os.remove(os.path.join('pickles/', 'players')) elif not os.path.isfile(logLoc + logfile): self.ftp = ftptail.ftptail() self.ftp.updateLog() self.where = os.path.getsize(logLoc + logfile) Pickle.dump(self.where, open(os.path.join('pickles/', 'where'), 'w')) #initialize rcon'ness self.rcon = RCon.RCon(host, port, rconPass) #set up a dict to hold players referenced by game slot number if os.path.isfile(os.path.join('pickles/', 'players')): self.players = Pickle.load(open(os.path.join('pickles/', 'players'), 'r')) else: self.players = clients.clients() #load or create the chatqueue if os.path.isfile(os.path.join('pickles/', 'chat')): self.chat = Pickle.load(open(os.path.join('pickles/', 'chat'), 'r')) else: self.chat = [] self.chatq([time.strftime('%H:%M:%S', time.localtime()), 'Monitor has started']) #download new log bits self.ftp = ftptail.ftptail() self.ftp.updateLog() #parse our new info, this is the 'meat of the program ;) self.where = Pickle.load(open(os.path.join('pickles/', 'where'), 'r')) while 1: if self.where == os.path.getsize(logLoc + logfile): break f = open(logLoc + logfile, 'r') f.seek(self.where) line = f.readline() self.where = f.tell() #read a line, mark our spot if line: line = line.lstrip() line = line.strip('\n') line = line.strip('\r') # windows and linux each use different endlines, hur hur hur line = re.sub(Logparse.removeTime, "", line, 1) #InitGame signifies either a server start or a map start if line.count('InitGame') > 0: logLine = line.split("\\") self.map = logLine[26] self.gtype = logLine[22] #dump these as soon as we get them :D Pickle.dump(self.gtype, open(os.path.join('pickles/', 'gtype'), 'w')) Pickle.dump(self.map, open(os.path.join('pickles/', 'map'), 'w')) self.chatq([time.strftime('%H:%M:%S', time.localtime()), 'Map changed to %s' % self.map]) #reset streak and teamkills each map. for p in self.players.getAll(): p.teamkills = 0 p.teamkiller = None p.streak = 0 else: for i in Logparse.lineTypes: #this is so clever, i could poop. m = re.match(i, line) if m: break if m and hasattr(self, m.group('action')): getattr(self, m.group('action'))(m) #if we are handling multiple loglines at once we need a slight pause to avoid rcon block time.sleep(.5) #Pickle the players - I will never get over the naming scheme of this Pickle.dump(self.players, open(os.path.join('pickles/', 'players'), 'w')) Pickle.dump(self.chat, open(os.path.join('pickles/', 'chat'), 'w')) Pickle.dump(self.where, open(os.path.join('pickles/', 'where'), 'w')) #remove our lock self.ftp.close() os.remove(os.path.join('') + '.monitor.lock')