Exemple #1
0
    def perform_action(self, packet, connection):
        enemy_connection = None
        for c in self.connections:
            if connection.addr != c.addr:  # Find the enemy team
                enemy_connection = c
        if packet.ACTION == CLIENT_REQUEST_DATABASE:
            # Team object requested
            if self.debug:
                s_print(f'SERVER: Client requested database')
            self.send_packet(connection, SERVER_UPDATE_DATABASE, self.database)
        elif packet.ACTION == CLIENT_REQUEST_TEAM_NAME:
            if connection.team is None:  # Cant request a team name if one is already there.
                connection.team = Team(packet.data)
                self.send_packet(connection, SERVER_UPDATE_TEAMS,
                                 (connection.team, enemy_connection.team))
                self.send_packet(enemy_connection, SERVER_UPDATE_TEAMS,
                                 (enemy_connection.team, connection.team))
            else:
                self.send_packet(
                    connection, SERVER_REQUEST_DENIED,
                    "Couldn't grant team name as team already has one")

        elif packet.ACTION == CLIENT_REQUEST_PLAYER_FOR_TEAM:
            if connection.team is not None:
                drafted_player = self.database.draft_player(packet.data)
                if drafted_player is not None:
                    connection.team.add_to_roster(drafted_player)
                    self.send_packet(connection, SERVER_UPDATE_TEAMS,
                                     (connection.team, enemy_connection.team))
                    self.send_packet(enemy_connection, SERVER_UPDATE_TEAMS,
                                     (enemy_connection.team, connection.team))
                else:
                    self.send_packet(
                        connection, SERVER_REQUEST_DENIED,
                        "tried to draft an already drafted player")
                    if self.debug:
                        s_print(
                            f'{connection.team.teamname} tried to draft an already drafted player'
                        )
            else:
                self.send_packet(connection, SERVER_REQUEST_DENIED,
                                 "Tried to draft without having a team")
                if self.debug:
                    s_print(
                        f'{connection.addr} tried to draft without having a team'
                    )
        elif packet.ACTION == CLIENT_REQUEST_MATCH:
            if connection.team.is_ready() and enemy_connection.team.is_ready():
                combat_log = start_match(connection.team,
                                         enemy_connection.team)
                self.send_packet(connection, SERVER_UPDATE_MATCH, combat_log)
                self.send_packet(enemy_connection, SERVER_UPDATE_MATCH,
                                 combat_log)
            else:
                self.send_packet(connection, SERVER_REQUEST_DENIED,
                                 "A team is lacking players")
def create_match():
    mcode = rand(4)
    with open('mcode.txt', 'a') as mfile:
        mfile.write(mcode + '\n')
    over = int(input('ENTER THE NUMBER OF OVERS: '))
    #get team details
    team = []
    for teams in range(2):
        if (teams == 1):
            team.append('TEAM')
        t = input('\nENTER TEAM NAME: ')
        team.append(t)
        print(' ENTER PLAYERS OF TEAM ' + t +
              ' (ADD (c) for Captain and (wk) for WicketKeeper) :')
        for players in range(11):
            print('PLAYER ', players + 1, end=" ")
            p = input('\t\t\t')
            team.append(p)
    fname = 'teams/team_' + mcode + '.txt'
    str_team = '\n'.join(team)
    with open(fname, 'w') as tfile:
        tfile.write(str_team)
    pos = team.index('TEAM')
    team1 = team[0:pos]
    team1_name = team1[0]
    team2 = team[pos + 1:]
    team2_name = team2[0]
    system('cls')
    print('\nMATCH CODE=', mcode, '\n\n')
    time.sleep(1)
    print('\t\t\t\t ' + f"{team1_name:<6}vs{team2_name:>6}")
    for i in range(12):
        print('\t\t\t' + f"{team1[i]:<20}{team2[i]:>20}")
    won_toss, toss = get_toss(team1_name, team2_name)
    print(won_toss + ' won the toss and choose to' + toss)
    team = zip(team1, team2)
    start_match(mcode, team, won_toss, toss, over)
Exemple #3
0
 def test_start_match_argument_types_int(self):
     with self.assertRaises(ValueError):
         match.start_match(1213, 5123)
Exemple #4
0
 def test_start_match_argument_float(self):
     with self.assertRaises(ValueError):
         match.start_match(1213.4, 5123.2)
Exemple #5
0
 def test_start_match(self):
     self.assertEqual(match.start_match('Arsenal', 'Tottenham'),
                      'Match between Arsenal and Tottenham starts')