forked from s464965/WMICraft
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import json
|
|
import random
|
|
import string
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
import numpy
|
|
import numpy.typing as npt
|
|
from os import listdir
|
|
from os.path import isfile, join
|
|
|
|
|
|
# Save map to file
|
|
def export_map(grid: npt.NDArray):
|
|
json_data = {"map": grid.tolist()}
|
|
|
|
now = datetime.now()
|
|
file_name = "map_" + now.strftime("%Y_%m_%d_%H_%M_%S") + ".json"
|
|
path = Path("../../resources/maps/")
|
|
file_to_open = path / file_name
|
|
|
|
with open(file_to_open, "w+") as write_file:
|
|
json.dump(json_data, write_file)
|
|
print("Saved map to file " + file_name)
|
|
|
|
|
|
def import_random_map() -> object:
|
|
path = "resources/maps"
|
|
files = [f for f in listdir(path) if isfile(join(path, f))]
|
|
random_map_name = random.choice(files)
|
|
return import_map(random_map_name)
|
|
|
|
|
|
# Read map from file
|
|
def import_map(file_name: string) -> object:
|
|
file_to_open = "resources/maps/" + file_name
|
|
with open(file_to_open, "r") as read_file:
|
|
print("Reading map from file " + file_name)
|
|
decoded_json = json.load(read_file)
|
|
|
|
decoded_grid = numpy.asarray(decoded_json["map"])
|
|
print(decoded_grid)
|
|
return decoded_grid.tolist()
|