refactoring backend structure

This commit is contained in:
Michele 2021-02-15 23:35:55 +00:00
parent dcc59f7b69
commit 0e1c16ab91
36 changed files with 786 additions and 454 deletions

View file

@ -6,7 +6,7 @@ using System.Collections.Concurrent;
using Sledgemapper.Shared.Entities;
using Sledgemapper.Clients;
using System;
using Sledgemapper.Api.Data;
using Sledgemapper.Api.Infrastructure.Data;
using Microsoft.AspNetCore.Authorization;
using Sledgemapper.Api.Models;
using Sledgemapper.Helpers;
@ -17,13 +17,13 @@ namespace Sledgemapper.Api.Hubs
public class SledgemapperHub : Hub<ISledgemapperClient>
{
private static readonly ConcurrentDictionary<int, string> UserColors = new();
private readonly MyDbContext _dbContext;
private readonly DataContext _datacontext;
private readonly SledgemapperDbContext _dbContext;
// private readonly DataContext _datacontext;
public SledgemapperHub(MyDbContext dbContext, DataContext datacontext)
public SledgemapperHub(SledgemapperDbContext dbContext/*, DataContext datacontext*/)
{
_dbContext = dbContext;
_datacontext = datacontext;
// _datacontext = datacontext;
}
// other colors
@ -89,7 +89,7 @@ namespace Sledgemapper.Api.Hubs
public async Task Ping(string sessionName, Tile location)
{
var userId = int.Parse(Context.User.Identity.Name);
var user = _datacontext.Users.First(u => u.Id == userId);
var user = _dbContext.Users.First(u => u.Id == userId);
var player = new Player { UserId = userId, Initials = user.Initials, Position = new Tile { X = 0, Y = 0 }, Color = UserColors[userId] };
await Clients.Group(sessionName).Ping(new Ping{X=location.X, Y=location.Y, Player=player});
@ -106,7 +106,7 @@ namespace Sledgemapper.Api.Hubs
_dbContext.SessionUsers.Add(userSession);
await _dbContext.SaveChangesAsync();
await Groups.AddToGroupAsync(Context.ConnectionId, session.SessionName);
var user = _datacontext.Users.First(u => u.Id == userId);
var user = _dbContext.Users.First(u => u.Id == userId);
var player = new Player { UserId = userId, Initials = user.Initials, Position = new Tile { X = 0, Y = 0 }, Color = UserColors[userId] };
@ -131,7 +131,7 @@ namespace Sledgemapper.Api.Hubs
{
var userId = int.Parse(Context.User.Identity.Name);
var SessionUsers = _dbContext.SessionUsers.Where(m => m.SessionId == sessionId).OrderBy(m => m.UserId).ToList();
var user = _datacontext.Users.First(u => u.Id == userId);
var user = _dbContext.Users.First(u => u.Id == userId);
var player = new Player { UserId = userId, Initials = user.Initials, Position = tile, Color = UserColors[userId] };
await Clients.Group(sessionName).PlayerUpdate(player);
}