update match scores

This commit is contained in:
chsakell
2016-09-30 16:01:17 +03:00
parent 8e6c0efe48
commit b0040ac178
12 changed files with 173 additions and 28 deletions

View File

@@ -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<Broadcaster>
{
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<Match> 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, MatchViewModel>(match);
await Clients.All.updateMatch(_matchVM);
}
}
}
}

25
Core/FeedGenerator.cs Normal file
View File

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

View File

@@ -7,7 +7,8 @@ namespace LiveGameFeed.Core.Mappings
{
protected override void Configure()
{
Mapper.CreateMap<Match, MatchViewModel>();
Mapper.CreateMap<Match, MatchViewModel>()
.ForMember(m => m.Type, map => map.MapFrom(m => m.Type.ToString()));
Mapper.CreateMap<Feed, FeedViewModel>();
}
}

View File

@@ -27,7 +27,7 @@ namespace LiveGameFeed.Data
HostScore = 0,
GuestScore = 0,
MatchDate = DateTime.Now,
Type = "Football",
Type = MatchTypeEnums.Football,
Feeds = new List<Feed>
{
new Feed()
@@ -45,7 +45,7 @@ namespace LiveGameFeed.Data
HostScore = 0,
GuestScore = 0,
MatchDate = DateTime.Now,
Type = "Football",
Type = MatchTypeEnums.Football,
Feeds = new List<Feed>
{
new Feed()
@@ -63,7 +63,7 @@ namespace LiveGameFeed.Data
HostScore = 0,
GuestScore = 0,
MatchDate = DateTime.Now,
Type = "Basketball",
Type = MatchTypeEnums.Basketball,
Feeds = new List<Feed>
{
new Feed()

View File

@@ -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<Feed> Feeds { get; set; }
}

10
Models/MatchTypeEnum.cs Normal file
View File

@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
namespace LiveGameFeed.Models
{
public enum MatchTypeEnums {
Football,
Basketball
}
}

View File

@@ -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<LiveGameContext>(options => options.UseInMemoryDatabase());
// Repositories
services.AddScoped<IMatchRepository, MatchRepository>();
services.AddScoped<IFeedRepository, FeedRepository>();
services.AddSingleton<IMatchRepository, MatchRepository>();
services.AddSingleton<IFeedRepository, FeedRepository>();
// Timer service configuration
services.AddSingleton<ITimerService, TimerService>();
@@ -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);
}

View File

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

View File

@@ -7,16 +7,16 @@
</thead>
<tbody>
<tr>
<td><span class="teamName">{{match.host}}</span></td>
<td><span class="teamScore"> {{match.hostScore}} </span></td>
<td><span class="teamName">{{match.Host}}</span></td>
<td><span class="teamScore"> {{match.HostScore}} </span></td>
</tr>
<tr>
<td><span class="teamName"> {{match.guest}} </span></td>
<td><span class="teamScore">{{match.guestScore}}</span></td>
<td><span class="teamName"> {{match.Guest}} </span></td>
<td><span class="teamScore">{{match.GuestScore}}</span></td>
</tr>
<tr>
<td>
<span class="label label-success">{{match.type}}</span>
<span class="label label-success">{{match.Type}}</span>
</td>
<td>
<button type="button" class="btn btn-default btn-xs">

View File

@@ -10,6 +10,8 @@ export interface FeedProxy {
export interface FeedClient {
userConnected: (user: any) => void;
userDisconnected: (id: string) => void;
updateMatch: (match: Match) => void;
messageReceived: (message: string) => void;
}
@@ -21,12 +23,11 @@ export enum ConnectionState {
/* LiveGameFeed related interfaces */
export interface Match {
id: number;
host: string;
guest: string;
hostScore: number;
guestScore: number;
matchDate: Date;
type: string;
feeds: any
Id: number;
Host: string;
Guest: string;
HostScore: number;
GuestScore: number;
MatchDate: Date;
Type: string;
}

View File

@@ -5,7 +5,7 @@ import 'rxjs/add/operator/toPromise';
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";
import { FeedSignalR, FeedProxy, FeedClient, ConnectionState } from '../interfaces';
import { FeedSignalR, FeedProxy, FeedClient, ConnectionState, Match } from '../interfaces';
@Injectable()
export class FeedService {
@@ -13,15 +13,21 @@ export class FeedService {
currentState = ConnectionState.Disconnected;
connectionState: Observable<ConnectionState>;
userConnected: Observable<any>;
updateMatch: Observable<Match>;
messageReceived: Observable<string>;
private connectionStateSubject = new Subject<ConnectionState>();
private userConnectedSubject = new Subject<any>();
private updateMatchSubject = new Subject<Match>();
private messageReceivedSubject = new Subject<string>();
constructor(private http: Http) {
this.connectionState = this.connectionStateSubject.asObservable();
this.userConnected = this.userConnectedSubject.asObservable();
this.updateMatch = this.updateMatchSubject.asObservable();
this.messageReceived = this.messageReceivedSubject.asObservable();
}
@@ -38,6 +44,13 @@ export class FeedService {
*/
feedHub.client.userConnected = user => this.onUserConnected(user);
/**
* @desc callback when a message is received
* @param String to, the conversation id
* @param Message data, the message
*/
feedHub.client.updateMatch = match => this.onUpdateMatch(match);
/**
* @desc callback when a message is received
* @param String to, the conversation id
@@ -83,6 +96,10 @@ export class FeedService {
this.userConnectedSubject.next(user);
}
private onUpdateMatch(match: Match) {
this.updateMatchSubject.next(match);
}
private onMessageReceived(message: string) {
console.log(message);
this.messageReceivedSubject.next(message);

View File

@@ -8,7 +8,7 @@
}
},
"TimeService": {
"DueTime": 3000,
"Period": 1500
"DueTime": 6000,
"Period": 5000
}
}