diff --git a/README.md b/README.md index 39af52c..065c6f1 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,27 @@ -# README # +# Generic Lottery game number checker # +Currently includes Powerball and Megamillions. -This README would normally document whatever steps are necessary to get your application up and running. +You add your picks to {game}/picks.py, and run `./play -g {gamename}` eg `.play.py -g powerball` -### What is this repository for? ### +The script will pull the current winning numbers from an rss feed, and compare them to each ticket you list in the picks.py file for that game. -* Quick summary -* Version -* [Learn Markdown](https://bitbucket.org/tutorials/markdowndemo) - -### How do I get set up? ### - -* Summary of set up -* Configuration -* Dependencies -* Database configuration -* How to run tests -* Deployment instructions - -### Contribution guidelines ### - -* Writing tests -* Code review -* Other guidelines - -### Who do I talk to? ### - -* Repo owner or admin -* Other community or team contact \ No newline at end of file +The output per ticket: +```text +Winning Numbers: [2, 13, 32, 33, 48] +Winning Powerball: 22 +Your Numbers: (1, 2, 3, 4, 5) +Your Powerball: 9 +Matched Numbers: [2] +Matched Powerball: False +Not a Winner! +``` +OR +```text +Winning Numbers: [2, 13, 32, 33, 48] +Winning Powerball: 22 +Your Numbers: (1, 2, 3, 4, 5) +Your Powerball: 22 +Matched Numbers: [2] +Matched Powerball: True +Ticket is worth: 8 +``` \ No newline at end of file diff --git a/lib/Feed.py b/lib/Feed.py new file mode 100644 index 0000000..7aefea2 --- /dev/null +++ b/lib/Feed.py @@ -0,0 +1,36 @@ +import feedparser + + +class Feed: + + def __init__(self, feed): + self.feed = 'https://www.texaslottery.com/export/sites/lottery/rss/tlc_latest.xml' + self.parsed_feed = feedparser.parse(self.feed) + self.game_entries = feed + self.modifiers = { + 'Billion': 1000000000, + 'Million': 1000000 + } + + def get_winning_numbers(self): + entry = self.game_entries + nums_pre = self.parsed_feed.entries[entry['winning_numbers']].summary + nums_pre = nums_pre.replace(entry['special_ball'], '-').replace(f' {entry["multiplier"]}', '-') + nums = nums_pre.split(' - ') + + winning_numbers = nums[:5] + + for idx, num in enumerate(nums[:5]): + winning_numbers[idx] = int(num) + + special_ball = int(nums[5]) + multiplier = int(nums[6]) + return {'numbers': winning_numbers, 'special_ball': special_ball, 'multiplier': multiplier} + + def get_jackpot(self): + entry = self.game_entries + jp_pre = self.parsed_feed.entries[entry['jackpot']].summary + jp_pre = jp_pre[jp_pre.index('Cash'):] + jp_pre = jp_pre[jp_pre.index(':') + 3:] + jp_data = jp_pre.split() + return float(jp_data[0]) * self.modifiers[jp_data[1]] diff --git a/lib/Ticket.py b/lib/Ticket.py new file mode 100644 index 0000000..aeb8924 --- /dev/null +++ b/lib/Ticket.py @@ -0,0 +1,70 @@ +class Ticket: + + def __init__(self, my_picks, my_special_ball): + self.jackpot = 0 + self.my_picks = my_picks + self.my_special_ball = my_special_ball + self.winning_numbers = [] + self.special_ball = None + self.multiplier = None + self.matched_numbers = [] + self.matched_special_ball = None + self.winnings = 0 + self.feed = None + + def get_jp_amount(self): + self.jackpot = self.feed.get_jackpot() + + def get_winning_numbers(self): + winners = self.feed.get_winning_numbers() + self.winning_numbers = winners['numbers'] + self.special_ball = winners['special_ball'] + self.multiplier = winners['multiplier'] + + def process_ticket(self): + self.check_numbers() + self.check_special_ball() + self.get_jp_amount() + special_ball_wins = self.get_winnings_with_special_ball(self.jackpot) + non_special_ball_wins = self.get_winnings_without_special_ball() + self.process_winnings(special_ball_wins, non_special_ball_wins) + + def check_numbers(self): + for num in self.my_picks: + if num in self.winning_numbers: + self.matched_numbers.append(num) + + def check_special_ball(self): + self.matched_special_ball = False + + if self.special_ball is not None and self.my_special_ball == self.special_ball: + self.matched_special_ball = True + + def process_winnings(self, special_ball_wins, non_special_ball_wins): + wins = non_special_ball_wins + + if self.matched_special_ball: + wins = special_ball_wins + + self.winnings = wins[self.multiplier][len(self.matched_numbers)] + self.output_ticket() + + def output_ticket(self): + special_ball = self.feed.game_entries["special_ball"] + print(f'Winning Numbers: {self.winning_numbers}') + print(f'Winning {special_ball}: {self.special_ball}') + print(f'Your Numbers: {self.my_picks}') + print(f'Your {special_ball}: {self.my_special_ball}') + print(f'Matched Numbers: {self.matched_numbers}') + print(f'Matched {special_ball}: {"True" if self.matched_special_ball else "False"}') + if self.winnings > 0: + print(f'Ticket is worth: {self.winnings}') + else: + print(f'Not a Winner!') + print() + + def get_winnings_with_special_ball(self, jp): + pass + + def get_winnings_without_special_ball(self): + pass diff --git a/megamillions/MegaMillions.py b/megamillions/MegaMillions.py new file mode 100644 index 0000000..7653ac7 --- /dev/null +++ b/megamillions/MegaMillions.py @@ -0,0 +1,18 @@ +from lib.Ticket import Ticket +from lib.Feed import Feed +from .feed import feed +from .winning_structure import get_winnings_mb, get_winnings_no_mb + + +class MegaMillions(Ticket): + + def __init__(self, my_picks, my_special_ball): + super().__init__(my_picks, my_special_ball) + self.feed = Feed(feed) + self.get_winning_numbers() + + def get_winnings_with_special_ball(self, jp): + return get_winnings_mb(self.jackpot) + + def get_winnings_without_special_ball(self): + return get_winnings_no_mb() diff --git a/megamillions/__init__.py b/megamillions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/megamillions/feed.py b/megamillions/feed.py new file mode 100644 index 0000000..65805be --- /dev/null +++ b/megamillions/feed.py @@ -0,0 +1,6 @@ +feed = { + 'winning_numbers': 2, + 'jackpot': 3, + 'special_ball': 'MegaBall', + 'multiplier': 'Megaplier' +} diff --git a/megamillions/game.py b/megamillions/game.py new file mode 100755 index 0000000..fbd7e38 --- /dev/null +++ b/megamillions/game.py @@ -0,0 +1,13 @@ +#! /usr/bin/python3 + +from .picks import my_picks +from .MegaMillions import MegaMillions + +cur_picks = [] +cur_pb = 0 + +for idx, tkt in enumerate(my_picks): + cur_picks = tkt['picks'] + cur_pb = tkt['mb'] + pbtkt = MegaMillions(cur_picks, cur_pb) + pbtkt.process_ticket() diff --git a/megamillions/picks.py b/megamillions/picks.py new file mode 100644 index 0000000..f6bd662 --- /dev/null +++ b/megamillions/picks.py @@ -0,0 +1,4 @@ +my_picks = [ + {'picks': (1, 2, 3, 4, 5), 'mb': 9}, + {'picks': (2, 3, 4, 5, 6), 'mb': 19}, +] diff --git a/megamillions/winning_structure.py b/megamillions/winning_structure.py new file mode 100644 index 0000000..87adac9 --- /dev/null +++ b/megamillions/winning_structure.py @@ -0,0 +1,16 @@ +def get_winnings_mb(jackpot_cash): + return { + 2: [4, 8, 20, 400, 20_000, jackpot_cash], + 3: [6, 12, 30, 600, 30_000, jackpot_cash], + 4: [8, 16, 40, 800, 40_000, jackpot_cash], + 5: [10, 20, 50, 1_000, 50_000, jackpot_cash] + } + + +def get_winnings_no_mb(): + return { + 2: [0, 0, 0, 20, 1_000, 2_000_000], + 3: [0, 0, 0, 30, 1_500, 3_000_000], + 4: [0, 0, 0, 40, 2_000, 4_000_000], + 5: [0, 0, 0, 50, 2_500, 5_000_000] + } diff --git a/play.py b/play.py new file mode 100755 index 0000000..3d40f00 --- /dev/null +++ b/play.py @@ -0,0 +1,27 @@ +#!/usr/bin/python3 + +import getopt +import sys +import importlib + + +def main(argv): + game = '' + usage = 'play.py -g ' + + try: + opts, args = getopt.getopt(argv, "hg:", ["game="]) + except getopt.GetoptError: + print(usage) + sys.exit(2) + for opt, arg in opts: + if opt == '-h': + print(usage) + sys.exit() + elif opt in ("-g", "--game"): + game = arg + importlib.import_module(f'{game}.game') + + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/powerball/Powerball.py b/powerball/Powerball.py new file mode 100644 index 0000000..1d548a9 --- /dev/null +++ b/powerball/Powerball.py @@ -0,0 +1,18 @@ +from lib.Ticket import Ticket +from lib.Feed import Feed +from .feed import feed +from .winning_structure import get_winnings_pb, get_winnings_no_pb + + +class Powerball(Ticket): + + def __init__(self, my_picks, my_special_ball): + super().__init__(my_picks, my_special_ball) + self.feed = Feed(feed) + self.get_winning_numbers() + + def get_winnings_with_special_ball(self, jp): + return get_winnings_pb(self.jackpot) + + def get_winnings_without_special_ball(self): + return get_winnings_no_pb() diff --git a/powerball/__init__.py b/powerball/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/powerball/feed.py b/powerball/feed.py new file mode 100644 index 0000000..244e7c3 --- /dev/null +++ b/powerball/feed.py @@ -0,0 +1,6 @@ +feed = { + 'winning_numbers': 0, + 'jackpot': 1, + 'special_ball': 'Powerball', + 'multiplier': 'Power Play' +} diff --git a/powerball/game.py b/powerball/game.py new file mode 100755 index 0000000..ab2f8e3 --- /dev/null +++ b/powerball/game.py @@ -0,0 +1,13 @@ +#! /usr/bin/python3 + +from .picks import my_picks +from .Powerball import Powerball + +cur_picks = [] +cur_pb = 0 + +for idx, tkt in enumerate(my_picks): + cur_picks = tkt['picks'] + cur_pb = tkt['pb'] + pbtkt = Powerball(cur_picks, cur_pb) + pbtkt.process_ticket() diff --git a/powerball/picks.py b/powerball/picks.py new file mode 100644 index 0000000..fcd1e89 --- /dev/null +++ b/powerball/picks.py @@ -0,0 +1,4 @@ +my_picks = [ + {'picks': (1, 2, 3, 4, 5), 'pb': 22}, + {'picks': (2, 3, 4, 5, 6), 'pb': 19}, +] diff --git a/powerball/winning_structure.py b/powerball/winning_structure.py new file mode 100644 index 0000000..63dace2 --- /dev/null +++ b/powerball/winning_structure.py @@ -0,0 +1,20 @@ +def get_winnings_pb(jackpot_cash): + return { + 0: [4, 4, 7, 100, 50_000, jackpot_cash], + 2: [8, 8, 14, 200, 100_000, jackpot_cash], + 3: [12, 12, 21, 300, 150_000, jackpot_cash], + 4: [16, 16, 28, 400, 200_000, jackpot_cash], + 5: [20, 20, 35, 500, 250_000, jackpot_cash], + 10: [40, 40, 70, 1_000, 500_000, jackpot_cash] + } + + +def get_winnings_no_pb(): + return { + 0: [0, 0, 0, 7, 100, 1_000_000], + 2: [0, 0, 0, 14, 200, 2_000_000], + 3: [0, 0, 0, 21, 300, 2_000_000], + 4: [0, 0, 0, 28, 400, 2_000_000], + 5: [0, 0, 0, 35, 500, 2_000_000], + 10: [0, 0, 0, 70, 1_000, 2_000_000] + }