-pero que...
This commit is contained in:
46
jscore.cpp
46
jscore.cpp
@@ -16,23 +16,32 @@
|
||||
|
||||
namespace jscore {
|
||||
|
||||
using namespace std;
|
||||
struct user {
|
||||
std::string name;
|
||||
string name;
|
||||
int points;
|
||||
};
|
||||
std::vector<user> score;
|
||||
vector<user> score;
|
||||
|
||||
#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
|
||||
using namespace std;
|
||||
int sock;
|
||||
struct sockaddr_in client;
|
||||
int PORT = 9911;
|
||||
const char *HOST = "jaildoctor.duckdns.org";
|
||||
|
||||
#ifdef WIN32
|
||||
WSADATA WsaData;
|
||||
#endif
|
||||
|
||||
std::string sendRequest(const std::string request) {
|
||||
bool jscore_error = false;
|
||||
string error_message;
|
||||
|
||||
void setErrorMessage(string message) {
|
||||
jscore_error = true;
|
||||
error_message = message;
|
||||
}
|
||||
|
||||
string sendRequest(const string request) {
|
||||
#ifdef WIN32
|
||||
int ret = WSAStartup(0x101,&WsaData);
|
||||
if (ret != 0) return 0;
|
||||
@@ -40,7 +49,7 @@ namespace jscore {
|
||||
struct hostent * host = gethostbyname(HOST);
|
||||
|
||||
if ( (host == NULL) || (host->h_addr == NULL) ) {
|
||||
printf("Error retrieving DNS information.\n");
|
||||
setErrorMessage("Error retrieving DNS information.\n");
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -52,19 +61,19 @@ namespace jscore {
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
if (sock < 0) {
|
||||
printf("Error creating socket.\n");
|
||||
setErrorMessage("Error creating socket.\n");
|
||||
return "";
|
||||
}
|
||||
|
||||
if ( connect(sock, (struct sockaddr *)&client, sizeof(client)) < 0 ) {
|
||||
close(sock);
|
||||
printf("Could not connect\n");
|
||||
setErrorMessage("Could not connect\n");
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string r = request + " HTTP/1.1\r\nHost: "+HOST+"\r\nConnection: close\r\n\r\n\r\n";
|
||||
string r = request + " HTTP/1.1\r\nHost: "+HOST+"\r\nConnection: close\r\n\r\n\r\n";
|
||||
if (send(sock, r.c_str(), r.length(), 0) != (int)r.length()) {
|
||||
printf("Error sending request.\n");
|
||||
setErrorMessage("Error sending request.\n");
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -87,9 +96,10 @@ namespace jscore {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
const bool initOnlineScore(std::string game) {
|
||||
std::string strbuff = sendRequest("GET /score-list.php?game=" + game);
|
||||
|
||||
const bool initOnlineScore(string game) {
|
||||
string strbuff = sendRequest("GET /score-list.php?game=" + game);
|
||||
if (jscore_error) return jscore_error;
|
||||
|
||||
user u;
|
||||
char buffer[1024];
|
||||
strcpy(buffer, strbuff.c_str());
|
||||
@@ -103,28 +113,30 @@ namespace jscore {
|
||||
*p=0; u.points = atoi(str); p++; str=p;
|
||||
score.push_back(u);
|
||||
}
|
||||
return jscore_error;
|
||||
}
|
||||
|
||||
const int getNumUsers() {
|
||||
return score.size();
|
||||
}
|
||||
std::string getUserName(const int index) {
|
||||
string getUserName(const int index) {
|
||||
return score[index].name;
|
||||
}
|
||||
const int getPoints(const int index) {
|
||||
return score[index].points;
|
||||
}
|
||||
|
||||
const bool updateUserPoints(std::string game, std::string user, const int points) {
|
||||
std::string strbuff = sendRequest("GET /score-update.php?game=" + game + "&user=" + user + "&points=" + to_string(points));
|
||||
const bool updateUserPoints(string game, string user, const int points) {
|
||||
string strbuff = sendRequest("GET /score-update.php?game=" + game + "&user=" + user + "&points=" + to_string(points));
|
||||
initOnlineScore(game);
|
||||
return jscore_error;
|
||||
}
|
||||
|
||||
std::string getUserData(std::string game, std::string user) {
|
||||
string getUserData(string game, string user) {
|
||||
return sendRequest("GET /getuserdata.php?game=" + game + "&user=" + user);
|
||||
}
|
||||
|
||||
void setUserData(std::string game, std::string user, std::string data) {
|
||||
void setUserData(string game, string user, string data) {
|
||||
sendRequest("GET /setuserdata.php?game=" + game + "&user=" + user + "&data=" + data);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user