This commit is contained in:
Michele Scandura 2020-11-17 22:10:52 +00:00
parent 0a4b8ebeb2
commit e988f5c310
13 changed files with 135 additions and 72 deletions

View file

@ -34,7 +34,7 @@ namespace Sledgemapper.Api.Commands
double timestamp; double timestamp;
Sledgemapper.Shared.Entities.Session mapSession; Sledgemapper.Shared.Entities.Session mapSession;
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName); var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
snapshot = _dbcontext.Snapshots.FirstOrDefault(m => m.SessionId == session.SessionId); snapshot = _dbcontext.Snapshots.OrderByDescending(s => s.Timestamp).FirstOrDefault(m => m.SessionId == session.SessionId);
if (snapshot is null) if (snapshot is null)
{ {
@ -89,14 +89,19 @@ namespace Sledgemapper.Api.Commands
} }
} }
} }
var newSnapshot = new Snapshot{ if (mapUpdates.Any())
{
var newSnapshot = new Snapshot
{
SessionId = session.SessionId, SessionId = session.SessionId,
Timestamp = mapUpdates.Max(mapSession => mapSession.Timestamp), Timestamp = mapUpdates.Max(mapSession => mapSession.Timestamp),
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(mapSession) Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(mapSession)
}; };
await _dbcontext.Snapshots.AddAsync(newSnapshot); await _dbcontext.Snapshots.AddAsync(newSnapshot);
await _dbcontext.SaveChangesAsync(); await _dbcontext.SaveChangesAsync();
}
return mapSession; return mapSession;

View file

@ -60,12 +60,16 @@ namespace Sledgemapper.Api.Data
modelBuilder.Entity<UserConnection>(entity => modelBuilder.Entity<UserConnection>(entity =>
{ {
entity.HasKey(e => e.UserConnectionId); entity.HasKey(e => e.UserConnectionId);
entity.HasIndex(e => e.UserId);
}); });
modelBuilder.Entity<SessionUser>().ToTable("SessionUser", "dbo"); modelBuilder.Entity<SessionUser>().ToTable("SessionUser", "dbo");
modelBuilder.Entity<SessionUser>(entity => modelBuilder.Entity<SessionUser>(entity =>
{ {
entity.HasKey(e => e.SessionUserId); entity.HasKey(e => e.SessionUserId);
entity.HasIndex(e => e.SessionId);
}); });
modelBuilder.Entity<Snapshot>().ToTable("Snapshot", "dbo"); modelBuilder.Entity<Snapshot>().ToTable("Snapshot", "dbo");

View file

