Skip to content

Commit

Permalink
Making the filters works
Browse files Browse the repository at this point in the history
  • Loading branch information
mbertani-eb committed Oct 28, 2022
1 parent f630a12 commit 2577ae3
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 91 deletions.
123 changes: 73 additions & 50 deletions development/common/match_result_text.py
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
13 changes: 13 additions & 0 deletions development/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
57 changes: 53 additions & 4 deletions development/templates/development/new_match_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,62 @@ <h1>Match details</h1>
{% if data %}
<h1>Match details</h1>
{% for log in data %}
<script>
addLogToArray(`{{log|safe}}`)
</script>
{% endfor %}
<script>
addLogToArray(`{{log|safe}}`)
</script>
{% endfor %}
{% endif %}
{% if not data %}
<div>There are no logs to display</div>
{% endif %}
{% if data %}
<p>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Download
</button>
</p>
<div class="collapse" id="collapseExample">
<div class="card card-body">
<div class="container">
<div class="row">
<div class="col">
<label for="valid_move">Validity</label>
<select class="form-control" name="valid_move" id="valid_move">
<optgroup label="Validity">
{% for states in move_states %}
{% if states != "gameover" %}
<option value="{{ states }}">{{ states|title }}</option>
{% endif %}
{% endfor %}
</optgroup>
</select>
</div>
<div class="col">
<label for="action_kind">Kind of action</label>
<select class="form-control" name="valid_move" id="action_kind">
<optgroup label="Kind of action">
{% for kind in actions_kind %}
<option value="{{ kind }}">{{ kind|title }}</option>
{% endfor %}
</optgroup ptgroup>
</select>
</div>

<div class="col">
<br>
<input
class="btn btn-primary"
type="submit"
value="Download as .txt"
onclick="getLogsFiltered()"
/>
</div>
</div>
</div>
</div>
</div>
<br>
<hr>
<div class="buttonContainer">
<button class="btn btn-primary" onclick="modifyIndex('-')"><<<<<</button>
<button class="btn btn-primary" onclick="modifyIndex('+')">>>>>></button>
Expand Down Expand Up @@ -56,7 +103,9 @@ <h3>Board</h3>
</div>
</section>
<script>
let textArray = {{ text|safe }};
firstView()

</script>
{% endif %}
</div>
Expand Down
4 changes: 4 additions & 0 deletions development/tests/test_match_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
Expand Down
68 changes: 34 additions & 34 deletions development/tests/test_match_result_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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')
Expand All @@ -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)
15 changes: 12 additions & 3 deletions development/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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


Expand All @@ -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


Expand Down
Loading

0 comments on commit 2577ae3

Please sign in to comment.