def read_rotation_files() -> Tuple[int, int, List[Card]]: runs_str = redis.get_str('decksite:rotation:summary:runs') runs_percent_str = redis.get_str('decksite:rotation:summary:runs_percent') cards = redis.get_list('decksite:rotation:summary:cards') if runs_str is not None and runs_percent_str is not None and cards is not None: return int(runs_str), int(runs_percent_str), [ Card(c, predetermined_values=True) for c in cards ] lines = [] fs = files() if len(fs) == 0: if not os.path.isdir(configuration.get_str('legality_dir')): raise DoesNotExistException( 'Invalid configuration. Could not find legality_dir.') return (0, 0, []) latest_list = open(fs[-1], 'r').read().splitlines() for filename in fs: for line in get_file_contents(filename): line = text.sanitize(line) lines.append(line.strip()) scores = Counter(lines).most_common() runs = scores[0][1] runs_percent = round(round(runs / TOTAL_RUNS, 2) * 100) cs = oracle.cards_by_name() cards = [] for name, hits in scores: c = process_score(name, hits, cs, runs, latest_list) if c is not None: cards.append(c) redis.store('decksite:rotation:summary:runs', runs, ex=604800) redis.store('decksite:rotation:summary:runs_percent', runs_percent, ex=604800) redis.store('decksite:rotation:summary:cards', cards, ex=604800) return (runs, runs_percent, cards)
def rotation_redis_store() -> Tuple[int, int, List[Card]]: lines = [] fs = files() if len(fs) == 0: if not os.path.isdir(os.path.expanduser(configuration.get_str('legality_dir'))): print('WARNING: Could not find legality_dir.') return (0, 0, []) with open(fs[-1], 'r') as f: latest_list = f.read().splitlines() for filename in fs: for line in get_file_contents(filename): line = text.sanitize(line) lines.append(line.strip()) scores = Counter(lines).most_common() runs = scores[0][1] runs_percent = round(round(runs / TOTAL_RUNS, 2) * 100) cs = oracle.cards_by_name() cards = [] card_names_by_status: Dict[str, List[str]] = {} for name, hits in scores: c = process_score(name, hits, cs, runs, latest_list) if c is not None: cards.append(c) classify_by_status(c, card_names_by_status) redis.store('decksite:rotation:summary:runs', runs, ex=604800) redis.store('decksite:rotation:summary:runs_percent', runs_percent, ex=604800) redis.store('decksite:rotation:summary:cards', cards, ex=604800) if 'Undecided' in card_names_by_status: redis.sadd('decksite:rotation:summary:undecided', *card_names_by_status['Undecided'], ex=604800) if 'Legal' in card_names_by_status: redis.sadd('decksite:rotation:summary:legal', *card_names_by_status['Legal'], ex=604800) if 'Not Legal' in card_names_by_status: redis.sadd('decksite:rotation:summary:notlegal', *card_names_by_status['Not Legal'], ex=604800) return (runs, runs_percent, cards)
def make_final_list() -> None: planes = fetcher_internal.fetch_json( 'https://api.scryfall.com/cards/search?q=t:plane%20or%20t:phenomenon' )['data'] plane_names = [p['name'] for p in planes] files = rotation.files() lines: List[str] = [] for line in fileinput.input(files): line = text.sanitize(line) if line in plane_names: print(f'DISCARDED: [{line}] is a plane.') continue lines.append(line) scores = Counter(lines).most_common() passed: List[str] = [] for name, count in scores: if count >= TOTAL_RUNS / 2: passed.append(name) final = list(passed) if is_supplemental(): temp = set(passed) final = list(temp.union([c + '\n' for c in fetcher.legal_cards()])) final.sort() h = open(os.path.join(configuration.get_str('legality_dir'), 'legal_cards.txt'), mode='w', encoding='utf-8') h.write(''.join(final)) h.close() print('Generated legal_cards.txt. {0}/{1} cards.'.format( len(passed), len(scores)))
def make_final_list() -> None: files = rotation.files() lines: List[str] = [] for line in fileinput.input(files): line = text.sanitize(line) lines.append(line) scores = Counter(lines).most_common() passed: List[str] = [] for name, count in scores: if count >= TOTAL_RUNS / 2: passed.append(name) final = list(passed) if is_supplemental(): temp = set(passed) final = list(temp.union([c + '\n' for c in fetcher.legal_cards()])) final.sort() h = open(os.path.join(configuration.get_str('legality_dir'), 'legal_cards.txt'), mode='w', encoding='utf-8') h.write(''.join(final)) h.close() print('Generated legal_cards.txt. {0}/{1} cards.'.format( len(passed), len(scores)))
def make_final_list() -> None: planes = fetch_tools.fetch_json('https://api.scryfall.com/cards/search?q=t:plane%20or%20t:phenomenon')['data'] bad_names = [p['name'] for p in planes] bad_names.extend(BANNED_CARDS) files = rotation.files() lines: List[str] = [] for line in fileinput.input(files): line = text.sanitize(line) if line.strip() in bad_names: continue lines.append(line) scores = Counter(lines).most_common() passed: List[str] = [] for name, count in scores: if count >= rotation.TOTAL_RUNS / 2: passed.append(name) final = list(passed) final.sort() h = open(os.path.join(configuration.get_str('legality_dir'), 'legal_cards.txt'), mode='w', encoding='utf-8') h.write(''.join(final)) h.close() print('Generated legal_cards.txt. {0}/{1} cards.'.format(len(passed), len(scores))) setcode = rotation.next_rotation_ex().mtgo_code h = open(os.path.join(configuration.get_str('legality_dir'), f'{setcode}_legal_cards.txt'), mode='w', encoding='utf-8') h.write(''.join(final)) h.close() do_push()
def read_rotation_files(self) -> None: lines = [] files = rotation.files() if len(files) == 0: if not os.path.isdir(configuration.get_str('legality_dir')): raise DoesNotExistException('Invalid configuration. Could not find legality_dir.') self.runs = 0 self.runs_percent = 0 return self.latest_list = open(files[-1], 'r').read().splitlines() for filename in files: for line in self.get_file_contents(filename): line = text.sanitize(line) lines.append(line.strip()) scores = Counter(lines).most_common() self.runs = scores[0][1] self.runs_percent = round(round(self.runs / 168, 2) * 100) self.cs = oracle.cards_by_name() for name, hits in scores: self.process_score(name, hits)
def read_rotation_files(self) -> None: lines = [] files = rotation.files() if len(files) == 0: files = glob.glob( os.path.join(configuration.get_str('legality_dir'), '*.jar')) if len(files) == 0: raise DoesNotExistException( 'Invalid configuration. Could not find Legality Checker') self.runs = 0 self.runs_percent = 0 return for line in fileinput.input(files): line = text.sanitize(line) lines.append(line.strip()) scores = Counter(lines).most_common() self.runs = scores[0][1] self.runs_percent = round(round(self.runs / 168, 2) * 100) self.cs = oracle.cards_by_name() for name, hits in scores: self.process_score(name, hits)
def test_sanitize() -> None: assert text.sanitize("Lim-Dûl's High Guard") == "Lim-Dûl's High Guard" assert text.sanitize("Lim-Dûl's High Guard") == "Lim-Dûl's High Guard" assert text.sanitize('Kongming, "Sleeping Dragon"' ) == 'Kongming, "Sleeping Dragon"'