Skip to content

Commit

Permalink
Team Registration Limits (PuzzleServer#228)
Browse files Browse the repository at this point in the history
Applying limits on number of teams, number of players, and number of externals per team.

Admins have god powers and can add teams over the limit, players over the limit, and externals over the limit - but players cannot.

[We aren't validating the employee aliases but we could choose to manually audit them if desired.]
  • Loading branch information
kenny-young committed Feb 5, 2019
1 parent dd527cf commit cc7cbaa
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 109 deletions.
3 changes: 3 additions & 0 deletions ServerCore/Pages/Events/Create.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public IActionResult OnGet()
Event.LockoutIncorrectGuessPeriod = 1;
Event.LockoutDurationMultiplier = 2;
Event.MaxSubmissionCount = 50;
Event.MaxNumberOfTeams = 120;
Event.MaxExternalsPerTeam = 9;
Event.MaxTeamSize = 12;

return Page();
}
Expand Down
3 changes: 3 additions & 0 deletions ServerCore/Pages/Events/CreateDemo.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public async Task<IActionResult> OnPostAsync()
Event.LockoutIncorrectGuessPeriod = 1;
Event.LockoutDurationMultiplier = 2;
Event.MaxSubmissionCount = 50;
Event.MaxNumberOfTeams = 120;
Event.MaxExternalsPerTeam = 9;
Event.MaxTeamSize = 12;
_context.Events.Add(Event);

await _context.SaveChangesAsync();
Expand Down
25 changes: 18 additions & 7 deletions ServerCore/Pages/Teams/AddMember.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,41 @@ public async Task<IActionResult> OnGetAddMemberAsync(int teamId, int userId, int
return NotFound("Team membership change is not currently active.");
}

TeamMembers Member = new TeamMembers();

Team team = await _context.Teams.FirstOrDefaultAsync(m => m.ID == teamId);
if (team == null)
{
return NotFound("Could not find team with ID '" + teamId + "'. Check to make sure the team hasn't been removed.");
return NotFound($"Could not find team with ID '{teamId}'. Check to make sure the team hasn't been removed.");
}

var currentTeamMembers = await _context.TeamMembers.Where(members => members.Team.ID == team.ID).ToListAsync();
if (currentTeamMembers.Count >= Event.MaxTeamSize && EventRole != EventRole.admin)
{
return NotFound($"The team '{team.Name}' is full.");
}
Member.Team = team;

PuzzleUser user = await _context.PuzzleUsers.FirstOrDefaultAsync(m => m.ID == userId);
if (user == null)
{
return NotFound("Could not find user with ID '" + userId + "'. Check to make sure the user hasn't been removed.");
return NotFound($"Could not find user with ID '{userId}'. Check to make sure the user hasn't been removed.");
}

if (user.EmployeeAlias == null && currentTeamMembers.Where((m) => m.Member.EmployeeAlias == null).Count() >= Event.MaxExternalsPerTeam)
{
return NotFound($"The team '{team.Name}' is already at its maximum count of non-employee players, and '{user.Email}' has no registered alias.");
}
Member.Member = user;

if (await (from teamMember in _context.TeamMembers
where teamMember.Member == user &&
teamMember.Team.Event == Event
select teamMember).AnyAsync())
{
return NotFound("User is already on a team in this event.");
return NotFound($"'{user.Email}' is already on a team in this event.");
}

TeamMembers Member = new TeamMembers();
Member.Team = team;
Member.Member = user;

if (applicationId != -1)
{
TeamApplication application = await (from app in _context.TeamApplications
Expand Down
5 changes: 5 additions & 0 deletions ServerCore/Pages/Teams/Create.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public async Task<IActionResult> OnPostAsync()

using (IDbContextTransaction transaction = _context.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
{
if (await _context.Teams.Where((t) => t.Event == Event).CountAsync() >= Event.MaxNumberOfTeams)
{
return NotFound("Registration is full. No further teams may be created at the present time.");
}

_context.Teams.Add(Team);

if (EventRole == EventRole.play)
Expand Down
111 changes: 62 additions & 49 deletions ServerCore/Pages/Teams/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,90 @@
<h2>All Teams</h2>

<p>
@if (Model.Event.IsTeamRegistrationActive && Model.EventRole == ModelBases.EventRole.admin)
@if (Model.EventRole == ModelBases.EventRole.admin)
{
if (!Model.Event.IsTeamRegistrationActive)
{
<div class="alert alert-danger" role="alert">
This event is not currently open for registration.
</div>
}
else if (Model.Event.MaxNumberOfTeams <= Model.Teams.Count)
{
<div class="alert alert-danger" role="alert">
This event is full.
</div>
}

<a asp-page="Create">Create New</a>
}
else
{
<div class="alert alert-danger" role="alert">
This event is not currently open for registration.
</div>
}
</p>
<h4>Registered Teams: @Model.Teams.Count/@Model.Event.MaxNumberOfTeams, Registered Players: @Model.PlayerCount/@(Model.Event.MaxNumberOfTeams * Model.Event.MaxTeamSize)</h4>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Teams[0].Name)
@Html.DisplayNameFor(model => model.Teams.FirstOrDefault().Key.Name)
</th>
<th>
Size
</th>
<th>
@Html.DisplayNameFor(model => model.Teams[0].RoomID)
@Html.DisplayNameFor(model => model.Teams.FirstOrDefault().Key.RoomID)
</th>
<th>
@Html.DisplayNameFor(model => model.Teams[0].CustomRoom)
@Html.DisplayNameFor(model => model.Teams.FirstOrDefault().Key.CustomRoom)
</th>
<th>
@Html.DisplayNameFor(model => model.Teams[0].PrimaryContactEmail)
@Html.DisplayNameFor(model => model.Teams.FirstOrDefault().Key.PrimaryContactEmail)
</th>
<th>
@Html.DisplayNameFor(model => model.Teams[0].PrimaryPhoneNumber)
@Html.DisplayNameFor(model => model.Teams.FirstOrDefault().Key.PrimaryPhoneNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.Teams[0].SecondaryPhoneNumber)
@Html.DisplayNameFor(model => model.Teams.FirstOrDefault().Key.SecondaryPhoneNumber)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Teams) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.RoomID)
</td>
<td>
@Html.DisplayFor(modelItem => item.CustomRoom)
</td>
<td>
@Html.DisplayFor(modelItem => item.PrimaryContactEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.PrimaryPhoneNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.SecondaryPhoneNumber)
</td>
<td>

</td>
<td>
<a asp-page="./Status" asp-route-teamId="@item.ID">Status</a>
@if (Model.EventRole == ModelBases.EventRole.admin)
{
<div>
|
<a asp-page="./Details" asp-route-teamId="@item.ID">View</a> |
<a asp-page="./Members" asp-route-teamId="@item.ID">Members</a>
</div>
}
</td>
</tr>
}
@foreach (var item in Model.Teams)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Key.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Value)
</td>
<td>
@Html.DisplayFor(modelItem => item.Key.RoomID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Key.CustomRoom)
</td>
<td>
@Html.DisplayFor(modelItem => item.Key.PrimaryContactEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.Key.PrimaryPhoneNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Key.SecondaryPhoneNumber)
</td>
<td></td>
<td>
<a asp-page="./Status" asp-route-teamId="@item.Key.ID">Status</a>
@if (Model.EventRole == ModelBases.EventRole.admin)
{
<div>
|
<a asp-page="./Details" asp-route-teamId="@item.Key.ID">View</a> |
<a asp-page="./Members" asp-route-teamId="@item.Key.ID">Members</a>
</div>
}
</td>
</tr>
}
</tbody>
</table>
12 changes: 3 additions & 9 deletions ServerCore/Pages/Teams/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ServerCore.DataModel;
using ServerCore.ModelBases;

