Skip to content
Merged
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
1 change: 1 addition & 0 deletions TrackerLibrary/DataAccess/IDataConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface IDataConnection
void CreateTeam(TeamModel model);
void CreateTournament(TournamentModel model);
void UpdateMatchup(MatchupModel model);
void CompleteTournament(TournamentModel model);
List<TeamModel> GetTeam_All();
List<PersonModel> GetPerson_All();
List<TournamentModel> GetTournament_All();
Expand Down
12 changes: 12 additions & 0 deletions TrackerLibrary/DataAccess/SqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,5 +373,17 @@ public void UpdateMatchup(MatchupModel model)
}
}
}

public void CompleteTournament(TournamentModel model)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
{
var p = new DynamicParameters();

p.Add("@id", model.Id);

connection.Execute("dbo.spTournaments_Complete", p, commandType: CommandType.StoredProcedure);
}
}
}
}
15 changes: 15 additions & 0 deletions TrackerLibrary/DataAccess/TextConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,20 @@ public void UpdateMatchup(MatchupModel model)
{
model.UpdateMatchupToFile();
}

// TODO: Create a way to archive a tournament instead of deleting it.
public void CompleteTournament(TournamentModel model)
{
List<TournamentModel> tournaments = GlobalConfig.TournamentFile.
FullFilePath().
LoadFile().
ConvertToTournamentModels();

tournaments.Remove(model);

tournaments.SaveToTournamentFile();

TournamentLogic.UpdateTournamentResults(model);
}
}
}
21 changes: 19 additions & 2 deletions TrackerLibrary/EmailLogic.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
using System.Net.Mail;
using System.Collections.Generic;
using System.Net.Mail;

namespace TrackerLibrary
{
public class EmailLogic
{
public static void SendEmail(string to, string subject, string body)
{
SendEmail(new List<string> { to }, new List<string>(), subject, body);
}

public static void SendEmail(List<string> to, List<string> bcc, string subject, string body)
{
MailAddress fromMailAddress = new MailAddress(
GlobalConfig.AppKeyLookup("senderEmail"),
GlobalConfig.AppKeyLookup("senderDisplayName"));

MailMessage mail = new MailMessage();
mail.To.Add(to);

foreach (string email in to)
{
mail.To.Add(email);
}

foreach (string email in bcc)
{
mail.Bcc.Add(email);
}
// TODO: Continue here 39:42

mail.From = fromMailAddress;
mail.Subject = subject;
mail.Body = body;
Expand Down
9 changes: 8 additions & 1 deletion TrackerLibrary/Models/TournamentModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace TrackerLibrary.Models
{
Expand All @@ -7,6 +8,7 @@ namespace TrackerLibrary.Models
/// </summary>
public class TournamentModel
{
public event EventHandler<DateTime> OnTournamentComplete;
/// <summary>
/// The unique identifier for the tournament.
/// </summary>
Expand All @@ -31,5 +33,10 @@ public class TournamentModel
/// The matchups that the differents teams have to play
/// </summary>
public List<List<MatchupModel>> Rounds { get; set; } = new List<List<MatchupModel>>();

public void CompleteTournament()
{
OnTournamentComplete?.Invoke(this, DateTime.Now);
}
}
}
95 changes: 95 additions & 0 deletions TrackerLibrary/TournamentLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,101 @@ private static int CheckCurrentRound(this TournamentModel model)
{
output++;
}
else
{
return output;
}
}

CompleteTournament(model);

return output - 1;
}

private static void CompleteTournament(TournamentModel model)
{
GlobalConfig.Connection.CompleteTournament(model);
TeamModel winner = model.Rounds.Last().First().Winner;
TeamModel runnerUp = model.Rounds.Last().First()
.Entries.First(e => e.TeamCompeting != winner)
.TeamCompeting;

decimal winnerPrize = 0;
decimal runnerUpPrize = 0;

if (model.Prizes.Count > 0)
{
decimal totalIncome = model.EnteredTeams.Count * model.EntryFee;

PrizeModel firstPlacePrize = model.Prizes.FirstOrDefault(x => x.PlaceNumber == 1);
PrizeModel secondPlacePrize = model.Prizes.FirstOrDefault(x => x.PlaceNumber == 2);

if (firstPlacePrize != null)
{
winnerPrize = firstPlacePrize.CalculatePrizePayout(totalIncome);
}

if ((secondPlacePrize != null))
{
runnerUpPrize = secondPlacePrize.CalculatePrizePayout(totalIncome);
}
}

// Send email To all tournament

string subject = string.Empty;
StringBuilder body = new StringBuilder();

subject = $"In {model.TournamentName}, {winner.TeamName} has won!";

body.AppendLine("<h1>We have a WINNER!</h1>");
body.AppendLine("<p>Congratulations to our winner on a great tournament.</p>");
body.AppendLine("<br/>");

if (winnerPrize > 0)
{
body.AppendLine($"<p>{winner.TeamName} will receive ${winnerPrize}</p>");
}

if (runnerUpPrize > 0)
{
body.AppendLine($"<p>{runnerUp.TeamName} will receive ${runnerUpPrize}</p>");
}

body.AppendLine("<p>Thanks for a great tournament everyone!</p>");
body.AppendLine("<p>~Tournament Tracker</p>");

List<string> bcc = new List<string>();

foreach (var team in model.EnteredTeams)
{
foreach (var teamMember in team.TeamMembers)
{
if (teamMember.EmailAddress.Length > 0)
{
bcc.Add(teamMember.EmailAddress);
}
}
}

EmailLogic.SendEmail(new List<string>(), bcc, subject, body.ToString());

model.CompleteTournament();
}

private static decimal CalculatePrizePayout(this PrizeModel prize, decimal totalIncome)
{
decimal output = 0;

if (prize.PrizeAmount > 0)
{
output = prize.PrizeAmount;
}
else
{
output = Decimal.Multiply(
totalIncome,
Convert.ToDecimal(prize.PrizePercentage / 100));
}

return output;
Expand Down
7 changes: 7 additions & 0 deletions TrackerUI/TournamentViewerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ public TournamentViewerForm(TournamentModel tournamentModel)

tournament = tournamentModel;

tournament.OnTournamentComplete += Tournament_OnTournamentComplete;

WireUpLists();

LoadFormData();

LoadRounds();
}

private void Tournament_OnTournamentComplete(object sender, DateTime e)
{
this.Close();
}

private void LoadFormData()
{
tournamentName.Text = tournament.TournamentName;
Expand Down
Loading
Loading