From ac1d5371f28fb45b67da4446720ee1f80c08226f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Mon, 20 Apr 2020 23:44:09 +0200 Subject: [PATCH] Now it is possible to finnish a tournament with scores and mails --- TrackerLibrary/DataAccess/IDataConnection.cs | 1 + TrackerLibrary/DataAccess/SqlConnector.cs | 11 +++ TrackerLibrary/DataAccess/TextConnector.cs | 14 +++ TrackerLibrary/EmailLogic.cs | 8 +- TrackerLibrary/Models/TournamentModel.cs | 7 ++ TrackerLibrary/TournamentLogic.cs | 99 +++++++++++++++++++- TrackerUI/Program.cs | 2 +- TrackerUI/TournamentViewerForm.Designer.cs | 2 +- TrackerUI/TournamentViewerForm.cs | 7 ++ 9 files changed, 144 insertions(+), 7 deletions(-) diff --git a/TrackerLibrary/DataAccess/IDataConnection.cs b/TrackerLibrary/DataAccess/IDataConnection.cs index af01698..6323266 100644 --- a/TrackerLibrary/DataAccess/IDataConnection.cs +++ b/TrackerLibrary/DataAccess/IDataConnection.cs @@ -12,6 +12,7 @@ namespace TrackerLibrary.DataAccess void CreateTeam(TeamModel model); void CreateTournament(TournamentModel model); void UpdateMatchup(MatchupModel model); + void CompleteTournament(TournamentModel model); List GetTeam_All(); List GetPerson_All(); List GetTournament_All(); diff --git a/TrackerLibrary/DataAccess/SqlConnector.cs b/TrackerLibrary/DataAccess/SqlConnector.cs index 17f4a18..63ff553 100644 --- a/TrackerLibrary/DataAccess/SqlConnector.cs +++ b/TrackerLibrary/DataAccess/SqlConnector.cs @@ -321,5 +321,16 @@ namespace TrackerLibrary.DataAccess } } } + + public void CompleteTournament(TournamentModel model) + { + // spTournaments_Complete + 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); + } + } } } diff --git a/TrackerLibrary/DataAccess/TextConnector.cs b/TrackerLibrary/DataAccess/TextConnector.cs index 2bd29a8..2dedf9c 100644 --- a/TrackerLibrary/DataAccess/TextConnector.cs +++ b/TrackerLibrary/DataAccess/TextConnector.cs @@ -126,5 +126,19 @@ namespace TrackerLibrary.DataAccess { model.UpdateMatchupToFile(); } + + public void CompleteTournament(TournamentModel model) + { + List tournaments = GlobalConfig.TournamentFile + .FullFilePath() + .LoadFile() + .ConvertToTournamentModels(); + + tournaments.Remove(model); + + tournaments.SaveToTournamentFile(); + + TournamentLogic.UpdateTournamentResults(model); + } } } diff --git a/TrackerLibrary/EmailLogic.cs b/TrackerLibrary/EmailLogic.cs index f8272b2..af1de76 100644 --- a/TrackerLibrary/EmailLogic.cs +++ b/TrackerLibrary/EmailLogic.cs @@ -8,11 +8,17 @@ namespace TrackerLibrary { public static class EmailLogic { - public static void SendEmail(List to,string subject, string body) + public static void SendEmail(string to, string subject, string body) + { + SendEmail(new List { to }, new List(), subject, body); + } + + public static void SendEmail(List to, List bcc, string subject, string body) { MailAddress fromMailAddress = new MailAddress(GlobalConfig.AppKeyLookup("senderEmail"), GlobalConfig.AppKeyLookup("senderDisplayName")); MailMessage mail = new MailMessage(); to.ForEach(x => mail.To.Add(x)); + bcc.ForEach(x => mail.Bcc.Add(x)); mail.From = fromMailAddress; mail.Subject = subject; mail.Body = body; diff --git a/TrackerLibrary/Models/TournamentModel.cs b/TrackerLibrary/Models/TournamentModel.cs index 4fdacde..bfdc276 100644 --- a/TrackerLibrary/Models/TournamentModel.cs +++ b/TrackerLibrary/Models/TournamentModel.cs @@ -6,6 +6,8 @@ namespace TrackerLibrary.Models { public class TournamentModel { + public event EventHandler OnTournamentComplete; + /// /// The unique identifier for this tournament /// @@ -30,5 +32,10 @@ namespace TrackerLibrary.Models /// The matchups per round /// public List> Rounds { get; set; } = new List>(); + + public void CompleteTournament() + { + OnTournamentComplete?.Invoke(this,DateTime.Now); + } } } diff --git a/TrackerLibrary/TournamentLogic.cs b/TrackerLibrary/TournamentLogic.cs index 2765b6e..526bf7a 100644 --- a/TrackerLibrary/TournamentLogic.cs +++ b/TrackerLibrary/TournamentLogic.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Configuration; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using TrackerLibrary.Models; @@ -77,7 +78,6 @@ namespace TrackerLibrary return; } - List to = new List(); string subject; StringBuilder body = new StringBuilder(); @@ -101,10 +101,10 @@ namespace TrackerLibrary body.AppendLine("~Tournament Tracker"); } - to.Add(p.EmailAddress); - + string to = p.EmailAddress; - EmailLogic.SendEmail( to, subject, body.ToString()); ; + + EmailLogic.SendEmail(to, subject, body.ToString()); ; } private static int CheckCurrentRound(this TournamentModel model) @@ -116,7 +116,98 @@ namespace TrackerLibrary { output += 1; } + else + { + return output; + } } + // Tournament is complete + CpmpleteTournament(model); + return output - 1; + } + + private static void CpmpleteTournament(TournamentModel model) + { + GlobalConfig.Connection.CompleteTournament(model); + TeamModel winners = model.Rounds.Last().First().Winner; + TeamModel runnerUp = model.Rounds.Last().First().Entries.Where(x => x.TeamCompeting != winners).First().TeamCompeting; + + decimal winnerPrize = 0; + decimal runnerUpPrize = 0; + + if (model.Prizes.Count > 0) + { + decimal totalIncome = model.EnteredTeams.Count * model.EntryFee; + + PrizeModel firstPlacePrize = model.Prizes.Where(x => x.PlaceNumber == 1).FirstOrDefault(); + PrizeModel secondPlacePrize = model.Prizes.Where(x => x.PlaceNumber == 2).FirstOrDefault(); + if (firstPlacePrize != null) + { + winnerPrize = firstPlacePrize.CalculatePrizePayout(totalIncome); + } + if (secondPlacePrize != null) + { + runnerUpPrize = secondPlacePrize.CalculatePrizePayout(totalIncome); + } + } + + // Send Email to all tournament + + List to = new List(); + string subject; + StringBuilder body = new StringBuilder(); + + subject = $"In {model.TournamentName}, {winners.TeamName} has won"; + + body.AppendLine("

We have a WINNER!

"); + body.AppendLine("

Congratulations to our winner on a great tournament.

"); + body.AppendLine("
"); + + if (winnerPrize > 0) + { + body.AppendLine($"

{winners.TeamName} will receive ${winnerPrize}

"); + } + + if (runnerUpPrize > 0) + { + body.AppendLine($"

{runnerUp.TeamName} will receive ${runnerUpPrize}

"); + } + + body.AppendLine("

Thanks for a great tournament everyone

"); + body.AppendLine("~Tournament Tracker"); + + + List bcc = new List(); + + foreach (TeamModel t in model.EnteredTeams) + { + foreach (PersonModel p in t.TeamMembers) + { + if (p.EmailAddress.Length > 0) + { + bcc.Add(p.EmailAddress); + } + } + } + + EmailLogic.SendEmail(new List(), 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; } diff --git a/TrackerUI/Program.cs b/TrackerUI/Program.cs index da40480..c8ed92e 100644 --- a/TrackerUI/Program.cs +++ b/TrackerUI/Program.cs @@ -19,7 +19,7 @@ namespace TrackerUI Application.SetCompatibleTextRenderingDefault(false); // Initialize the database connections - GlobalConfig.InitializeConnections(DatabaseType.TextFile); + GlobalConfig.InitializeConnections(DatabaseType.Sql); Application.Run(new TournamentDashboardForm()); } } diff --git a/TrackerUI/TournamentViewerForm.Designer.cs b/TrackerUI/TournamentViewerForm.Designer.cs index 95a9995..c16c5f8 100644 --- a/TrackerUI/TournamentViewerForm.Designer.cs +++ b/TrackerUI/TournamentViewerForm.Designer.cs @@ -188,7 +188,7 @@ this.scoreButton.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.scoreButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.scoreButton.ForeColor = System.Drawing.Color.DodgerBlue; - this.scoreButton.Location = new System.Drawing.Point(616, 286); + this.scoreButton.Location = new System.Drawing.Point(613, 287); this.scoreButton.Name = "scoreButton"; this.scoreButton.Size = new System.Drawing.Size(125, 58); this.scoreButton.TabIndex = 13; diff --git a/TrackerUI/TournamentViewerForm.cs b/TrackerUI/TournamentViewerForm.cs index 134c445..fa6da38 100644 --- a/TrackerUI/TournamentViewerForm.cs +++ b/TrackerUI/TournamentViewerForm.cs @@ -23,12 +23,19 @@ namespace TrackerUI public TournamentViewerForm(TournamentModel tournamentModel) { InitializeComponent(); + 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;