/// <summary> /// Adds a new user data contract to the user list and updates all active clients /// </summary> /// <param name="objUser"></param> public void Join(UserDC objUser) { try { // Add the new user object users.Add(objUser.ID, objUser); logger.logInfo(objUser.Name + " joined the game"); // Update all existing clients updateAllClients(false, false); } catch (Exception ex) { logger.logError(ex.Message); throw ex; } }
/// <summary> /// Returns a string of the winner's name and score for the current game /// </summary> /// <param name="endGame"></param> /// <param name="noMoreCards"></param> /// <returns></returns> private string getWinner(bool endGame, bool noMoreCards) { try { // Game has ended if (endGame || noMoreCards) { UserDC highestUser = new UserDC("highestUser"); // Each user in the game foreach (var user in users.Values) { // New highest score if (highestUser.Score < user.Score) { highestUser = user; } } string result = ""; // No score attained if (highestUser.Score == 0) { result = "Nobody wins, please play again"; } else if (users.Count > 1) { // Ran out of cards if (noMoreCards) { result = "End of deck, " + highestUser.Name + " wins with a score of " + highestUser.Score + "!"; } // Normal condition else { result = "The winner is " + highestUser.Name + " with a score of " + highestUser.Score + "!"; } } else { // Ran out of cards if (noMoreCards) { result = "End of deck, you scored " + highestUser.Score; } // Normal condition else { result = "You scored " + highestUser.Score; } // User got a good score if (highestUser.Score > 40) { result += ", Good Job!"; } } logger.logInfo("Game Over: \"" + result + "\""); return result; } // Game continues else { return ""; } } catch (Exception ex) { logger.logError(ex.Message); throw ex; } }
/// <summary> /// Begin service negotations /// </summary> /// <param name="username"></param> public void join(string username) { try { // Create new user object objUser = new UserDC(username); objUser.ID = clientCallbackId; // Join the server remoteProxy.Join(objUser); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }