Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>
#include "room.h"
//#include "mainmenu.h"
#include "playermenu.h"
#include "jobmenu.h"
#include "librarymenu.h"
#include "player.h"
#include <sqlite3.h>
#include <Windows.h>
#include "menuclass.h"
#include "menuoptions.h"
#include "gymmenu.h"
#include <thread>
#include <sstream>
#include <sqlite3.h>
using namespace std;
using std::cout; // slow text
using std::flush;
using std::string;
using std::this_thread::sleep_for;
using std::chrono::milliseconds;
#pragma comment (lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define IP_ADDRESS "127.0.0.1" //change this to your IPV4 address
#define DEFAULT_PORT "3504"
struct client_type
{
SOCKET socket;
int id;
char received_message[DEFAULT_BUFLEN];
};
void slow_print(const string& message, unsigned int millis_per_char)
{
for (const char c : message)
{
cout << c << flush;
sleep_for(milliseconds(millis_per_char));
}
}
int process_client(client_type& new_client)
{
while (1)
{
memset(new_client.received_message, 0, DEFAULT_BUFLEN);
if (new_client.socket != 0)
{
int iResult = recv(new_client.socket, new_client.received_message, DEFAULT_BUFLEN, 0);
if (iResult != SOCKET_ERROR)
cout << new_client.received_message << endl;
}
}
if (WSAGetLastError() == WSAECONNRESET)
cout << "The server has disconnected" << endl;
return 0;
}
int main()
{
Sql sql;
bool exists = sql.Query(false);
int selection;
cout << "1 - Enter Chat client" << endl;
cout << "2 - Enter Game" << endl;
cin >> selection;
if (selection == 1)
{
WSAData wsa_data;
struct addrinfo* result = NULL, * ptr = NULL, hints;
string sent_message = "";
client_type client = { INVALID_SOCKET, -1, "" };
int iResult = 0;
string message;
cout << "Turning the engines on...\n";
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsa_data);
if (iResult != 0) {
cout << "WSAStartup() failed with error: " << iResult << endl;
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
cout << "Connecting...\n";
// Resolve the server address and port
iResult = getaddrinfo(static_cast<LPCTSTR>(IP_ADDRESS), DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
cout << "getaddrinfo() failed with error: " << iResult << endl;
WSACleanup();
system("pause");
return 1;
}
// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a SOCKET for connecting to server
client.socket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (client.socket == INVALID_SOCKET) {
cout << "socket() failed with error: " << WSAGetLastError() << endl;
WSACleanup();
system("pause");
return 1;
}
// Connect to server.
iResult = connect(client.socket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(client.socket);
client.socket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (client.socket == INVALID_SOCKET) {
cout << "Unable to connect to server!" << endl;
WSACleanup();
system("pause");
return 1;
}
cout << "Successfully Connected" << endl;
//Obtain id from server for this client;
recv(client.socket, client.received_message, DEFAULT_BUFLEN, 0);
message = client.received_message;
if (message != "Server is full")
{
client.id = atoi(client.received_message);
//when i wrap threads in a class it throws an error otherwise outside the class it work :(
thread threads(process_client, ref(client));
//thread threads(&client::process_client, ref(client));
//thread threads(&client::process_client, this, ref(client));
//thread threads(&client::process_client, this);
while (1)
{
int warningcounter = 0;
string warning = "Type something please!";
getline(cin, sent_message);
iResult = send(client.socket, sent_message.c_str(), strlen(sent_message.c_str()), 0);
if (iResult <= 0)
{
//cout << "send() function error ---> " << WSAGetLastError() << endl;
// break;
// warningcounter++; //warningcounter = warningcounter + 1;
cout << warning << endl;
//cout << warningcounter << endl;
}
else if (sent_message == "!")
{
cout << "You broke the rule of the server! adios ;)" << endl;
main();
}
}
//Shutdown the connection since no more data will be sent
threads.detach();
}
else
cout << client.received_message << endl;
cout << "Shutting down socket..." << endl;
iResult = shutdown(client.socket, SD_SEND);
if (iResult == SOCKET_ERROR) {
cout << "shutdown() function failed ----> " << WSAGetLastError() << endl;
closesocket(client.socket);
WSACleanup();
system("pause");
return 1;
}
closesocket(client.socket);
WSACleanup();
system("pause");
return 1;
}
else if (selection == 2)
{
room MainInterface;
MainInterface.Draw();
//int exit; // to prevent console closing
//cin >> exit;
MenuSwitch* game = new MenuSwitch(); // declares a pointer (game) and allocates memory to (size of(MenuSwitch))
//MainMenu* mainMenu = new MainMenu(game);
PlayerMenu* playerMenu = new PlayerMenu(game);
JobMenu* jobMenu = new JobMenu(game);
LibraryMenu* libraryMenu = new LibraryMenu(game);
Gym* gymMenu = new Gym(game);
game->assignMenu(playerMenu, jobMenu, libraryMenu, gymMenu);
string message = "\n You have been imprisoned \n You believe you did not do the crimes committed;\n You need to escape the prison to clear your name!\n Tread Carefully. \n There are traps and riddles which need to be avoided and answered to gain an insight on how to escape.\n \n";
slow_print(message, 10);
cout << "1 - Game Menu." << endl;
cout << "2 - Job Menu." << endl;
cout << "3 - Library Menu" << endl;
cout << "4 - Gym Menu" << endl;
cout << "Enter Selection:" << endl;
int menuInput;
cin >> menuInput;
while (true)
{
if (menuInput == 0)
{
system("cls");
main();
}
if (menuInput == 1 || menuInput == 2 || menuInput == 3 || menuInput == 4)
{
game->currentMenu->update(menuInput);
}
else
{
cout << "Input 1 - Player Menu, 2 - Job Menu, 3 - Library Menu or 4 - Gym Menu || to change menu, or 0 to return to the main menu." << endl;
}
cin >> menuInput;
}
}
//else if (selection == 3)
// {
// //system("start chatserver.exe");
//
// system("pause");
// }
};