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

Modify game event template, Implement logic on join event #107

Open
wants to merge 1 commit into
base: main
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
6 changes: 6 additions & 0 deletions game_event/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ def validate_game_event(court, ball_game, min_number_of_players, max_number_of_p
@staticmethod
def is_ball_game_playable_at_court(court, ball_game):
return CourtBallGame.is_ball_game_playable(court, ball_game)

def is_event_full(self):
from game_event_player.models import GameEventPlayer
Copy link
Collaborator

@ZvikaNaorCohen ZvikaNaorCohen Jun 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this import to the beginning of the file?
Also in def is_event_time_available(self, desired_event_time) in player/models.py.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, It will cause a circular dependency


current_players = GameEventPlayer.objects.filter(game_event=self).count()
return current_players == self.max_number_of_players
153 changes: 76 additions & 77 deletions game_event/templates/game_event/game-event.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,49 @@
{% block content %}
{% load static %}
<!DOCTYPE html>

<html>
<head>
<head>
<link href="/static/css/chat.css" rel="stylesheet" />

<title>Game Event</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/game-event.css' %}">

</head>
<body>
{% if time %}
<h1>Event Details</h1>
<p>Time: {{ time }}</p>
<p>Level: {{ level_of_game }}</p>
<p>Min Players: {{ min_number_of_players }}</p>
<p>Max Players: {{ max_number_of_players }}</p>
<p>Court: {{ court }} {{ neighborhood }}</p>
<p>Game: {{ ball_game }}</p>
<h2>Players:</h2>
<table class="nice-table">
<thead>
<tr>
<style>
.body-text {
margin-left: 70px;
}
</style>
<script>
window.addEventListener('DOMContentLoaded', (event) => {
setTimeout(() => {
var messages = document.getElementsByClassName('message-container-general');
while (messages.length > 0) {
var message = messages[0];
message.remove();
}
}, 4000);
});
</script>

</head>

<body class="body-text">
{% if time %}
<h1>Event Details</h1>
<p>Time: {{ time }}</p>
<p>Level: {{ level_of_game }}</p>
<p>Min Players: {{ min_number_of_players }}</p>
<p>Max Players: {{ max_number_of_players }}</p>
<p>Court: {{ court }} {{ neighborhood }}</p>
<p>Game: {{ ball_game }}</p>
<h2>Players:</h2>
<table class="nice-table">
<thead>
<tr>
<th> </th>
<th>Name</th>
<th>Brings Ball</th>
</tr>
</thead>
</thead>
<tbody>
{% for player in event_players %}
<tr>
Expand All @@ -46,46 +63,40 @@ <h2>Players:</h2>
{% endfor %}
</tbody>
</table>
<div>
{% if in_event %}
<a href="/game-events/remove-from-event/{{ id }}/"
class="round-button"
onclick="window.open(this.href, 'Remove Event', 'width=400,height=250,left='+((screen.width-400)/2)+',top='+((screen.height-515)/2)); return false;">
Remove Event
</a>
<div class="top-right">
<h1>Chat With Players!</h1>
<body>

{% if all_messages %}
<div id="savedText">
<div class="message-box">
{% for message in all_messages %}

{% if message.user_id.user.username == request.user.username %}
<div class="message-container">
<span class="message-time">{{ message.time_sent|date:"F d, H:i" }}</span>
<span class="name">{{ message.user_id.user.username }}</span><br>
<p><span class="message">{{ message.text }}</span></p>
</div>
<div class="button-container">
<a href="/game-events/remove-from-event/{{ id }}" class="round-button remove-event-button">
Leave Event
</a>
</div>
<div class="top-right">
<h1>Chat With Players!</h1>
<body>
{% if all_messages %}
<div id="savedText">
<div class="message-box">
{% for message in all_messages %}

{% if message.user_id.user.username == request.user.username %}
<div class="message-container">
<span class="message-time">{{ message.time_sent|date:"F d, H:i" }}</span>
<span class="name">{{ message.user_id.user.username }}</span><br>
<p><span class="message">{{ message.text }}</span></p>
</div>
{% else %}
<div class="message-other-user-container">
<span class="message-other-time">{{ message.time_sent|date:"F d, H:i" }}</span>
<span class="name-other">{{ message.user_id.user.username }}</span><br>
<p><span class="message-other">{{ message.text }}</span></p>
</div>
<div class="message-other-user-container">
<span class="message-other-time">{{ message.time_sent|date:"F d, H:i" }}</span>
<span class="name-other">{{ message.user_id.user.username }}</span><br>
<p><span class="message-other">{{ message.text }}</span></p>
</div>
{% endif %}
{% endfor %}


</div>
</div>

{% endfor %}
</div>
</div>
{% else %}
<div id="savedText" style="display: none;">
<textarea id="savedTextbox" rows="12" cols="50" readonly></textarea>
</div>
<textarea id="savedTextbox" rows="12" cols="50" readonly></textarea>
</div>
{% endif %}

<div class="input">
Expand All @@ -95,7 +106,7 @@ <h1>Chat With Players!</h1>
<button onclick="saveText(); showSaveBox();">Send</button>
</div>
</form>
<script>
<script>
function saveText() {
var textbox = document.getElementById('mytextbox');
var savedTextbox = document.getElementById('savedTextbox');
Expand All @@ -106,38 +117,26 @@ <h1>Chat With Players!</h1>
savedTextDiv.style.display = 'block';
}

</script>
{% if messages %}
<div class="row my-4">
<div class="col">
<div class="alert alert-danger" role="alert">
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endif %}

