From 214382c7879fe22d401ca39845fa07be9eda8c94 Mon Sep 17 00:00:00 2001 From: Ethan Rylko Date: Tue, 3 Oct 2023 19:03:19 -0400 Subject: [PATCH] Mega iffy client update --- .../NetWorth/client/ClientInterface.hpp | 108 +++++++++++++++++- .../Interfaces/NetWorth/client/clientmain.cpp | 76 +----------- 2 files changed, 111 insertions(+), 73 deletions(-) diff --git a/source/Interfaces/NetWorth/client/ClientInterface.hpp b/source/Interfaces/NetWorth/client/ClientInterface.hpp index 2d6024fd..caf70c35 100644 --- a/source/Interfaces/NetWorth/client/ClientInterface.hpp +++ b/source/Interfaces/NetWorth/client/ClientInterface.hpp @@ -11,8 +11,10 @@ #include #include #include +#include #include "../NetworkInterface.hpp" +#include "../../TrashInterface.hpp" using namespace sf; @@ -20,14 +22,118 @@ namespace cse491 { namespace netWorth{ class ClientInterface : public NetworkingInterface { private: + std::unique_ptr mTrash; /// Display interface + + sf::IpAddress mIp; /// Destination IP address + sf::UdpSocket mSocket; /// UDP socket for sending and receiving + unsigned short mPort; /// Destination port protected: public: - ClientInterface(size_t id, const std::string & name) : NetworkingInterface(id, name) { } + /** + * ClientInterface constructor (NetworkingInterface superclass) + * @param id Interface identifier + * @param name Interface name + * @param ip_string String for destination IP address, make into IpAddress object + * @param port Destination port number + */ + ClientInterface(size_t id, const std::string & name, + const std::string & ip_string, + unsigned short port) : NetworkingInterface(id, name) { + mIp = sf::IpAddress(ip_string); + mPort = port; + } + + /** + * Default destructor + */ ~ClientInterface() = default; + /** + * Establish connection with server + * @return True if successful, false if error + */ + bool EstablishConnection() { + // send ping to server + std::string message = "Ping!"; + if (mSocket.send(message.c_str(), message.size() + 1, mIp, mPort) != Socket::Status::Done) { + std::cout << "Could not connect to " << mIp << " at port " << mPort << std::endl; + return false; + } + + // receive pong from server + size_t received; + char data[100]; // TODO: Make packets instead of C strings + if (mSocket.receive(data, 100, received, mIp, mPort) != sf::Socket::Done) + { + std::cout << "Failure to receive" << std::endl; + return false; + } + std::cout << data << std::endl; + + return true; + } + + /** + * Game loop + */ + void Loop() { + char input; + bool valid_input = false; + bool wait_for_input = true; + sf::Packet recv_pkt; + std::string recv_str; + + // receive initial map + if (mSocket.receive(recv_pkt, mIp, mPort) != sf::Socket::Done) + { + std::cout << "Failure to receive" << std::endl; + return; + } + + recv_pkt >> recv_str; + std::cout << recv_str; + + while (input != 'q') + { + do { + std::cin >> input; + } while (!std::cin && wait_for_input); + + switch (input) { + case 'w': case 'W': valid_input = true; break; + case 'a': case 'A': valid_input = true; break; + case 's': case 'S': valid_input = true; break; + case 'd': case 'D': valid_input = true; break; + case 'q': case 'Q': valid_input = true; break; + default: valid_input = false; + } + + // If we waited for input, but don't understand it, notify the user. + if (wait_for_input && !valid_input) { + std::cout << "Unknown key '" << input << "'." << std::endl; + } else { + if (mSocket.send(&input, 1, mIp, mPort) != Socket::Status::Done) { + std::cout << "Could not connect to " << mIp << " at port " << mPort << std::endl; + return; + } + + if (mSocket.receive(recv_pkt, mIp, mPort) != sf::Socket::Done) + { + std::cout << "Failure to receive" << std::endl; + return; + } + + recv_pkt >> recv_str; + std::cout << recv_str; + } + + valid_input = false; + } + + } }; //End of ClientInterface }// End of namespace NetWorth diff --git a/source/Interfaces/NetWorth/client/clientmain.cpp b/source/Interfaces/NetWorth/client/clientmain.cpp index e0500cf6..e64c11c7 100644 --- a/source/Interfaces/NetWorth/client/clientmain.cpp +++ b/source/Interfaces/NetWorth/client/clientmain.cpp @@ -9,6 +9,7 @@ #include "../../../Agents/PacingAgent.hpp" #include "../../../Worlds/MazeWorld.hpp" #include "networkingagent.hpp" +#include "ClientInterface.hpp" int main(int argc, char *argv[]) { @@ -17,81 +18,12 @@ int main(int argc, char *argv[]) return 1; } - // create socket, get IP and port from args - sf::UdpSocket socket; std::string ip_string(argv[1]); - sf::IpAddress server_ip(ip_string); unsigned short port = stoi(std::string(argv[2])); + cse491::netWorth::ClientInterface interface(1, "client", ip_string, port); - // send ping to server - std::string message = "Ping!"; - if (socket.send(message.c_str(), message.size() + 1, server_ip, port) != Socket::Status::Done) { - std::cout << "Could not connect to " << ip_string << " at port " << port << std::endl; - return 1; - } - - // receive pong from server - size_t received; - char data[100]; - if (socket.receive(data, 100, received, server_ip, port) != sf::Socket::Done) - { - std::cout << "Failure to receive" << std::endl; - return 1; - } - std::cout << data << std::endl; - - char input; - bool valid_input = false; - bool wait_for_input = true; - sf::Packet recv_pkt; - std::string recv_str; - - // receive initial map - if (socket.receive(recv_pkt, server_ip, port) != sf::Socket::Done) - { - std::cout << "Failure to receive" << std::endl; - return 1; - } - - recv_pkt >> recv_str; - std::cout << recv_str; - - while (input != 'q') - { - do { - std::cin >> input; - } while (!std::cin && wait_for_input); - - switch (input) { - case 'w': case 'W': valid_input = true; break; - case 'a': case 'A': valid_input = true; break; - case 's': case 'S': valid_input = true; break; - case 'd': case 'D': valid_input = true; break; - case 'q': case 'Q': valid_input = true; break; - default: valid_input = false; - } - - // If we waited for input, but don't understand it, notify the user. - if (wait_for_input && !valid_input) { - std::cout << "Unknown key '" << input << "'." << std::endl; - } else { - if (socket.send(&input, 1, server_ip, port) != Socket::Status::Done) { - std::cout << "Could not connect to " << ip_string << " at port " << port << std::endl; - return 1; - } - - if (socket.receive(recv_pkt, server_ip, port) != sf::Socket::Done) - { - std::cout << "Failure to receive" << std::endl; - return 1; - } - - recv_pkt >> recv_str; - std::cout << recv_str; - } - - valid_input = false; - } + if (!interface.EstablishConnection()) return 1; + interface.Loop(); return 0; }