Skip to content

Commit

Permalink
Add logic to create or join event
Browse files Browse the repository at this point in the history
  • Loading branch information
amitkaplansky committed May 31, 2023
1 parent 8b1c394 commit 56a4b60
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 12 deletions.
7 changes: 7 additions & 0 deletions game_event/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ 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

current_players = GameEventPlayer.objects.filter(game_event=self).count()
return current_players == self.max_number_of_players
58 changes: 52 additions & 6 deletions game_event/templates/game_event/result.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,62 @@
<html>
<head>
<link rel="stylesheet" type="text/css" href="{% static 'css/game-event.css' %}">
<style>
body {
display: flex;
align-items: center;
justify-content: flex-start;
height: 90vh;

}
.error-message {
background-color:#ece2db;
color: #FF5252;
padding: 15px;
margin-bottom: 15px;
font-size: 20px;
font-weight: bold;
font-family: "Haas Grot Text R Web", "Helvetica Neue", Helvetica, Arial, sans-serif;
text-align: left;

width: fit-content;
border: 5px solid #a7898e;
border-radius: 5px;
}
.col-md-6 {
max-width: 340px;
margin-left: 0;
margin-right: auto;
}
</style>
</head>
<body>
{% if event_exists == False %}
<h1>Can not remove from event, Event Does not Exist! </h1>
{% elif reached_max == True %}
<h1>Max number of Player reached! You can't join </h1>
{% elif in_event %}
<h1>You joined the event!</h1>
<div>
<h1>Can not remove from event, Event Does not Exist!</h1>
</div>
{% else %}
<h1>You are not in the event!</h1>
<div>
{% if messages %}
<div class="row my-4">
<div class="col-md-6 mx-auto">
<div class="alert alert-danger" role="alert">
<ul class="messages col-12">
{% for message in messages %}
<li{% if message.tags %} class="error-message {{ message.tags }}"{% else %} class="error-message"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% else %}
{% if in_event %}
<h1>You joined the event!</h1>
{% else %}
<h1>You are not in the event!</h1>
{% endif %}
{% endif %}
</div>
{% endif %}
</body>
</html>
26 changes: 20 additions & 6 deletions game_event/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def join_event(request, id):
GameEvent.objects.get(pk=id)
except GameEvent.DoesNotExist:
return render(request, 'game_event/join-event.html', {'id': id, 'event_exists': False})

return render(request, 'game_event/join-event.html', {'id': id, 'event_exists': True})


Expand All @@ -139,14 +139,28 @@ def process_answer_game_event(request, id):
player = Player.objects.get(user=request.user)
ball_responsible = request.POST.get('answer') == 'yes'
content = {}
num_of_players_in_event = len(GameEventPlayer.objects.filter(game_event=event))
if num_of_players_in_event < event.max_number_of_players:

is_event_full = event.is_event_full()
is_event_time_available = player.is_event_time_available(event.time)

error_messages = []
if is_event_full:
error_messages.append("The event is full! cannot join event")
if not is_event_time_available:
error_messages.append("You have scheduling conflicts. Resolve them before joining the event")

if error_messages:
for error in error_messages:
messages.error(request, error)
else:
GameEventPlayer.objects.create(game_event=event, player=player, ball_responsible=ball_responsible).save()
in_event = GameEventPlayer.objects.filter(game_event=event, player=player).exists()
content["in_event"] = in_event
content["reached_max"] = False
else:
content["reached_max"] = True

content['event_exists'] = True

return render(request, 'game_event/result.html', content)




10 changes: 10 additions & 0 deletions player/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.auth.models import User
from django.utils import timezone
from django.db import models
import datetime
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -54,3 +55,12 @@ def validate_and_save(self, birth_date, favorite_ball_game):

else:
self.save()

def is_event_time_available(self, desired_event_time):
from game_event_player.models import GameEventPlayer
TIME_LIMIT = 4
start_time_limit = desired_event_time - timezone.timedelta(hours=TIME_LIMIT)
end_time_limit = desired_event_time + timezone.timedelta(hours=TIME_LIMIT)
conflicting_events = GameEventPlayer.objects.filter(player=self, game_event__time__range=(start_time_limit, end_time_limit))
return not conflicting_events.exists()

0 comments on commit 56a4b60

Please sign in to comment.