Skip to content

Commit

Permalink
Fix Bug in MatchGenerator and update Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanDeveloper committed Jun 22, 2017
1 parent 2ebde0e commit 1a67fff
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace LigaManagerAdminClient.Controllers
{
public class GenerateMatchesWindowController
{

}
}
9 changes: 9 additions & 0 deletions LigaManagerAdminClient/LigaManagerAdminClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Controllers\AddMatchWindowController.cs" />
<Compile Include="Controllers\AddSeasonWindowController.cs" />
<Compile Include="Controllers\AddTeamWindowController.cs" />
<Compile Include="Controllers\GenerateMatchesWindowController.cs" />
<Compile Include="Controllers\MenuWindowController.cs" />
<Compile Include="Framework\AdminClientHelper.cs" />
<Compile Include="Framework\RelayCommand.cs" />
Expand All @@ -90,6 +91,7 @@
<Compile Include="ViewModels\AddSeasonWindowViewModel.cs" />
<Compile Include="ViewModels\AddTeamWindowViewModel.cs" />
<Compile Include="ViewModels\BettorWindowViewModel.cs" />
<Compile Include="ViewModels\GenerateMatchesWindowViewModel.cs" />
<Compile Include="ViewModels\MatchesWindowViewModel.cs" />
<Compile Include="ViewModels\MenuWindowViewModel.cs" />
<Compile Include="ViewModels\SeasonWindowViewModel.cs" />
Expand All @@ -109,6 +111,9 @@
<Compile Include="Views\BettorWindow.xaml.cs">
<DependentUpon>BettorWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Views\GenerateMatchesWindow.xaml.cs">
<DependentUpon>GenerateMatchesWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Views\MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -238,6 +243,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\GenerateMatchesWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\MainWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using LigaManagerAdminClient.Framework;

namespace LigaManagerAdminClient.ViewModels
{
public class GenerateMatchesWindowViewModel : ViewModelBase
{

}
}
12 changes: 12 additions & 0 deletions LigaManagerAdminClient/Views/GenerateMatchesWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Window x:Class="LigaManagerAdminClient.Views.GenerateMatchesWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LigaManagerAdminClient.Views"
mc:Ignorable="d"
Title="GenerateMatchesWindow" Height="300" Width="300">
<Grid>

</Grid>
</Window>
27 changes: 27 additions & 0 deletions LigaManagerAdminClient/Views/GenerateMatchesWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LigaManagerAdminClient.Views
{
/// <summary>
/// Interaction logic for GenerateMatchesWindow.xaml
/// </summary>
public partial class GenerateMatchesWindow : Window
{
public GenerateMatchesWindow()
{
InitializeComponent();
}
}
}
4 changes: 2 additions & 2 deletions LigaManagerBettorClient/Views/DetailMatchWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<Button Content="Abbrechen" Command="{Binding CancelCommand}" HorizontalAlignment="Right" Margin="0,20,10,0" Grid.Row="5" VerticalAlignment="Top" Width="175" Height="50"/>
<Label Content="Tipp" FontSize="24" HorizontalAlignment="Left" Margin="10,25,0,0" VerticalAlignment="Top" Height="45" Width="70" Grid.Row="3"/>
<Label Content="Ergebnis" FontSize="18" HorizontalAlignment="Left" Margin="20,25,0,0" Grid.Row="2" VerticalAlignment="Top" Height="34" Width="78" Grid.RowSpan="2"/>
<TextBlock PreviewTextInput="NumberValidationTextBox" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Left" FontSize="18" Text="{Binding Match.HomeTeamScore}" Height="23" Margin="85,30,0,0" Grid.Row="2" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBlock PreviewTextInput="NumberValidationTextBox" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Left" FontSize="18" Text="{Binding Match.AwayTeamScore}" Height="23" Margin="330,29,0,0" Grid.Row="2" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBlock FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Left" FontSize="18" Text="{Binding Match.HomeTeamScore}" Height="23" Margin="85,30,0,0" Grid.Row="2" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBlock FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Left" FontSize="18" Text="{Binding Match.AwayTeamScore}" Height="23" Margin="330,29,0,0" Grid.Row="2" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="HomeTeamBet" Text="{Binding Bet.HomeTeamScore}" TextChanged="OnTextChanged" TextAlignment="Center" HorizontalAlignment="Left" Height="23" Margin="85,53,0,0" Grid.Row="4" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="AwayTeamBet" Text="{Binding Bet.AwayTeamScore}" TextChanged="OnTextChanged" TextAlignment="Center" HorizontalAlignment="Left" Height="23" Margin="330,53,0,0" Grid.Row="4" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label Content="Geändert am " HorizontalAlignment="Left" Margin="0,134,0,0" Grid.Row="4" VerticalAlignment="Top" Height="26" Width="82"/>
Expand Down
Binary file modified LigaManagerServer/Database/LigaManager.db3
Binary file not shown.
4 changes: 2 additions & 2 deletions LigaManagerServer/Mappings/BetMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public BetMap()
Map(x => x.HomeTeamScore).Not.Nullable().Default("0");
Map(x => x.AwayTeamScore).Not.Nullable().Default("0");

References(x => x.Bettor).Column("BettorId").Not.Nullable().Cascade.None();
References(x => x.Match).Column("MatchId").Not.Nullable().Cascade.None();
References(x => x.Bettor).Column("BettorId").ForeignKey("Id").Not.Nullable().Cascade.All();
References(x => x.Match).Column("MatchId").ForeignKey("Id").Not.Nullable().Cascade.All();
}
}
}
3 changes: 1 addition & 2 deletions LigaManagerServer/Mappings/TeamMap.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FluentNHibernate.Mapping;
using LigaManagerServer.Models;
using LigaManagerServer.Models;

namespace LigaManagerServer.Mappings
{
Expand Down
135 changes: 86 additions & 49 deletions LigaManagerServer/Services/AdminClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using LigaManagerServer.Contracts;
using LigaManagerServer.Interfaces;
using LigaManagerServer.Models;
using NHibernate.Event;
using NHibernate.Util;

namespace LigaManagerServer.Services
{
Expand Down Expand Up @@ -113,6 +111,7 @@ public bool AddSeason(Season season)
{
var seasons = _seasonPersistenceService.GetAll();
var max = seasons.Max(x => x.Sequence);
//increase season
season.Sequence = max + 1;
return _seasonPersistenceService.Add(season);
}
Expand Down Expand Up @@ -191,48 +190,83 @@ public void GenerateMatches(Season season, DateTime beginDateTime, DateTime endD
}
}
var result = GenerateMatches(new List<Match>(), matchesOfSeason, dateTimes, 1);
result.Sort((x,y) => x.DateTime.CompareTo(y.DateTime));
var matches = _matchPersistenceService.GetAll();
var filteredResult = result.FindAll(x =>
{
var finded = matches.FindAll(y => x.Season.Equals(y.Season) && x.HomeTeam.Equals(y.HomeTeam) &&
x.AwayTeam.Equals(y.AwayTeam));

if (finded.IsEmpty())
{
return true;
}
return false;
});
if (filteredResult.Any())
filteredResult.ForEach(x => _matchPersistenceService.Add(x));
}
}

private List<Match> GenerateMatches(List<Match> result, List<Match> matches, List<DateTime> dayOfWeeks, int matchDay)
/// <summary>
/// Generate all Matches of a Season, including first match and return match.
/// </summary>
/// <param name="result"></param>
/// <param name="matches"></param>
/// <param name="dateTimes"></param>
/// <param name="matchDay"></param>
/// <returns></returns>
private List<Match> GenerateMatches(List<Match> result, List<Match> matches, List<DateTime> dateTimes, int matchDay)
{
if (!matches.IsEmpty())
if (matches.IsEmpty()) return result;
foreach (var dateTime in dateTimes)
{
foreach (var dateTime in dayOfWeeks)
foreach (var match in matches)
{
foreach (var match in matches)
// first we fill all matches for friday and sunday
if (dateTime.DayOfWeek == DayOfWeek.Friday || dateTime.DayOfWeek == DayOfWeek.Sunday)
{
if (dateTime.DayOfWeek == DayOfWeek.Friday)
{
var currentMatch = match;
matches.Remove(match);
currentMatch.DateTime = dateTime;
currentMatch.MatchDay = matchDay;
result.Add(currentMatch);
// remove from group
dayOfWeeks.Remove(dateTime);
return GenerateMatches(result, matches, dayOfWeeks, matchDay);

}
else if (dateTime.DayOfWeek == DayOfWeek.Sunday)
{
var currentMatch = match;
matches.Remove(match);
currentMatch.DateTime = dateTime;
currentMatch.MatchDay = matchDay;
result.Add(currentMatch);
dayOfWeeks.Remove(dateTime);
return GenerateMatches(result, matches, dayOfWeeks, dateTime.Hour == 17 ? ++matchDay : matchDay);
}
// remove match from list
var currentMatch = match;
matches.Remove(match);
// set dateTime of match and matchday
currentMatch.DateTime = dateTime;
currentMatch.MatchDay = matchDay;
result.Add(currentMatch);
// remove from group
dateTimes.Remove(dateTime);
// only if the date is sunday at 17:30 we have to increase the matchday, otherwise it's not necessary
return GenerateMatches(result, matches, dateTimes, dateTime.DayOfWeek == DayOfWeek.Sunday && dateTime.Hour == 17 && dateTime.Minute == 30 ? ++matchDay : matchDay);
}
}
return result;
}
else
return SetSaturdayMatches(matches,result,dateTimes);
}
/// <summary>
/// Set all left matches to the Saturday.
/// </summary>
/// <param name="matches"></param>
/// <param name="result"></param>
/// <param name="dateTimes"></param>
/// <returns></returns>
private List<Match> SetSaturdayMatches(List<Match> matches, List<Match> result, List<DateTime> dateTimes)
{
if (matches.IsEmpty()) return result;
var matchDay = 1;
foreach (var dateTime in dateTimes)
{
return result;
if (matches.Any())
{
// remove match from list
var currentMatch = matches.First();
matches.Remove(matches.First());
// set dateTime of match and matchday
currentMatch.DateTime = dateTime;
currentMatch.MatchDay = matchDay;
result.Add(currentMatch);
}
matchDay++;
}

return SetSaturdayMatches(matches, result, dateTimes);
}

