-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyugidb.py
161 lines (134 loc) · 4.41 KB
/
yugidb.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
File to manage querying to database to get file metadata.
The file handles CACHES folder. Essential pulls the 4000+
card names in order to all_cards.json and loads it into memory
as a lookup table for names and id values. get_card_stat()
can load more meta data about the card.
Meta data is cached locally in 10 files for easier cache object loading
and keeps the last read meta data file cache in memory. Ejects it if a card
stat is not found.
"""
import requests
import os
import json
import card_util as cu
DEBUG_DB = False
DATA_DIR = "data/"
AC_FILE = DATA_DIR + "allcards.json"
# Functions to connect to yugioh db.
ALL_CARDS = None
# Loaded card stats in memory
CARD_STATS = {}
# Stats cache
STATS_ID_CACHE = -1
STATS_CACHE = []
# Create a directory if not applicable
def check_dir():
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
# Function to return the meta data of a card.
def get_card_stat(id):
global CARD_STATS
global STATS_CACHE
global STATS_ID_CACHE
id = int(id)
# Check first if in memory
if str(id) in CARD_STATS:
return CARD_STATS[str(id)]
# check if there is anything in the previous read cache
# if applicable
if id % 10 == STATS_ID_CACHE:
val = cu.search_card(id, STATS_CACHE)
if val != None:
CARD_STATS[val["id"]] = val
return val
number = id % 10
fname = DATA_DIR + "cardmeta" + str(number) + ".json"
data = []
# Check if in file:
if os.path.isfile(fname):
with open(fname, "r") as inf:
data = json.load(inf)
if DEBUG_DB:
print("Finding card meta data for " + str(id))
val = cu.search_card(str(id), data)
STATS_CACHE = data
STATS_ID_CACHE = id % 10
# object found
if val != None:
CARD_STATS[val["id"]] = val
return val
else:
# create file
with open(fname, "w") as inf:
inf.write("[]")
if DEBUG_DB:
print("Fetching card meta online data for: " + str(id))
# Fetch online and then write to file.
response = requests.get(
"https://db.ygoprodeck.com/api/cardinfo.php?name=" + str(id),
headers={"Content-Type": "application/json"}
)
val = json.loads(response.text)
val = val[0]
CARD_STATS[val["id"]] = val["id"]
# Write everytime.
# Flush our results when done
data = []
with open(fname, "r") as js:
data = json.load(js)
data.append(val)
with open(fname, "w") as js:
json.dump(data, js)
return val
# Function to unload all yugioh card names to id.
def unload_card_names():
ALL_CARDS = None
# Load all the card names from file.
# Note: Only function that checks data directory
# this is okay because this function must be called.
# Must call this before find_card() will work.
# Loads entire unique ID to card name DB.
def load_card_names():
global ALL_CARDS
if ALL_CARDS != None:
return
check_dir()
if not os.path.isfile(AC_FILE):
print("Fetching AC File")
response = requests.get(
"https://db.ygoprodeck.com/api/allcards.php",
headers={"Content-Type": "application/json"}
)
ALL_CARDS = json.loads(response.text)
print("Saving AC File")
with open(AC_FILE, "w") as outf:
outf.write(response.text)
else:
print("Loading AC File")
with open(AC_FILE, "r") as inf:
ALL_CARDS = json.load(inf)
# Remove the extra array
ALL_CARDS = ALL_CARDS[0]
#### QUERY FUNCTIONS ####
# Can only work if load_card_names() is first called
# Searches the DB of cardnames to id to find the card dictionary.
# See py_terminal for returned values.
#
# Implementation is mainly a linear search because a game should
# not have too many queries to do. Can be more efficient if data
# type is sorted for binary search.
def find_card(name):
"""Finds cards with name"""
name = name.lower()
for c in ALL_CARDS:
if c["name"].lower() == name:
return c
return None
def find_card_with_id(card_id):
"""Finds cards given id"""
card_id = int(card_id)
for c in ALL_CARDS:
if card_id == c["id"]:
return c
return None