def test_area_inserted_touching_left_right():
    area_1 = Area(10, 20, 30, 'Area 1')
    area_2 = Area(50, 20, 10, 'Area 2')
    main_range = Range(area_1, area_1.min_lat, area_1.max_lat)
    main_range.add(area_2, area_2.min_lat, area_2.max_lat)
    assert main_range.space == [-20, 40, 60]
    assert main_range.areas == {(-20, 40): {area_1}, (40, 60): {area_2}}
def test_area_inserted_after():
    area_1 = Area(10, 20, 30, 'Area 1')
    area_2 = Area(50, 20, 5, 'Area 2')
    main_range = Range(area_1, area_1.min_lat, area_1.max_lat)
    main_range.add(area_2, area_2.min_lat, area_2.max_lat)
    assert main_range.space == [-20, 40, 45, 55]
    assert main_range.areas == {(-20, 40): {area_1}, (45, 55): {area_2}}
def test_area_inserted_before():
    area_1 = Area(10, 20, 30, 'Area 1')
    area_2 = Area(-30, 20, 5, 'Area 2')
    main_range = Range(area_1, area_1.min_lat, area_1.max_lat)
    main_range.add(area_2, area_2.min_lat, area_2.max_lat)
    assert main_range.space == [-35, -25, -20, 40]
    assert main_range.areas == {(-20, 40): {area_1}, (-35, -25): {area_2}}
def test_area_inserted_aligns():
    area_1 = Area(10, 20, 30, 'Area 1')
    area_2 = Area(10, 20, 30, 'Area 2')
    main_range = Range(area_1, area_1.min_lat, area_1.max_lat)
    main_range.add(area_2, area_2.min_lat, area_2.max_lat)
    assert main_range.space == [-20, 40]
    assert main_range.areas == {(-20, 40): {area_1, area_2}}
Exemplo n.º 5
0
    def play_hand(self, feedback_file_name, action):
        date_time = datetime.datetime.now().strftime("%m-%d-%y %H:%M:%S")
        deck = Deck()
        deck.create()
        deck.shuffle()
        hand = Hand()
        hand.get_hand(deck)
        hand.order_hand()
        hand_type = hand.hand_type()
        position = Position()
        position.current_position(self.num_players)
        position_min = position.min_open()
        r = Range(self.hand_range)
        correct_decision, hand_percent, total_cards = r.correct_decision(
            hand_type, position_min)
        min_open_hand = r.min_open_card(position_min)
        decision = Decision(action).decision()

        if self.show_feedback:
            if decision == correct_decision:
                feedback = "Correct"  # report feedback
                self.score += 1
            else:
                feedback = "Incorrect"  # report feedback
            feedback_file_name.save_hand(self.hand_num, date_time,
                                         self.hand_range, feedback, position,
                                         position_min, min_open_hand, hand,
                                         hand_type, hand_percent, decision,
                                         correct_decision, self.score)
            self.hand_num += 1
        else:
            self.hand_num += 1

        return self.hand_num
def test_area_inserted_crossing_on_the_left():
    area_1 = Area(10, 20, 30, 'Area 1')
    area_2 = Area(-30, 20, 20, 'Area 2')
    main_range = Range(area_1, area_1.min_lat, area_1.max_lat)
    main_range.add(area_2, area_2.min_lat, area_2.max_lat)
    assert main_range.space == [-50, -20, -10, 40]
    assert main_range.areas == {
        (-50, -20): {area_2},
        (-20, -10): {area_1, area_2},
        (-10, 40): {area_1}
    }
def test_area_inserted_outside():
    area_1 = Area(10, 20, 30, 'Area 1')
    area_2 = Area(10, 20, 40, 'Area 2')
    main_range = Range(area_1, area_1.min_lat, area_1.max_lat)
    main_range.add(area_2, area_2.min_lat, area_2.max_lat)
    assert main_range.space == [-30, -20, 40, 50]
    assert main_range.areas == {
        (-30, -20): {area_2},
        (-20, 40): {area_1, area_2},
        (40, 50): {area_2}
    }
def test_area_inserted_crossing_on_the_right():
    area_1 = Area(10, 20, 30, 'Area 1')
    area_2 = Area(30, 20, 20, 'Area 2')
    main_range = Range(area_1, area_1.min_lat, area_1.max_lat)
    main_range.add(area_2, area_2.min_lat, area_2.max_lat)
    assert main_range.space == [-20, 10, 40, 50]
    assert main_range.areas == {
        (-20, 10): {area_1},
        (10, 40): {area_1, area_2},
        (40, 50): {area_2}
    }