@ -17,11 +17,10 @@ using Sledgemapper.Helpers;
namespace SignalRChat.Hubs namespace SignalRChat.Hubs
{ {
[Authorize] [Authorize]
public class SledgemapperHub : Hub<ISledgemapperClient> public class SledgemapperHub : Hub<ISledgemapperClient>
{ {
private static readonly ConcurrentDictionary<int, string> UserColors = new ConcurrentDictionary<int, string>();
private readonly MyDbContext _dbContext; private readonly MyDbContext _dbContext;
private readonly DataContext _datacontext; private readonly DataContext _datacontext;
@ -35,14 +34,14 @@ namespace SignalRChat.Hubs
// #cca300, #20f200, #004011, #00e6d6, #005c73, #0057d9, #d900ca, #660029, #d9003a // #cca300, #20f200, #004011, #00e6d6, #005c73, #0057d9, #d900ca, #660029, #d9003a
// private static Dictionary<string, Session> _sessions = new Dictionary<string, Session>(); // private static Dictionary<string, Session> _sessions = new Dictionary<string, Session>();
public List<string> Colors = new List<string>{ public List<string> Colors = new List<string>{
"#CC0000", "#e6194B",
"#20f200", "#f58231",
"#FFCC00", "#3cb44b",
"#006666", "#000075",
"#660029", "#911eb4",
"#0000CC", "#800000",
"#663399", "#808000",
"#CC0099"}; "#469990"};
public async Task NewTile(string sessionName, Tile tile) public async Task NewTile(string sessionName, Tile tile)
{ {
@ -147,37 +146,20 @@ namespace SignalRChat.Hubs
if (session != null) if (session != null)
{ {
// var newSession = new Session();
var userSession = new SessionUser { SessionId = session.SessionId, UserId = userId }; var userSession = new SessionUser { SessionId = session.SessionId, UserId = userId };
_dbContext.SessionUsers.Add(userSession); _dbContext.SessionUsers.Add(userSession);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
//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().
// //Select((r, index) => new { Place = index, Name = r })
// Select((p, index) => new Player
// {
// Initials = p.Initials,
// UserId = userId,
// Color = Colors[index],
// Position = new Tile { X = 0, Y = 0 }
// }).ToList();
//await _dbContext.SaveChangesAsync();
await Groups.AddToGroupAsync(Context.ConnectionId, session.SessionName); await Groups.AddToGroupAsync(Context.ConnectionId, session.SessionName);
var user = _datacontext.Users.First(u => u.Id == userId); 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 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 player = new Player { UserId = userId, Initials = user.Initials, Position = new Tile { X = 0, Y = 0 }, Color = UserColors[userId] };
await Clients.Group(session.SessionName).NewPlayer(player);
await Clients.Group(session.SessionName).RefreshPlayers();
var newSession = new Sledgemapper.Shared.Entities.Session var newSession = new Sledgemapper.Shared.Entities.Session
{ {
SessionName = sessionName, SessionName = session.SessionName,
SessionId = session.SessionId SessionId = session.SessionId
}; };
@ -235,6 +217,8 @@ namespace SignalRChat.Hubs
var userConnection = new UserConnection { ConnectionId = Context.ConnectionId, UserId = userId }; var userConnection = new UserConnection { ConnectionId = Context.ConnectionId, UserId = userId };
_dbContext.UserConnections.Add(userConnection); _dbContext.UserConnections.Add(userConnection);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
var availableColor = Colors.Where(m => !UserColors.Values.Contains(m)).First();
UserColors.AddOrUpdate(userId, availableColor, (key, oldValue) => availableColor);
await base.OnConnectedAsync(); await base.OnConnectedAsync();
} }

Binary file not shown.

View file

@ -49,7 +49,7 @@ namespace SignalRChat
services.AddSignalR(); services.AddSignalR();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services.AddMediatR(typeof(Startup)); services.AddMediatR(typeof(Startup));
services.AddDbContext<MyDbContext>(options => options.UseSqlite("Data Source=sledgemapper.db")); services.AddDbContext<MyDbContext>(options => {options.UseSqlite("Data Source=sledgemapper.db"); options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);});
// services.AddEntityFrameworkSqlite().AddDbContext<MyDbContext>(); // services.AddEntityFrameworkSqlite().AddDbContext<MyDbContext>();
// configure strongly typed settings objects // configure strongly typed settings objects

Binary file not shown.

View file

@ -15,5 +15,6 @@ namespace Sledgemapper.Clients
Task NewPlayer(Player player); Task NewPlayer(Player player);
Task PlayerUpdate(Player player); Task PlayerUpdate(Player player);
Task UpdateMap(Session player); Task UpdateMap(Session player);
Task RefreshPlayers();
} }
} }

View file

@ -85,6 +85,11 @@ namespace Sledgemapper
SessionData.Map.TryAdd(tile.ToString(), tile); SessionData.Map.TryAdd(tile.ToString(), tile);
}); });
Connection.On("RefreshPlayers", () =>
{
Connection?.SendAsync("UpdatePosition", SessionData.SessionName, SessionData.SessionId, SessionData.Players.First(p=>p.UserId==int.Parse(_authenticateResponse.Id)));
});
Connection.On<Wall>("NewWall", (tile) => Connection.On<Wall>("NewWall", (tile) =>
{ {
SessionData.Walls.Remove(tile.ToString(), out var _); SessionData.Walls.Remove(tile.ToString(), out var _);

View file

@ -21,7 +21,6 @@ namespace Sledgemapper
[Post("/session/{sessionName}/snapshot")] [Post("/session/{sessionName}/snapshot")]
Task SaveSnapshot([Body] Session session, string sessionName); Task SaveSnapshot([Body] Session session, string sessionName);
[Post("/session/{sessionName}/tile")] [Post("/session/{sessionName}/tile")]
Task NewTile([Body] Tile tile, string sessionName); Task NewTile([Body] Tile tile, string sessionName);

View file

@ -766,7 +766,10 @@ namespace Sledgemapper
} }
using StreamReader file = File.OpenText(dialog.FilePath); using StreamReader file = File.OpenText(dialog.FilePath);
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
_sessionData = (Session)serializer.Deserialize(file, typeof(Session)); var loadData = (Session)serializer.Deserialize(file, typeof(Session));
_sessionData.Map=loadData.Map;
_sessionData.Overlays=loadData.Overlays;
_sessionData.Walls=loadData.Walls;
}; };
dialog.ShowModal(_desktop); dialog.ShowModal(_desktop);

View file

@ -1,4 +1,4 @@
/* Generated by MyraPad at 17/11/2020 15:10:52 */ /* Generated by MyraPad at 17/11/2020 22:10:31 */
using Myra.Graphics2D; using Myra.Graphics2D;
using Myra.Graphics2D.TextureAtlases; using Myra.Graphics2D.TextureAtlases;
using Myra.Graphics2D.UI; using Myra.Graphics2D.UI;
@ -174,6 +174,51 @@ namespace Sledgemapper.UI
verticalSplitPane1.Widgets.Add(verticalStackPanel2); verticalSplitPane1.Widgets.Add(verticalStackPanel2);
verticalSplitPane1.Widgets.Add(verticalStackPanel3); verticalSplitPane1.Widgets.Add(verticalStackPanel3);
var label1 = new Label();
label1.Text = "Connection status:";
lblConnectionStatus = new Label();
lblConnectionStatus.Text = "Disconnected";
lblConnectionStatus.MinWidth = 100;
lblConnectionStatus.Id = "lblConnectionStatus";
var verticalSeparator1 = new VerticalSeparator();
var label2 = new Label();
label2.Text = "Username:";
lblUsername = new Label();
lblUsername.Text = "n/a";
lblUsername.MinWidth = 100;
lblUsername.Id = "lblUsername";
var verticalSeparator2 = new VerticalSeparator();
var label3 = new Label();
label3.Text = "Session name:";
lblSessionName = new Label();
lblSessionName.Text = "n/a";
lblSessionName.MinWidth = 100;
lblSessionName.Id = "lblSessionName";
var horizontalStackPanel1 = new HorizontalStackPanel();
horizontalStackPanel1.Spacing = 10;
horizontalStackPanel1.Proportions.Add(new Proportion
{
Type = Myra.Graphics2D.UI.ProportionType.Auto,
});
horizontalStackPanel1.Height = 25;
horizontalStackPanel1.Background = new SolidBrush("#333333FF");
horizontalStackPanel1.Widgets.Add(label1);
horizontalStackPanel1.Widgets.Add(lblConnectionStatus);
horizontalStackPanel1.Widgets.Add(verticalSeparator1);
horizontalStackPanel1.Widgets.Add(label2);
horizontalStackPanel1.Widgets.Add(lblUsername);
horizontalStackPanel1.Widgets.Add(verticalSeparator2);
horizontalStackPanel1.Widgets.Add(label3);
horizontalStackPanel1.Widgets.Add(lblSessionName);
Proportions.Add(new Proportion Proportions.Add(new Proportion
{ {
@ -185,6 +230,7 @@ namespace Sledgemapper.UI
}); });
Widgets.Add(_mainMenu); Widgets.Add(_mainMenu);
Widgets.Add(verticalSplitPane1); Widgets.Add(verticalSplitPane1);
Widgets.Add(horizontalStackPanel1);
} }
@ -202,5 +248,8 @@ namespace Sledgemapper.UI
public Grid GridTiles; public Grid GridTiles;
public Grid GridWalls; public Grid GridWalls;
public Grid GridOverlays; public Grid GridOverlays;
public Label lblConnectionStatus;
public Label lblUsername;
public Label lblSessionName;
} }
} }

View file

@ -59,5 +59,18 @@
</ScrollViewer> </ScrollViewer>
</VerticalStackPanel> </VerticalStackPanel>
</VerticalSplitPane> </VerticalSplitPane>
<HorizontalStackPanel Spacing="10" Height="25" Background="#333333FF">
<HorizontalStackPanel.Proportions>
<Proportion Type="Auto" />
</HorizontalStackPanel.Proportions>
<Label Text="Connection status:" />
<Label Text="Disconnected" MinWidth="100" Id="lblConnectionStatus" />
<VerticalSeparator />
<Label Text="Username:" />
<Label Text="n/a" MinWidth="100" Id="lblUsername" />
<VerticalSeparator />
<Label Text="Session name:" />
<Label Text="n/a" MinWidth="100" Id="lblSessionName" />
</HorizontalStackPanel>
</VerticalStackPanel> </VerticalStackPanel>
</Project> </Project>