added auto matching for teams in event

This commit is contained in:
Danhia 2022-01-15 00:44:25 +01:00
parent c360457152
commit cd5dcd9093
9 changed files with 151 additions and 27 deletions

View File

@ -0,0 +1,22 @@
# Generated by Django 3.1.5 on 2022-01-14 23:19
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('events', '0005_auto_20211227_1556'),
]
operations = [
migrations.RemoveField(
model_name='team',
name='size',
),
migrations.AddField(
model_name='team',
name='auto',
field=models.BooleanField(default=False),
),
]

View File

@ -22,9 +22,9 @@ class Team(models.Model):
name = models.CharField(max_length=200)
password = models.CharField(max_length=200)
event = models.ForeignKey(Event, on_delete=models.CASCADE, null=True)
size = models.PositiveIntegerField(default=1)
score = models.PositiveIntegerField(default=0, db_index=True)
last_submission_date = models.DateTimeField('Last Submission Date', default=timezone.now)
auto = models.BooleanField(default=False)
def __str__(self):
return self.name

View File

@ -10,17 +10,12 @@
</div>
<div class="ctf-footer">
<div class="col-sm-8 col-md-6 mx-auto">
{% if logged == True%}
{% if registered == False %}
<span class="message error-msg">{% trans "You need to be registered to the event." %}</span>
{% else %}
{% if notexist == True %}
<span class="message error-msg">{% trans "Team does not exist." %}</span>
{% elif wrongpwd == True %}
<span class="message error-msg">{% trans "Wrong password submited." %}</span>
{% elif max == True %}
<span class="message error-msg">{% trans "Maximum size reached." %}</span>
{% elif exist == True %}
{% if exist == True %}
<span class="message error-msg">{% trans "Name already taken." %}</span>
{% endif %}
<h2>Create Team</h2>
@ -32,15 +27,6 @@
<input type="submit" name="" class="form-control" value="{% trans "Create Team" %}">
</div>
</form>
<h2>Join Team</h2>
<form method="post" action="{% url 'events:join_team' event_slug=event.slug %}" class="join-team-form">
{% csrf_token %}
<div class="form-group">
<input class="form-control" type="text" name="teamname" placeholder="{% trans "Team name" %} *" maxlength="150" required="" id="id_teamname"></br>
<input class="form-control" type="password" name="password" placeholder="{% trans "Password" %} *" required="" id="id_password"></br>
<input type="submit" name="" class="form-control" value="{% trans "Join Team" %}">
</div>
</form>
{% endif %}
{% else %}
<h4>{% trans "You need to be logged to access this event." %}</h4>
@ -48,13 +34,26 @@
</div>
</div>
</div>
</div>
<div class="d-none d-md-block col-10 col-md-3 right-sidebar">
<ul class="list-group">
<li class="list-group-item">{{ event.name }}</li>
<li class="list-group-item">{% trans "Start at" %} : {{ event.start_date }}</li>
<li class="list-group-item">{% trans "End at" %} : {{ event.end_date }}</li>
<li class="list-group-item">{% trans "Starts at" %} : {{ event.start_date | date:'H:i d-m-y'}}</li>
<li class="list-group-item">{% trans "Ends at" %} : {{ event.end_date | date:'H:i d-m-y'}}</li>
</ul>
<ul class="list-group">
<a href="{% url 'events:create_team' event_slug=event.slug %}" class="list-group-item">{% trans "Create Team" %}</a>
<a href="{% url 'events:join_team' event_slug=event.slug %}" class="list-group-item">{% trans "Join Team" %}</a>
</ul>
<ul class="list-group">
<form method='GET' action="{% url 'events:find_team' event_slug=event.slug %}">
{%csrf_token%}
<li class="list-group-item">
<input class="form-control" type="submit" value="{% trans "Find me a team !" %}">
</li>
</form>
</ul>
</div>
</div>
{% endblock %}

View File

