-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f630a12
commit 2577ae3
Showing
8 changed files
with
235 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,77 @@ | ||
import json | ||
|
||
CELL_SIZE = 5 | ||
LARGE = 17 | ||
ROW_SIZE = LARGE * CELL_SIZE | ||
|
||
|
||
def add_text_if_data(currentString, data, index): | ||
newStr = currentString | ||
newStr += "---------------------------- Player Command -------------------------------------\\n" | ||
newStr += "Action: " + data[index]["action"] + "\\n" | ||
innerData = data[index]["data"] | ||
innerData = json.loads(innerData) | ||
newStr += "Coordinates: " + str(innerData["from_row"]) + ", " + str(innerData["from_col"]) + "\\n" | ||
newStr += "Direction: " + innerData["direction"] + "\\n\\n" | ||
return newStr | ||
|
||
|
||
def add_text_if_not_data(currentString, myData): | ||
currentString += "--------------- Resulting Discovered Board after Player Command ------------------\\n" | ||
currentString += "Player 1 Score: " + myData["score_1"] + "\\n" | ||
currentString += "Player 2 Score: " + myData["score_2"] + "\\n" | ||
currentString += "Player 1 Name: " + myData["player_1"] + "\\n" | ||
currentString += "Player 2 Name: " + myData["player_2"] + "\\n" | ||
currentString += "Remaining turns: " + myData["remaining_turns"] + "\\n" | ||
if "from_col" in myData.keys(): | ||
currentString += ("Last command coords: " + myData["from_row"] + | ||
", " + myData["from_col"] + "\\n") | ||
if "direction" in myData.keys(): | ||
currentString += "Last command direction: " + myData["direction"] + "\\n" | ||
return currentString | ||
|
||
|
||
def add_board_if_not_data(currentString, myData): | ||
currentString += "Board as seen by not-active player:" + "\\n" + "\\n" | ||
row = '' | ||
for count, char in enumerate(myData["board"]): | ||
row += char | ||
if (count + 1) % ROW_SIZE == 0: | ||
currentString += row + "\\n" | ||
row = '' | ||
currentString += "\\n\\n" | ||
return currentString | ||
|
||
|
||
def generate_text(data): | ||
myStr = "Document start\\n\\n" | ||
for index, myData in enumerate(data): | ||
if "data" in data[index].keys(): | ||
myStr = add_text_if_data(myStr, data, index) | ||
from development.constants import ( | ||
ACTION, | ||
DIRECTION, | ||
FROM_COL, | ||
FROM_ROW, | ||
PLAYER_1, | ||
PLAYER_2, | ||
ROW_SIZE, | ||
SCORE_1, | ||
SCORE_2, | ||
TURNS | ||
) | ||
|
||
|
||
def add_text_if_data(data): | ||
eol = '\n' | ||
inner_data = json.loads(data['data']) if "data" in data else {} | ||
parsed_data = ( | ||
f"---------------------------- Player Command -------------------------------------{eol}" | ||
f"{ f'Action: {data[ACTION]}{eol}' if ACTION in data else ''}" | ||
f"{ f'Coordinates: { inner_data[FROM_ROW]}, { inner_data[FROM_COL]}{eol}' if inner_data else ''}" | ||
f"{ f'Direction: {inner_data[DIRECTION]}{eol}{eol}' if inner_data else ''}" | ||
) | ||
|
||
return parsed_data | ||
|
||
|
||
def add_text_if_not_data(log_data): | ||
|
||
eol = '\n' | ||
parsed_data = ( | ||
f"--------------- Resulting Discovered Board after Player Command ------------------{eol}" | ||
f"{ f'Player 1 Score: {log_data[SCORE_1]}{eol}' if SCORE_1 in log_data else ''}" | ||
f"{ f'Player 2 Score: {log_data[SCORE_2]}{eol}' if SCORE_2 in log_data else ''}" | ||
f"{ f'Player 1 Name: {log_data[PLAYER_1]}{eol}' if PLAYER_1 in log_data else ''}" | ||
f"{ f'Player 2 Name: {log_data[PLAYER_2]}{eol}' if PLAYER_2 in log_data else ''}" | ||
f"{ f'Remaining turns: {log_data[TURNS]}{eol}' if TURNS in log_data else ''}" | ||
f"{ f'Last command coords: {log_data[FROM_ROW]}, {log_data[FROM_COL]}{eol}' if FROM_ROW in log_data else ''}" | ||
f"{ f'Last command direction: {log_data[DIRECTION]}{eol}' if DIRECTION in log_data else ''}" | ||
) | ||
return parsed_data | ||
|
||
|
||
def add_board_if_not_data(data): | ||
parsed_data = str() | ||
if "board" in data: | ||
|
||
parsed_data += "Board as seen by not-active player:" + "\n" + "\n" | ||
row = '' | ||
for count, char in enumerate(data["board"]): | ||
row += char | ||
if (count + 1) % ROW_SIZE == 0: | ||
parsed_data += row + "\n" | ||
row = '' | ||
parsed_data += "\n\n" | ||
return parsed_data | ||
|
||
|
||
def generate_text(all_data): | ||
|
||
logs_as_string_list = list() | ||
for index, data in enumerate(all_data): | ||
if "data" in all_data[index]: | ||
logs_as_string_list.append(add_text_if_data(all_data[index])) | ||
else: | ||
myStr = add_text_if_not_data(myStr, myData) | ||
myStr = add_board_if_not_data(myStr, myData) | ||
logs_as_string_list.append( | ||
add_text_if_not_data(data) + add_board_if_not_data(data) | ||
) | ||
|
||
return logs_as_string_list | ||
|
||
|
||
def generate_text_str(data): | ||
myStr = "Document start\n\n" + ''.join(generate_text(data)) | ||
return myStr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
from django.test import TestCase | ||
|
||
from development.common.match_result_text import generate_text | ||
from development.common.match_result_text import generate_text_str | ||
|
||
|
||
class TestMatchResultText(TestCase): | ||
|
@@ -64,38 +64,38 @@ class TestMatchResultText(TestCase): | |
} | ||
|
||
sample_answer = ( | ||
'Document start\\n\\n' + | ||
'---------------------------- Player Command -------------------------------------\\n' + | ||
'Action: SHOOT\\n' + | ||
'Coordinates: 8, 0\\n' + | ||
'Direction: NORTH\\n\\n' + | ||
'--------------- Resulting Discovered Board after Player Command ------------------\\n' + | ||
'Player 1 Score: 1000\\n' + | ||
'Player 2 Score: 0\\n' + | ||
'Player 1 Name: VirtualEnv\\n' + | ||
'Player 2 Name: [email protected]\\n' + | ||
'Remaining turns: 199\\n' + | ||
'Last command coords: 8, 0\\n' + | ||
'Last command direction: NORTH\\n' + | ||
'Board as seen by not-active player:\\n\\n' + | ||
'################################################################################ R \\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'##F##################################################################################\\n' + | ||
'################################################################################ R \\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'#####################################################################################\\n' + | ||
'################################################################################ R \\n' + | ||
'\\n\\n' | ||
'Document start\n\n' + | ||
'---------------------------- Player Command -------------------------------------\n' + | ||
'Action: SHOOT\n' + | ||
'Coordinates: 8, 0\n' + | ||
'Direction: NORTH\n\n' + | ||
'--------------- Resulting Discovered Board after Player Command ------------------\n' + | ||
'Player 1 Score: 1000\n' + | ||
'Player 2 Score: 0\n' + | ||
'Player 1 Name: VirtualEnv\n' + | ||
'Player 2 Name: [email protected]\n' + | ||
'Remaining turns: 199\n' + | ||
'Last command coords: 8, 0\n' + | ||
'Last command direction: NORTH\n' + | ||
'Board as seen by not-active player:\n\n' + | ||
'################################################################################ R \n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'##F##################################################################################\n' + | ||
'################################################################################ R \n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'#####################################################################################\n' + | ||
'################################################################################ R \n' + | ||
'\n\n' | ||
) | ||
|
||
@patch('json.loads') | ||
|
@@ -111,5 +111,5 @@ def test_generate_text_should_return_corresponding_text( | |
"turn_token": "d2ab9417-6301-430b-b536-8d6b6be5e21b", | ||
} | ||
sample_data = [self.sample_log_1, self.sample_log_2] | ||
answer = generate_text(sample_data) | ||
answer = generate_text_str(sample_data) | ||
self.assertEqual(answer, self.sample_answer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.