Coverage for bundesliga_tippspiel/db/user_generated/Bet.py: 97%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

49 statements  

1"""LICENSE 

2Copyright 2017 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of bundesliga-tippspiel. 

5 

6bundesliga-tippspiel is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11bundesliga-tippspiel is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with bundesliga-tippspiel. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20from typing import Any, List, Optional 

21from jerrycan.base import db 

22from jerrycan.db.ModelMixin import ModelMixin 

23from jerrycan.db.User import User 

24from bundesliga_tippspiel.db.match_data.Match import Match 

25 

26 

27class Bet(ModelMixin, db.Model): 

28 """ 

29 Model that describes the 'bets' SQL table 

30 """ 

31 

32 MAX_POINTS: int = 15 

33 POSSIBLE_POINTS: List[int] = [0, 3, 7, 10, 12, 15] 

34 

35 def __init__(self, *args, **kwargs): 

36 """ 

37 Initializes the Model 

38 :param args: The constructor arguments 

39 :param kwargs: The constructor keyword arguments 

40 """ 

41 super().__init__(*args, **kwargs) 

42 

43 __tablename__ = "bets" 

44 __table_args__ = ( 

45 db.ForeignKeyConstraint( 

46 ("home_team_abbreviation", "away_team_abbreviation", 

47 "league", "season", "matchday"), 

48 (Match.home_team_abbreviation, Match.away_team_abbreviation, 

49 Match.league, Match.season, Match.matchday) 

50 ), 

51 ) 

52 

53 league: str = db.Column(db.String(255), primary_key=True) 

54 season: int = db.Column(db.Integer, primary_key=True) 

55 matchday: int = db.Column(db.Integer, primary_key=True) 

56 home_team_abbreviation: str = db.Column(db.String(3), primary_key=True) 

57 away_team_abbreviation: str = db.Column(db.String(3), primary_key=True) 

58 user_id: int = db.Column( 

59 db.Integer, 

60 db.ForeignKey("users.id"), 

61 primary_key=True 

62 ) 

63 

64 home_score: int = db.Column(db.Integer, nullable=False) 

65 away_score: int = db.Column(db.Integer, nullable=False) 

66 points: int = db.Column(db.Integer, nullable=True) 

67 

68 user: User = db.relationship( 

69 "User", backref=db.backref("bets", cascade="all, delete") 

70 ) 

71 match: Match = db.relationship("Match", overlaps="bets") 

72 

73 def __repr__(self) -> str: 

74 """ 

75 :return: A string with which the object may be generated 

76 """ 

77 params = "" 

78 

79 for key, val in self.__json__().items(): 

80 if key == "points": 

81 continue 

82 params += "{}={}, ".format(key, repr(val)) 

83 params = params.rsplit(",", 1)[0] 

84 

85 return "{}({})".format(self.__class__.__name__, params) 

86 

87 def __eq__(self, other: Any) -> bool: 

88 """ 

89 Checks the model object for equality with another object 

90 :param other: The other object 

91 :return: True if the objects are equal, False otherwise 

92 """ 

93 if isinstance(other, Bet): 

94 return self.user_id == other.user_id \ 

95 and self.home_team_abbreviation == \ 

96 other.home_team_abbreviation \ 

97 and self.away_team_abbreviation == \ 

98 other.away_team_abbreviation \ 

99 and self.home_score == other.home_score \ 

100 and self.away_score == other.away_score 

101 else: 

102 return False # pragma: no cover 

103 

104 def evaluate(self) -> Optional[int]: 

105 """ 

106 Evaluates the current points score on this bet 

107 :return: The calculated points (or None if the math hasn't started yet) 

108 """ 

109 if not self.match.has_started: 109 ↛ 110line 109 didn't jump to line 110, because the condition on line 109 was never true

110 return None 

111 

112 points = 0 

113 bet_diff = self.home_score - self.away_score 

114 match_diff = \ 

115 self.match.home_current_score - self.match.away_current_score 

116 

117 if bet_diff == match_diff: # Correct goal difference 

118 points += 5 

119 

120 if bet_diff * match_diff > 0: # Correct winner 

121 points += 7 

122 elif bet_diff == 0 and match_diff == 0: # Draw 

123 points += 7 

124 

125 if self.home_score == self.match.home_current_score \ 

126 or self.away_score == self.match.away_current_score: 

127 points += 3 

128 

129 return points