I don't know what I'm doing anymore

This commit is contained in:
Michele 2020-11-16 00:03:42 +00:00
parent ea9cc32534
commit 8fdee0cb67
17 changed files with 342 additions and 116 deletions

View file

@ -12,6 +12,8 @@ using Sledgemapper.Api.Data;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Authorization;
using Sledgemapper.Api.Models;
using Sledgemapper.Helpers;
namespace SignalRChat.Hubs
{
@ -20,11 +22,15 @@ namespace SignalRChat.Hubs
[Authorize]
public class SledgemapperHub : Hub<ISledgemapperClient>
{
public SledgemapperHub()
{
private readonly MyDbContext _dbContext;
private readonly DataContext _datacontext;
public SledgemapperHub(MyDbContext dbContext, DataContext datacontext)
{
_dbContext = dbContext;
_datacontext = datacontext;
}
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>{"CC0000",
"CC3300",
"FFCC00",
@ -114,60 +120,116 @@ namespace SignalRChat.Hubs
await Clients.Group(sessionName).DeleteOverlay(tile);
}
public async Task<Session> NewSession(string sessionName, string initials)
// public async Task<Session> NewSession(string sessionName, string initials)
// {
// var userId = int.Parse(Context.User.Identity.Name);
// // var user = this.Context.GetHttpContext().;
// var session = new Session();
// session.Colors = new List<string>(Colors);
// session.Colors.Shuffle();
// var player = new Player { Position = new Tile { X = 0, Y = 0 }, ConnectionId = Context.ConnectionId, Color = session.Colors[0], Initials = initials };
// session.Players.Add(player);
// _sessions.Add(sessionName, session);
// await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
// return session;
// }
public async Task<Sledgemapper.Shared.Entities.Session> JoinSession(string sessionName, string initials)
{
var session = _dbContext.Sessions.FirstOrDefault(s => s.SessionName == sessionName);
var userId = int.Parse(Context.User.Identity.Name);
// var user = this.Context.GetHttpContext().;
var session = new Session();
session.Colors = new List<string>(Colors);
session.Colors.Shuffle();
var player = new Player { Position = new Tile { X = 0, Y = 0 }, ConnectionId = Context.ConnectionId, Color = session.Colors[0], Initials = initials };
session.Players.Add(player);
_sessions.Add(sessionName, session);
await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
return session;
}
public async Task<Session> JoinSession(string sessionName, string initials)
{
if (_sessions.ContainsKey(sessionName))
if (session != null)
{
var session = _sessions[sessionName];
var player = new Player { Position = new Tile { X = 0, Y = 0 }, ConnectionId = Context.ConnectionId, Color = session.Colors[session.Players.Count], Initials = initials };
session.Players.Add(player);
await Clients.Group(sessionName).NewPlayer(player);
// 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)).
Select(p => new Player
{
Initials = p.Initials,
UserId = userId,
Position = new Tile { X = 0, Y = 0 }
}).ToList();
await _dbContext.SaveChangesAsync();
await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
return session;
var newSession = new Sledgemapper.Shared.Entities.Session
{
Players = players,
SessionName = sessionName
};
return newSession;
}
else
{
return null;
}
// if (_sessions.ContainsKey(sessionName))
// {
// var session = _sessions[sessionName];
// var player = new Player { Position = new Tile { X = 0, Y = 0 }, ConnectionId = Context.ConnectionId, Color = session.Colors[session.Players.Count], Initials = initials };
// session.Players.Add(player);
// await Clients.Group(sessionName).NewPlayer(player);
// return session;
// }
// else
// {
// return null;
// }
}
public async Task UpdatePosition(string sessionName, Tile tile)
{
var player = _sessions[sessionName].Players.First(m => m.ConnectionId == Context.ConnectionId);
player.Position = tile;
await Clients.Group(sessionName).PlayerUpdate(player);
// var userId = int.Parse(Context.User.Identity.Name);
// var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionName == sessionName);
// var player = _sessions[sessionName].Players.First(m => m.ConnectionId == Context.ConnectionId);
// player.Position = tile;
// await Clients.Group(sessionName).PlayerUpdate(player);
}
public async Task<Session> Refresh(string sessionName)
// public async Task<Session> Refresh(string sessionName)
// {
// return _sessions[sessionName];
// }
// public async Task Sync(string sessionName, Session map)
// {
// _sessions[sessionName].Map = map.Map;
// _sessions[sessionName].Overlays = map.Overlays;
// _sessions[sessionName].Walls = map.Walls;
// await Clients.Group(sessionName).UpdateMap(_sessions[sessionName]);
// }
public override async Task OnConnectedAsync()
{
return _sessions[sessionName];
var userId = int.Parse(Context.User.Identity.Name);
//var result = _connectedPlayers.AddOrUpdate(Context.ConnectionId, userId,(key, oldValue) => userId);
var userConnection = new UserConnection { ConnectionId = Context.ConnectionId, UserId = userId };
_dbContext.UserConnections.Add(userConnection);
await _dbContext.SaveChangesAsync();
await base.OnConnectedAsync();
}
public async Task Sync(string sessionName, Session map)
public override Task OnDisconnectedAsync(Exception exception)
{
_sessions[sessionName].Map = map.Map;
_sessions[sessionName].Overlays = map.Overlays;
_sessions[sessionName].Walls = map.Walls;
await Clients.Group(sessionName).UpdateMap(_sessions[sessionName]);
_connectedPlayers.TryRemove(Context.ConnectionId, out var userId);
return base.OnDisconnectedAsync(exception);
}
ConcurrentDictionary<string, int> _connectedPlayers = new ConcurrentDictionary<string, int>();
}
}