implement
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using AipsCore.Application.Abstract.MessageBroking;
|
||||
using AipsCore.Application.Common.Message.MoveShape;
|
||||
using AipsCore.Application.Models.Shape.Command.MoveShape;
|
||||
using AipsRT.Model.Whiteboard;
|
||||
using AipsRT.Model.Whiteboard.Shapes;
|
||||
using AipsRT.Model.Whiteboard.Structs;
|
||||
using AipsRT.Services.Interfaces;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
@@ -41,17 +44,89 @@ public class WhiteboardHub : Hub
|
||||
.SendAsync("Leaved", Context.UserIdentifier!);
|
||||
}
|
||||
|
||||
private Guid CurrentUserId => Guid.Parse(Context.UserIdentifier!);
|
||||
private Whiteboard CurrentWhiteboard => _whiteboardManager.GetWhiteboardForUser(CurrentUserId)!;
|
||||
|
||||
private async Task ResetCurrentUser()
|
||||
{
|
||||
await Clients.Caller.SendAsync("InitWhiteboard", CurrentWhiteboard);
|
||||
}
|
||||
|
||||
private async Task SendToOthers(string methodName, object? arg)
|
||||
{
|
||||
await Clients.GroupExcept(CurrentWhiteboard.WhiteboardId.ToString(), Context.ConnectionId)
|
||||
.SendAsync(methodName, arg);
|
||||
}
|
||||
|
||||
public async Task AddRectangle(Rectangle rectangle)
|
||||
{
|
||||
var whiteboard = _whiteboardManager.GetWhiteboardForUser(Guid.Parse(Context.UserIdentifier!))!;
|
||||
|
||||
rectangle.OwnerId = Guid.Parse(Context.UserIdentifier!);
|
||||
rectangle.OwnerId = CurrentUserId;
|
||||
var whiteboard = CurrentWhiteboard;
|
||||
|
||||
await _messagingService.CreatedRectangle(whiteboard.WhiteboardId, rectangle);
|
||||
|
||||
whiteboard.AddRectangle(rectangle);
|
||||
|
||||
await Clients.GroupExcept(whiteboard.WhiteboardId.ToString(), Context.ConnectionId)
|
||||
.SendAsync("AddedRectangle", rectangle);
|
||||
await SendToOthers("AddedRectangle", rectangle);
|
||||
}
|
||||
|
||||
public async Task AddArrow(Arrow arrow)
|
||||
{
|
||||
arrow.OwnerId = CurrentUserId;
|
||||
var whiteboard = CurrentWhiteboard;
|
||||
|
||||
await _messagingService.CreatedArrow(whiteboard.WhiteboardId, arrow);
|
||||
|
||||
whiteboard.AddArrow(arrow);
|
||||
|
||||
await SendToOthers("AddedArrow", arrow);
|
||||
}
|
||||
|
||||
public async Task AddLine(Line line)
|
||||
{
|
||||
line.OwnerId = CurrentUserId;
|
||||
var whiteboard = CurrentWhiteboard;
|
||||
|
||||
await _messagingService.CreateLine(whiteboard.WhiteboardId, line);
|
||||
|
||||
whiteboard.AddLine(line);
|
||||
|
||||
await SendToOthers("AddedLine", line);
|
||||
}
|
||||
|
||||
public async Task AddTextShape(TextShape textShape)
|
||||
{
|
||||
textShape.OwnerId = CurrentUserId;
|
||||
var whiteboard = CurrentWhiteboard;
|
||||
|
||||
await _messagingService.CreateTextShape(whiteboard.WhiteboardId, textShape);
|
||||
|
||||
whiteboard.AddTextShape(textShape);
|
||||
|
||||
await SendToOthers("AddedTextShape", textShape);
|
||||
}
|
||||
|
||||
public async Task MoveShape(MoveShapeCommand moveShape)
|
||||
{
|
||||
var whiteboard = CurrentWhiteboard;
|
||||
|
||||
var shape = whiteboard.Shapes.Find(s => s.Id.ToString() == moveShape.ShapeId);
|
||||
|
||||
if (shape is null || shape.OwnerId != CurrentUserId)
|
||||
{
|
||||
await ResetCurrentUser();
|
||||
return;
|
||||
}
|
||||
|
||||
shape.Move(new Position(moveShape.NewPositionX, moveShape.NewPositionY));
|
||||
|
||||
await SendToOthers("MovedShape", moveShape);
|
||||
}
|
||||
|
||||
public async Task PlaceShape(MoveShapeCommand moveShape)
|
||||
{
|
||||
await MoveShape(moveShape);
|
||||
|
||||
await _messagingService.MoveShape(moveShape);
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,11 @@ public class Arrow : Shape
|
||||
public Position EndPosition { get; set; }
|
||||
|
||||
public int Thickness { get; set; }
|
||||
|
||||
public override void Move(Position newPosition)
|
||||
{
|
||||
var difference = newPosition - EndPosition;
|
||||
EndPosition += difference;
|
||||
base.Move(newPosition);
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,11 @@ public class Line : Shape
|
||||
public Position EndPosition { get; set; }
|
||||
|
||||
public int Thickness { get; set; }
|
||||
|
||||
public override void Move(Position newPosition)
|
||||
{
|
||||
var difference = newPosition - EndPosition;
|
||||
EndPosition += difference;
|
||||
base.Move(newPosition);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ public static class ShapeMappingExtensions
|
||||
return new Rectangle()
|
||||
{
|
||||
Id = shape.Id,
|
||||
OwnerId = shape.AuthorId,
|
||||
Position = new Position(shape.PositionX, shape.PositionY),
|
||||
Color = shape.Color,
|
||||
EndPosition = new Position(shape.EndPositionX!.Value, shape.EndPositionY!.Value),
|
||||
@@ -23,6 +24,7 @@ public static class ShapeMappingExtensions
|
||||
return new Arrow()
|
||||
{
|
||||
Id = shape.Id,
|
||||
OwnerId = shape.AuthorId,
|
||||
Position = new Position(shape.PositionX, shape.PositionY),
|
||||
Color = shape.Color,
|
||||
EndPosition = new Position(shape.EndPositionX!.Value, shape.EndPositionY!.Value),
|
||||
@@ -35,6 +37,7 @@ public static class ShapeMappingExtensions
|
||||
return new Line()
|
||||
{
|
||||
Id = shape.Id,
|
||||
OwnerId = shape.AuthorId,
|
||||
Position = new Position(shape.PositionX, shape.PositionY),
|
||||
Color = shape.Color,
|
||||
EndPosition = new Position(shape.EndPositionX!.Value, shape.EndPositionY!.Value),
|
||||
@@ -47,6 +50,7 @@ public static class ShapeMappingExtensions
|
||||
return new TextShape()
|
||||
{
|
||||
Id = shape.Id,
|
||||
OwnerId = shape.AuthorId,
|
||||
Position = new Position(shape.PositionX, shape.PositionY),
|
||||
Color = shape.Color,
|
||||
TextValue = shape.TextValue!,
|
||||
|
||||
@@ -7,4 +7,11 @@ public class Rectangle : Shape
|
||||
public Position EndPosition { get; set; }
|
||||
|
||||
public int BorderThickness { get; set; }
|
||||
|
||||
public override void Move(Position newPosition)
|
||||
{
|
||||
var difference = newPosition - Position;
|
||||
EndPosition += difference;
|
||||
base.Move(newPosition);
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,9 @@ public abstract class Shape
|
||||
public Position Position { get; set; }
|
||||
|
||||
public string Color { get; set; }
|
||||
|
||||
public virtual void Move(Position newPosition)
|
||||
{
|
||||
Position = newPosition;
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,14 @@ public struct Position
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
|
||||
public static Position operator -(Position position, Position otherPosition)
|
||||
{
|
||||
return new Position(position.X - otherPosition.X, position.Y - otherPosition.Y);
|
||||
}
|
||||
|
||||
public static Position operator +(Position position, Position otherPosition)
|
||||
{
|
||||
return new Position(position.X + otherPosition.X, position.Y + otherPosition.Y);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using AipsCore.Application.Models.Shape.Command.CreateTextShape;
|
||||
using AipsCore.Application.Models.Shape.Command.MoveShape;
|
||||
using AipsRT.Model.Whiteboard.Shapes;
|
||||
|
||||
namespace AipsRT.Services.Interfaces;
|
||||
@@ -5,4 +7,9 @@ namespace AipsRT.Services.Interfaces;
|
||||
public interface IMessagingService
|
||||
{
|
||||
Task CreatedRectangle(Guid whiteboardId, Rectangle rectangle);
|
||||
Task CreatedArrow(Guid whiteboardId, Arrow arrow);
|
||||
Task CreateLine(Guid whiteboardId, Line line);
|
||||
Task CreateTextShape(Guid whiteboardId, TextShape textShape);
|
||||
|
||||
Task MoveShape(MoveShapeCommand moveShape);
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
using AipsCore.Application.Abstract.MessageBroking;
|
||||
using AipsCore.Application.Common.Message.AddArrow;
|
||||
using AipsCore.Application.Common.Message.AddLine;
|
||||
using AipsCore.Application.Common.Message.AddRectangle;
|
||||
using AipsCore.Application.Common.Message.AddTextShape;
|
||||
using AipsCore.Application.Common.Message.MoveShape;
|
||||
using AipsCore.Application.Models.Shape.Command.CreateArrow;
|
||||
using AipsCore.Application.Models.Shape.Command.CreateLine;
|
||||
using AipsCore.Application.Models.Shape.Command.CreateRectangle;
|
||||
using AipsCore.Application.Models.Shape.Command.CreateTextShape;
|
||||
using AipsCore.Application.Models.Shape.Command.MoveShape;
|
||||
using AipsRT.Model.Whiteboard.Shapes;
|
||||
using AipsRT.Services.Interfaces;
|
||||
|
||||
@@ -33,4 +41,63 @@ public class MessagingService : IMessagingService
|
||||
|
||||
await _messagePublisher.PublishAsync(message);
|
||||
}
|
||||
|
||||
public async Task CreatedArrow(Guid whiteboardId, Arrow arrow)
|
||||
{
|
||||
var command = new CreateArrowCommand(
|
||||
arrow.Id.ToString(),
|
||||
whiteboardId.ToString(),
|
||||
arrow.OwnerId.ToString(),
|
||||
arrow.Position.X,
|
||||
arrow.Position.Y,
|
||||
arrow.Color,
|
||||
arrow.EndPosition.X,
|
||||
arrow.EndPosition.Y,
|
||||
arrow.Thickness);
|
||||
|
||||
var message = new AddArrowMessage(command);
|
||||
|
||||
await _messagePublisher.PublishAsync(message);
|
||||
}
|
||||
|
||||
public async Task CreateLine(Guid whiteboardId, Line line)
|
||||
{
|
||||
var command = new CreateLineCommand(
|
||||
line.Id.ToString(),
|
||||
whiteboardId.ToString(),
|
||||
line.OwnerId.ToString(),
|
||||
line.Position.X,
|
||||
line.Position.Y,
|
||||
line.Color,
|
||||
line.EndPosition.X,
|
||||
line.EndPosition.Y,
|
||||
line.Thickness);
|
||||
|
||||
var message = new AddLineMessage(command);
|
||||
|
||||
await _messagePublisher.PublishAsync(message);
|
||||
}
|
||||
|
||||
public async Task CreateTextShape(Guid whiteboardId, TextShape textShape)
|
||||
{
|
||||
var command = new CreateTextShapeCommand(
|
||||
textShape.Id.ToString(),
|
||||
whiteboardId.ToString(),
|
||||
textShape.OwnerId.ToString(),
|
||||
textShape.Position.X,
|
||||
textShape.Position.Y,
|
||||
textShape.Color,
|
||||
textShape.TextValue,
|
||||
textShape.TextSize);
|
||||
|
||||
var message = new AddTextShapeMessage(command);
|
||||
|
||||
await _messagePublisher.PublishAsync(message);
|
||||
}
|
||||
|
||||
public async Task MoveShape(MoveShapeCommand moveShape)
|
||||
{
|
||||
var message = new MoveShapeMessage(moveShape);
|
||||
await _messagePublisher.PublishAsync(message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user