Skip to content

Commit

Permalink
misc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya246 committed Jul 29, 2022
1 parent 2da8ec3 commit 67c3c9b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
2 changes: 1 addition & 1 deletion include/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ inline std::future<void> inputReader;
inline std::string serverAddress = "", name = "",
inputBuffer = "";
inline sf::String chatBuffer = "";
inline unsigned short port = 0;
inline unsigned short port = 7817;
inline movement lastControls, controls;
inline double delta = 1.0 / 60.0,
globalTime = 0.0,
Expand Down
18 changes: 14 additions & 4 deletions include/strings.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
#pragma once

#include <regex>
#include <vector>

using namespace std;

namespace obf {
void stripSpecialChars(std::string&);
inline regex int_regex = regex("[0-9]*"),
double_regex = regex("[0-9]*(\\.[0-9]*)?"),
boolt_regex = regex("([tT][rR][uU][eE])|1"), boolf_regex = regex("([fF][aA][lL][sS][eE])|0");

void splitString(const string&, vector<string>&, char);
void stripSpecialChars(string&);

int parseToml(const std::string&);
int parseTomlFile(const std::string&);
int parseToml(const string&);
int parseTomlFile(const string&);

void parseCommand(const std::string_view&);
void parseCommand(const string&);

}
48 changes: 33 additions & 15 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,30 @@
#include <functional>
#include <future>
#include <iostream>
#include <regex>

using namespace obf;

void connectToServer() {
serverSocket = new sf::TcpSocket;
printf("See https://github.com/Ilya246/orbitfight/blob/master/SERVERS.md for 24/7 servers\n");
while (true) {
printf("Specify server port\n");
std::cin >> port;
printf("Specify server address\n");
std::cin >> serverAddress;
printf("Specify server address:port: ");
getline(std::cin, serverAddress);
std::vector<std::string> addressPort;
splitString(serverAddress, addressPort, ':');
std::string address = addressPort[0];
if (addressPort.size() == 2) {
if (std::regex_match(addressPort[1], int_regex)) {
port = stoi(addressPort[1]);
} else {
printf("Specified server port (%s) is not an integer.\n", addressPort[1].c_str());
}
}
if (serverSocket->connect(serverAddress, port) != sf::Socket::Done) {
printf("Could not connect to %s:%u.\n", serverAddress.c_str(), port);
printf("Could not connect to %s:%u.\n", address.c_str(), port);
} else {
printf("Connected to %s:%u.\n", serverAddress.c_str(), port);
printf("Connected to %s:%u.\n", address.c_str(), port);
sf::Packet nicknamePacket;
nicknamePacket << Packets::Nickname << name;
serverSocket->send(nicknamePacket);
Expand Down Expand Up @@ -60,7 +69,7 @@ int main(int argc, char** argv) {
out.open(configDocFile);
out << "NOTE: configs changed using the console will not be saved" << std::endl;
out << "predictSteps: As a client, how many steps of [predictDelta] ticks to simulate for trajectory prediction (int)" << std::endl;
out << "port: Used both as the port to host on and to specify port for autoConnect (short uint)" << std::endl;
out << "port: Used both as the port to host on and to specify port for autoConnect if server address does not contain port (short uint)" << std::endl;
out << "predictDelta: As a client, how many ticks to advance every prediction simulation step (double)" << std::endl;
out << "predictSpacing: As a client, how many seconds to wait between trajectory prediction simulations (double)" << std::endl;
out << "NOTE: any clients will have to have the same physics-related configs as the server for them to work properly" << std::endl;
Expand Down Expand Up @@ -91,7 +100,7 @@ int main(int argc, char** argv) {
} else {
if (name.empty()) {
printf("Specify a username.\n");
std::cin >> name;
getline(std::cin, name);
out << "\nname = " << name << std::endl;
}
}
Expand Down Expand Up @@ -130,14 +139,24 @@ int main(int argc, char** argv) {

systemCenter = new Attractor(true);

if (autoConnect && !serverAddress.empty() && port != 0) {
printf("Connecting automatically to %s:%u.\n", serverAddress.c_str(), port);
if (autoConnect && !serverAddress.empty()) {
std::vector<std::string> addressPort;
splitString(serverAddress, addressPort, ':');
std::string address = addressPort[0];
if (addressPort.size() == 2) {
if (std::regex_match(addressPort[1], int_regex)) {
port = stoi(addressPort[1]);
} else {
printf("Specified server port (%s) is not an integer.\n", addressPort[1].c_str());
}
}
printf("Connecting automatically to %s:%u.\n", address.c_str(), port);
serverSocket = new sf::TcpSocket;
if (serverSocket->connect(serverAddress, port) != sf::Socket::Done) [[unlikely]] {
printf("Could not connect to %s:%u.\n", serverAddress.c_str(), port);
if (serverSocket->connect(address, port) != sf::Socket::Done) [[unlikely]] {
printf("Could not connect to %s:%u.\n", address.c_str(), port);
connectToServer();
} else {
printf("Connected to %s:%u.\n", serverAddress.c_str(), port);
printf("Connected to %s:%u.\n", address.c_str(), port);
sf::Packet nicknamePacket;
nicknamePacket << Packets::Nickname << name;
serverSocket->send(nicknamePacket);
Expand All @@ -151,8 +170,7 @@ int main(int argc, char** argv) {
if (headless) {
if(!inputWaiting){
if(!inputBuffer.empty()){
std::string_view view(inputBuffer);
parseCommand(view);
parseCommand(inputBuffer);
inputBuffer.clear();
}
inputReader = std::async(std::launch::async, inputListen);
Expand Down
11 changes: 4 additions & 7 deletions src/strings.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "globals.hpp"
#include "net.hpp"
#include "strings.hpp"
#include "types.hpp"

#include <fstream>
Expand All @@ -11,11 +12,7 @@ using namespace std;

namespace obf {

static const regex int_regex = regex("[0-9]*"),
double_regex = regex("[0-9]*(\\.[0-9]*)?"),
boolt_regex = regex("([tT][rR][uU][eE])|1"), boolf_regex = regex("([fF][aA][lL][sS][eE])|0");

void splitString(string_view str, vector<string_view> &vec, char delim) {
void splitString(const string& str, vector<string>& vec, char delim) {
size_t last = 0;
for (size_t i = 0; i < str.size(); i++) {
if (str[i] == delim) {
Expand Down Expand Up @@ -180,8 +177,8 @@ int parseTomlFile(const string& filename) {
return 0;
}

void parseCommand (const string_view& command) {
vector<string_view> args;
void parseCommand (const string& command) {
vector<string> args;
splitString(command, args, ' ');
if (args.size() == 0) {
printf("Invalid command.\n");
Expand Down

0 comments on commit 67c3c9b

Please sign in to comment.