def act_team_change(self, team, player_a, player_b, source=None): e = Event() e.type = Event.EV_TYPE_CHANGE e.team = team e.player_a = player_a e.player_b = player_b self.act_event(e, source)
def parse_data(json_data): """parse the json file into Event object * Qualy: starting grid is based on qualy * Race: starting grid is based on the selected lap the method may ask for user input Args: json_data ([type]): json dict of server dump Returns: [type]: Event Object """ ev = Event() ev.name = json_data['trackName'] leaders = json_data['sessionResult']['leaderBoardLines'] i = 1 for team_json in leaders: car = team_json['car'] team = Team() team.defaultGridPosition = i team.forcedCarModel = car['carModel'] team.raceNumber = car['raceNumber'] team.carId = car['carId'] for driver in car['drivers']: d = Driver() d.firstName = driver['firstName'] d.lastName = driver['lastName'] d.shortName = driver['shortName'] d.playerID = driver['playerId'] team.drivers.append(d) ev.teams.append(team) i += 1 if json_data['sessionType'] == 'Q': print('detected Qualy session') return ev elif json_data['sessionType'] == 'R': print('detected Race session') return parse_race_abort(json_data, ev) else: print('unsupported session type, applying qualy procedure') return ev return ev
def create_event(username, title, members, event_length=None, event_deadline=None): """ Create an event with the given details. Parameters: username (str): username of event creator title (str): name of event members ([str]): list of usernames of additional event members event_length (int): event length in hours event_deadline (datetime.date): latest desired event date Returns: event_id (int): unique ID of new event Exceptions: AuthError if any of: the user is not logged in InputError if any of: username does not exist a username in the members list does not exist title is longer than 100 characters or empty event_length is less than 1 or greater than 14 * 24 (fortnight) event_deadline is a date in the past """ check_username(username) for u in members: check_username(u) check_logged_in(username) if not len(title) or len(title) > MAX_TITLE: raise InputError("Title length is invalid") if (event_length != None and (event_length < MIN_EVENT or event_length > MAX_EVENT)): raise InputError("Event length is invalid") if event_deadline and event_deadline < datetime.now().date(): raise InputError("Event deadline is invalid") new_event = Event(data.event_next_id, title, username) data.event_next_id += 1 for u in members: new_event.member_usernames.add(u) new_event.event_length = event_length new_event.event_deadline = event_deadline data.events[new_event.event_id] = new_event return new_event.event_id
def create_normalize_event(e_i, e_j): """ created temp Event instance base on e_i and e_j members or None: exp: e_i = Events('First', 'Second and some', 'some_url_1'), e_j = Events('Second', 'Some and First', 'some_url_2') will be returned: Events('First', 'Second', 'TEMP EVENT') :param e_i: Event instance :param e_j: Event instance :return: """ a1 = e_i.first_member.lower() a2 = e_i.second_member.lower() a3 = e_j.first_member.lower() a4 = e_j.second_member.lower() first = '' second = '' if a1 in a3 or a3 in a1: first = a1 if len(a1) < len(a3) else a3 second = a2 if len(a2) < len(a4) else a4 if a1 in a4 or a4 in a1: first = a1 if len(a1) < len(a4) else a4 second = a2 if len(a2) < len(a3) else a3 if not first or not second: return None sorted_members = sorted([first, second]) first = sorted_members[0] second = sorted_members[1] logger.info('Created normalized: {f} - {s}: {url}'.format(f=first, s=second, url='NORMALIZED_EVENT')) return Event(first_member=first, second_member=second, url='NORMALIZED_EVENT')
def post(self): self.response.write("Event Added.") event = Event() event.name = self.request.get('name') event.location = self.request.get('location') event.date = self.request.get('date') event.time = self.request.get('time') event.put() self.response.write("""<html><body><table><tr><td><form action="/main" method="post"><input type="submit" value="Main Menu"></form></td></tr></table></html></body>""")
def load_data(self) -> List[Event]: """ Reads the event data from the CSV file. """ with open(self.filename) as _file: lines = _file.readlines() if self.limit: lines = lines[:self.limit] data = [Event.read_event(line) for line in tqdm(lines, 'Loading data')] return data
def __init__(self, command_prefix='!'): self.socket = socket() self.connected = False self.command_prefix = command_prefix self.on_message_received = Event() self.on_message_sent = Event() self.on_whisper_received = Event() self.on_whisper_sent = Event() self.on_command_executed = Event() self.on_command_triggered = Event()
def act_switch_teams(self, source=None): e = Event() e.type = Event.EV_TYPE_SWAP if self.order == [None, None]: e.red_team = self.teams[0] e.blue_team = self.teams[1] else: e.red_team = self.order[1] e.blue_team = self.order[0] self.act_event(e, source)
def get(self): events = Event.query().fetch(20) self.response.out.write('<html>' + STYLE + '<body><div class="contentBox"><h1>Events</h1><table><tr class="head"><td>Name</td><td>Location</td><td>Date</td><td>Time</td></tr>') i = 0; for event in events: i=i+1 self.response.out.write('<tr') if(i%2 == 0): self.response.out.write(' class="odd"') self.response.out.write('><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % (event.name, event.location, event.date, event.time)) self.response.out.write('</table><form action="/main" method="post"><input type="submit" value="Main Menu"></form></div></body></html>')
def parse_html_element_to_event(html): import re findings = re.search(r'title=\".{1,200}\"\shref=\"(/.{1,50}){1,20}\">', html) if not findings: logger.warning('Could not parse HTML element via regex') return None title_and_href_parts = findings.group(0).split('href=') raw_title = title_and_href_parts[0].split('title')[1].strip() raw_title = raw_title.replace('\"', '') first, second = raw_title.split(' - ') raw_href = re.sub(r'[\">]', '', title_and_href_parts[1]) url = 'https://winline.ru{appendix}'.format(appendix=raw_href) e = Event(first_member=first, second_member=second, url=url) logger.info('Created: %s' % e) return e
def parse_element_to_event(element): """ extract data from DOM-element :param element: DOM-object, WebElement type :return: event object """ title = '' try: url = element.get_attribute("href") title = element.get_attribute("title") first, second = title.split(" - ") logger.info('Created: {}/{}:[{}]'.format(first, second, url)) if not (url and first and second): logger.error('Some info parsed as empty string: [{f}][{s}][{url}]'.format(f=first, s=second, url=url)) return Event(first, second, url) except Exception as e: logger.error('Could not took some info from element (title-[{title}]): {err}. None will be returned.' .format(err=e, title=title)) return None
def create_event(self, arg): ''' creates a new event instance ''' # creating a new event from Event class event_name = arg['<name>'] start_date = datetime.strptime(arg['<start_date>'], "%d-%m-%Y" ) end_date = datetime.strptime(arg['<end_date>'], "%d-%m-%Y" ) venue = arg['<venue>'] event = Event(name=event_name, start_date=start_date, end_date=end_date, venue=venue) try: # add event object to session for db commit Tickets.session.add(event) Tickets.session.commit() return "Event created" except Exception as e: print (e) raise e
def parse_server_result(file): if file is None or not os.path.exists(file): print('couldn\'t locate file ' + file) return None ev = Event() try: with open(file, 'r') as json_utf8: json_lines = json_utf8.readlines() json_str = ' '.join(json_lines) data = json.loads(json_str) except json.JSONDecodeError: with open(file, 'r', encoding='utf_16_le') as json_utf16: json_lines = json_utf16.readlines() json_str = ' '.join(json_lines) data = json.loads(json_str) event = serverResult.parse_data(data) return event
async def parse_config(suppress_message=False): async for message in config.config_channel.history(limit=None): try: if message.content.startswith("On deck emoji"): config.on_deck = message.content.split(" ")[-1] elif message.content.startswith("Approve emoji"): config.approve = message.content.split(" ")[-1] elif message.content.startswith("Event"): _, day, name, tz, start, *_ = message.content.split(",") channel = message.channel_mentions[0] host = message.mentions[0] event = Event(day.strip(), name.strip(), tz.strip(), start.strip(), channel, host) config.events.append(event) elif message.content.startswith("Respond in"): config.response_channel = message.channel_mentions[0] elif message.content.startswith("Voice template"): config.voice_template = message.channel_mentions[0] except: pass if not suppress_message: await config.response_channel.send("Ingested new configuration")
def import_log(match, session, logfile): first_team = session.query(Team).filter(Team.name == "Matematici").one() second_team = session.query(Team).filter(Team.name == "Fisici").one() events = [] first_players = set() second_players = set() def add_player(p): if not p in goalPerPlayer: goalPerPlayer[p] = 0 timePerPlayer[p] = 0 try: fname, lname = p.split(' ', 1) except ValueError: fname = p lname = '' player = Player.get_or_create(session, fname, lname, None) return player def add_pair(p): if not p in goalPerPair: goalPerPair[p] = 0 timePerPair[p] = 0 def split_and_add_players(data): playersList = data.split(',') playersList.sort() players = tuple(playersList) p1 = add_player(players[0]) p2 = add_player(players[1]) add_pair(players) return players, (p1, p2) log = dict() for line in codecs.open(logfile, encoding='utf-8'): if line.startswith("#"): continue match_ = line_re.match(line) if match_ == None: print "Bad line: %s" % (line) sys.exit(1) else: log[int(match_.group(1))] = (match_.group(2), match_.group(3)) firstGoal = 0 secondGoal = 0 firstBest = 0 secondBest = 0 swapped = False goalPerPlayer = dict() goalPerPair = dict() timePerPlayer = dict() timePerPair = dict() freqPerPlayer = dict() freqPerPair = dict() firstLastTime = None secondLastTime = None firstPlayers = (None, None) secondPlayers = (None, None) startTime = None stopTime = None lastTime = None frames = log.keys() frames.sort() for time in frames: if lastTime != None: assert(time > lastTime) lastTime = time cmd = log[time][0] data = log[time][1] goal = 0 first = None if cmd == 'TEAM_SWAP': swapped = not swapped # Event ev = Event() ev.match = match ev.timestamp = datetime.datetime.fromtimestamp(float(time) / 1000) ev.type = Event.EV_TYPE_SWAP ev.source = Event.EV_SOURCE_MANUAL ev.red_team = first_team if not swapped else second_team ev.blue_team = second_team if not swapped else first_team events.append(ev) elif cmd == 'RED_GOAL': goal = 1 first = True elif cmd == 'BLUE_GOAL': goal = 1 first = False elif cmd == 'RED_GOAL_UNDO': goal = -1 first = True elif cmd == 'BLUE_GOAL_UNDO': goal = -1 first = False elif cmd == 'FIRST_TEAM_CHANGE': if firstLastTime != None: timeDelta = time - firstLastTime timePerPair[firstPlayers] += timeDelta timePerPlayer[firstPlayers[0]] += 0.5 * timeDelta timePerPlayer[firstPlayers[1]] += 0.5 * timeDelta firstLastTime = time firstPlayers, (p1, p2) = split_and_add_players(data) first_players.add(p1) first_players.add(p2) # Event ev = Event() ev.match = match ev.timestamp = datetime.datetime.fromtimestamp(float(time) / 1000) ev.type = Event.EV_TYPE_CHANGE ev.source = Event.EV_SOURCE_MANUAL ev.team = first_team ev.player_a = p1 ev.player_b = p2 events.append(ev) elif cmd == 'SECOND_TEAM_CHANGE': if secondLastTime != None: timeDelta = time - secondLastTime timePerPair[secondPlayers] += timeDelta timePerPlayer[secondPlayers[0]] += 0.5 * timeDelta timePerPlayer[secondPlayers[1]] += 0.5 * timeDelta secondLastTime = time secondPlayers, (p1, p2) = split_and_add_players(data) second_players.add(p1) second_players.add(p2) # Event ev = Event() ev.match = match ev.timestamp = datetime.datetime.fromtimestamp(float(time) / 1000) ev.type = Event.EV_TYPE_CHANGE ev.source = Event.EV_SOURCE_MANUAL ev.team = second_team ev.player_a = p1 ev.player_b = p2 events.append(ev) elif cmd == 'START_MATCH': firstLastTime = time secondLastTime = time startTime = time # Match match.sched_begin = datetime.datetime.fromtimestamp(float(startTime) / 1000) match.begin = datetime.datetime.fromtimestamp(float(startTime) / 1000) _, first_team_name, second_team_name = data.split(',') assert(first_team_name == "Matematici") assert(second_team_name == "Fisici") match.team_a = first_team match.team_b = second_team # Initial swap ev = Event() ev.match = match ev.timestamp = match.begin ev.type = Event.EV_TYPE_SWAP ev.source = Event.EV_SOURCE_MANUAL ev.red_team = first_team ev.blue_team = second_team events.append(ev) elif cmd == 'STOP_MATCH': if firstLastTime != None: timeDelta = time - firstLastTime timePerPair[firstPlayers] += timeDelta timePerPlayer[firstPlayers[0]] += 0.5 * timeDelta timePerPlayer[firstPlayers[1]] += 0.5 * timeDelta if secondLastTime != None: timeDelta = time - secondLastTime timePerPair[secondPlayers] += timeDelta timePerPlayer[secondPlayers[0]] += 0.5 * timeDelta timePerPlayer[secondPlayers[1]] += 0.5 * timeDelta stopTime = time match.sched_end = datetime.datetime.fromtimestamp(float(stopTime) / 1000) match.end = datetime.datetime.fromtimestamp(float(stopTime) / 1000) elif cmd == 'START_LOG': pass else: print "Unknown command: %s" % (cmd) #print (first, goal) if first == None: assert(goal == 0) if first != None: ev = Event() ev.match = match ev.timestamp = datetime.datetime.fromtimestamp(float(time) / 1000) assert(goal == 1 or goal == -1) if goal == 1: ev.type = Event.EV_TYPE_GOAL else: ev.type = Event.EV_TYPE_GOAL_UNDO ev.source = Event.EV_SOURCE_MANUAL if swapped: first = not first if first: ev.team = first_team assert(goal != 0) firstGoal += goal goalPerPlayer[firstPlayers[0]] += 0.5 * goal goalPerPlayer[firstPlayers[1]] += 0.5 * goal goalPerPair[firstPlayers] += goal if firstGoal > secondGoal and firstBest < firstGoal - secondGoal: firstBest = firstGoal - secondGoal else: ev.team = second_team assert(goal != 0) secondGoal += goal goalPerPlayer[secondPlayers[0]] += 0.5 * goal goalPerPlayer[secondPlayers[1]] += 0.5 * goal goalPerPair[secondPlayers] += goal if secondGoal > firstGoal and secondBest < secondGoal - firstGoal: secondBest = secondGoal - firstGoal events.append(ev) session.add(match) # Player association for player in first_players: playermatch = PlayerMatch() playermatch.match = match playermatch.player = player playermatch.team = first_team session.add(playermatch) session.flush() for player in second_players: playermatch = PlayerMatch() playermatch.match = match playermatch.player = player playermatch.team = second_team session.add(playermatch) session.flush() for ev in events: session.add(ev) assert(ev.check_type()) session.flush() session.commit()
def replay_match(orig_match_id, mult=1.0, initial_wait=0.0): session = Session() # Retrieve the original match and prepare the new event list # (already sorted by SQLAlchemy) orig_match = session.query(Match).filter(Match.id == orig_match_id).one() events = [((min(max(x.timestamp, orig_match.begin), orig_match.end) - orig_match.begin).total_seconds(), x) for x in orig_match.events] ref_time = 0.0 for i in xrange(len(events)): delta = events[i][0] - ref_time ref_time = events[i][0] events[i] = (delta / mult, events[i][1]) # Replicate the original match match = Match() match.sched_begin = datetime.datetime.now() + datetime.timedelta(seconds=0.5 * initial_wait) match.sched_end = match.sched_begin + multiply_timedelta(1.0 / mult, orig_match.sched_end - orig_match.sched_begin) match.name = "Replay of \"%s\"" % (orig_match.name) match.team_a = orig_match.team_a match.team_b = orig_match.team_b session.add(match) # Add the advantage phases phase = AdvantagePhase() phase.match = match phase.start_sec = 0 phase.advantage = 10 session.add(phase) phase = AdvantagePhase() phase.match = match phase.start_sec = 30 * 60 phase.advantage = 5 session.add(phase) phase = AdvantagePhase() phase.match = match phase.start_sec = 60 * 60 phase.advantage = 3 session.add(phase) # Replicate the player_matches for orig_pm in session.query(PlayerMatch).filter(PlayerMatch.match == orig_match): pm = PlayerMatch() pm.match = match pm.player = orig_pm.player pm.team = orig_pm.team session.add(pm) # Flush and commit session.flush() session.commit() # Print match ID and start wait print "> Feeding match with ID %d" % (match.id) print "> Scheduled times: %s -- %s" % (match.sched_begin, match.sched_end) print "> Waiting initial %f seconds..." % (initial_wait) time.sleep(initial_wait) # Set begin match.begin = datetime.datetime.now() print "> Match begins at %s" % (match.begin) session.commit() # Replay events for wait_secs, orig_ev in events: print "> Waiting %f seconds..." % (wait_secs) time.sleep(wait_secs) ev = Event() ev.timestamp = datetime.datetime.now() ev.match = match ev.type = orig_ev.type ev.source = orig_ev.source ev.team = orig_ev.team ev.player_a = orig_ev.player_a ev.player_b = orig_ev.player_b ev.red_team = orig_ev.red_team ev.blue_team = orig_ev.blue_team ev.phase = orig_ev.phase print "> Pushing event of type %s and timestamp %s" % (ev.type, ev.timestamp) session.add(ev) ev.check_type() session.commit() # Set end match.end = datetime.datetime.now() print "> Match ends at %s" % (match.end) session.commit() print "> Finished feeding match with ID %d" % (match.id) print "> Scheduled times were: %s -- %s" % (match.sched_begin, match.sched_end)
def addEvent(event, title): ev = Event() ev.title = titles ev.primary = ""
def import_from_2012(): old_session = OldSession() session = Session() # Match and teams match = Match() match.name = "24 ore 2012" first_team = session.query(Team).filter(Team.name == "Matematici").one() second_team = session.query(Team).filter(Team.name == "Fisici").one() match.team_a = first_team match.team_b = second_team # Team mapping _team_map = {} old_first_team = old_session.query(OldTeam).filter(OldTeam.name == "Matematici").one() old_second_team = old_session.query(OldTeam).filter(OldTeam.name == "Fisici").one() _team_map[old_first_team.id] = first_team _team_map[old_second_team.id] = second_team team_map = lambda x: _team_map[x.id] # Player mapping _player_map = {} player_matches = [] found_player_id = set() for old_player in old_session.query(OldPlayer).all(): player = Player.get_or_create(session, old_player.fname, old_player.lname, None) _player_map[old_player.id] = player player_match = PlayerMatch() player_match.player = player player_match.match = match player_match.team = team_map(old_player.team) player_matches.append(player_match) player_map = lambda x: _player_map[x.id] # Events events = [] for old_event in old_session.query(OldEvent).order_by(OldEvent.timestamp).all(): if old_event.type == 'sched_begin': match.sched_begin = old_event.timestamp elif old_event.type == 'begin': match.begin = old_event.timestamp elif old_event.type == 'sched_end': match.sched_end = old_event.timestamp elif old_event.type == 'end': match.end = old_event.timestamp else: event = Event() event.match = match event.timestamp = old_event.timestamp event.source = Event.EV_SOURCE_MANUAL event.type = old_event.type if old_event.type in [Event.EV_TYPE_GOAL, Event.EV_TYPE_GOAL_UNDO]: event.team = _team_map[int(old_event.param)] elif old_event.type == Event.EV_TYPE_SWAP: old_red_team_id, old_blue_team_id = map(int, old_event.param.split(',')) event.red_team = _team_map[old_red_team_id] event.blue_team = _team_map[old_blue_team_id] elif old_event.type == Event.EV_TYPE_CHANGE: old_team_id, old_player_a_id, old_player_b_id = map(int, old_event.param.split(',')) event.team = _team_map[old_team_id] event.player_a = _player_map[old_player_a_id] event.player_b = _player_map[old_player_b_id] found_player_id.add(event.player_a.id) found_player_id.add(event.player_b.id) else: assert(False, "Command not supported") events.append(event) session.add(match) for pm in player_matches: if pm.player.id in found_player_id: session.add(pm) for ev in events: session.add(ev) assert(ev.check_type()) session.flush() old_session.rollback() session.commit()
class Bot: def __init__(self, command_prefix='!'): self.socket = socket() self.connected = False self.command_prefix = command_prefix self.on_message_received = Event() self.on_message_sent = Event() self.on_whisper_received = Event() self.on_whisper_sent = Event() self.on_command_executed = Event() self.on_command_triggered = Event() def disconnect(self): self.socket.close() self.connected = False def run(self): if not self.connected: self._connect() while True: try: msg = self._get_next_socket_msg() self._process_message(msg) except Exception as e: print(f'exception occured in run():\n\t{e}') continue def send(self, content): self.send_raw(f'PRIVMSG #{cfg["channel"]} :{content}') def send_raw(self, content): self.socket.send(encode(content)) def _process_message(self, raw_str): msg = str_to_message(raw_str) if msg is not None: if not self._process_command(msg): self.on_message_received.trigger(msg) def _process_command(self, msg: Message): if msg.content.startswith(self.command_prefix): parts = shlex.split(msg.content) parts[0] = parts[0][1:] cmd = ([c for c in commands if c.name == parts[0]] or (None, ))[0] if cmd: try: cmd(msg, parts[1:]) except Exception as e: print(f'error executing command {cmd.name}:\n\t{e}') return True return False def _connect(self, log_errors=True): try: self.socket.connect(('irc.chat.twitch.tv', 6667)) self._login() self.connected = True except Exception as e: if log_errors: print(f'error occured when trying to connect to chat: \n\t{e}') def _login(self): self.socket.send(encode(f'PASS {cfg["oauth"]}')) self.socket.send(encode(f'NICK {cfg["username"]}')) self.socket.send(encode(f'JOIN #{cfg["channel"]}')) def _get_next_socket_msg(self): return decode(self.socket.recv(1024))
def test_for_object_instance(self): event = Event(1, 'birthday', 11 - 11 - 2016, 18 - 11 - 2016) self.assertIsInstance(event, Event, msg="Check class Instance")
def act_goal_undo(self, team, source=None): e = Event() e.type = Event.EV_TYPE_GOAL_UNDO e.team = team self.act_event(e, source)