private void ConnectToServer() { Console.WriteLine(ClientUI.Connecting()); bool connected; do { Entities.Client client = AskForCredentials(); SocketConnection = clientProtocol.ConnectToServer(); object[] request = BuildRequest(Command.Login, client.Username, client.Password); SocketConnection.SendMessage(request); var response = new Response(SocketConnection.ReadMessage()); connected = response.HadSuccess(); if (connected) { clientToken = response.GetClientToken(); clientUsername = client.Username; ClientUI.LoginSuccessful(); } else { Console.WriteLine(response.ErrorMessage()); } } while (!connected); }
internal void LoopMenu() { Init(); bool exit = false; while (!exit) { Console.WriteLine(ClientUI.Title(clientUsername)); int option = Menus.ClientControllerLoopMenu(); if (option == 4) { exit = true; } MapOptionToActionOfMainMenu(option); if (!exitGame) { ClientUI.Clear(); } if (exitGame) { ClientUI.ClearBoard(); } exitGame = false; } }
private Entities.Client AskForCredentials() { ClientUI.LoginTitle(); ClientUI.InsertUsername(); string username = Input.RequestUsernameAndPassword("Insert Username: "******"Insert Password: "); return(new Entities.Client(username, password)); }
private void Play() { exitGame = false; timesOut = false; LastPlayerWantsToLeave = false; int input = Menus.SelectRoleMenu(); input--; string role = ""; if (input == 0) { role = "Monster"; } if (input == 1) { role = "Survivor"; } if (input == 2) { goto End; } SocketConnection.SendMessage(BuildRequest(Command.SelectRole, role)); var response = new Response(SocketConnection.ReadMessage()); if (response.HadSuccess()) { Console.WriteLine("You are now a " + role); ClientUI.Clear(); JoinGame(); goto End; } else { Console.WriteLine(response.ErrorMessage()); } End :; }
private bool ListConnectedClients() { bool serverHasClients; object[] request = BuildRequest(Command.ListConnectedClients); SocketConnection.SendMessage(request); var response = new Response(SocketConnection.ReadMessage()); if (response.HadSuccess()) { ClientUI.TheseAreTheConnectedPlayers(); List <string> connectedClients = response.UserList(); PrintPlayers(connectedClients); serverHasClients = connectedClients.Count > 0; } else { Console.WriteLine(response.ErrorMessage()); serverHasClients = false; } return(serverHasClients); }
private void Init() { Console.WriteLine(ClientUI.Title()); ConnectToServer(); ClientUI.Clear(); }
private void JoinGame() { SocketConnection.SendMessage(BuildRequest(Command.JoinGame)); var response = new Response(SocketConnection.ReadMessage()); if (response.HadSuccess()) { BoardUI.DrawBoard(clientUsername, response.GetPlayerPosition()); Console.WriteLine("Action: "); if (timer == null) { timer = new Thread(() => TimesOut()); timer.Start(); } while (!exitGame && !timesOut) { string myAction = Input.RequestInput(); if (timesOut) { goto End; } if (myAction.Equals("exit")) { RemovePlayerFromGame(); exitGame = true; } else { SocketConnection.SendMessage(BuildRequest(Command.DoAction, myAction)); var sendActionResponse = new Response(SocketConnection.ReadMessage()); if (sendActionResponse.HadSuccess()) { List <string> actionResponse = sendActionResponse.GetDoActionResponse(); RefreshBoard(actionResponse); ShowIfGameFinished(actionResponse, false); } else if (sendActionResponse.IsInvalidAction()) { Console.WriteLine(sendActionResponse.ErrorMessage()); Console.WriteLine("Action: "); } else if (sendActionResponse.PlayerIsDead()) { Console.WriteLine(sendActionResponse.ErrorMessage()); string st = AskServerIfGameHasFinished(); if (st != "GameFinished") { Console.WriteLine("Please wait until game has finished. Type any key to continue..."); } else { goto End; } } else { Console.WriteLine(sendActionResponse.ErrorMessage()); RemovePlayerFromGame(); } } } ClientUI.Clear(); } else if (response.GameIsFull()) { Console.WriteLine(response.ErrorMessage()); } else { Console.WriteLine(response.ErrorMessage()); string aux = AskServerIfGameHasFinished(); } End :; }
private void UploadImage(string username) { AskForPath: ClientUI.InsertAvatarPath(); string path = Input.RequestInput(); if (path.Equals("EXIT", StringComparison.OrdinalIgnoreCase)) { goto End; } path = Path.Combine(path, ""); const int CHUNK_SIZE = 9999; try { FileInfo fileInfo = new FileInfo(path); string extension = fileInfo.Extension; int totalLength = (int)fileInfo.Length; Command command = Command.SendPicturePart; double splittingTimes = (double)totalLength / CHUNK_SIZE; SocketConnection.SendMessage(BuildRequest(Command.ReadyToSendPicture, username, totalLength, extension)); var response = new Response(SocketConnection.ReadMessage()); if (response.HadSuccess()) { using (FileStream fs = fileInfo.OpenRead()) { var read = 0; while (read < totalLength) { if (splittingTimes < 1 || splittingTimes == 1) { command = Command.SendLastPicturePart; } byte[] parts = new byte[CHUNK_SIZE]; read += fs.Read(parts, 0, CHUNK_SIZE); var stringBasedPart = Convert.ToBase64String(parts); SocketConnection.SendMessage(BuildRequest(command, stringBasedPart)); var keepSendingResponse = new Response(SocketConnection.ReadMessage()); splittingTimes--; if (!keepSendingResponse.HadSuccess()) { Console.WriteLine("\n -> Picture coudn't be uploaded\n"); goto End; } } } Console.WriteLine("\n -> Upload was successful\n"); } else { Console.WriteLine(response.ErrorMessage()); } } catch (Exception e) { Console.WriteLine("\n -> The path is invalid.\n Please try again or type 'exit' to leave.\n"); goto AskForPath; } End :; }