From 452ef4c91df387111656524bfe7bd100940c4172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Tue, 15 Nov 2022 17:03:57 +0100 Subject: [PATCH] =?UTF-8?q?A=C3=B1adido=20jscore.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/common/jscore.cpp | 123 +++++++++++++++++++++++++++++++++++++++ source/common/jscore.h | 14 +++++ source/common/notify.cpp | 2 +- 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 source/common/jscore.cpp create mode 100644 source/common/jscore.h diff --git a/source/common/jscore.cpp b/source/common/jscore.cpp new file mode 100644 index 0000000..f9710c0 --- /dev/null +++ b/source/common/jscore.cpp @@ -0,0 +1,123 @@ +#include "jscore.h" +#include +#include +#include +//#include +//#include +//#include +#include +#include +#include + +namespace jscore { + + struct user { + std::string name; + int points; + }; + std::vector 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); + } + +}; + diff --git a/source/common/jscore.h b/source/common/jscore.h new file mode 100644 index 0000000..089d7b5 --- /dev/null +++ b/source/common/jscore.h @@ -0,0 +1,14 @@ +#pragma once +#include + +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); +}; + diff --git a/source/common/notify.cpp b/source/common/notify.cpp index d6bdbb5..e92a166 100644 --- a/source/common/notify.cpp +++ b/source/common/notify.cpp @@ -75,7 +75,7 @@ void Notify::update() { const float step = (notifications.at(i).counter / (float)height); - const int alpha = 255 * step; + const int alpha = 255 * (1 - step); notifications.at(i).rect.y--; notifications.at(i).texture->setAlpha(alpha);