-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcard.py
More file actions
79 lines (64 loc) · 2.19 KB
/
Copy pathcard.py
File metadata and controls
79 lines (64 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""File to house classes for card logic files to inherit off of"""
import os
import importlib.machinery
import yugidb as db
from abc import ABC
from os_paths import CARD_LOGIC_FOLDER
# The functions to load
LOAD_FUNCTIONS = [
"summon"
]
class CardLogic(metaclass=ABC):
"""
Class to manage card logic. Register to this class in order to get
various classes
"""
def summon(self):
pass
def get_logic(self, uid):
"""
Tries to load the card logic class module and get the class
through subclasses.
TODO: Classes must be directly inherited. Need to add more to recurse check.
"""
subclass_name = "{}.py".format(str(uid))
# Try to load the file into memory first and then return the value.
loader = importlib.machinery.SourceFileLoader(
CARD_LOGIC_FOLDER,
subclass_name
)
loader.load_module()
# Get the sub class value that we want
for cls in CardLogic.__subclasses__:
if cls.__name__ == subclass_name:
# Create an instance of that class
return cls()
return None
class Card:
"""
Represents a card class.
Goes through each class defined in card_logic and loads them as needed in order to
get card value.
"""
# Public values
uid = None
name = None
stats = None
# Define the number of cards available for this particular value
count = 1
def __init__(self, name_or_id):
"""Receives a card id or name to load card logic and meta-data."""
try:
# Have id, try to get name
self.uid = int(name_or_id)
self.name = db.find_card_with_id(self.uid)["name"]
except ValueError:
# Have name instead, get the id
self.name = name_or_id
self.uid = db.find_card(self.name)["id"]
# Get the stats of the cards from db and store into object
# Can be lazy evaluation or stored into one giant lookup for
# better commpression.
self.stats = db.get_card_stat(self.uid)
# Load logic that this card uses
self.logic = CardLogic.get_logic(self.uid)