/// <summary> /// GameOnComplete /// completion callback for Game() /// if true heal all players and start new game /// </summary> /// <param name="result"></param> private void GameOnComplete(IAsyncResult result) { bool iResult = false; GameOperation del; AsyncResult asyncObj = (AsyncResult)result; if (asyncObj.EndInvokeCalled == false) { del = (GameOperation)asyncObj.AsyncDelegate; iResult = del.EndInvoke(asyncObj); } asyncObj.AsyncWaitHandle.Close(); if (m_players.Count >= 5 && iResult) // if players agree to continue { GameOperation game = Game; AsyncCallback callback = GameOnComplete; foreach (var player in m_players) { player.Value.MaxHeal(); // heal players } m_boss = SelectBoss(); // select new boss game.BeginInvoke(callback, null); // async call to gamne } else { foreach (var user in m_clients) // remove all players from game { if (m_players.ContainsKey(user.Key.UserID)) { m_players.Remove(user.Key.UserID); } } } }
public void SelectHero(User user, Hero hero) { try { if (m_players.ContainsKey(user.UserID)) // change users hero { m_players[user.UserID] = hero; foreach (var item in m_clients) { item.Value.NotifyClient(user.UserName + " has joined the fray."); // notifies clients } } else if (m_players.Count < 12) // adds new player { m_players.Add(user.UserID, hero); foreach (var item in m_clients) { item.Value.NotifyClient(user.UserName + " has joined the fray."); // notifies clients } } else // notify the user that the serve is full { m_clients[user].NotifyClient("Server Full"); } if (m_players.Count == 5) // begin game { GameOperation gameDel = Game; AsyncCallback callback = this.GameOnComplete; gameDel.BeginInvoke(callback, null); // start game with async call } } catch (CommunicationException) { Console.WriteLine("Error Comunicating with client"); if (m_clients.ContainsKey(user)) // if there is an error communicating with the client { m_clients.Remove(user); // remove the client } } }