/// <summary>
Expand All @@ -244,29 +278,32 @@ private List<Match> GenerateMatches(List<Match> result, List<Match> matches, Lis
/// <returns></returns>
private List<DateTime> CreateDateTimes(DateTime beginn, DateTime end, List<DateTime> result)
{
if (!beginn.Equals(end))
if (beginn.Equals(end)) return result;
switch (beginn.DayOfWeek)
{
if (DayOfWeek.Friday == beginn.DayOfWeek)
{
case DayOfWeek.Friday:
result.Add(new DateTime(beginn.Year, beginn.Month, beginn.Day, 20,30,0));
}
else if (DayOfWeek.Sunday == beginn.DayOfWeek)
{
break;
case DayOfWeek.Sunday:
result.Add(new DateTime(beginn.Year, beginn.Month, beginn.Day, 15, 30, 0));
result.Add(new DateTime(beginn.Year, beginn.Month, beginn.Day, 17, 30, 0));
}
else if (DayOfWeek.Saturday == beginn.DayOfWeek)
{
break;
case DayOfWeek.Saturday:
result.Add(new DateTime(beginn.Year, beginn.Month, beginn.Day, 15, 30, 0));
}

return CreateDateTimes(beginn.AddDays(1), end, result);
}
else
{
return result;
break;
case DayOfWeek.Monday:
break;
case DayOfWeek.Tuesday:
break;
case DayOfWeek.Wednesday:
break;
case DayOfWeek.Thursday:
break;
default:
throw new ArgumentOutOfRangeException();
}

return CreateDateTimes(beginn.AddDays(1), end, result);
}
}
}
2 changes: 1 addition & 1 deletion LigaManagerTest/AdminClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class AdminClientTest
public void GenerateMatchesTest()
{
var adminClient = new AdminClientServiceClient();
adminClient.GenerateMatches(adminClient.GetSeasons().First(), DateTime.Now, DateTime.Now.AddDays(60));
adminClient.GenerateMatches(adminClient.GetSeasons().First(), DateTime.Now, DateTime.Now.AddDays(180));
}
}
}
26 changes: 25 additions & 1 deletion LigaManagerTest/BetTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using LigaManagerServer.Interfaces;
using System;
using System.Linq;
using LigaManagerServer.Interfaces;
using LigaManagerServer.Models;
using LigaManagerServer.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -9,11 +11,33 @@ namespace LigaManagerTest
public class BetTest
{
private readonly IPersistenceService<Bet> _betService = new PersistenceService<Bet>();
private readonly IPersistenceService<Match> _matchService = new PersistenceService<Match>();
[TestMethod]
public void GetBetsTest()
{
var bets = _betService.GetAll();
if (bets == null) Assert.Fail();
}

[TestMethod]
public void AddBetsTest()
{
var bets = _betService.GetAll();
var match = _matchService.Get(299);
var bet = bets.First();
var newBet = new Bet
{
Match = match,
DateTime = DateTime.Now,
Bettor = bet.Bettor,
AwayTeamScore = 2,
HomeTeamScore = 3
};
/*var findAll = bets.Find(x => x.Match.Equals(match) && x.Bettor.Equals(bet.Bettor));
_betService.Delete(findAll);
bet.DateTime = DateTime.Now;*/
var isAdded = _betService.Add(newBet);
Assert.IsTrue(isAdded);
}
}
}
Binary file added LigaManagerTest/Database/LigaManager.db3
Binary file not shown.
27 changes: 26 additions & 1 deletion LigaManagerTest/MatchTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using LigaManagerServer.Interfaces;
using System;
using System.Linq;
using LigaManagerServer.Interfaces;
using LigaManagerServer.Models;
using LigaManagerServer.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -15,5 +17,28 @@ public void GetMatchesTest()
var matches = _matchService.GetAll();
if (matches == null) Assert.Fail();
}

[TestMethod]
public void DeleteMatchesTest()
{
var matches = _matchService.GetAll();
var isDeleted = _matchService.Delete(matches.First());
matches = _matchService.GetAll();
Assert.IsTrue(isDeleted);
}

[TestMethod]
public void AddMatchesTest()
{
var matches = _matchService.GetAll();
var match = matches.First();
_matchService.Delete(match);
match.Id = 0;
match.DateTime = DateTime.Now.AddHours(30);
match.Season.Sequence = 100;
var isDeleted = _matchService.Add(match);
matches = _matchService.GetAll();
Assert.IsTrue(isDeleted);
}
}
}
Loading

0 comments on commit 1a67fff

Please sign in to comment.