funziona
This commit is contained in:
parent
0a4b8ebeb2
commit
e988f5c310
13 changed files with 135 additions and 72 deletions
|
@ -34,10 +34,10 @@ namespace Sledgemapper.Api.Commands
|
|||
double timestamp;
|
||||
Sledgemapper.Shared.Entities.Session mapSession;
|
||||
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)
|
||||
{
|
||||
|
||||
|
||||
timestamp = 0;
|
||||
mapSession = new Shared.Entities.Session();
|
||||
}
|
||||
|
@ -59,11 +59,11 @@ namespace Sledgemapper.Api.Commands
|
|||
mapSession.NewTile(tile, tile.ID);
|
||||
break;
|
||||
case "W":
|
||||
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
|
||||
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
|
||||
mapSession.NewWall(wall, wall.ID);
|
||||
break;
|
||||
case "O":
|
||||
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
|
||||
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
|
||||
mapSession.NewOverlay(overlay, overlay.ID);
|
||||
break;
|
||||
|
||||
|
@ -79,24 +79,29 @@ namespace Sledgemapper.Api.Commands
|
|||
mapSession.DeleteTile(tile);
|
||||
break;
|
||||
case "W":
|
||||
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
|
||||
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
|
||||
mapSession.DeleteWall(wall);
|
||||
break;
|
||||
case "O":
|
||||
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
|
||||
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
|
||||
mapSession.DeleteOverlay(overlay);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
var newSnapshot = new Snapshot{
|
||||
SessionId=session.SessionId,
|
||||
Timestamp=mapUpdates.Max(mapSession=>mapSession.Timestamp),
|
||||
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(mapSession)
|
||||
if (mapUpdates.Any())
|
||||
{
|
||||
var newSnapshot = new Snapshot
|
||||
{
|
||||
SessionId = session.SessionId,
|
||||
Timestamp = mapUpdates.Max(mapSession => mapSession.Timestamp),
|
||||
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(mapSession)
|
||||
};
|
||||
await _dbcontext.Snapshots.AddAsync(newSnapshot);
|
||||
await _dbcontext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
await _dbcontext.Snapshots.AddAsync(newSnapshot);
|
||||
await _dbcontext.SaveChangesAsync();
|
||||
|
||||
|
||||
return mapSession;
|
||||
|
|
|
@ -38,7 +38,7 @@ namespace Sledgemapper.Api.Controllers
|
|||
{
|
||||
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)
|
||||
|
|
|
@ -60,12 +60,16 @@ namespace Sledgemapper.Api.Data
|
|||
modelBuilder.Entity<UserConnection>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.UserConnectionId);
|
||||
entity.HasIndex(e => e.UserId);
|
||||
|
||||
});
|
||||
|
||||
modelBuilder.Entity<SessionUser>().ToTable("SessionUser", "dbo");
|
||||
modelBuilder.Entity<SessionUser>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.SessionUserId);
|
||||
entity.HasIndex(e => e.SessionId);
|
||||
|
||||
});
|
||||
|
||||
modelBuilder.Entity<Snapshot>().ToTable("Snapshot", "dbo");
|
||||
|
|
|
@ -17,11 +17,10 @@ using Sledgemapper.Helpers;
|
|||
|
||||
namespace SignalRChat.Hubs
|
||||
{
|
||||
|
||||
|
||||
[Authorize]
|
||||
public class SledgemapperHub : Hub<ISledgemapperClient>
|
||||
{
|
||||
private static readonly ConcurrentDictionary<int, string> UserColors = new ConcurrentDictionary<int, string>();
|
||||
private readonly MyDbContext _dbContext;
|
||||
private readonly DataContext _datacontext;
|
||||
|
||||
|
@ -35,14 +34,14 @@ namespace SignalRChat.Hubs
|
|||
// #cca300, #20f200, #004011, #00e6d6, #005c73, #0057d9, #d900ca, #660029, #d9003a
|
||||
// private static Dictionary<string, Session> _sessions = new Dictionary<string, Session>();
|
||||
public List<string> Colors = new List<string>{
|
||||
"#CC0000",
|
||||
"#20f200",
|
||||
"#FFCC00",
|
||||
"#006666",
|
||||
"#660029",
|
||||
"#0000CC",
|
||||
"#663399",
|
||||
"#CC0099"};
|
||||
"#e6194B",
|
||||
"#f58231",
|
||||
"#3cb44b",
|
||||
"#000075",
|
||||
"#911eb4",
|
||||
"#800000",
|
||||
"#808000",
|
||||
"#469990"};
|
||||
|
||||
public async Task NewTile(string sessionName, Tile tile)
|
||||
{
|
||||
|
@ -147,37 +146,20 @@ namespace SignalRChat.Hubs
|
|||
|
||||
if (session != null)
|
||||
{
|
||||
// var newSession = new Session();
|
||||
var userSession = new SessionUser { SessionId = session.SessionId, UserId = userId };
|
||||
_dbContext.SessionUsers.Add(userSession);
|
||||
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);
|
||||
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
|
||||
{
|
||||
SessionName = sessionName,
|
||||
SessionName = session.SessionName,
|
||||
SessionId = session.SessionId
|
||||
};
|
||||
|
||||
|
@ -202,7 +184,7 @@ namespace SignalRChat.Hubs
|
|||
// }
|
||||
}
|
||||
|
||||
public async Task UpdatePosition(string sessionName,int sessionId, 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);
|
||||
|
@ -235,6 +217,8 @@ namespace SignalRChat.Hubs
|
|||
var userConnection = new UserConnection { ConnectionId = Context.ConnectionId, UserId = userId };
|
||||
_dbContext.UserConnections.Add(userConnection);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
var availableColor = Colors.Where(m => !UserColors.Values.Contains(m)).First();
|
||||
UserColors.AddOrUpdate(userId, availableColor, (key, oldValue) => availableColor);
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
|
@ -245,7 +229,7 @@ namespace SignalRChat.Hubs
|
|||
{
|
||||
_dbContext.UserConnections.Remove(userConnection);
|
||||
}
|
||||
|
||||
|
||||
var userSessions = _dbContext.SessionUsers.Where(m => m.UserId == userConnection.UserId).ToList();
|
||||
{
|
||||
foreach (var userSession in userSessions)
|
||||
|
|
Binary file not shown.
|
@ -49,7 +49,7 @@ namespace SignalRChat
|
|||
services.AddSignalR();
|
||||
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
||||
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>();
|
||||
// configure strongly typed settings objects
|
||||
|
|
Binary file not shown.
|
@ -15,5 +15,6 @@ namespace Sledgemapper.Clients
|
|||
Task NewPlayer(Player player);
|
||||
Task PlayerUpdate(Player player);
|
||||
Task UpdateMap(Session player);
|
||||
Task RefreshPlayers();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,36 +85,41 @@ namespace Sledgemapper
|
|||
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) =>
|
||||
{
|
||||
SessionData.Walls.Remove(tile.ToString(), out var _);
|
||||
SessionData.Walls.TryAdd(tile.ToString(), tile);
|
||||
});
|
||||
|
||||
Connection.On<Overlay>("NewOverlay", (tile) =>
|
||||
{
|
||||
SessionData.Overlays.Remove(tile.ToString(), out var _);
|
||||
SessionData.Overlays.TryAdd(tile.ToString(), tile);
|
||||
});
|
||||
Connection.On<Overlay>("NewOverlay", (tile) =>
|
||||
{
|
||||
SessionData.Overlays.Remove(tile.ToString(), out var _);
|
||||
SessionData.Overlays.TryAdd(tile.ToString(), tile);
|
||||
});
|
||||
|
||||
Connection.On<Player>("NewPlayer", (player) =>
|
||||
{
|
||||
var p = SessionData.Players.FirstOrDefault(m => m.UserId == player.UserId);
|
||||
if (p is null)
|
||||
{
|
||||
SessionData.Players.Add(player);
|
||||
}
|
||||
else
|
||||
{
|
||||
p.Color = player.Color;
|
||||
p.Position = player.Position;
|
||||
}
|
||||
});
|
||||
{
|
||||
var p = SessionData.Players.FirstOrDefault(m => m.UserId == player.UserId);
|
||||
if (p is null)
|
||||
{
|
||||
SessionData.Players.Add(player);
|
||||
}
|
||||
else
|
||||
{
|
||||
p.Color = player.Color;
|
||||
p.Position = player.Position;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Task<string> GetToken()
|
||||
{
|
||||
return Task.FromResult(_authenticateResponse.Token);
|
||||
return Task.FromResult(_authenticateResponse.Token);
|
||||
}
|
||||
|
||||
public async Task<bool> Register(RegisterModel registerModel)
|
||||
|
@ -186,7 +191,7 @@ namespace Sledgemapper
|
|||
if (getToken == null) throw new ArgumentNullException(nameof(getToken));
|
||||
this.getToken = getToken;
|
||||
|
||||
//if (myConfigurationService.VerifySslCertificate == false)
|
||||
//if (myConfigurationService.VerifySslCertificate == false)
|
||||
//{
|
||||
ServerCertificateCustomValidationCallback =
|
||||
(message, certificate, chain, sslPolicyErrors) => true;
|
||||
|
|
|
@ -21,7 +21,6 @@ namespace Sledgemapper
|
|||
[Post("/session/{sessionName}/snapshot")]
|
||||
Task SaveSnapshot([Body] Session session, string sessionName);
|
||||
|
||||
|
||||
[Post("/session/{sessionName}/tile")]
|
||||
Task NewTile([Body] Tile tile, string sessionName);
|
||||
|
||||
|
|
|
@ -766,7 +766,10 @@ namespace Sledgemapper
|
|||
}
|
||||
using StreamReader file = File.OpenText(dialog.FilePath);
|
||||
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);
|
||||
|
|
|
@ -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.TextureAtlases;
|
||||
using Myra.Graphics2D.UI;
|
||||
|
@ -174,6 +174,51 @@ namespace Sledgemapper.UI
|
|||
verticalSplitPane1.Widgets.Add(verticalStackPanel2);
|
||||
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
|
||||
{
|
||||
|
@ -185,6 +230,7 @@ namespace Sledgemapper.UI
|
|||
});
|
||||
Widgets.Add(_mainMenu);
|
||||
Widgets.Add(verticalSplitPane1);
|
||||
Widgets.Add(horizontalStackPanel1);
|
||||
}
|
||||
|
||||
|
||||
|
@ -202,5 +248,8 @@ namespace Sledgemapper.UI
|
|||
public Grid GridTiles;
|
||||
public Grid GridWalls;
|
||||
public Grid GridOverlays;
|
||||
public Label lblConnectionStatus;
|
||||
public Label lblUsername;
|
||||
public Label lblSessionName;
|
||||
}
|
||||
}
|
|
@ -59,5 +59,18 @@
|
|||
</ScrollViewer>
|
||||
</VerticalStackPanel>
|
||||
</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>
|
||||
</Project>
|
Loading…
Add table
Add a link
Reference in a new issue