From 2577ae35591d27ccce2895ac9214ce76b60d60eb Mon Sep 17 00:00:00 2001 From: Matias Bertani Date: Thu, 27 Oct 2022 16:14:24 -0300 Subject: [PATCH] Making the filters works --- development/common/match_result_text.py | 123 +++++++++++------- development/constants.py | 13 ++ .../development/new_match_details.html | 57 +++++++- development/tests/test_match_details.py | 4 + development/tests/test_match_result_text.py | 68 +++++----- development/views.py | 15 ++- static/js/filter_logs.js | 44 +++++++ templates/base.html | 2 + 8 files changed, 235 insertions(+), 91 deletions(-) create mode 100644 static/js/filter_logs.js diff --git a/development/common/match_result_text.py b/development/common/match_result_text.py index a20a07d2..bbb391c8 100644 --- a/development/common/match_result_text.py +++ b/development/common/match_result_text.py @@ -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 diff --git a/development/constants.py b/development/constants.py index 1b83915a..9e587bf6 100644 --- a/development/constants.py +++ b/development/constants.py @@ -13,3 +13,16 @@ SHOOT_ACTION = 'SHOOT' WALL_ACTION = 'WALL' ALL_ACTIONS = 'all' + + +CELL_SIZE = 5 +LARGE = 17 +ROW_SIZE = LARGE * CELL_SIZE +SCORE_1 = "score_1" +SCORE_2 = "score_2" +PLAYER_1 = "player_1" +PLAYER_2 = "player_2" +TURNS = "remaining_turns" +FROM_COL = "from_col" +FROM_ROW = "from_row" +DIRECTION = "direction" diff --git a/development/templates/development/new_match_details.html b/development/templates/development/new_match_details.html index 4600f201..c8059075 100644 --- a/development/templates/development/new_match_details.html +++ b/development/templates/development/new_match_details.html @@ -8,15 +8,62 @@

Match details

{% if data %}

Match details

{% for log in data %} - - {% endfor %} + + {% endfor %} {% endif %} {% if not data %}
There are no logs to display
{% endif %} {% if data %} +

+ +

+
+
+
+
+
+ + +
+
+ + +
+ +
+
+ +
+
+
+
+
+
+
@@ -56,7 +103,9 @@

Board

{% endif %} diff --git a/development/tests/test_match_details.py b/development/tests/test_match_details.py index 4caf9b86..582de0ce 100644 --- a/development/tests/test_match_details.py +++ b/development/tests/test_match_details.py @@ -127,8 +127,12 @@ def test_should_shows_pass_data_to_template_when_it_is_received_from_server( @patch('development.views.get_logs') @patch('development.views.generate_text') + @patch.object(FilterLogs, 'possible_states', new_callable=PropertyMock) + @patch.object(FilterLogs, 'possible_actions', new_callable=PropertyMock) def test_should_shows_pass_data_to_template_when_it_is_received_from_server_in_new_match( self, + mocked_possible_action, + mocked_possible_states, mocked_get_log, mocked_generate_text, ): diff --git a/development/tests/test_match_result_text.py b/development/tests/test_match_result_text.py index f66877b0..e44c01a8 100644 --- a/development/tests/test_match_result_text.py +++ b/development/tests/test_match_result_text.py @@ -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: guiojeda@gmail.com\\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: guiojeda@gmail.com\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) diff --git a/development/views.py b/development/views.py index 54ab5a80..771cf673 100644 --- a/development/views.py +++ b/development/views.py @@ -12,7 +12,7 @@ get_matches_of_connected_user, get_matches_results, ) -from .common.match_result_text import generate_text +from .common.match_result_text import generate_text, generate_text_str from development.forms import ChallengeForm from development.server_requests import ( get_logs, @@ -85,13 +85,12 @@ def get_context_data(self, **kwargs): filter_logs = FilterLogs(data) move_states = filter_logs.possible_states actions_kind = filter_logs.possible_actions - context['move_states'] = move_states context['actions_kind'] = actions_kind context['data'] = data context['prev_page'] = response['prev'] context['next_page'] = response['next'] - context['text'] = generate_text(data) + context['text'] = generate_text_str(data) return context @@ -111,7 +110,17 @@ def get_context_data(self, **kwargs): details_for_front = {} for index, element in enumerate(response['details']): details_for_front[str(index)] = element + + data = response['details'] + filter_logs = FilterLogs(data) + move_states = filter_logs.possible_states + actions_kind = filter_logs.possible_actions + + context['move_states'] = move_states + context['actions_kind'] = actions_kind context['data'] = response['details'] + context['text'] = generate_text(data) + return context diff --git a/static/js/filter_logs.js b/static/js/filter_logs.js new file mode 100644 index 00000000..00c8206e --- /dev/null +++ b/static/js/filter_logs.js @@ -0,0 +1,44 @@ + +const getLogsFiltered = () => { + let filteredLogs = []; + stateOfAction = selectedOption("valid_move") + kindOfAction = selectedOption("action_kind") + for (let i = 0; i < listOfLogs.length; i = i+2 ){ + let showLog = true + if (isNotAll(kindOfAction) & + listOfLogs[i].action != kindOfAction) { + showLog = false + } + else if (isNotAll(stateOfAction) & + listOfLogs[i + 1].state != stateOfAction) { + showLog = false + } + if (showLog) { + filteredLogs.push(textArray[i]) + filteredLogs.push(textArray[i+1]) + } + } + download('logs.txt', filteredLogs) +} + +const isNotAll = (filter) => { + return filter != 'all' +} + +const selectedOption = (filterId) => { + let filter = $(`#${filterId}`).get(0); + return filter.options[filter.selectedIndex].value +} + + +function download(filename, dataToDownload ) { + var element = document.createElement('a'); + element.setAttribute('href', 'data:text/plain;charset=unicode,' + encodeURIComponent(dataToDownload)); + element.setAttribute('download', filename); + element.style.display = 'none'; + document.body.appendChild(element); + element.click(); + document.body.removeChild(element); +} + + diff --git a/templates/base.html b/templates/base.html index 478ad0c8..6e83ef8c 100644 --- a/templates/base.html +++ b/templates/base.html @@ -29,6 +29,8 @@ + +