This commit is contained in:
Michele Scandura 2020-11-17 15:16:14 +00:00
parent 9ef43bb9f6
commit 0a4b8ebeb2
14 changed files with 157 additions and 28 deletions

View file

@ -8,6 +8,8 @@ namespace Sledgemapper.Api.Commands
public double Timestamp { get; private set; }
public string SessionName { get; private set; }
public int UserId { get; set; }
public int SessionId {get;set;}
public BaseCommand(string sessionName, int userId)
{
@ -15,5 +17,12 @@ namespace Sledgemapper.Api.Commands
SessionName = sessionName;
UserId=userId;
}
public BaseCommand(int sessionId, int userId)
{
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
SessionId = sessionId;
UserId=userId;
}
}
}

View file

@ -0,0 +1,50 @@
using System.Transactions;
using System.Net.Mail;
using MediatR;
using Sledgemapper.Api.Data;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Handlers;
using System.Linq;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class SaveNewSnapshotCommand : BaseCommand<bool>
{
public Session Session { get; set; }
public SaveNewSnapshotCommand(string sessionName, Session session, int userId) : base(sessionName, userId)
{
Session = session;
}
}
public class SaveNewSnapshotCommandHandler : IRequestHandler<SaveNewSnapshotCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public SaveNewSnapshotCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(SaveNewSnapshotCommand notification, CancellationToken cancellationToken)
{
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
var newSnapshot = new Sledgemapper.Api.Models.Snapshot{
SessionId=session.SessionId,
Timestamp=notification.Timestamp,
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(notification.Session)
};
await _dbcontext.Snapshots.AddAsync(newSnapshot);
await _dbcontext.SaveChangesAsync();
return true;
}
}
}

View file

@ -2,12 +2,13 @@ using System.Transactions;
using System.Net.Mail;
using MediatR;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Handlers;
using System.Linq;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
@ -42,7 +43,7 @@ namespace Sledgemapper.Api.Commands
UserId = notification.UserId
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewTileNotification(notification.SessionName, notification.Tile, notification.UserId));
await _mediator.Publish(new NewTileNotification(session, notification.Tile, notification.UserId));
return true;
}

View file

@ -33,6 +33,13 @@ namespace Sledgemapper.Api.Controllers
return result;
}
[HttpPost("snapshot")]
public async Task Post(string sessionName, [FromBody] Session session)
{
var userId = int.Parse(HttpContext.User.Identity.Name);
await _mediator.Send(new SaveNewSnapshotCommand(sessionName, session, userId));
}
[HttpPost("tile")]
public async Task Post(string sessionName, [FromBody] Tile tile)
{

View file

@ -15,7 +15,8 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(NewTileNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.SessionName).NewTile(notification.Tile);
await _hub.Clients.Groups(notification.Session.SessionName).NewTile(notification.Tile);
}
}
}

View file

@ -152,7 +152,7 @@ namespace SignalRChat.Hubs
_dbContext.SessionUsers.Add(userSession);
await _dbContext.SaveChangesAsync();
var usersSession = _dbContext.SessionUsers.Where(m => m.SessionId == session.SessionId).Select(m => m.UserId).ToList();
//var usersSession = _dbContext.SessionUsers.Where(m => m.SessionId == session.SessionId).Select(m => m.UserId).ToList();
// var players = _datacontext.
// Users.
// Where(m => usersSession.Contains(m.Id)).ToList().
@ -165,17 +165,20 @@ namespace SignalRChat.Hubs
// Position = new Tile { X = 0, Y = 0 }
// }).ToList();
await _dbContext.SaveChangesAsync();
//await _dbContext.SaveChangesAsync();
await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
await Groups.AddToGroupAsync(Context.ConnectionId, session.SessionName);
var user = _datacontext.Users.First(u => u.Id == userId);
var SessionUsers = _dbContext.SessionUsers.Where(m => m.SessionId == session.SessionId).OrderBy(m => m.UserId).ToList();
var player = new Player { UserId = userId, Initials = user.Initials, Position = new Tile { X = 0, Y = 0 }, Color = Colors[SessionUsers.IndexOf(SessionUsers.FirstOrDefault(m => m.UserId == userId))] };
var u = SessionUsers.FirstOrDefault(m => m.UserId == userId);
var player = new Player { UserId = userId, Initials = user.Initials, Position = new Tile { X = 0, Y = 0 }, Color = Colors[SessionUsers.IndexOf(u)] };
await Clients.Group(sessionName).NewPlayer(player);
var newSession = new Sledgemapper.Shared.Entities.Session
{
SessionName = sessionName
SessionName = sessionName,
SessionId = session.SessionId
};
return newSession;
@ -199,11 +202,11 @@ namespace SignalRChat.Hubs
// }
}
public async Task UpdatePosition(string sessionName, Tile tile)
public async Task UpdatePosition(string sessionName,int sessionId, Tile tile)
{
var userId = int.Parse(Context.User.Identity.Name);
var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionName == sessionName);
var SessionUsers = _dbContext.SessionUsers.Where(m => m.SessionId == session.SessionId).OrderBy(m => m.UserId).ToList();
//var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionName == sessionName);
var SessionUsers = _dbContext.SessionUsers.Where(m => m.SessionId == sessionId).OrderBy(m => m.UserId).ToList();
var user = _datacontext.Users.First(u => u.Id == userId);
var player = new Player { UserId = userId, Initials = user.Initials, Position = tile, Color = Colors[SessionUsers.IndexOf(SessionUsers.FirstOrDefault(m => m.UserId == userId))] };
await Clients.Group(sessionName).PlayerUpdate(player);
@ -242,6 +245,7 @@ namespace SignalRChat.Hubs
{
_dbContext.UserConnections.Remove(userConnection);
}
var userSessions = _dbContext.SessionUsers.Where(m => m.UserId == userConnection.UserId).ToList();
{
foreach (var userSession in userSessions)

View file

@ -1,4 +1,5 @@
using MediatR;
using Sledgemapper.Shared.Entities;
using System;
namespace Sledgemapper.Api.Handlers
@ -8,6 +9,8 @@ namespace Sledgemapper.Api.Handlers
public double Timestamp { get; private set; }
public string SessionName { get; private set; }
public int UserId { get; private set; }
public int SessionId { get; set; }
public Models.Session Session { get; set; }
public BaseNotification(string sessionName, int userId)
{
@ -15,5 +18,17 @@ namespace Sledgemapper.Api.Handlers
SessionName = sessionName;
UserId = userId;
}
public BaseNotification(Models.Session session, int userId)
{
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
Session = session;;
UserId = userId;
}
public BaseNotification(int sessionId, string sessionName, int userId) : this(sessionName, userId)
{
SessionId = sessionId;
}
}
}

View file

@ -1,3 +1,6 @@
using Sledgemapper.Api.Models;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Handlers
@ -6,7 +9,12 @@ namespace Sledgemapper.Api.Handlers
{
public Tile Tile { get; private set; }
public NewTileNotification(string sessionName, Tile tile, int userId) : base(sessionName, userId)
public NewTileNotification(Models.Session session, Tile tile, int userId) : base(session, userId)
{
Tile = tile;
}
public NewTileNotification(int sessionId, string sessionName, Tile tile, int userId) : base(sessionId, sessionName, userId)
{
Tile = tile;
}

Binary file not shown.