122 lines
4.5 KiB
C#
122 lines
4.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Collections.Concurrent;
|
|
using System;
|
|
|
|
namespace Sledgemapper.Shared.Entities
|
|
{
|
|
public class TileAddedEventArgs : EventArgs
|
|
{
|
|
public Tile Tile { get; set; }
|
|
public TileAddedEventArgs(Tile tile) => Tile = tile;
|
|
}
|
|
|
|
public class OverlayAddedEventArgs : EventArgs
|
|
{
|
|
public Overlay Overlay { get; set; }
|
|
public OverlayAddedEventArgs(Overlay overlay) => Overlay = overlay;
|
|
}
|
|
|
|
public class SessionData
|
|
{
|
|
public event EventHandler<TileAddedEventArgs> RaiseTileAddedEvent;
|
|
public event EventHandler<OverlayAddedEventArgs> RaiseOverlayAddedEvent;
|
|
|
|
public SessionData()
|
|
{
|
|
Map = new ConcurrentDictionary<string, Tile>();
|
|
Overlays = new ConcurrentDictionary<string, Overlay>();
|
|
Walls = new ConcurrentDictionary<string, Wall>();
|
|
Players = new List<Player>();
|
|
Colors = new List<string>();
|
|
}
|
|
|
|
public ConcurrentDictionary<string, Tile> Map { get; set; }
|
|
public ConcurrentDictionary<string, Wall> Walls { get; set; }
|
|
public ConcurrentDictionary<string, Overlay> Overlays { get; set; }
|
|
public bool IsValid { get; set; }
|
|
public List<Player> Players { get; set; }
|
|
public List<string> Colors;
|
|
|
|
public void NewTile(Tile selectedTile, string tileId)
|
|
{
|
|
var tileExist = Map.TryGetValue(selectedTile.ToString(), out var tile);
|
|
Tile newTile;
|
|
if (tileExist)
|
|
{
|
|
Map.TryRemove(tile.ToString(), out var _);
|
|
if (tile.ID == tileId)
|
|
{
|
|
newTile = new Tile { X = selectedTile.X, Y = selectedTile.Y, ID = tileId, Rotation = (tile.Rotation + 1) % 4 };
|
|
}
|
|
else
|
|
{
|
|
newTile = new Tile { X = selectedTile.X, Y = selectedTile.Y, ID = tileId };
|
|
}
|
|
}
|
|
else
|
|
{
|
|
newTile = new Tile { X = selectedTile.X, Y = selectedTile.Y, ID = tileId };
|
|
}
|
|
|
|
Map.TryAdd(newTile.ToString(), newTile);
|
|
|
|
OnRaiseTileAddedEvent(new TileAddedEventArgs(newTile));
|
|
}
|
|
|
|
public void NewOverlay(Overlay selectedOverlay, string tileId)
|
|
{
|
|
var overlayExist = Overlays.TryGetValue(selectedOverlay.ToString(), out var overlay);
|
|
Overlay newOverlay;
|
|
if (overlayExist)
|
|
{
|
|
Overlays.TryRemove(overlay.ToString(), out var rrtile);
|
|
if (overlay.ID == tileId)
|
|
{
|
|
newOverlay = new Overlay { X = overlay.X, Y = overlay.Y, ID = tileId, Intersection = overlay.Intersection, Rotation = (overlay.Rotation + 1) % 4 };
|
|
}
|
|
else
|
|
{
|
|
newOverlay = new Overlay { X = selectedOverlay.X, Y = selectedOverlay.Y, ID = tileId, Intersection = selectedOverlay.Intersection };
|
|
}
|
|
}
|
|
else
|
|
{
|
|
newOverlay = new Overlay { X = selectedOverlay.X, Y = selectedOverlay.Y, ID = tileId, Intersection = selectedOverlay.Intersection };
|
|
}
|
|
|
|
Overlays.TryAdd(newOverlay.ToString(), newOverlay);
|
|
OnRaiseOverlayAddedEvent(new OverlayAddedEventArgs(newOverlay));
|
|
|
|
}
|
|
|
|
protected virtual void OnRaiseTileAddedEvent(TileAddedEventArgs e)
|
|
{
|
|
// Make a temporary copy of the event to avoid possibility of
|
|
// a race condition if the last subscriber unsubscribes
|
|
// immediately after the null check and before the event is raised.
|
|
EventHandler<TileAddedEventArgs> raiseEvent = RaiseTileAddedEvent;
|
|
|
|
// Event will be null if there are no subscribers
|
|
if (raiseEvent != null)
|
|
{
|
|
// Call to raise the event.
|
|
raiseEvent(this, e);
|
|
}
|
|
}
|
|
|
|
protected virtual void OnRaiseOverlayAddedEvent(OverlayAddedEventArgs e)
|
|
{
|
|
// Make a temporary copy of the event to avoid possibility of
|
|
// a race condition if the last subscriber unsubscribes
|
|
// immediately after the null check and before the event is raised.
|
|
EventHandler<OverlayAddedEventArgs> raiseEvent = RaiseOverlayAddedEvent;
|
|
|
|
// Event will be null if there are no subscribers
|
|
if (raiseEvent != null)
|
|
{
|
|
// Call to raise the event.
|
|
raiseEvent(this, e);
|
|
}
|
|
}
|
|
}
|
|
}
|