Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Online highscore  (Read 15787 times)

0 Members and 1 Guest are viewing this topic.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
Online highscore
« on: August 05, 2008, 12:44:44 pm »
Hi I'm trying to add an online highscore to my game, this is my approach:
(The php-files used works)

1. Add new score to mysql database.
Code: [Select]
sf::SocketTCP sock;

sf::IPAddress server("www.dabostudios.net");

if(sock.Connect(80, server))
{
stringstream ss, ss2;
ss << iTime;
ss2 << iPoints;

// Add new score
string req = "GET /add_highscore.php?player=" +sName +"&time=" +ss.str() +"&score=" +ss2.str() +" HTTP/1.1\r\n"
"Host: www.dabostudios.net\r\n"
"\r\n";

if(sock.Send(req.c_str(), req.length()) != sf::Socket::Done)
exit(EXIT_FAILURE);

sock.Close();
}


2. Create a txt-file with the top scores from the database to later download.
Code: [Select]
if(sock.Connect(80, server))
{
// Write highscore txt-file
string req = "GET /write_highscore.php HTTP/1.1\r\n"
"Host: www.dabostudios.net\r\n"
"\r\n";

if(sock.Send(req.c_str(), req.length()) != sf::Socket::Done)
exit(EXIT_FAILURE);

sock.Close();
}


3. Download txt-file.
Code: [Select]
if(sock.Connect(80, server))
{
// Get highscore
string req = "GET /highscore.txt HTTP/1.1\r\n"
"Host: www.dabostudios.net\r\n"
"\r\n";

if(sock.Send(req.c_str(), req.length()) != sf::Socket::Done)
exit(EXIT_FAILURE);

char buffer[1024];
std::size_t received;

while(1)
{
sock.Receive(buffer, sizeof(buffer), received);

if(received > 0)
{
buffer[received] = '\0';
data.append(buffer);
}
else
break;
}

sock.Close();
}


When I check the created txt-file all scores seems to be there, but still all but the latest score are downloaded. I even cleared the mysql database and the old scores still showed up (they were not in the txt-file which is suppose to be downloaded and read from). It's like the old txt-file is cached somewhere or something like that and used instead.

Btw, does anyone see anything wrong in my code or approach?

EDIT: I've done some more brief testing, it seems to work if I sleep for 2 seconds (1 second won't work) before downloading the txt-file with all the scores. How can this be? Is my lightning fast laptop out working the web server ;)

 

anything