</script>
</body>


</div>
</div>
{% else %}
<a href="/game-events/join-event/{{ id }}/"
class="round-button"
onclick="window.open(this.href, 'Join Event', 'width=400,height=250,left='+((screen.width-400)/2)+',top='+((screen.height-515)/2)); return false;">
Join Event
<div class="button-container">
<a href="/game-events/join-event/{{ id }}/" class="round-button join-event-button">
Join Event
</a>
</div>
{% endif %}
</div>
{% for message in messages %}
<div class="message-container-general {% if message.tags %}{{ message.tags }}{% endif %}">
{{ message }}
</div>
{% endfor %}
{% else %}
<div>
<h1> Event Does not exist. </h1>
</div>
{% endif %}
</body>
</html>
{%endblock%}
{%endblock%}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a new line here

15 changes: 9 additions & 6 deletions game_event/templates/game_event/join-event.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
<html>
<head>
<link rel="stylesheet" type="text/css" href="{% static 'css/game-event.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'css/join-game-event.css' %}">

</head>
<body>
<body class="body">
{% if event_exists %}
<h1>Game Event</h1>
<div class="container">
<h1>Join Game Event</h1>
<form method="POST" action="{% url 'process_answer_game_event' id=id %}">
{% csrf_token %}
<p>Are you bringing a ball?</p>
<label>
<p class="other-text">Are you bringing a ball?</p>
<label class="other-text">
<input type="radio" name="answer" value="yes" class="other-text">
Yes
</label>
<label>
<label class="other-text">
<input type="radio" name="answer" value="no" class="other-text">
No
</label>
Expand All @@ -24,7 +27,7 @@ <h1>Game Event</h1>
</form>
</div>
{% else %}
<div>
<div class="container">
<h1> Can not join, Event Does Not exist! </h1>
</div>
{% endif %}
Expand Down
19 changes: 0 additions & 19 deletions game_event/templates/game_event/result.html

This file was deleted.

47 changes: 32 additions & 15 deletions game_event/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
from .views import process_game_event_form, get_weather_from_api
from datetime import timedelta
import pytest
from django.db import IntegrityError
from unittest import mock
import urllib


TEST_ID = 2
TEST_TIME = timezone.now()
TEST_LEVEL = 3
Expand Down Expand Up @@ -232,7 +230,7 @@ def test_fail_loading_site_game_event(self, client, player):
def test_success_loading_site_join_game_event(self, client, game_event, player):
client.force_login(player.user)
url = reverse('join_event', args=[game_event.id])
response = client.get(url)
response = client.get(url, follow=True)
assert response.status_code == 200