@ -108,8 +108,8 @@
<div class="d-none d-md-block col-10 col-md-3 right-sidebar">
<ul class="list-group">
<li class="list-group-item">{{ event.name }}</li>
<li class="list-group-item">{% trans "Starts at" %} : {{ event.start_date }}</li>
<li class="list-group-item">{% trans "Ends at" %} : {{ event.end_date }}</li>
<li class="list-group-item">{% trans "Starts at" %} : {{ event.start_date | date:'H:i d-m-y'}}</li>
<li class="list-group-item">{% trans "Ends at" %} : {{ event.end_date | date:'H:i d-m-y'}}</li>
{% if ended == False and IsRegistered == False %}
<form method='POST' action="{% url 'events:subscribe_event' event_slug=event.slug %}">
{%csrf_token%}

View File

@ -33,8 +33,8 @@
<div class="d-none d-md-block col-10 col-md-3 right-sidebar">
<ul class="list-group">
<li class="list-group-item">{{ event.name }}</li>
<li class="list-group-item">{% trans "Start at" %} : {{ event.start_date }}</li>
<li class="list-group-item">{% trans "End at" %} : {{ event.end_date }}</li>
<li class="list-group-item">{% trans "Starts at" %} : {{ event.start_date | date:'H:i d-m-y'}}</li>
<li class="list-group-item">{% trans "Ends at" %} : {{ event.end_date | date:'H:i d-m-y'}}</li>
</ul>
</div>

View File

@ -0,0 +1,64 @@
{% extends 'base.html' %}
{% block content %}
{% load i18n %}
<div class="row">
<div class="col-sm-12 col-md-9">
<div class="ctf-block">
<div class="ctf-head">
<h3>{{ event.name }}</h3>
<small>{% trans "This event starts at" %} : {{ event.start_date }}</small>
</div>
<div class="ctf-footer">
<div class="col-sm-8 col-md-6 mx-auto">
{% if logged == True%}
{% if registered == False %}
<span class="message error-msg">{% trans "You need to be registered to the event." %}</span>
{% else %}
{% if notexist == True %}
<span class="message error-msg">{% trans "Team does not exist." %}</span>
{% elif wrongpwd == True %}
<span class="message error-msg">{% trans "Wrong password submited." %}</span>
{% elif max == True %}
<span class="message error-msg">{% trans "Maximum size reached." %}</span>
{% elif exist == True %}
{% endif %}
<h2>Join Team</h2>
<form method="post" action="{% url 'events:join_team' event_slug=event.slug %}" class="join-team-form">
{% csrf_token %}
<div class="form-group">
<input class="form-control" type="text" name="teamname" placeholder="{% trans "Team name" %} *" maxlength="150" required="" id="id_teamname"></br>
<input class="form-control" type="password" name="password" placeholder="{% trans "Password" %} *" required="" id="id_password"></br>
<input type="submit" name="" class="form-control" value="{% trans "Join Team" %}">
</div>
</form>
{% endif %}
{% else %}
<h4>{% trans "You need to be logged to access this event." %}</h4>
{% endif %}
</div>
</div>
</div>
</div>
<div class="d-none d-md-block col-10 col-md-3 right-sidebar">
<ul class="list-group">
<li class="list-group-item">{{ event.name }}</li>
<li class="list-group-item">{% trans "Starts at" %} : {{ event.start_date | date:'H:i d-m-y' }}</li>
<li class="list-group-item">{% trans "Ends at" %} : {{ event.end_date | date:'H:i d-m-y' }}</li>
</ul>
<ul class="list-group">
<a href="{% url 'events:join_team' event_slug=event.slug %}" class="list-group-item">{% trans "Join Team" %}</a>
<a href="{% url 'events:create_team' event_slug=event.slug %}" class="list-group-item">{% trans "Create Team" %}</a>
</ul>
<ul class="list-group">
<form method='GET' action="{% url 'events:find_team' event_slug=event.slug %}">
{%csrf_token%}
<li class="list-group-item">
<input class="form-control" type="submit" value="{% trans "Find me a team !" %}">
</li>
</form>
</ul>
</div>
</div>
{% endblock %}