namespace ServerCore.Pages.Teams
{
[Authorize(Policy = "IsEventAdminOrEventAuthor")]
public class IndexModel : EventSpecificPageModel
public class IndexModel : TeamListBase
{
public IndexModel(PuzzleServerContext serverContext, UserManager<IdentityUser> userManager) : base(serverContext, userManager)
{
}

public IList<Team> Teams { get;set; }

public async Task<IActionResult> OnGetAsync()
{
Teams = await _context.Teams.Where(team => team.Event == Event).ToListAsync();
await LoadTeamDataAsync();
return Page();
}
}
Expand Down
20 changes: 14 additions & 6 deletions ServerCore/Pages/Teams/List.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,30 @@
}

<p>
@if (Model.Event.IsTeamRegistrationActive)
@if (!Model.Event.IsTeamRegistrationActive)
{
<h2>Register for @Model.Event.Name</h2>
<h2>Registration closed</h2>
<div>
You are not yet part of a team for this event. Create a new team or request to join one below!
You are not part of a team for this event, and unfortuantely we're no longer accepting new teams into the event. :(
</div>
<a asp-page="Create">Create a new team</a>
}
else
else if (Model.Event.MaxNumberOfTeams <= Model.Teams.Count)
{
<h2>Registration closed</h2>
<h2>Registration full</h2>
<div>
You are not part of a team for this event, and unfortuantely we're no longer accepting new teams into the event. :(
</div>
}
else
{
<h2>Register for @Model.Event.Name</h2>
<div>
You are not yet part of a team for this event. Create a new team or request to join one below!
</div>
<a asp-page="Create">Create a new team</a>
}
</p>
<h4>Registered Teams: @Model.Teams.Count/@Model.Event.MaxNumberOfTeams, Registered Players: @Model.PlayerCount/@(Model.Event.MaxNumberOfTeams * Model.Event.MaxTeamSize)</h4>
@if (Model.Event.IsTeamRegistrationActive)
{
<table class="table">
Expand Down
34 changes: 3 additions & 31 deletions ServerCore/Pages/Teams/List.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using ServerCore.DataModel;
using ServerCore.ModelBases;

namespace ServerCore.Pages.Teams
{
/// <summary>
/// Player-facing list of all teams
/// </summary>
[AllowAnonymous]
public class ListModel : EventSpecificPageModel
public class ListModel : TeamListBase
{
public ListModel(PuzzleServerContext serverContext, UserManager<IdentityUser> userManager) : base(serverContext, userManager)
{
}

/// <summary>
/// All teams
/// </summary>
public Dictionary<Team, int> Teams { get;set; }

/// <summary>
/// True if the current user is on a team
/// </summary>
Expand All @@ -48,24 +37,7 @@ public async Task<IActionResult> OnGetAsync()
UserOnTeam = false;
}

//Teams = await _context.Teams.ToListAsync();
List<Team> allTeams = await (from team in _context.Teams
where team.Event == Event
select team).ToListAsync();

Teams = await (from team in _context.Teams
where team.Event == Event
join teamMember in _context.TeamMembers on team equals teamMember.Team
group teamMember by teamMember.Team into teamCounts
select new { Team = teamCounts.Key, Count = teamCounts.Count() }).ToDictionaryAsync(x => x.Team, x => x.Count);

foreach (Team team in allTeams)
{
if (!Teams.ContainsKey(team))
{
Teams[team] = 0;
}
}
await LoadTeamDataAsync();

return Page();
}
Expand Down
Loading

0 comments on commit cc7cbaa

Please sign in to comment.