Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature for saving and running game maps. #488

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rose/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 57 additions & 2 deletions rose/server/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os.path
import socket
import logging
import argparse
Expand All @@ -23,20 +24,74 @@ 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":
config.is_track_random = False
else:
config.is_track_random = True

"""
If the argument is "-r <path of file(json) to read>", 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 <path of file(json) to save the track>", 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)
Expand Down
79 changes: 78 additions & 1 deletion rose/server/track.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
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

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"""
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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")