Now it is possible to finnish a tournament with scores and mails

This commit is contained in:
2020-04-20 23:44:09 +02:00
parent 5d0d891024
commit ac1d5371f2
9 changed files with 144 additions and 7 deletions

View File

@ -12,6 +12,7 @@ namespace TrackerLibrary.DataAccess
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();

View File

@ -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);
}
}
}
}

View File

@ -126,5 +126,19 @@ namespace TrackerLibrary.DataAccess
{
model.UpdateMatchupToFile();
}
public void CompleteTournament(TournamentModel model)
{
List<TournamentModel> tournaments = GlobalConfig.TournamentFile
.FullFilePath()
.LoadFile()
.ConvertToTournamentModels();
tournaments.Remove(model);
tournaments.SaveToTournamentFile();
TournamentLogic.UpdateTournamentResults(model);
}
}
}

View File

@ -8,11 +8,17 @@ namespace TrackerLibrary
{
public static class EmailLogic
{
public static void SendEmail(List<string> to,string subject, string body)
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();
to.ForEach(x => mail.To.Add(x));
bcc.ForEach(x => mail.Bcc.Add(x));
mail.From = fromMailAddress;
mail.Subject = subject;
mail.Body = body;

View File

@ -6,6 +6,8 @@ namespace TrackerLibrary.Models
{
public class TournamentModel
{
public event EventHandler<DateTime> OnTournamentComplete;
/// <summary>
/// The unique identifier for this tournament
/// </summary>
@ -30,5 +32,10 @@ namespace TrackerLibrary.Models
/// The matchups per round
/// </summary>
public List<List<Models.MatchupModel>> Rounds { get; set; } = new List<List<Models.MatchupModel>>();
public void CompleteTournament()
{
OnTournamentComplete?.Invoke(this,DateTime.Now);
}
}
}

View File

@ -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<string> to = new List<string>();
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<string> to = new List<string>();
string subject;
StringBuilder body = new StringBuilder();
subject = $"In {model.TournamentName}, {winners.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>{winners.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("~Tournament Tracker");
List<string> bcc = new List<string>();
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<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;
}

View File

@ -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());
}
}

View File

@ -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;

View File

@ -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;