👔 Complete #26 -- Code PlayerManager
continuous-integration/drone/push Build is passing Details

pull/43/head
Alexis Drai 3 years ago
parent 3c2799d363
commit 5c3dccfa04

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -8,25 +10,84 @@ namespace Model
{ {
public class PlayerManager : IManager<Player> public class PlayerManager : IManager<Player>
{ {
public Player Add(ref Player toAdd) /// <summary>
/// a hashset of the players that this manager is in charge of
/// <br/>
/// the hash is based on the player's unique property (name)
/// </summary>
private readonly HashSet<Player> players;
public PlayerManager()
{ {
throw new NotImplementedException(); players = new HashSet<Player>();
} }
public IEnumerable<Player> GetAll() /// <summary>
/// add a new player
/// </summary>
/// <param name="toAdd">player to be added</param>
/// <returns>added player, or null if <paramref name="toAdd"/> was null</returns>
public Player Add(ref Player toAdd)
{ {
throw new NotImplementedException(); if (toAdd != null)
{
players.Add(toAdd);
}
return toAdd;
} }
/// <summary>
/// may never get implemented in the model, go through GetOneByName() in the meantime
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public Player GetOneById(int id) public Player GetOneById(int id)
{ {
throw new NotImplementedException(); throw new NotImplementedException("may never get implemented\ngo through GetOneByName() in the meantime");
} }
public void Remove(ref Player toRemove)
/// <summary>
/// finds the player with that name and returns A COPY OF IT
/// <br/>
/// that copy does not belong to this manager's players, so it should not be modified
/// </summary>
/// <param name="name">a player's unique name</param>
/// <returns>player with said name</returns>
public Player GetOneByName(string name)
{ {
throw new NotImplementedException(); if (!String.IsNullOrEmpty(name))
{
Player result = players.Where(p => p.Name.Equals(name)).FirstOrDefault();
return new Player(result.Name); // will return null if no such player exists
}
return null; // we also want ot return null if no name was provided
} }
/// </summary>
/// get a READ ONLY enumerable of all players belonging to this manager
/// </summary>
/// <returns>a readonly list of all this manager's players</returns>
public IEnumerable<Player> GetAll() => players.AsEnumerable();
/// <summary>
/// update a player from <paramref name="before"/> to <paramref name="after"/>
/// </summary>
/// <param name="before">player to be updated</param>
/// <param name="after">player in the state that it needs to be in after the update</param>
/// <returns>updated player</returns>
public Player Update(ref Player before, ref Player after) public Player Update(ref Player before, ref Player after)
{ {
throw new NotImplementedException(); Remove(ref before);
return Add(ref after);
}
/// <summary>
/// remove a player
/// </summary>
/// <param name="toRemove">player to be removed</param>
public void Remove(ref Player toRemove)
{
players.Remove(toRemove);
} }
} }
} }

Loading…
Cancel
Save