def test_fail_loading_site_join_game_event(self, client, player):
Expand All @@ -244,30 +242,30 @@ def test_fail_loading_site_join_game_event(self, client, player):
def test_success_loading_site_remove_game_event(self, client, game_event, game_event_player):
client.force_login(game_event_player.player.user)
url = reverse('remove_from_event', args=[game_event.id])
response = client.get(url)
response = client.get(url, follow=True)
assert response.status_code == 200

def test_fail_loading_site_remove_game_event_from_player(self, client, player):
client.force_login(player.user)
response = client.get('/game-events/remove-from-event/-1/')
response = client.get('/game-events/remove-from-event/-1/', follow=True)
assert response.status_code == 200
assert b"Can not remove from event, Event Does not Exist! " in response.content
assert b"Event Does not exist." in response.content

def test_fail_loading_remove_from_event_after_deleting_the_game_event(self, client, game_event,
game_event_player):
client.force_login(game_event_player.player.user)
url = reverse('remove_from_event', args=[game_event.id])
response = client.get(url)
response = client.get(url, follow=True)
assert response.status_code == 200
game_event.delete()
response = client.get(url)
response = client.get(url, follow=True)
assert response.status_code == 200
assert b"Can not remove from event, Event Does not Exist! " in response.content
assert b"Event Does not exist." in response.content

def test_remove_player_from_event(self, client, game_event, game_event_player):
client.force_login(game_event_player.player.user)
url = reverse('remove_from_event', args=[game_event.id])
response = client.get(url)
response = client.get(url, follow=True)
assert response.status_code == 200
with pytest.raises(ObjectDoesNotExist):
GameEventPlayer.objects.get(game_event=game_event, player=game_event_player.player)
Expand All @@ -281,24 +279,43 @@ def test_remove_player_that_is_not_in_event(self, client, game_event, player):
def test_add_player_to_event(self, client, game_event, player):
client.force_login(player.user)
url = reverse('process_answer_game_event', args=[game_event.id])
response = client.post(url, {'answer': 'yes'})
response = client.post(url, {'answer': 'yes'}, follow=True)
assert response.status_code == 200
assert response.context['in_event']
assert GameEventPlayer.objects.filter(game_event=game_event, player=player).exists()

def test_fail_add_player_to_event_already_in_it(self, client, game_event, player):
client.force_login(player.user)
url = reverse('process_answer_game_event', args=[game_event.id])
response = client.post(url, {'answer': 'yes'})
response = client.post(url, {'answer': 'yes'}, follow=True)
assert response.status_code == 200
url = reverse('join_event', args=[game_event.id])
response = client.get(url, follow=True)
assert response.status_code == 200
assert b"Already in event!" in response.content

def test_player_can_not_join_full_event(self, client, five_game_event_players, player, game_event):
client.force_login(player.user)
url = reverse('join_event', args=[game_event.id])
response = client.get(url, follow=True)
assert response.status_code == 200
with pytest.raises(IntegrityError):
response = client.post(url, {'answer': 'yes'})
assert b"The event is full! can not join" in response.content

def test_event_time_constraint(self, client, game_event, game_event_player, court, court_ball_game):
client.force_login(game_event_player.player.user)
event = GameEvent.create(time=game_event.time + timedelta(hours=2), level_of_game=0,
min_number_of_players=2, max_number_of_players=5, court=court,
ball_game=BallGame.Basketball)
url = reverse('join_event', args=[event.id])
response = client.get(url, follow=True)
assert response.status_code == 200
assert b"You have scheduling conflicts. Resolve them before joining the event" in response.content

def test_event_deletion_on_last_player_leave(self, client, game_event, game_event_player):
count_game_events_before = GameEvent.objects.count()
client.force_login(game_event_player.player.user)
url = reverse('remove_from_event', args=[game_event.id])
response = client.get(url)
response = client.get(url, follow=True)
assert response.status_code == 200
assert count_game_events_before - 1 == GameEvent.objects.count()
with pytest.raises(ObjectDoesNotExist):
Expand Down
Loading