def __init__(self): self.user_id = server.GetNextUniqueId() self.events = [] self.handle = 'User %03d' % server.GetNextUniqueId() self.given_name = 'Ana Itza' self.locale = 'en-US' self.get_event_handler = None self.prefs = tbmatch.match_pb2.PlayerPreferences()
def Poll(self): logging.debug('running matchmaker poll (%d users)', len(self.queue_users)) while len(self.queue_users) >= 2: p1 = self.queue_users.pop() p2 = self.queue_users.pop() # TODO put these 2 users in a temporary queue for re-queue in the case echo test failed # found a match match_id = server.GetNextUniqueId() # create intermediate proto structures game_config = server.models.match.CreateGameConfig(match_id, p1.user, p1.gameplay_options.character, p2.user, p2.gameplay_options.character) game_session = server.models.match.CreateGameSessionRequest(p1.gameplay_options.character, p2.gameplay_options.character) p1port, p2port = server.portal.StartGameSession(game_session, game_config, p1.user, p2.user) game_endpoint_config1 = server.models.match.CreateGameEndpointConfig(0, p1port, game_session.spec[0].secret) game_endpoint_config2 = server.models.match.CreateGameEndpointConfig(1, p2port, game_session.spec[1].secret) # create the final proto payload and send them as events to the matched users status = tbmatch.event_pb2.WaitMatchProgressEvent.MATCH wait_match_progress_event1 = server.models.match.CreateWaitMatchProgressEvent(status, match_id, game_config, game_endpoint_config1) event1 = tbmatch.event_pb2.Event() event1.type = tbmatch.event_pb2.Event.E_WAIT_MATCH_PROGRESS event1.wait_match_progress.CopyFrom(wait_match_progress_event1) p1.user.SendEvent(event1) event2 = tbmatch.event_pb2.Event() wait_match_progress_event2 = server.models.match.CreateWaitMatchProgressEvent(status, match_id, game_config, game_endpoint_config2) event2.type = tbmatch.event_pb2.Event.E_WAIT_MATCH_PROGRESS event2.wait_match_progress.CopyFrom(wait_match_progress_event2) p2.user.SendEvent(event2)
def __init__(self, name): self.name = name self.members = {} self.queue = [] self.lobby_id = server.GetNextUniqueId() self.owner_user_id = None self.join_code = ''.join( random.choice(string.letters).upper() for i in xrange(5))
def __init__(self, username="******"): self.user_id = server.GetNextUniqueId() self.events = [] self.handle = "{0} #{1}".format(username, self.user_id) self.given_name = 'Ana Itza' self.locale = 'en-US' self.get_event_handler = None self.prefs = tbmatch.match_pb2.PlayerPreferences()
def SendEvent(self, e): # sometimes it's useful to broadcast one event to multiple parties. # make a copy of the event structure here so those have the proper # event id event = tbmatch.event_pb2.Event() event.CopyFrom(e) event.event_id = server.GetNextUniqueId() self.events.append(event) self.Log('appending event to list (id:{0})'.format(event.event_id)) handler = self.get_event_handler if handler: self.Log('calling pending get event handler to send events') self.SendPendingEvents(handler) self.get_event_handler = None
def StartMatchIfReady(self): if len(self.members) < 2: return p1 = self.members[self.queue[0]] p2 = self.members[self.queue[1]] if not p1.ready or not p2.ready: return # found a match match_id = server.GetNextUniqueId() # create intermediate proto structures game_config = server.models.match.CreateGameConfig( match_id, p1.user, p1.character, p2.user, p2.character) game_session = server.models.match.CreateGameSessionRequest( p1.character, p2.character) p1port, p2port = server.portal.StartGameSession( game_session, game_config, p1.user, p2.user) game_endpoint_config1 = server.models.match.CreateGameEndpointConfig( 0, p1port, game_session.spec[0].secret, 1000) game_endpoint_config2 = server.models.match.CreateGameEndpointConfig( 1, p2port, game_session.spec[1].secret, 1000) # send lobby match start events to both players event1 = tbmatch.event_pb2.Event() event1.type = tbmatch.event_pb2.Event.E_LOBBY_MATCH_START event1.lobby_match_start.lobby_id = self.lobby_id event1.lobby_match_start.match_id = match_id event1.lobby_match_start.config.CopyFrom(game_config) event1.lobby_match_start.endpoint.CopyFrom(game_endpoint_config1) p1.user.SendEvent(event1) event2 = tbmatch.event_pb2.Event() event2.type = tbmatch.event_pb2.Event.E_LOBBY_MATCH_START event2.lobby_match_start.lobby_id = self.lobby_id event2.lobby_match_start.match_id = match_id event2.lobby_match_start.config.CopyFrom(game_config) event2.lobby_match_start.endpoint.CopyFrom(game_endpoint_config2) p2.user.SendEvent(event2)