Exemplo n.º 9
0
    def bulk_create(self, areas):
        """
        :param list(tuple) areas: area description should contain latitude, longitude, radius and id.

        >>> RangeBasedAreaIndex().bulk_create([(-24.5, 134.8, 20, 'Australia')])
        """
        areas = self._create_areas(areas)
        first_area = areas.pop()
        self.latitudes = Range(first_area, first_area.min_lat,
                               first_area.max_lat)
        self.longitudes = Range(first_area, first_area.min_long,
                                first_area.max_long)
        for area in areas:
            self.latitudes.add(area, area.min_lat, area.max_lat)
            self.longitudes.add(area, area.min_long, area.max_long)
def test_area_inserted_touching_on_all_sides():
    main_area = Area(0, 20, 10, 'Area 1')
    left = Area(-20, 20, 10, 'Area 2')
    left_right = Area(-6, 20, 4, 'Area 3')
    right_left = Area(6, 20, 4, 'Area 4')
    right = Area(20, 20, 10, 'Area 5')
    main_range = Range(main_area, main_area.min_lat, main_area.max_lat)
    main_range.add(left, left.min_lat, left.max_lat)
    main_range.add(left_right, left_right.min_lat, left_right.max_lat)
    main_range.add(right_left, right_left.min_lat, right_left.max_lat)
    main_range.add(right, right.min_lat, right.max_lat)
    assert main_range.space == [-30, -10, -2, 2, 10, 30]
    assert main_range.areas == {
        (-30, -10): {left},
        (-10, -2): {main_area, left_right},
        (-2, 2): {main_area},
        (2, 10): {main_area, right_left},
        (10, 30): {right}
    }
def test_area_inserted_between():
    left = Area(-20, 20, 10, 'Left')
    right = Area(20, 20, 10, 'Right')
    middle = Area(0, 20, 5, 'Middle')
    main_range = Range(left, left.min_lat, left.max_lat)
    main_range.add(right, right.min_lat, right.max_lat)
    main_range.add(middle, middle.min_lat, middle.max_lat)
    assert main_range.space == [-30, -10, -5, 5, 10, 30]
    assert main_range.areas == {
        (-30, -10): {left},
        (-10, -5): set(),
        (-5, 5): {middle},
        (5, 10): set(),
        (10, 30): {right}
    }
def test_area_inserted_crossing_on_both_sides_and_the_middle():
    main_area = Area(0, 20, 20, 'Main')
    left_cross = Area(-20, 20, 30, 'Left Cross')
    right_cross = Area(20, 20, 30, 'Right Cross')
    main_range = Range(main_area, main_area.min_lat, main_area.max_lat)
    main_range.add(left_cross, left_cross.min_lat, left_cross.max_lat)
    main_range.add(right_cross, right_cross.min_lat, right_cross.max_lat)
    assert main_range.space == [-50, -20, -10, 10, 20, 50]
    assert main_range.areas == {
        (-50, -20): {left_cross},
        (-20, -10): {main_area, left_cross},
        (-10, 10): {main_area, left_cross, right_cross},
        (10, 20): {main_area, right_cross},
        (20, 50): {right_cross}
    }
Exemplo n.º 13
0
class RangeBasedAreaIndex(BaseAreaIndex):
    def __init__(self):
        self.latitudes = None
        self.longitudes = None

    def bulk_create(self, areas):
        """
        :param list(tuple) areas: area description should contain latitude, longitude, radius and id.

        >>> RangeBasedAreaIndex().bulk_create([(-24.5, 134.8, 20, 'Australia')])
        """
        areas = self._create_areas(areas)
        first_area = areas.pop()
        self.latitudes = Range(first_area, first_area.min_lat,
                               first_area.max_lat)
        self.longitudes = Range(first_area, first_area.min_long,
                                first_area.max_long)
        for area in areas:
            self.latitudes.add(area, area.min_lat, area.max_lat)
            self.longitudes.add(area, area.min_long, area.max_long)

    def query(self, lat, long):
        """
        :param float lat:
        :param float long:
        :return: all areas that include given point.
        :rtype: set(Area)

        >>> RangeBasedAreaIndex().query(0.0, 0.0)
        set()

        """
        if self.longitudes and self.latitudes:
            possible_areas = self.latitudes.search(lat).union(
                self.longitudes.search(long))
            return self._find_areas(possible_areas, lat, long)
        else:
            return set()
