app/gameimpl/x01_match.py
def record_statistics(self, player_index, visit, result):
        """Store stats both for in-memory immediate use and send a message to the stats consumer (micro)service

        :return:
        """
        if result is not 0:
            visit.remove_trailing_darts(result)  # a double finished the game, so ignore any subsequent darts

        self.match.visits[player_index].append(visit)

        # Calculate first 9 if, and only if, this is the 3rd visit
        if len(self.match.visits[player_index]) == 3:
            self.first9[player_index] = (STARTING_TOTAL - self.scores[player_index]) / 3

        # Calculate single-dart average taking account of a double being hit with dart 1 or 2 when checking out
        num_darts_thrown = (len(self.match.visits[player_index]) - 1) * 3
        num_darts_thrown += 3 if result is 0 else result

        if result is not 0:
            self.match.winning_num_darts = num_darts_thrown
            self.match.winning_player_index = player_index

        self.averages[player_index] = (STARTING_TOTAL - self.scores[player_index]) / num_darts_thrown

        # send a message using RabbitMQ - note this is too implementation specific and should be abstracted, i.e. move
        # rabbitmq specific code to a separate service layer class
        # Let's do something simple - store the darts so a lifetime 3-dart average can be calculated; this is something
        # of a lower priority than the ongoing match, so can be backgrounded / temporally-decoupled somewhat with a
        # message queue (handle load better).
        # We will need to serialize the darts list - JSON is very suitable for this

        username = self.match.players[player_index]
        match_type = "X01"
        darts = []
        for dart in visit.darts:
            darts.append([dart.multiplier, dart.segment])
        message = [username, match_type, darts]
        connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))  # we could parameterize the host
        channel = connection.channel()
        channel.queue_declare(queue='player-stats')
        channel.basic_publish(exchange='',
                              routing_key='player-stats',
                              body=json.dumps(message))
        connection.close()
Store stats both for in-memory immediate use and send a message to the stats consumer (micro)service

        :return:
        
Store stats both for in-memory immediate use and send a message to the stats consumer (micro)service
Store stats both for in - memory immediate use and send a message to the stats consumer ( micro ) service
def record_statistics(self, player_index, visit, result):
        if result is not 0:
            visit.remove_trailing_darts(result)  
        self.match.visits[player_index].append(visit)
        if len(self.match.visits[player_index]) == 3:
            self.first9[player_index] = (STARTING_TOTAL - self.scores[player_index]) / 3
        num_darts_thrown = (len(self.match.visits[player_index]) - 1) * 3
        num_darts_thrown += 3 if result is 0 else result
        if result is not 0:
            self.match.winning_num_darts = num_darts_thrown
            self.match.winning_player_index = player_index
        self.averages[player_index] = (STARTING_TOTAL - self.scores[player_index]) / num_darts_thrown
        username = self.match.players[player_index]
        match_type = "X01"
        darts = []
        for dart in visit.darts:
            darts.append([dart.multiplier, dart.segment])
        message = [username, match_type, darts]
        connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))  
        channel = connection.channel()
        channel.queue_declare(queue='player-stats')
        channel.basic_publish(exchange='',
                              routing_key='player-stats',
                              body=json.dumps(message))
        connection.close()
def record_statistics ( self , player_index , visit , result ) : if result is not 0 : visit . remove_trailing_darts ( result ) self . match . visits [ player_index ] . append ( visit ) if len ( self . match . visits [ player_index ] ) == 3 : self . first9 [ player_index ] = ( STARTING_TOTAL - self . scores [ player_index ] ) / 3 num_darts_thrown = ( len ( self . match . visits [ player_index ] ) - 1 ) * 3 num_darts_thrown += 3 if result is 0 else result if result is not 0 : self . match . winning_num_darts = num_darts_thrown self . match . winning_player_index = player_index self . averages [ player_index ] = ( STARTING_TOTAL - self . scores [ player_index ] ) / num_darts_thrown username = self . match . players [ player_index ] match_type = "X01" darts = [ ] for dart in visit . darts : darts . append ( [ dart . multiplier , dart . segment ] ) message = [ username , match_type , darts ] connection = pika . BlockingConnection ( pika . ConnectionParameters ( 'localhost' ) ) channel = connection . channel ( ) channel . queue_declare ( queue = 'player-stats' ) channel . basic_publish ( exchange = '' , routing_key = 'player-stats' , body = json . dumps ( message ) ) connection . close ( )
Store stats both for in-memory immediate use and send a message to the stats consumer (micro)service
Store stats both for in - memory immediate use and send a message to the stats consumer ( micro ) service
"""Store stats both for in-memory immediate use and send a message to the stats consumer (micro)service

        :return:
        """ # a double finished the game, so ignore any subsequent darts # Calculate first 9 if, and only if, this is the 3rd visit # Calculate single-dart average taking account of a double being hit with dart 1 or 2 when checking out # send a message using RabbitMQ - note this is too implementation specific and should be abstracted, i.e. move # rabbitmq specific code to a separate service layer class # Let's do something simple - store the darts so a lifetime 3-dart average can be calculated; this is something # of a lower priority than the ongoing match, so can be backgrounded / temporally-decoupled somewhat with a # message queue (handle load better). # We will need to serialize the darts list - JSON is very suitable for this # we could parameterize the host
None
self
player_index
visit
result