]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CMedalServer.cpp
Coalesce printf() statements in main.cpp, make them translatable.
[quix0rs-blobwars.git] / src / CMedalServer.cpp
1 /*
2 Copyright (C) 2010-2011 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "headers.h"
22
23 MedalServer::MedalServer()
24 {
25         lock = SDL_CreateMutex();
26         
27         connected = false;
28         gotRuby = false;
29         
30         message[0] = 0;
31         rubyMessage[0] = 0;
32 }
33
34 MedalServer::~MedalServer()
35 {
36         if (connected)
37         {
38                 close();
39         }
40 }
41
42 bool MedalServer::connect(const char *privateKey)
43 {
44         if (SDLNet_ResolveHost(&ip, MEDAL_SERVER_HOST, MEDAL_SERVER_PORT) == -1)
45         {
46                 printf("ERROR: SDLNet_ResolveHost: %s\n", SDLNet_GetError());
47                 return false;
48         }
49         
50         debug(("Connected %s to %s:%d\n", privateKey, MEDAL_SERVER_HOST, MEDAL_SERVER_PORT));
51         
52         strlcpy(this->privateKey, privateKey, sizeof this->privateKey);
53         connected = true;
54         
55         return true;
56 }
57
58 #if !UNIX
59 extern char *strtok_r(char *s1, const char *s2, char **lasts);
60 #endif
61                 
62 int MedalServer::postMedal(const char *str)
63 {
64         if (!connected)
65         {
66                 return 0;
67         }
68         
69         char *store;
70         
71         char medal[128];
72         strlcpy(medal, str, sizeof medal);
73         
74         for (unsigned int i = 0 ; i < strlen(medal) ; i++)
75         {
76                 if (medal[i] == ' ' || medal[i] == '#' || medal[i] == ',')
77                 {
78                         medal[i] = '_';
79                 }
80         }
81         
82         debug(("Attempting to post medal 'MBS_%s'\n", medal));
83         
84         TCPsocket socket = SDLNet_TCP_Open(&ip);
85         
86         if (!socket)
87         {
88                 printf("ERROR: SDLNet_TCP_Open: %s\n", SDLNet_GetError());
89                 return 0;
90         }
91         
92         char out[1024];
93         
94         snprintf(out, sizeof out, "GET /addMedal/%s/MBS_%s HTTP/1.1\nHost: %s\nUser-Agent:BWMBS%.2f-%d\n\n", privateKey, medal, MEDAL_SERVER_HOST, VERSION, RELEASE);
95         
96         //printf("%s\n", out);
97         
98         int len = strlen(out) + 1;
99         
100         if (SDLNet_TCP_Send(socket, (void*)out, len) < len)
101         {
102                 printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
103                 printf("Disconnected\n");
104                 SDLNet_TCP_Close(socket);
105                 close();
106                 return 0;
107         }
108         
109         char *in = new char[1024];
110
111         SDLNet_TCP_Recv(socket, in, 512);
112         
113         //printf("%s\n", in);
114         
115         int response = 0;
116         char *token = strtok_r(in, "\n", &store);
117         
118         while (token != NULL)
119         {
120                 if (strstr(token, "MEDAL_RESPONSE"))
121                 {
122                         sscanf(token, "%*s %d %[^\n\r]", &response, message);
123                         
124                         if (response == 4)
125                         {
126                                 strlcpy(rubyMessage, message, sizeof rubyMessage);
127                                 gotRuby = true;
128                         }
129                         else
130                         {
131                                 break;
132                         }
133                 }
134                 
135                 token = strtok_r(NULL, "\n", &store);
136         }
137         
138         debug(("MedalServer Response: %d '%s'\n", response, message))
139         
140         delete[] in;
141         
142         SDLNet_TCP_Close(socket);
143         
144         return response;
145 }
146
147 const char *MedalServer::getMessage()
148 {
149         return message;
150 }
151
152 bool MedalServer::hasRuby()
153 {
154         return gotRuby;
155 }
156
157 const char *MedalServer::getRubyMessage()
158 {
159         gotRuby = false;
160         
161         return rubyMessage;
162 }
163
164 void MedalServer::close()
165 {
166         connected = false;
167         
168         SDL_DestroyMutex(lock);
169 }