From b0040ac178d639a7540e8efe8bddb558f541ad88 Mon Sep 17 00:00:00 2001 From: chsakell Date: Fri, 30 Sep 2016 16:01:17 +0300 Subject: [PATCH] update match scores --- Controllers/HomeController.cs | 83 ++++++++++++++++++- Core/FeedGenerator.cs | 25 ++++++ .../DomainToViewModelMappingProfile.cs | 3 +- Data/LiveGameDbInitializer.cs | 6 +- Models/Match.cs | 2 +- Models/MatchTypeEnum.cs | 10 +++ Startup.cs | 11 ++- app/home/home.component.ts | 11 ++- app/shared/components/match.component.html | 10 +-- app/shared/interfaces.ts | 17 ++-- app/shared/services/feed.service.ts | 19 ++++- appsettings.json | 4 +- 12 files changed, 173 insertions(+), 28 deletions(-) create mode 100644 Core/FeedGenerator.cs create mode 100644 Models/MatchTypeEnum.cs diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index a967f86..c7fa6fc 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -1,18 +1,31 @@ using System; +using System.Collections.Generic; +using AutoMapper; using LiveGameFeed.Controllers; using LiveGameFeed.Core.MvcTimer; +using LiveGameFeed.Data.Abstract; +using LiveGameFeed.Data.Repositories; using LiveGameFeed.Hubs; +using LiveGameFeed.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR.Infrastructure; -namespace ChatLe.Controllers +namespace LiveGameFeed.Controllers { public class HomeController : ApiHubController { + IMatchRepository _matchRepository; + IFeedRepository _feedRepository; + + private Object lockOb = new Object(); public HomeController(IConnectionManager signalRConnectionManager, - ITimerService timerService) + ITimerService timerService, + IMatchRepository matchRepository, + IFeedRepository feedRepository) : base(signalRConnectionManager) { + _matchRepository = matchRepository; + _feedRepository = feedRepository; timerService.TimerElapsed += _feed_Generate; } @@ -25,8 +38,74 @@ namespace ChatLe.Controllers { TimerEventArgs eventsArgs = e as TimerEventArgs; System.Diagnostics.Debug.WriteLine("hello from home ApiHubController.cs.."); + lock (this.lockOb) + { + UpdateScores(); + } //await Clients.All.userConnected(DateTime.Now); //_coolMessageHubContext.Clients.All.newCpuValue(eventsArgs.Value); } + + private async void UpdateScores() + { + Random r = new Random(); + bool updateHost = r.Next(0, 2) == 0; + + IEnumerable matches = _matchRepository.GetAll(); + + foreach (var match in matches) + { + if (match.Type == MatchTypeEnums.Football) + { + if (updateHost) + { + match.HostScore++; + match.Feeds.Add(new Feed() + { + Description = "Goal for " + match.Host + "!", + CreatedAt = DateTime.Now, + MatchId = match.Id + }); + } + else + { + match.GuestScore++; + match.Feeds.Add(new Feed() + { + Description = "Goal for " + match.Guest + "!", + CreatedAt = DateTime.Now, + MatchId = match.Id + }); + } + } + else if (match.Type == MatchTypeEnums.Basketball) + { + if (updateHost) + { + match.HostScore = match.HostScore + 2; + match.Feeds.Add(new Feed() + { + Description = "2 points for " + match.Host + "!", + CreatedAt = DateTime.Now, + MatchId = match.Id + }); + } + else + { + match.GuestScore = match.GuestScore + 2; + match.Feeds.Add(new Feed() + { + Description = "2 points for " + match.Guest + "!", + CreatedAt = DateTime.Now, + MatchId = match.Id + }); + } + } + + _matchRepository.Commit(); + MatchViewModel _matchVM = Mapper.Map(match); + await Clients.All.updateMatch(_matchVM); + } + } } } \ No newline at end of file diff --git a/Core/FeedGenerator.cs b/Core/FeedGenerator.cs new file mode 100644 index 0000000..ee93157 --- /dev/null +++ b/Core/FeedGenerator.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using LiveGameFeed.Models; + +namespace LiveGameFeed.Core +{ + public class FeedGenarator + { + public static Feed UpdateScore(Match match) { + Feed feed = new Feed(); + feed.MatchId = match.Id; + + if(match.Type == MatchTypeEnums.Football) + { + + } + else if (match.Type == MatchTypeEnums.Basketball) + { + + } + + return feed; + } + } +} \ No newline at end of file diff --git a/Core/Mappings/DomainToViewModelMappingProfile.cs b/Core/Mappings/DomainToViewModelMappingProfile.cs index 6b87c6a..4c60c49 100644 --- a/Core/Mappings/DomainToViewModelMappingProfile.cs +++ b/Core/Mappings/DomainToViewModelMappingProfile.cs @@ -7,7 +7,8 @@ namespace LiveGameFeed.Core.Mappings { protected override void Configure() { - Mapper.CreateMap(); + Mapper.CreateMap() + .ForMember(m => m.Type, map => map.MapFrom(m => m.Type.ToString())); Mapper.CreateMap(); } } diff --git a/Data/LiveGameDbInitializer.cs b/Data/LiveGameDbInitializer.cs index e420bc0..69c2dc1 100644 --- a/Data/LiveGameDbInitializer.cs +++ b/Data/LiveGameDbInitializer.cs @@ -27,7 +27,7 @@ namespace LiveGameFeed.Data HostScore = 0, GuestScore = 0, MatchDate = DateTime.Now, - Type = "Football", + Type = MatchTypeEnums.Football, Feeds = new List { new Feed() @@ -45,7 +45,7 @@ namespace LiveGameFeed.Data HostScore = 0, GuestScore = 0, MatchDate = DateTime.Now, - Type = "Football", + Type = MatchTypeEnums.Football, Feeds = new List { new Feed() @@ -63,7 +63,7 @@ namespace LiveGameFeed.Data HostScore = 0, GuestScore = 0, MatchDate = DateTime.Now, - Type = "Basketball", + Type = MatchTypeEnums.Basketball, Feeds = new List { new Feed() diff --git a/Models/Match.cs b/Models/Match.cs index 90cfce9..2cb7079 100644 --- a/Models/Match.cs +++ b/Models/Match.cs @@ -15,7 +15,7 @@ namespace LiveGameFeed.Models public int HostScore { get; set; } public int GuestScore { get; set; } public DateTime MatchDate { get; set; } - public string Type { get; set; } + public MatchTypeEnums Type { get; set; } public ICollection Feeds { get; set; } } diff --git a/Models/MatchTypeEnum.cs b/Models/MatchTypeEnum.cs new file mode 100644 index 0000000..8aad977 --- /dev/null +++ b/Models/MatchTypeEnum.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; + +namespace LiveGameFeed.Models +{ + public enum MatchTypeEnums { + Football, + Basketball + } +} diff --git a/Startup.cs b/Startup.cs index a242e4f..58ab3d5 100644 --- a/Startup.cs +++ b/Startup.cs @@ -13,6 +13,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using LiveGameFeed.Core.Mappings; using LiveGameFeed.Core.MvcTimer; +using Newtonsoft.Json.Serialization; namespace LiveGameFeed { @@ -34,10 +35,9 @@ namespace LiveGameFeed public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseInMemoryDatabase()); - // Repositories - services.AddScoped(); - services.AddScoped(); + services.AddSingleton(); + services.AddSingleton(); // Timer service configuration services.AddSingleton(); @@ -47,7 +47,10 @@ namespace LiveGameFeed AutoMapperConfiguration.Configure(); // Add framework services. - services.AddMvc(); + services + .AddMvc() + .AddJsonOptions(options => options.SerializerSettings.ContractResolver = + new DefaultContractResolver()); services.AddSignalR(options => options.Hubs.EnableDetailedErrors = true); } diff --git a/app/home/home.component.ts b/app/home/home.component.ts index f5a2b91..e0eaa52 100644 --- a/app/home/home.component.ts +++ b/app/home/home.component.ts @@ -41,7 +41,16 @@ export class HomeComponent implements OnInit { this.dataService.getMatches() .subscribe((res: Match[]) => { self.matches = res; - console.log(self.matches); + self.feedService.updateMatch.subscribe( + match => { + for(var i=0; i< self.matches.length; i++) + { + if (self.matches[i].Id === match.Id) { + self.matches[i] = match; + } + } + } + ); }, error => { console.log(error); diff --git a/app/shared/components/match.component.html b/app/shared/components/match.component.html index 4b89ae8..a70583c 100644 --- a/app/shared/components/match.component.html +++ b/app/shared/components/match.component.html @@ -7,16 +7,16 @@ - {{match.host}} - {{match.hostScore}} + {{match.Host}} + {{match.HostScore}} - {{match.guest}} - {{match.guestScore}} + {{match.Guest}} + {{match.GuestScore}} - {{match.type}} + {{match.Type}}