using System; using System.Collections.Generic; namespace NJDOTNET.Trivia { public abstract class GameServer : IGameServer { protected GameController controller; protected Question currentQuestion; protected readonly List clients = new List(); /// /// Gets the currently-connected clients. /// /// The currently-connected clients. public IEnumerable Clients { get { return clients; } } /// /// Gets the current game client for this request. /// /// The current game client. public abstract IGameClient GameClient { get; } /// /// Create a new game controller. /// /// A new game controller protected abstract GameController CreateNewGameController(); /// /// Starts the current game! /// public virtual void Start() { // If the game controller hasn't been created, // create a new game. if (controller == null) NewGame(); // Kick out the jams! controller.Start(); } /// /// Create a new game /// public virtual void NewGame() { // Create a new controller for this game controller = CreateNewGameController(); // Wire up controller events controller.OnGameStarted += OnGameStarted; controller.OnGameEnded += OnGameEnded; controller.OnNextQuestion += OnNextQuestion; } /// /// Registers the player to the current game. /// /// The player to register. public virtual void RegisterPlayer(Player player) { var client = GameClient; if (!clients.Contains(client)) clients.Add(client); controller.AddPlayer(player); } /// /// Gets the current question. /// /// The current question public virtual Question GetCurrentQuestion() { return currentQuestion; } /// /// Submits an answer. /// /// The answer. /// public virtual AnswerSubmissionResponse SubmitAnswer(Answer answer) { // TODO: Handle QuestionOption submission... throw new NotImplementedException(); } protected virtual void OnGameStarted(object sender, EventArgs e) { foreach (var client in Clients) client.HandleGameStart(e.Value); } protected virtual void OnGameEnded(object sender, EventArgs e) { foreach (var client in Clients) client.HandleGameEnd(e.Value); } protected virtual void OnNextQuestion(object sender, EventArgs e) { currentQuestion = e.Value; foreach (var client in Clients) client.HandleQuestionChanged(currentQuestion); } } }