def test_range_is_created():
    area = Area(10, 20, 30, 'Area 1')
    main_range = Range(area, area.min_lat, area.max_lat)
    assert main_range.space == [-20, 40]
    assert main_range.areas == {(-20, 40): {area}}
def test_query_found_in_multiple_areas():
    main_area = Area(0, 20, 20, 'Main')
    left_cross = Area(-20, 20, 30, 'Left Cross')
    right_cross = Area(20, 20, 30, 'Right Cross')
    main_range = Range(main_area, main_area.min_lat, main_area.max_lat)
    main_range.add(left_cross, left_cross.min_lat, left_cross.max_lat)
    main_range.add(right_cross, right_cross.min_lat, right_cross.max_lat)
    assert main_range.search(-50) == {left_cross}
    assert main_range.search(-40) == {left_cross}
    assert main_range.search(-20) == {main_area, left_cross}
    assert main_range.search(-15) == {main_area, left_cross}
    assert main_range.search(-10) == {main_area, left_cross, right_cross}
    assert main_range.search(0) == {main_area, left_cross, right_cross}
    assert main_range.search(10) == {main_area, left_cross, right_cross}
    assert main_range.search(15) == {main_area, right_cross}
    assert main_range.search(20) == {main_area, right_cross}
    assert main_range.search(40) == {right_cross}
    assert main_range.search(50) == {right_cross}
def test_query_not_found():
    area_1 = Area(0, 20, 10, 'Area 1')
    main_range = Range(area_1, area_1.min_lat, area_1.max_lat)
    assert main_range.search(-11) == []
    assert main_range.search(11) == []
    def play_hand(self, feedback_file_name):
        date_time = datetime.datetime.now().strftime("%m-%d-%y %H:%M:%S")
        print("Hand number: " + str(self.hand_num))

        deck = Deck()
        deck.create()
        deck.shuffle()

        hand = Hand()
        hand.get_hand(deck)
        print(hand)

        hand.order_hand()
        hand_type = hand.hand_type()

        position = Position()
        position.current_position(self.num_players)
        print(position)

        position_min = position.min_open()

        r = Range(self.hand_range)
        correct_decision, hand_percent, total_cards = r.correct_decision(
            hand_type, position_min)
        # Leaving the following several print statements for reference in case someone else isn't that familiar with hand range calculations.
        # print(correct_decision)
        # print(hand_percent)
        # print(total_cards)  # To confirm the hand percentage is correct: total_cards / 1326 = hand_percent

        min_open_hand = r.min_open_card(position_min)
        # print(min_open_hand)

        action = int(input("What is your decision? (4=Open, 6=Fold): ")
                     )  # For faster keyboard input
        decision = Decision(action).decision()

        if decision != "stop":
            if self.show_feedback:
                if decision == correct_decision:
                    # screen feedback
                    print(
                        "Good job! You should", correct_decision,
                        "because you want to play the top {0:.2f}".format(
                            position_min * 100) +
                        "% of your range; which ends at " +
                        str(min_open_hand) + ".\n" + str(hand) +
                        "is in the top {0:.2f}".format(hand_percent * 100) +
                        "% of starting hands for the " + self.hand_range +
                        " range.")
                    feedback = "Correct"  # report feedback
                    self.score += 1
                else:
                    print(
                        "Sorry, you should", correct_decision,
                        "because you want to play the top {0:.2f}".format(
                            position_min * 100) +
                        "% of your range; which ends at " +
                        str(min_open_hand) + ".\n" + str(hand) +
                        "is in the top {0:.2f}".format(hand_percent * 100) +
                        "% of starting hands for the " + self.hand_range +
                        " range.")
                    feedback = "Incorrect"  # report feedback
                feedback_file_name.save_hand(self.hand_num, date_time,
                                             self.hand_range, feedback,
                                             position, position_min,
                                             min_open_hand, hand, hand_type,
                                             hand_percent, decision,
                                             correct_decision, self.score)
                self.hand_num += 1
                print("Score: " + str(self.score) + "\n")
            else:
                self.hand_num += 1
                print()
        else:
            print("Thanks for playing.")

        return action
def test_query_found_in_one_area():
    area_1 = Area(0, 20, 10, 'Area 1')
    main_range = Range(area_1, area_1.min_lat, area_1.max_lat)
    assert main_range.search(0) == {area_1}
    assert main_range.search(-10) == {area_1}
    assert main_range.search(10) == {area_1}