61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using GameModel;
|
|
using GameModel.Contract;
|
|
using GameModel.Contracts;
|
|
using GameModel.FreeGame;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GameHandler.GameHandler
|
|
{
|
|
public class FreeGameHandler : IGameHandler
|
|
{
|
|
public const string GAMENAME_FREETRAINING = "Freies Spiel";
|
|
|
|
public event EventHandler GameExpenseOccured;
|
|
|
|
public static string GameName()
|
|
{
|
|
return GAMENAME_FREETRAINING;
|
|
}
|
|
|
|
public int GetCurrentPlayerId(IGameModel gameModel)
|
|
{
|
|
var gm = gameModel as FreeGameModel;
|
|
return gm.LastPlayerId;
|
|
}
|
|
|
|
public IGameModel InitGameModel(int[] playerIds, IGameSettings gameSettings)
|
|
{
|
|
var throws = new List<FreeGameThrow>(playerIds.Select(_ => FreeGameThrow.Create(_))).ToArray();
|
|
return new FreeGameModel(playerIds, playerIds.First(), throws);
|
|
}
|
|
|
|
public IGameModel Update(PinThrow pinThrow, IGameModel gameModel, BoardState boardStateBeforeUpdate,
|
|
ThrowState throwStateAfterUpdate, Guid gameStateId)
|
|
{
|
|
if (gameModel == null)
|
|
{
|
|
throw new ArgumentNullException("gameModel cannto be null");
|
|
}
|
|
|
|
var gm = gameModel as FreeGameModel;
|
|
|
|
var throws = gm.Throws.ToList();
|
|
var model = throws.FirstOrDefault(_ => _.PlayerId == pinThrow.PlayerId);
|
|
if (model == null)
|
|
{
|
|
throw new FreeGameThrowExcepetion("game model not initialized");
|
|
}
|
|
var model2 = model + pinThrow;
|
|
var idx = throws.IndexOf(model);
|
|
throws.Remove(model);
|
|
throws.Insert(idx, model2);
|
|
|
|
return gm with { LastPlayerId = pinThrow.PlayerId, Throws = throws.ToArray() };
|
|
}
|
|
}
|
|
}
|