diff --git a/rose/common/config.py b/rose/common/config.py index 613a2459..d9ec1435 100644 --- a/rose/common/config.py +++ b/rose/common/config.py @@ -12,6 +12,13 @@ number_of_cars = 4 is_track_random = True +track_file_name_read = "" +track_read_mode = False +max_line = 61 + +track_file_name_write = "" +track_write_mode = False + # Matrix matrix_height = 9 diff --git a/rose/server/main.py b/rose/server/main.py index 1f8774ba..5f4b1794 100644 --- a/rose/server/main.py +++ b/rose/server/main.py @@ -1,3 +1,4 @@ +import os.path import socket import logging import argparse @@ -23,13 +24,27 @@ def main(): default="random", choices=["random", "same"], help="Definition of driver tracks: random or same." - "If not specified, random will be used.", + "If not specified, random will be used.", + ) + parser.add_argument( + "--track_file_read", + "-r", + dest="track_file_read", + default="", + help="path of track data file.", + ) + parser.add_argument( + "--track_file_write", + "-w", + dest="track_file_write", + default="", + help="path of track data file.", ) args = parser.parse_args() """ If the argument is 'same', the track will generate the obstacles in the - same place for both drivers, otherwise, the obstacles will be genrated in + same place for both drivers, otherwise, the obstacles will be generated in random locations for each driver. """ if args.track_definition == "same": @@ -37,6 +52,46 @@ def main(): else: config.is_track_random = True + """ + If the argument is "-r ", the track will created from file with data. + Otherwise track will generated by default. + Server can read only json type file. + template of json file - + [ + ["", "", "", "", "", ""], + ["", "", "", "", "", ""], + . + . + . + ["", "", "", "", "", ""], + ["", "", "", "", "", ""] + ] + """ + if args.track_file_read != "": + config.track_file_name_read = args.track_file_read + file_exist = os.path.isfile(args.track_file_read) + + if file_exist: + config.track_read_mode = True + + """ + If the argument is "-w ", Generated track will saved in path that + user wrote. + template of json file - + [ + ["", "", "", "", "", ""], + ["", "", "", "", "", ""], + . + . + . + ["", "", "", "", "", ""], + ["", "", "", "", "", ""] + ] + """ + if args.track_file_write != "": + config.track_write_mode = True + config.track_file_name_write = args.track_file_write + log.info("starting server") g = game.Game() h = net.Hub(g) diff --git a/rose/server/track.py b/rose/server/track.py index 308283db..e851d477 100644 --- a/rose/server/track.py +++ b/rose/server/track.py @@ -1,10 +1,17 @@ import random +import ast from rose.common import config, obstacles +import os class Track(object): def __init__(self): self._matrix = None + self.file_read = None + self.line_read = None + + self.line_write = None + self.reset() # Game state interface @@ -12,7 +19,14 @@ def __init__(self): def update(self): """Go to the next game state""" self._matrix.pop() - self._matrix.insert(0, self._generate_row()) + if config.track_read_mode: + new_row = self._get_row_from_file() + else: + new_row = self._generate_row() + if config.track_write_mode: + self.save_line_to_file(new_row) + + self._matrix.insert(0, new_row) def state(self): """Return read only serialize-able state for sending to client""" @@ -42,6 +56,24 @@ def reset(self): [obstacles.NONE] * config.matrix_width for x in range(config.matrix_height) ] + # If server in reader mode, this code will reset variables of read file + if config.track_read_mode: + # if file is already opened, do not open it again + if self.file_read is None: + self.file_read = open(config.track_file_name_read).readlines() + self.line_read = 1 + + # If file already exist, this code will remove it + # to start new file for the new map. + if config.track_write_mode: + if os.path.exists(config.track_file_name_write): + # os.remove(config.track_file_name_write) + + file = open(config.track_file_name_write, "w") + file.write("[\n") + self.line_write = 0 + + # Private def _generate_row(self): @@ -52,6 +84,7 @@ def _generate_row(self): obstacles, but in different cells if 'is_track_random' is True. Otherwise, the tracks will be identical. """ + row = [obstacles.NONE] * config.matrix_width obstacle = obstacles.get_random_obstacle() if config.is_track_random: @@ -65,3 +98,47 @@ def _generate_row(self): for lane in range(config.max_players): row[cell + lane * config.cells_per_player] = obstacle return row + + def _get_row_from_file(self): + """ + Gets row from file(json) of map. + Function will get row by number of line to read(self.line_read) and increase value of + this variable by one. + If function get last line from file, value of self.line_read will set to 0. + + example: + row in file - ['', '', 'trash', 'trash', '', ''], + row returned - ['', '', 'trash', 'trash', '', ''] + """ + if self.line_read < config.max_line: + st = self.file_read[self.line_read][:-2] + else: + st = self.file_read[self.line_read] + + + + row = ast.literal_eval(st) + self.line_read += 1 + + if self.line_read > config.max_line: + self.line_read = 0 + + return row + + def save_line_to_file(self, row): + """ + Function will append current row to file(json structure). + file path is config.track_file_name_write + + example of row - ['', '', 'trash', 'trash', '', ''] + example of row in file - ["", "", "trash", "trash", "", ""], + """ + + with open(config.track_file_name_write, "a") as outfile: + self.line_write += 1 + outfile.write(str(row).replace("'", '"')) + if self.line_write == config.max_line: + outfile.write("\n]") + else: + outfile.write(",\n") +