using GameContract; using GameModel; using GameModel.Exceptions; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace GameHandler { public class GameService { public const int INFINIT_THROWS = 9999; private bool _isStarted = false; private ThrowHandler _th; private ThrowState _lastState; public GameService() { LoadGameHandler(); } public string ThrowModeName { get { if (_lastState != null) { return _lastState.BoardState.ThrowMode == ThrowMode.Decrease ? "Abräumen" : "Volle"; } return string.Empty; } } public ThrowState Start(string gameName) // TODO: add players { if (_isStarted) { throw new InvalidGameStateExcpetion("Game already started"); } if (!_isStarted) { _isStarted = true; } _th = new ThrowHandler(); var throwMode = GameTypeId == 0 ? ThrowMode.Reposition : ThrowMode.Decrease; var throwsPerRount = GameTypeId == 0 ? INFINIT_THROWS : 3; _lastState = ThrowState.Create(throwMode, throwsPerRount); return _lastState; } public ThrowState HandleThrow(PinThrow pinThrow) { if (!_isStarted) { throw new InvalidGameStateExcpetion("Game not started"); } _lastState = _th.Update(_lastState, pinThrow); return _lastState; } public void Stop() { if (!_isStarted) { throw new InvalidGameStateExcpetion("Game not started"); } _isStarted = false; } public static Dictionary LoadGameHandler() { var res = new Dictionary(); var type = typeof(IGameHandler); var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(p => type.IsAssignableFrom(p) && (!p.IsInterface)); foreach (var item in types) { MethodInfo staticMethodInfo = item.GetMethod("GetGameName"); string returnValue = Convert.ToString(staticMethodInfo.Invoke(null, new object[] { })); res.Add(returnValue, item); } return res; } } }