public static void ShowRegisteredMenu(ClientPlayer player) { MENU_CUSTOM_WELCOME = $"Hello, {player.Name}, you have {player.Chips} chips!"; Console.WriteLine(MENU_CUSTOM_WELCOME); MenuUtils.ShowColoredArt(); Console.WriteLine(MENU_PROMPT + MENU_CHANGE_USER + MENU_UNREGISTER + MENU_SHOW_GAMES + MENU_JOIN_GAME + MENU_CREATE_GAME + MENU_LEADERBOARD + MENU_EXIT); }
private void UnregisterUser() { if (Player == null) { Console.WriteLine("You are not registered"); return; } string jsonRequest = JsonConvert.SerializeObject(new Packet(Command.UNREGISTER_USER, true)); WritePacket(socket, jsonRequest); Packet packet = ReadPacket(socket); if (packet.Success) { Player = null; DebugUtils.WriteLine("[CLIENT] Account Deactivated"); MenuUtils.ShowUnregisteredMenu(); } else { DebugUtils.WriteLine("[CLIENT] Error in deactivating a user's account"); } }
private void RegisterUser() { if (Player != null) { Console.WriteLine("You are already registered"); return; } string username; do { Console.Write("Enter a username: "******"Invalid username. Please enter a username"); continue; } break; }while (true); string jsonRequest = JsonConvert.SerializeObject(new Packet(Command.REGISTER_USER, true, new List <object>() { username })); WritePacket(socket, jsonRequest); Packet packet = ReadPacket(socket); if (packet.Success) { Player = JsonConvert.DeserializeObject <ClientPlayer>(packet.DataToString()[0]); DebugUtils.WriteLine("[CLIENT] Player successfully registered"); MenuUtils.ShowRegisteredMenu(Player); } else { DebugUtils.WriteLine("[CLIENT] Error in registering a user"); } }
public static void ShowUnregisteredMenu() { Console.WriteLine(MENU_WELCOME); MenuUtils.ShowColoredArt(); Console.WriteLine(MENU_PROMPT + MENU_REGISTER + MENU_LEADERBOARD + MENU_EXIT); }
private void GameLoop() { DebugUtils.WriteLine($"[CLIENT] {Player.Name} in now in their GameLoop!"); bool isGameOngoing = true; while (isGameOngoing) { Packet packet = ReadPacket(socket); switch (packet.Command) { case Command.REQUEST_MOVE: // Clear any previous user input. while (Console.KeyAvailable) { Console.ReadKey(false); } Console.WriteLine($"\n{Player.Name}, you have {Player.Chips} chips...\nYour hand is:"); Player.Hand.ShowHand(); bool isValidChoice = false; int choice = -1; while (!isValidChoice) { Console.WriteLine("\n\nSTAY, FOLD, or RAISE? \nTo RAISE type 'RAISE n' where n is the number you want to raise by."); string input = Console.ReadLine().ToLower(); if (!String.IsNullOrEmpty(input)) { List <string> stringTokens = input.Split(' ').ToList(); string playerChoice = stringTokens[0]; switch (playerChoice) { case "fold": choice = -1; isValidChoice = true; break; case "stay": choice = 0; isValidChoice = true; break; case "raise": if (stringTokens.Count != 2) { Console.WriteLine("Please enter a valid number of chips."); break; } try { choice = int.Parse(stringTokens[1]); } catch (Exception) { Console.WriteLine("Please enter a valid number of chips."); break; } if (choice <= 0) { Console.WriteLine("You must bet at least 1 chip."); break; } if (choice > Player.Chips) { Console.WriteLine("You have insufficient funds to place this bet, please try again."); break; } isValidChoice = true; break; default: Console.WriteLine("Invalid call made. Try again."); break; } } } string jsonResponse = JsonConvert.SerializeObject(new Packet(Command.SEND_MOVE, true, new List <object>() { choice })); WritePacket(socket, jsonResponse); // If player folds, exit the function. if (choice == -1) { isGameOngoing = false; } break; case Command.GIVE_CARD: Card card = JsonConvert.DeserializeObject <Card>(packet.DataToString()[0]); Player.Hand.AddCard(card); Console.WriteLine("You received a card."); Player.Hand.ShowHand(); Console.WriteLine(); break; case Command.ADJUST_CHIPS: int chips = JsonConvert.DeserializeObject <int>(packet.DataToString()[0]); Player.Chips = chips; break; case Command.DISPLAY_MESSAGE: string message = JsonConvert.DeserializeObject <string>(packet.DataToString()[0]); Console.WriteLine(message); break; case Command.SHOW_HANDS: List <KeyValuePair <string, List <Card> > > players = JsonConvert.DeserializeObject <List <KeyValuePair <string, List <Card> > > >(packet.DataToString()[0]); foreach (KeyValuePair <string, List <Card> > player in players) { Console.Write(player.Key + " has "); Hand playerHand = new Hand(player.Value); playerHand.ShowHand(); Console.WriteLine(); } break; case Command.ANNOUCE_WINNER: string winnerName = JsonConvert.DeserializeObject <string>(packet.DataToString()[0]); List <Card> winnerCards = JsonConvert.DeserializeObject <List <Card> >(packet.DataToString()[1]); string winnerWinnings = JsonConvert.DeserializeObject <string>(packet.DataToString()[2]); // Only print the ranking if they finished the entire round and have five cards. if (winnerCards.Count == 5) { Console.Write($"\nThe winner is... {winnerName}! Their winning hand is "); Hand winnerHand = new Hand(winnerCards); winnerHand.PrintRanking(); } Console.WriteLine(winnerWinnings); isGameOngoing = false; break; case Command.REMOVE_PLAYER: string removeMessage = JsonConvert.DeserializeObject <string>(packet.DataToString()[0]); Console.WriteLine(removeMessage); isGameOngoing = false; break; } } // Empty the player's hand for further games Player.Hand.ClearHand(); Console.WriteLine("\nPress any key to return to main menu."); Console.ReadLine(); MenuUtils.ShowRegisteredMenu(Player); }
public void Run() { try { ConnectToServers(); MenuUtils.ShowUnregisteredMenu(); while (true) { Command command = GetUserMenuInput(); switch (command) { case Command.REGISTER_USER: RegisterUser(); break; case Command.CHANGE_NAME: ChangeName(); break; case Command.UNREGISTER_USER: UnregisterUser(); break; case Command.SHOW_GAMES: ShowGames(); break; case Command.JOIN_GAME: JoinGame(); break; case Command.CREATE_GAME: CreateGame(); break; case Command.VIEW_LEADERBOARD: ViewLeaderboard(); break; case Command.CHEAT_CODE: CheatCode(); break; case Command.CLOSE_SOCKET: CloseSocket(); return; } } } catch (SocketException) { Console.WriteLine("Could not connect to any servers. Please try again later."); Console.ReadLine(); } catch (Exception e) { Console.WriteLine($"[!] Error {e.GetType().Name}\n {e.StackTrace} \nPress any key to exit..."); Console.ReadLine(); } }