Skip to content

Instantly share code, notes, and snippets.

@dee1337
Created April 25, 2017 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dee1337/f6cd724ef4cf79c2c32aae89b1edc32c to your computer and use it in GitHub Desktop.
Save dee1337/f6cd724ef4cf79c2c32aae89b1edc32c to your computer and use it in GitHub Desktop.
League Ping
#include <stdio.h>
#include <string>
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
// src: http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c-using-posix/478960
std::string exec(const char* cmd) {
char buffer[128];
std::string result = "";
FILE* pipe = _popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
}
catch (...) {
_pclose(pipe);
throw;
}
_pclose(pipe);
return result;
}
// src: http://stackoverflow.com/questions/30073839/c-extract-number-from-the-middle-of-a-string/30074453
std::string first_numberstring(std::string const & str)
{
std::size_t const n = str.find_first_of("0123456789");
if (n != std::string::npos)
{
std::size_t const m = str.find_first_not_of("0123456789", n);
return str.substr(n, m != std::string::npos ? m - n : m);
}
return std::string();
}
// src: http://stackoverflow.com/questions/13172158/c-split-string-by-line
std::vector<std::string> split_string(const std::string& str, const std::string& delimiter)
{
std::vector<std::string> strings;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ((pos = str.find(delimiter, prev)) != std::string::npos)
{
if (!str.substr(prev, pos - prev).empty()) {
strings.push_back(str.substr(prev, pos - prev));
}
prev = pos + 1;
}
// To get the last substring (or only, if delimiter is not found)
strings.push_back(str.substr(prev));
return strings;
}
int main(int argc, char** argv) {
const char* old_euw = "ping 185.40.65.1";
const char* euw = "ping 104.160.141.3";
const char* na = "ping 104.160.131.3";
const char* eune = "ping 104.160.142.3";
const char* oce = "ping 104.160.156.1";
const char* lan = "ping 104.160.136.3";
cout << " ------==[ League Connection Tool by oO dee1337 Oo from EUW }==------" << endl;
int choice = -1;
while (true) {
cout << "Select your server: \n\tEUW[1] | NA[2] | EUNE[3] | OCE[4] | LAN[5] " << endl;
cin >> choice;
if (choice == 2) { euw = na; break; }
else if (choice == 3) { euw = eune; break; }
else if (choice == 4) { euw = oce; break; }
else if (choice == 5) { euw = lan; break; }
else if (choice == 1) { break; }
}
cout << "Please be patient for measurements..." << endl;
auto strings = split_string(exec(euw), "\n");
int x = 0;
auto itr = strings.begin();
for (itr = strings.begin(); itr != strings.end() - 1; itr++)
x++;
std::string line = strings[x - 1]; // THE LAST LINE ONLY
// SPLIT THE LAST LINE into 3 comma seperated items:
auto arr = split_string(line, ",");
int min = -1;
int max = -1;
int mean = -1;
string a = first_numberstring(arr[0]);
string b = first_numberstring(arr[1]);
string c = first_numberstring(arr[2]);
if (a.empty()) { return -1; }
min = std::stoi(a);
max = std::stoi(b);
mean = std::stoi(c);
cout << "min|max|mean ping during test: " << min << "|" << max << "|" << mean << endl;
// min-max delta shouldn't be to big -> else unstable
if ((max - min) > 50 || (min - max) > 50) {
cout << "You have an unstable connection\n Maybe try the reset tool in the end" << endl;
return 0;
}
if (mean > 150) {
cout << "Your ping is " << mean << " -> you should not play league now!" << endl;
}
if (110 < mean && mean < 150) {
cout << "Your ping is not optimal (" << mean << "ms) but you can play league to some extend" << endl;
}
if (70 < mean && mean < 100) {
cout << "Your ping is ok (" << mean << "ms), you can play league without ping problems" << endl;
}
if (0 < mean && mean < 70) {
cout << "Your ping is perfect (" << mean << "ms), get a penta!" << endl;
}
cout << "finished results, ty for waiting\n\n" << endl;
/* user input */
int input = -1;
cout << "Do you want to use the Connection Reset Tool to reset your Network Settings?\n(it will flush/release and renew your ipconfig) -> this will not harm your network" << endl;
cout << "press 1 to do so or any other key to leave" << endl;
cin >> input;
if (input == 1) {
cout << "Resetting Your IP Configuration:" << endl;
cout << "flushing dns..." << endl;
exec("ipconfig /flushdns");
cout << "releasing ipconfig..." << endl;
exec("ipconfig /release");
cout << "renewing ipconfig..." << endl;
exec("ipconfig /renew");
cout << "\nCool, that's it\n" << endl;
}
cout << "...press a key to close this window." << endl;
cin >> input;
return 0;
}
@rschik
Copy link

rschik commented May 7, 2017

Nice thing you got there. I'd have some minor things you could change:
-you could consider using a switch statement instead of your while loop in line 71
-you can replace lines 84-89 with std::string line = strings[strings.size() - 2]; there is no need to iterate over the whole vector
-you can replace lines 145-146 with a simple system("pause")

Kind regards,
some programming scrub

@dee1337
Copy link
Author

dee1337 commented May 19, 2017

@imkreislaufen:
thank you for feedback, actually this was a fast quick-and-dirty code :)

  • i didnt't use switch, because i want people to stay within the loop as long as they don't put in the right choice
  • 84-89 can really be changed, i had it just for debugging in the beginning and was to lazy to cut it
  • system("pause") somehow somtimes did not work everytime for some weird reason (had i before), that's why i went back to the cin version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment