- First commit
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
.vscode/*
|
||||||
|
*.php
|
||||||
|
*.exe
|
||||||
|
*.db
|
||||||
32
client.cpp
Normal file
32
client.cpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#include "jscore.h"
|
||||||
|
#include "stdio.h"
|
||||||
|
|
||||||
|
int main(int argc, char const *argv[]) {
|
||||||
|
|
||||||
|
// Per a agafar la tabla de puntuacions de "coffee"
|
||||||
|
jscore::initOnlineScore("test");
|
||||||
|
|
||||||
|
|
||||||
|
// Una vegada recuperada la tabla de puntuacions (ens la torna ordenada per max puntuació, el primer el max)...
|
||||||
|
|
||||||
|
// Per a agafar el nombre usuaris en la tabla
|
||||||
|
int numUsers = jscore::getNumUsers();
|
||||||
|
for (int i=0;i<numUsers;++i) {
|
||||||
|
// jscore::getUserName(i) agafar el nom de usuari en la posició i
|
||||||
|
// jscore::getPoints(i) agafar els punts de l'usuari en la posició i
|
||||||
|
printf("%s: %i\n", jscore::getUserName(i).c_str(), jscore::getPoints(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si volem ficar una puntuació per a un usuari: (si no existeix, el crea. Si existeix, l'actualitza)
|
||||||
|
jscore::updateUserPoints("test","JAILDES",10000);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Per a guardar una cadena arbitraria de dades asociada a un joc i un usuari
|
||||||
|
// ATENCIÓ: NO SE FA NINGUN TRACTAMENT A LA CADENA. NO FIQUES CARACTERS QUE NO DEURIEN ESTAR EN EL GET DE UNA URL
|
||||||
|
jscore::setUserData("test", "JAILDOC", "ABCDE/=1234");
|
||||||
|
|
||||||
|
// Per a recuperar una cadena arbitraria de dades asociada a un joc i un usuari
|
||||||
|
printf("userdata: %s\n", jscore::getUserData("test", "JAILDOC").c_str());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
123
jscore.cpp
Normal file
123
jscore.cpp
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
#include "jscore.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
//#include <sys/socket.h>
|
||||||
|
//#include <netdb.h>
|
||||||
|
//#include <netinet/in.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace jscore {
|
||||||
|
|
||||||
|
struct user {
|
||||||
|
std::string name;
|
||||||
|
int points;
|
||||||
|
};
|
||||||
|
std::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";
|
||||||
|
WSADATA WsaData;
|
||||||
|
|
||||||
|
std::string sendRequest(const std::string request) {
|
||||||
|
int ret = WSAStartup(0x101,&WsaData);
|
||||||
|
if (ret != 0) return 0;
|
||||||
|
|
||||||
|
struct hostent * host = gethostbyname(HOST);
|
||||||
|
|
||||||
|
if ( (host == NULL) || (host->h_addr == NULL) ) {
|
||||||
|
printf("Error retrieving DNS information.\n");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
bzero(&client, sizeof(client));
|
||||||
|
client.sin_family = AF_INET;
|
||||||
|
client.sin_port = htons( PORT );
|
||||||
|
memcpy(&client.sin_addr, host->h_addr, host->h_length);
|
||||||
|
|
||||||
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
|
if (sock < 0) {
|
||||||
|
printf("Error creating socket.\n");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( connect(sock, (struct sockaddr *)&client, sizeof(client)) < 0 ) {
|
||||||
|
close(sock);
|
||||||
|
printf("Could not connect\n");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::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");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
char cur;
|
||||||
|
char start[5]="\r\n\r\n";
|
||||||
|
int pos = 0;
|
||||||
|
while ( recv(sock, &cur, 1,0) > 0 ) {
|
||||||
|
if (cur==start[pos]) { pos++; if (pos == 4) break; } else { pos = 0; }
|
||||||
|
}
|
||||||
|
|
||||||
|
char buffer[1024]; buffer[0]=0; pos=0;
|
||||||
|
while ( recv(sock, &cur, 1,0) > 0 ) {
|
||||||
|
buffer[pos] = cur;
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
WSACleanup();
|
||||||
|
buffer[pos]=0;
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool initOnlineScore(std::string game) {
|
||||||
|
std::string strbuff = sendRequest("GET /score-list.php?game=" + game);
|
||||||
|
|
||||||
|
user u;
|
||||||
|
char buffer[1024];
|
||||||
|
strcpy(buffer, strbuff.c_str());
|
||||||
|
char *str = buffer;
|
||||||
|
char *p = str;
|
||||||
|
score.clear();
|
||||||
|
while (*p!=0) {
|
||||||
|
while (*p!=',') {p++;}
|
||||||
|
*p=0; u.name = str; p++; str=p;
|
||||||
|
while (*p!='\n') {p++;}
|
||||||
|
*p=0; u.points = atoi(str); p++; str=p;
|
||||||
|
score.push_back(u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const int getNumUsers() {
|
||||||
|
return score.size();
|
||||||
|
}
|
||||||
|
std::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) {
|
||||||
|
char dst[255];
|
||||||
|
std::string strbuff = sendRequest("GET /score-update.php?game=" + game + "&user=" + user + "&points=" + itoa(points, dst, 10));
|
||||||
|
initOnlineScore(game);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string getUserData(std::string game, std::string user) {
|
||||||
|
return sendRequest("GET /getuserdata.php?game=" + game + "&user=" + user);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUserData(std::string game, std::string user, std::string data) {
|
||||||
|
sendRequest("GET /setuserdata.php?game=" + game + "&user=" + user + "&data=" + data);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
14
jscore.h
Normal file
14
jscore.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace jscore {
|
||||||
|
const bool initOnlineScore(std::string game);
|
||||||
|
const int getNumUsers();
|
||||||
|
std::string getUserName(const int index);
|
||||||
|
const int getPoints(const int index);
|
||||||
|
|
||||||
|
const bool updateUserPoints(std::string game, std::string user, const int points);
|
||||||
|
std::string getUserData(std::string game, std::string user);
|
||||||
|
void setUserData(std::string game, std::string user, std::string data);
|
||||||
|
};
|
||||||
|
|
||||||
Reference in New Issue
Block a user