refactoring

This commit is contained in:
Michele Scandura 2020-11-05 20:03:01 +00:00
parent c293490995
commit 4dfbcff460
14 changed files with 240 additions and 82 deletions

View file

@ -0,0 +1,19 @@
using Sledgemapper.Shared.Entities;
using System;
using System.Threading.Tasks;
namespace Sledgemapper.Clients
{
public interface ISledgemapperClient
{
Task NewTile(Tile tile);
Task NewWall(Wall wall);
Task NewOverlay(Overlay overlay);
Task DeleteTile(Tile tile);
Task DeleteWall(Wall wall);
Task DeleteOverlay(Overlay overlay);
Task NewPlayer(Player player);
Task PlayerUpdate(Player player);
Task UpdateMap(SessionData player);
}
}

View file

@ -0,0 +1,18 @@
namespace Sledgemapper.Shared.Entities
{
public class Overlay
{
public int X { get; set; }
public int Y { get; set; }
public string ID { get; set; }
public bool Intersection { get; set; }
public int Rotation { get; set; }
public override string ToString()
{
return $"{X}_{Y}_{Intersection}";
}
}
}

View file

@ -0,0 +1,11 @@
namespace Sledgemapper.Shared.Entities
{
public class Player
{
public string ConnectionId { get; set; }
public string Color { get; set; }
public string Initials { get; set; }
public Tile Position { get; set; }
}
}

View file

@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Collections.Concurrent ;
namespace Sledgemapper.Shared.Entities
{
public class SessionData
{
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;
}
}

View file

@ -0,0 +1,19 @@
namespace Sledgemapper.Shared.Entities
{
public class Tile
{
public int X { get; set; }
public int Y { get; set; }
public string ID { get; set; }
public int Rotation { get; set; }
public override string ToString()
{
return $"{X}_{Y}";
}
}
}

View file

@ -0,0 +1,16 @@
namespace Sledgemapper.Shared.Entities
{
public class Wall
{
public int X { get; set; }
public int Y { get; set; }
public string ID { get; set; }
public int Rotation { get; set; }
public override string ToString()
{
return $"{X}_{Y}";
}
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
namespace SignalRChat.Hubs
{
public static class ExtensionMethods
{
private static Random rng = new Random();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
}

View file

@ -0,0 +1,141 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading;
using System.Diagnostics;
namespace System.Collections.Concurrent
{
/// <summary>
/// Provides a thread-safe dictionary for use with data binding.
/// </summary>
/// <typeparam name="TKey">Specifies the type of the keys in this collection.</typeparam>
/// <typeparam name="TValue">Specifies the type of the values in this collection.</typeparam>
[DebuggerDisplay("Count={Count}")]
public class ObservableConcurrentDictionary<TKey, TValue> :
ICollection<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>,
INotifyCollectionChanged, INotifyPropertyChanged
{
private readonly SynchronizationContext _context;
private readonly ConcurrentDictionary<TKey, TValue> _dictionary;
/// <summary>
/// Initializes an instance of the ObservableConcurrentDictionary class.
/// </summary>
public ObservableConcurrentDictionary()
{
_context = AsyncOperationManager.SynchronizationContext;
_dictionary = new ConcurrentDictionary<TKey, TValue>();
}
/// <summary>Event raised when the collection changes.</summary>
public event NotifyCollectionChangedEventHandler CollectionChanged;
/// <summary>Event raised when a property on the collection changes.</summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Notifies observers of CollectionChanged or PropertyChanged of an update to the dictionary.
/// </summary>
private void NotifyObserversOfChange()
{
var collectionHandler = CollectionChanged;
var propertyHandler = PropertyChanged;
_context.Post(s =>
{
collectionHandler?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
propertyHandler?.Invoke(this, new PropertyChangedEventArgs("Count"));
propertyHandler?.Invoke(this, new PropertyChangedEventArgs(nameof(Keys)));
propertyHandler?.Invoke(this, new PropertyChangedEventArgs(nameof(Values)));
}, null);
}
/// <summary>Attempts to add an item to the dictionary, notifying observers of any changes.</summary>
/// <param name="item">The item to be added.</param>
/// <returns>Whether the add was successful.</returns>
private bool TryAddWithNotification(KeyValuePair<TKey, TValue> item) => TryAddWithNotification(item.Key, item.Value);
/// <summary>Attempts to add an item to the dictionary, notifying observers of any changes.</summary>
/// <param name="key">The key of the item to be added.</param>
/// <param name="value">The value of the item to be added.</param>
/// <returns>Whether the add was successful.</returns>
private bool TryAddWithNotification(TKey key, TValue value)
{
bool result = _dictionary.TryAdd(key, value);
if (result) NotifyObserversOfChange();
return result;
}
/// <summary>Attempts to remove an item from the dictionary, notifying observers of any changes.</summary>
/// <param name="key">The key of the item to be removed.</param>
/// <param name="value">The value of the item removed.</param>
/// <returns>Whether the removal was successful.</returns>
private bool TryRemoveWithNotification(TKey key, out TValue value)
{
bool result = _dictionary.TryRemove(key, out value);
if (result) NotifyObserversOfChange();
return result;
}
/// <summary>Attempts to add or update an item in the dictionary, notifying observers of any changes.</summary>
/// <param name="key">The key of the item to be updated.</param>
/// <param name="value">The new value to set for the item.</param>
/// <returns>Whether the update was successful.</returns>
private void UpdateWithNotification(TKey key, TValue value)
{
_dictionary[key] = value;
NotifyObserversOfChange();
}
#region ICollection<KeyValuePair<TKey,TValue>> Members
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) => TryAddWithNotification(item);
void ICollection<KeyValuePair<TKey, TValue>>.Clear()
{
((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Clear();
NotifyObserversOfChange();
}
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) => ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Contains(item);
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) => ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).CopyTo(array, arrayIndex);
int ICollection<KeyValuePair<TKey, TValue>>.Count => ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Count;
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).IsReadOnly;
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) => TryRemoveWithNotification(item.Key, out _);
#endregion
#region IEnumerable<KeyValuePair<TKey,TValue>> Members
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).GetEnumerator();
#endregion
#region IDictionary<TKey,TValue> Members
public void Add(TKey key, TValue value) => TryAddWithNotification(key, value);
public bool ContainsKey(TKey key) => _dictionary.ContainsKey(key);
public ICollection<TKey> Keys => _dictionary.Keys;
public bool Remove(TKey key) => TryRemoveWithNotification(key, out _);
public bool TryGetValue(TKey key, out TValue value) => _dictionary.TryGetValue(key, out value);
public ICollection<TValue> Values => _dictionary.Values;
public TValue this[TKey key]
{
get => _dictionary[key];
set => UpdateWithNotification(key, value);
}
#endregion
}
}

View file

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>