"""DJ database creation."""
from collections import OrderedDict
import json
import time
import zlib

from common import _dir_listing, _link, _make_dir


def database():
    """database() -> None

    Create a DJ database using information in the local database.  The database
    is implemented as a collection of JSON files.  One JSON file is created for
    each DJ.  Refer to data_structures.txt for the format and contents of these
    files.

    """
    start_time = time.time()
    pop_db_dir = _make_dir(_link("pop_database_directory"))
    dj_db_dir = _make_dir(_link("dj_database_directory"))
    dj_index_file = _link("dj_index_file")
    db_contents = _dir_listing(pop_db_dir)
    djs = set()
    charts = ["nm", "hd", "mx"]
    disc_list = {
        "nm": {},
        "hd": {},
        "mx": {}
    }
    print("Writing DJ files...")
    for json_file in db_contents:
        with open(pop_db_dir + json_file, "rb") as f:
            data = json.loads(f.read().decode())
        for chart in charts:
            djs = djs.union([(record[1], record[2]) for record in data[chart]["ranking"]])
            if data[chart]["difficulty"]:
                disc_list[chart][data["name"]] = [9999, 0]
    djs = {dj[1]: OrderedDict([("name", dj[1]), ("icon", dj[0]), ("pop", OrderedDict((chart, dict(disc_list[chart])) for chart in charts))]) for dj in djs}
    for json_file in db_contents:
        with open(pop_db_dir + json_file, "rb") as f:
            data = json.loads(f.read().decode())
        for chart in charts:
            for record in data[chart]["ranking"]:
                djs[record[2]]["pop"][chart][data["name"]] = [record[0], record[3]]
    for chart in charts:
        for dj in djs:
            djs[dj]["pop"][chart] = sorted([(k, v[0], v[1]) for k, v in djs[dj]["pop"][chart].items()])
    for k, v in djs.items():
        with open("{}{}.json".format(dj_db_dir, zlib.crc32(k.encode())), "wb") as f:
            f.write(json.dumps(v).encode())
    with open(dj_index_file, "wb") as f:
        f.write(json.dumps([{"id": zlib.crc32(dj.encode()), "name": dj} for dj in sorted(djs.keys())]).encode())
    elapsed_time = round(time.time() - start_time)
    print("Database creation took {} seconds.".format(elapsed_time))

@@ -25,44 +25,8 @@
 %22%22%22%0A
-from collections import OrderedDict%0A
 impo
@@ -1180,24 +1180,17 @@
 %7Bdj%5B1%5D: 
-OrderedD
+d
 ict(%5B(%22n
@@ -1222,18 +1222,21 @@
 %5B0%5D)
-, 
+%5D + %5B
 (%22pop%22, 
 Orde
@@ -1235,27 +1235,15 @@
 p%22, 
-OrderedDict((
+%7B
 chart
-,
+:
  dic
@@ -1261,17 +1261,16 @@
 %5Bchart%5D)
-)
  for cha
@@ -1281,17 +1281,17 @@
 n charts
-)
+%7D
 )%5D) for 
@@ -1613,45 +1613,70 @@
 for 
-chart in charts:%0A        for dj in dj
+dj in djs:%0A        master_score = 0%0A        for chart in chart
 s:%0A 
@@ -1778,16 +1778,141 @@
 ems()%5D)%0A
+            master_score += sum(%5Bdata%5B2%5D for data in djs%5Bdj%5D%5B%22pop%22%5D%5Bchart%5D%5D)%0A        djs%5Bdj%5D%5B%22pop%22%5D%5B%22master%22%5D = master_score%0A
     for 

Use regular dictionaries, and track the master score.