View File

@ -16,4 +16,5 @@ urlpatterns = [
path('<str:event_slug>/team/<str:name>', views.team_info, name='team_info'),
path('<str:event_slug>/manage_team', views.manage_team, name='manage_team'),
path('<str:event_slug>/leave_team', views.leave_team, name='leave_team'),
path('find_team/<str:event_slug>', views.find_team, name='find_team'),
]

10
src/events/utils.py Normal file
View File

@ -0,0 +1,10 @@
from random import choice, randint
colors = ['blue', 'red', 'yellow', 'green', 'black', 'white', 'purple', 'orange', 'brown', 'fuchsia', 'gold', 'pink', 'cyan',
'magenta', 'pearl']
animals = ['tiger', 'bee', 'dog', 'cat', 'otter', 'lizard', 'horse', 'mouse', 'butterfly', 'dolphin', 'elephant', 'falcon', 'goat'
'cow', 'lion', 'ostrich']
def get_random_name():
return choice(colors) + choice(animals) + str(randint(0, 100))

View File

@ -9,6 +9,8 @@ from django.contrib.auth.models import User
from accounts.models import UserProfileInfo
from django.db.models import Q
from django.utils.translation import gettext_lazy as _
from .utils import get_random_name
from random import randint
def get_description_by_lang(ctf):
lang = get_language()
@ -246,17 +248,19 @@ def join_team(request, event_slug):
except:
team = None
if team is None:
return render(request, 'events/create_team.html', {'event' : ev, 'logged': True, 'wrongpwd': True, 'registered' : True, 'notexist' : True})
return render(request, 'events/join_team.html', {'event' : ev, 'logged': True, 'wrongpwd': True, 'registered' : True, 'notexist' : True})
else:
members = EventPlayer.objects.filter(team=team)
if request.POST.get('password') != team.password:
return render(request, 'events/create_team.html', {'event' : ev, 'logged': True, 'wrongpwd': True, 'registered' : True, 'notexist' : False})
return render(request, 'events/join_team.html', {'event' : ev, 'logged': True, 'wrongpwd': True, 'registered' : True, 'notexist' : False})
if members.count() >= ev.team_size:
return render(request, 'events/create_team.html', {'event' : ev, 'logged': True, 'wrongpwd': False, 'registered' : True, 'notexist' : False, 'max' : True})
return render(request, 'events/join_team.html', {'event' : ev, 'logged': True, 'wrongpwd': False, 'registered' : True, 'notexist' : False, 'max' : True})
else:
player = EventPlayer.objects.get(user=request.user, event=ev)
player.team = team
player.save()
else:
return render(request, 'events/join_team.html', {'event' : ev, 'logged': True, 'wrongpwd': False, 'registered' : True, 'notexist' : False})
return redirect('events:event_info', event_slug=event_slug)
@login_required
@ -396,3 +400,27 @@ def leave_team(request, event_slug):
team.delete()
return render(request, 'events/create_team.html', {'event' : event_info, 'logged': True, 'wrongpwd': False, 'registered' : True, 'notexist' : False})
@login_required
def find_team(request, event_slug):
event_info = get_object_or_404(Event, slug=event_slug)
teams = Team.objects.filter(event=event_info, auto=True)
team = None
player = EventPlayer.objects.get(user=request.user, event=event_info)
for t in teams:
if EventPlayer.objects.filter(team=t, event=event_info).count() < event_info.team_size:
team = t
break
if team is None:
teamname = get_random_name()
while Team.objects.filter(name=teamname, event=event_info).exists():
teamname = get_random_name()
team = Team(name=teamname, password="".join([str(randint(0,10)) for _ in range(16)]), event=event_info, auto=True)
team.save()
player.team = team
player.save()
return redirect('events:event_info', event_slug=event_slug)