progress
This commit is contained in:
parent
9ef43bb9f6
commit
0a4b8ebeb2
14 changed files with 157 additions and 28 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
50
Sledgemapper.Api/Commands/SaveNewSnapshotCommand.cs
Normal file
50
Sledgemapper.Api/Commands/SaveNewSnapshotCommand.cs
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
@ -37,6 +37,7 @@ namespace Sledgemapper.Shared.Entities
|
|||
public List<Player> Players { get; set; }
|
||||
public List<string> Colors { get; set; }
|
||||
public string SessionName { get; set; }
|
||||
public int SessionId { get; set; }
|
||||
|
||||
public void NewTile(Tile selectedTile, string tileId)
|
||||
{
|
||||
|
|
|
@ -18,6 +18,10 @@ namespace Sledgemapper
|
|||
[Post("/session/{sessionName}")]
|
||||
Task<bool> NewSession(string sessionName);
|
||||
|
||||
[Post("/session/{sessionName}/snapshot")]
|
||||
Task SaveSnapshot([Body] Session session, string sessionName);
|
||||
|
||||
|
||||
[Post("/session/{sessionName}/tile")]
|
||||
Task NewTile([Body] Tile tile, string sessionName);
|
||||
|
||||
|
|
|
@ -38,10 +38,10 @@ namespace Sledgemapper
|
|||
public Sledgemapper()
|
||||
{
|
||||
_graphics = new GraphicsDeviceManager(this);
|
||||
|
||||
_graphics.GraphicsProfile = GraphicsProfile.Reach;
|
||||
_graphics.PreferMultiSampling = false;
|
||||
//GraphicsDevice?.PresentationParameters.MultiSampleCount = 8;
|
||||
|
||||
_graphics.GraphicsProfile = GraphicsProfile.Reach;
|
||||
_graphics.PreferMultiSampling = false;
|
||||
//GraphicsDevice?.PresentationParameters.MultiSampleCount = 8;
|
||||
|
||||
Content.RootDirectory = "Content";
|
||||
_desktop = new Desktop();
|
||||
|
@ -91,9 +91,11 @@ namespace Sledgemapper
|
|||
_mainWidget.MenuConnectLogin.Selected += OnMenuConnectLoginSelected;
|
||||
_mainWidget.MenuConnectNew.Selected += OnMenuConnectNewSelected;
|
||||
_mainWidget.MenuConnectJoin.Selected += OnMenuConnectJoinSelected;
|
||||
_mainWidget.MenuConnectUpload.Selected += OnMenuConnectUploadSelected;
|
||||
_mainWidget.MenuConnectNew.Enabled = false;
|
||||
_mainWidget.MenuConnectJoin.Enabled = false;
|
||||
_mainWidget.MenuConnectSync.Enabled = false;
|
||||
_mainWidget.MenuConnectUpload.Enabled = false;
|
||||
|
||||
AddItemToToolGrid(_mainWidget.GridTiles, OnTileButtonClicked, "tiles");
|
||||
AddItemToToolGrid(_mainWidget.GridWalls, OnWallButtonClicked, "walls");
|
||||
|
@ -140,7 +142,7 @@ namespace Sledgemapper
|
|||
{
|
||||
_state._selectedTile.X = _state._hoveredTile.X;
|
||||
_state._selectedTile.Y = _state._hoveredTile.Y;
|
||||
_communicationManager.Connection?.SendAsync("UpdatePosition", _sessionData.SessionName, _state._selectedTile);
|
||||
_communicationManager.Connection?.SendAsync("UpdatePosition", _sessionData.SessionName, _sessionData.SessionId, _state._selectedTile);
|
||||
}
|
||||
|
||||
if (newState.IsKeyDown(Keys.LeftControl)
|
||||
|
@ -152,7 +154,7 @@ namespace Sledgemapper
|
|||
case InsertMode.Tile:
|
||||
_state._selectedTile.X = _state._hoveredTile.X;
|
||||
_state._selectedTile.Y = _state._hoveredTile.Y;
|
||||
_communicationManager.Connection?.SendAsync("UpdatePosition", _sessionData.SessionName, _state._selectedTile);
|
||||
_communicationManager.Connection?.SendAsync("UpdatePosition", _sessionData.SessionName, _sessionData.SessionId, _state._selectedTile);
|
||||
|
||||
_sessionData.NewTile(_state._selectedTile, _state._currentTileId);
|
||||
break;
|
||||
|
@ -258,7 +260,7 @@ namespace Sledgemapper
|
|||
|
||||
if (string.IsNullOrWhiteSpace(_sessionData.SessionName))
|
||||
{
|
||||
_spriteBatch.DrawRectangle(new Rectangle((_state._selectedTile.X * _state._tileSize)-2,(_state._selectedTile.Y * _state._tileSize)-2, _state._tileSize + 3, _state._tileSize + 3), Color.Red, 2);
|
||||
_spriteBatch.DrawRectangle(new Rectangle((_state._selectedTile.X * _state._tileSize) - 2, (_state._selectedTile.Y * _state._tileSize) - 2, _state._tileSize + 3, _state._tileSize + 3), Color.Red, 2);
|
||||
}
|
||||
|
||||
DrawPlayers();
|
||||
|
@ -432,9 +434,13 @@ namespace Sledgemapper
|
|||
_sessionData.Map = result.Map;
|
||||
_sessionData.Walls = result.Walls;
|
||||
_sessionData.Overlays = result.Overlays;
|
||||
|
||||
|
||||
_sessionData.MapEntityAdded -= OnMapEntityAdded;
|
||||
_sessionData.MapEntityDeleted -= OnMapEntityDeleted;
|
||||
_sessionData.MapEntityAdded += OnMapEntityAdded;
|
||||
_sessionData.MapEntityDeleted += OnMapEntityDeleted;
|
||||
_sessionData.SessionId = result.SessionId;
|
||||
_sessionData.SessionName = result.SessionName;
|
||||
}
|
||||
successful = result != null;
|
||||
}
|
||||
|
@ -475,19 +481,24 @@ namespace Sledgemapper
|
|||
{
|
||||
var result = await _communicationManager.Api.NewSession(localContent.TxtSession.Text);
|
||||
|
||||
// var session = await _communicationManager.Connection?.InvokeAsync<Session>("NewSession", localContent.TxtSession.Text, _authResponse.Initials);
|
||||
// var session = await _communicationManager.Connection?.InvokeAsync<Session>("NewSession", localContent.TxtSession.Text, _authResponse.Initials);
|
||||
if (result)
|
||||
{
|
||||
//_sessionData;
|
||||
|
||||
_sessionData.SessionName = localContent.TxtSession.Text;
|
||||
|
||||
_sessionData.MapEntityAdded -= OnMapEntityAdded;
|
||||
_sessionData.MapEntityDeleted -= OnMapEntityDeleted;
|
||||
_sessionData.MapEntityAdded += OnMapEntityAdded;
|
||||
// _sessionData.Players = session.Players;
|
||||
_sessionData.MapEntityDeleted += OnMapEntityDeleted;
|
||||
// _sessionData.Players = session.Players;
|
||||
|
||||
}
|
||||
successful = result;
|
||||
var result2 = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.TxtSession.Text);
|
||||
|
||||
_sessionData.SessionId = result2.SessionId;
|
||||
_sessionData.SessionName = localContent.TxtSession.Text;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -524,7 +535,7 @@ namespace Sledgemapper
|
|||
var successful = false;
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
_authResponse = await _communicationManager.Login(new AuthenticateModel
|
||||
{
|
||||
Username = localContent.TxtEmail.Text,
|
||||
|
@ -688,9 +699,19 @@ namespace Sledgemapper
|
|||
|
||||
private async void OnMenuConnectSyncSelected(object sender, EventArgs e)
|
||||
{
|
||||
// await _communicationManager.Connection?.InvokeAsync("Sync", _sessionData.SessionName, _sessionData);
|
||||
var serverMap = await _communicationManager.Api.Session(_sessionData.SessionName);
|
||||
|
||||
// await _communicationManager.Connection?.InvokeAsync("Sync", _sessionData.SessionName, _sessionData);
|
||||
var serverMap = await _communicationManager.Api.Session(_sessionData.SessionName);
|
||||
_sessionData.Overlays = serverMap.Overlays;
|
||||
_sessionData.Map = serverMap.Map;
|
||||
_sessionData.Walls = serverMap.Walls;
|
||||
}
|
||||
|
||||
private async void OnMenuConnectUploadSelected(object sender, EventArgs e)
|
||||
{
|
||||
await _communicationManager.Api.SaveSnapshot(_sessionData, _sessionData.SessionName);
|
||||
// _sessionData.Overlays = serverMap.Overlays;
|
||||
// _sessionData.Map = serverMap.Map;
|
||||
// _sessionData.Walls = serverMap.Walls;
|
||||
}
|
||||
|
||||
private void OnMenuConnectNewSelected(object sender, EventArgs e)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* Generated by MyraPad at 11/11/2020 09:47:53 */
|
||||
/* Generated by MyraPad at 17/11/2020 15:10:52 */
|
||||
using Myra.Graphics2D;
|
||||
using Myra.Graphics2D.TextureAtlases;
|
||||
using Myra.Graphics2D.UI;
|
||||
|
@ -64,6 +64,11 @@ namespace Sledgemapper.UI
|
|||
MenuConnectSync.ShortcutText = "Ctrl+Y";
|
||||
MenuConnectSync.Id = "MenuConnectSync";
|
||||
|
||||
MenuConnectUpload = new MenuItem();
|
||||
MenuConnectUpload.Text = "&Upload";
|
||||
MenuConnectUpload.ShortcutText = "Ctrl+U";
|
||||
MenuConnectUpload.Id = "MenuConnectUpload";
|
||||
|
||||
var menuItem1 = new MenuItem();
|
||||
menuItem1.Text = "&Connect";
|
||||
menuItem1.Items.Add(MenuConnectLogin);
|
||||
|
@ -71,6 +76,7 @@ namespace Sledgemapper.UI
|
|||
menuItem1.Items.Add(MenuConnectNew);
|
||||
menuItem1.Items.Add(MenuConnectJoin);
|
||||
menuItem1.Items.Add(MenuConnectSync);
|
||||
menuItem1.Items.Add(MenuConnectUpload);
|
||||
|
||||
MenuHelpAbout = new MenuItem();
|
||||
MenuHelpAbout.Text = "&About";
|
||||
|
@ -190,6 +196,7 @@ namespace Sledgemapper.UI
|
|||
public MenuItem MenuConnectNew;
|
||||
public MenuItem MenuConnectJoin;
|
||||
public MenuItem MenuConnectSync;
|
||||
public MenuItem MenuConnectUpload;
|
||||
public MenuItem MenuHelpAbout;
|
||||
public HorizontalMenu _mainMenu;
|
||||
public Grid GridTiles;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
<MenuItem Text="&New" ShortcutText="Ctrl+N" Id="MenuConnectNew" />
|
||||
<MenuItem Text="&Join" ShortcutText="Ctrl+J" Id="MenuConnectJoin" />
|
||||
<MenuItem Text="S&ync" ShortcutText="Ctrl+Y" Id="MenuConnectSync" />
|
||||
<MenuItem Text="&Upload" ShortcutText="Ctrl+U" Id="MenuConnectUpload" />
|
||||
</MenuItem>
|
||||
<MenuItem Text="&Help">
|
||||
<MenuItem Text="&About" Id="MenuHelpAbout" />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue