File: xmDatabase_web.cpp

package info (click to toggle)
xmoto 0.5.11+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 80,908 kB
  • sloc: cpp: 96,757; ansic: 22,196; sh: 4,940; makefile: 1,073; yacc: 289; sed: 16
file content (574 lines) | stat: -rw-r--r-- 17,923 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*=============================================================================
XMOTO

This file is part of XMOTO.

XMOTO is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

XMOTO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with XMOTO; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
=============================================================================*/

#include "xmDatabase.h"
#include "../GameText.h"
#include "../VXml.h"
#include "../helpers/VExcept.h"
#include "../helpers/Log.h"
#include "../WWW.h"
#include <sstream>

#define XM_NB_THIEFS_MAX 3

bool xmDatabase::webrooms_checkKeyExists_id_room(const std::string& i_id_room) {
  return checkKey("SELECT count(1) FROM webrooms "
		  "WHERE id_room=" + i_id_room + ";");
}

void xmDatabase::webrooms_addRoom(const std::string& i_id_room, const std::string& i_name,
  const std::string& i_highscoreUrl) {
  simpleSql("INSERT INTO webrooms("
	    "id_room,  name, highscoresUrl) "
	    "VALUES(" + i_id_room + ", " +
	    "\"" + protectString(i_name)         + "\", " +
	    "\"" + protectString(i_highscoreUrl) + "\");");
}

std::string xmDatabase::webhighscores_updateDB(FileDataType i_fdt,
					       const std::string& i_webhighscoresFile,
					       const std::string& i_websource) {
  XMLDocument v_xml;
  xmlNodePtr  v_xmlElt;
  std::string v_roomName;
  std::string v_roomId;
  std::string v_levelId;
  std::string v_player;
  std::string v_rplUrl;
  std::string v_strtime;
  std::string v_date;
  int v_time;
  unsigned int pos_1, pos_2;

  try {
    simpleSql("BEGIN TRANSACTION;");

    v_xml.readFromFile(i_fdt, i_webhighscoresFile);

    v_xmlElt = v_xml.getRootNode("xmoto_worldrecords");
    if(v_xmlElt == NULL) {
      throw Exception("unable to analyze xml highscore file");
    }

    /* get Room information */
    v_roomName = XMLDocument::getOption(v_xmlElt, "roomname");
    v_roomId   = XMLDocument::getOption(v_xmlElt, "roomid");

    if(v_roomId == "") {
      throw Exception("error : unable to analyze xml highscore file");
    }

    /* be sure the room is into the webrooms table */
    if(webrooms_checkKeyExists_id_room(v_roomId) == false) {
      webrooms_addRoom(v_roomId, v_roomName, i_websource);
    }

    simpleSql("DELETE FROM webhighscores WHERE id_room=" + v_roomId + ";");

    for(xmlNodePtr pSubElem = XMLDocument::subElement(v_xmlElt, "worldrecord");
	pSubElem != NULL;
	pSubElem = XMLDocument::nextElement(pSubElem)) {

      v_levelId = XMLDocument::getOption(pSubElem, "level_id");
      if(v_levelId == "") {
	continue;
      }

      v_player = XMLDocument::getOption(pSubElem, "player");
      if(v_player == "") {
	continue;
      }

      /* time */
      v_strtime = XMLDocument::getOption(pSubElem, "time");
      if(v_strtime == "") {
	continue;
      }

      pos_1 = v_strtime.find(":", 0);
      if(pos_1 == std::string::npos || pos_1 == v_strtime.length()-1)
	continue;
      pos_2 = v_strtime.find(":", pos_1+1);
      if(pos_2 == std::string::npos)
	continue;

      v_time =
	atoi(v_strtime.substr(0, pos_1).c_str())               * 6000 +
	atoi(v_strtime.substr(pos_1+1, pos_2-pos_1-1).c_str()) * 100  +
        atoi(v_strtime.substr(pos_2+1, v_strtime.length()-pos_2-1).c_str());

      /* replay */
      v_rplUrl = XMLDocument::getOption(pSubElem, "replay");
      if(v_rplUrl == "") {
	continue;
      }

      /* date */
      v_date = XMLDocument::getOption(pSubElem, "date");
      if(v_date == "") {
	continue;
      }

      std::ostringstream v_secondTime;
      v_secondTime << v_time;

      simpleSql("INSERT INTO webhighscores(id_room, id_level, id_profile, finishTime, date, fileUrl)"
		"VALUES("   + v_roomId   + ", \"" +
		protectString(v_levelId) + "\", \"" +
		protectString(v_player)  + "\", "   +
		v_secondTime.str()       + ", \""   +
		v_date                   + "\", \"" +
		protectString(v_rplUrl)  + "\");");
    }
    simpleSql("COMMIT;");
  } catch(Exception &e) {
    simpleSql("ROLLBACK;");
    throw e;
  }
  return v_roomId;
}

std::string xmDatabase::webrooms_getName(const std::string& i_id_room) {
  char **v_result;
  unsigned int nrow;
  std::string v_res;

  v_result = readDB("SELECT name FROM webrooms WHERE id_room=" + i_id_room + ";",
		    nrow);
  if(nrow != 1) {
    read_DB_free(v_result);
    return ""; /* not found */
  }
  v_res = getResult(v_result, 1, 0, 0);

  read_DB_free(v_result);
  return v_res;
}

int xmDatabase::webrooms_getHighscoreTime(const std::string& i_id_room,
					  const std::string& i_id_level) {
  char **v_result;
  unsigned int nrow;
  std::string v_res;

  v_result = readDB("SELECT finishTime FROM webhighscores "
		    "WHERE id_room="  + i_id_room +
		    " AND id_level=\"" + protectString(i_id_level) + "\";",
		    nrow);
  if(nrow != 1) {
    read_DB_free(v_result);
    return -1; /* not found */
  }
  v_res = getResult(v_result, 1, 0, 0);

  read_DB_free(v_result);
  return atoi(v_res.c_str());
}

bool xmDatabase::isOnTheWeb(const std::string& i_id_level) {
  return checkKey("SELECT count(1) FROM weblevels WHERE id_level=\"" + protectString(i_id_level) + "\";");
}

bool xmDatabase::isWebVoteLocked(const std::string& i_id_level) {
  return checkKey("SELECT count(1) FROM weblevels WHERE id_level=\"" + protectString(i_id_level) + "\" AND vote_locked=1;");
}

void xmDatabase::weblevels_updateDB(FileDataType i_fdt, const std::string& i_weblevelsFile) {
  XMLDocument v_xml;
  xmlNodePtr  v_xmlElt;
  
  try {
    simpleSql("BEGIN TRANSACTION;");
    simpleSql("DELETE FROM weblevels;");

    v_xml.readFromFile(i_fdt, i_weblevelsFile);

    v_xmlElt = v_xml.getRootNode("xmoto_levels");
    if(v_xmlElt == NULL) {
      throw Exception("unable to analyze xml file");
    }

    for(xmlNodePtr pSubElem = XMLDocument::subElement(v_xmlElt, "level");
	pSubElem != NULL;
	pSubElem = XMLDocument::nextElement(pSubElem)) {

      std::string v_levelId, v_levelName, v_url, v_MD5sum_web;
      std::string v_difficulty, v_quality, v_creationDate;
      std::string v_crappy, v_children_compliant, v_vote_locked;
      std::string v_packname, v_packnum;

      v_levelId = XMLDocument::getOption(pSubElem, "level_id");
      if(v_levelId == "")
	continue;

      v_levelName = XMLDocument::getOption(pSubElem, "name");
      if(v_levelName == "")
	continue;

      v_packname = XMLDocument::getOption(pSubElem, "packname");
      if(v_packname != "") {
	v_packnum = XMLDocument::getOption(pSubElem, "packnum");
      }

      v_url = XMLDocument::getOption(pSubElem, "url");
      if(v_url == "")
	continue;

      v_MD5sum_web = XMLDocument::getOption(pSubElem, "sum");
      if(v_MD5sum_web == "")
	continue;
	      
      /* web information */
      v_difficulty = XMLDocument::getOption(pSubElem, "web_difficulty");
      if(v_difficulty == "")
	continue;
      for(unsigned int i=0; i<v_difficulty.length(); i++) {
	if(v_difficulty[i] == ',')
	  v_difficulty[i] = '.';
      }

      v_quality = XMLDocument::getOption(pSubElem, "web_quality");
      if(v_quality == "")
	continue;
      for(unsigned int i=0; i<v_quality.length(); i++) {
	if(v_quality[i] == ',')
	  v_quality[i] = '.';
      }

      v_creationDate = XMLDocument::getOption(pSubElem, "creation_date");
      if(v_creationDate == "")
	continue;

      v_crappy = XMLDocument::getOption(pSubElem, "crappy");
      if(v_crappy == "") {
	v_crappy = "0";
      } else {
	v_crappy = v_crappy == "true" ? "1" : "0";
      }

      v_children_compliant = XMLDocument::getOption(pSubElem, "children_compliant");
      if(v_children_compliant == "") {
	v_children_compliant = "1";
      } else {
	v_children_compliant = v_children_compliant == "true" ? "1" : "0";
      }

      v_vote_locked = XMLDocument::getOption(pSubElem, "vote_locked");
      if(v_vote_locked == "") {
	v_vote_locked = "0";
      } else {
	v_vote_locked = v_vote_locked == "true" ? "1" : "0";
      }

      // add the level
      simpleSql("INSERT INTO weblevels(id_level, name, packname, packnum, fileUrl, "
		"checkSum, difficulty, quality, creationDate, crappy, children_compliant, vote_locked) VALUES (\"" +
		protectString(v_levelId)    + "\", \"" +
		protectString(v_levelName)  + "\", \"" +
		protectString(v_packname)  + "\", \"" +
		protectString(v_packnum)  + "\", \"" +
		protectString(v_url)        + "\", \"" +
		protectString(v_MD5sum_web) + "\", "   +
		v_difficulty   + ", "   +
		v_quality      + ", \"" +
		v_creationDate + "\", " +
		v_crappy       + ", "   +
		v_children_compliant + ", " +
		v_vote_locked + ");");

    }
    simpleSql("COMMIT;");
  } catch(Exception &e) {
    simpleSql("ROLLBACK;");
    throw e;
  }
}

void xmDatabase::webrooms_updateDB(FileDataType i_fdt, const std::string& i_webroomsFile) {
  XMLDocument v_xml;
  xmlNodePtr  v_xmlElt;

  std::string v_RoomName, v_RoomHighscoreUrl, v_RoomId;

  try {
    simpleSql("BEGIN TRANSACTION;");
    simpleSql("DELETE FROM webrooms;");

    v_xml.readFromFile(i_fdt, i_webroomsFile);

    v_xmlElt = v_xml.getRootNode("xmoto_rooms");
    if(v_xmlElt == NULL) {
      throw Exception("unable to analyze xml file");
    }
  
    for(xmlNodePtr pSubElem = XMLDocument::subElement(v_xmlElt, "room");
	pSubElem != NULL;
	pSubElem = XMLDocument::nextElement(pSubElem)) {

      v_RoomName = XMLDocument::getOption(pSubElem, "name");
      if(v_RoomName == "") continue;
  
      v_RoomHighscoreUrl = XMLDocument::getOption(pSubElem, "highscores_url");
      if(v_RoomName == "") continue;

      v_RoomId = XMLDocument::getOption(pSubElem, "id");
      if(v_RoomId == "") continue;

      webrooms_addRoom(v_RoomId, v_RoomName, v_RoomHighscoreUrl);
    }
    simpleSql("COMMIT;");
  } catch(Exception &e) {
    simpleSql("ROLLBACK;");
    throw e;
  }
}

void xmDatabase::webthemes_updateDB(FileDataType i_fdt, const std::string& i_webThemesFile) {
  XMLDocument v_xml;
  xmlNodePtr  v_xmlElt;
  std::string v_themeName, v_url, v_MD5sum_web;

  try {
    simpleSql("BEGIN TRANSACTION;");
    simpleSql("DELETE FROM webthemes;");
    
    v_xml.readFromFile(i_fdt, i_webThemesFile);

    v_xmlElt = v_xml.getRootNode("xmoto_themes");
    if(v_xmlElt == NULL) {
      throw Exception("unable to analyze xml file");
    }

    for(xmlNodePtr pSubElem = XMLDocument::subElement(v_xmlElt, "theme");
        pSubElem != NULL;
        pSubElem = XMLDocument::nextElement(pSubElem)) {

      v_themeName = XMLDocument::getOption(pSubElem, "name");
      if(v_themeName == "") continue;

      v_url = XMLDocument::getOption(pSubElem, "url");
      if(v_url == "") continue;

      v_MD5sum_web = XMLDocument::getOption(pSubElem, "sum");
      if(v_MD5sum_web == "") continue;
      
      webthemes_addTheme(v_themeName, v_url, v_MD5sum_web);
    }
    simpleSql("COMMIT;");
  } catch(Exception &e) {
    simpleSql("ROLLBACK;");
    throw e;
  }
}

void xmDatabase::webthemes_addTheme(const std::string& i_id_theme, const std::string& i_url,
				    const std::string& i_checkSum) {
  simpleSql("INSERT INTO webthemes("
	    "id_theme,  fileUrl, checkSum) "
	    "VALUES(\"" + protectString(i_id_theme) + "\", " +
	    "\""        + protectString(i_url)      + "\", " +
	    "\""        + protectString(i_checkSum) + "\");");
}

void xmDatabase::webLoadDataFirstTime() {
  char **v_result;
  unsigned int nrow;
  bool v_update = false;

  /* updating weblevels table with defaults */
  try {
    /* if the is any row already in the table, no update is done */
    v_result = readDB("SELECT count(1) FROM weblevels;", nrow);
    if(nrow == 1) {
      v_update = atoi(getResult(v_result, 1, 0, 0)) == 0;
    }
    read_DB_free(v_result);

    if(v_update) {
      LogInfo("Loading weblevels with delivered weblevels.xml");
      weblevels_updateDB(FDT_DATA, "default/weblevels.xml"); // FDT_DATA because first loading file a really a data
    }
  } catch(Exception &e) {
    /* ok, no pb */
    LogWarning("Loading delivered weblevels.xml failed");
  }

  /* updating webhighscores table with defaults */
  try {
    /* if the is any row already in the table, no update is done */
    v_result = readDB("SELECT count(1) FROM webhighscores;", nrow);
    if(nrow == 1) {
      v_update = atoi(getResult(v_result, 1, 0, 0)) == 0;
    }
    read_DB_free(v_result);

    if(v_update) {
      LogInfo("Loading webhighscores with delivered webhighscores.xml");
      webhighscores_updateDB(FDT_DATA, "default/webhighscores.xml", DEFAULT_WEBHIGHSCORES_URL); // FDT_DATA because first loading file a really a data
    }
  } catch(Exception &e) {
    /* ok, no pb */
    LogWarning("Loading delivered webhighscores.xml failed");
  }
}

void xmDatabase::updateMyHighscoresFromHighscores(const std::string& i_id_profile) {
  char **v_result;
  unsigned int nrow;

  /* get my highscores which are in webhighscores but not in levels_mywebhighscores */
  try {
    v_result = readDB("SELECT a.id_profile, a.id_room, a.id_level "
                      "FROM webhighscores AS a "
		      "LEFT OUTER JOIN levels_mywebhighscores AS b ON (a.id_room    = b.id_room     "
		                                                  "AND a.id_level   = b.id_level    "
		                                                  "AND a.id_profile = b.id_profile) "
		      "WHERE a.id_profile = \"" + protectString(i_id_profile) + "\" "
		      "AND   b.id_profile IS NULL;",
		      nrow);

    if(nrow > 0) {
      try {
	simpleSql("BEGIN TRANSACTION;");
	for(unsigned int i=0; i<nrow; i++) {
	  simpleSql("INSERT INTO levels_mywebhighscores("
		    "id_profile, id_room, id_level) "
		    "VALUES(\"" + protectString(getResult(v_result, 3, i, 0)) + "\", " +
		    getResult(v_result, 3, i, 1)                              + ", " +
		    "\""        + protectString(getResult(v_result, 3, i, 2)) + "\");");
	}
	simpleSql("COMMIT;");
      } catch(Exception &e) {
	simpleSql("ROLLBACK;");
      }
    }

    read_DB_free(v_result);
  } catch(Exception &e) {
    /* ok, no pb */
    LogWarning("Unable to update my highscores");
  }

  /* reset levels i get back */
  updateMyHighscoresKnownStolenBack(i_id_profile);
}

void xmDatabase::updateMyHighscoresKnownStolenBack(const std::string& i_id_profile) {
  char **v_result;
  unsigned int nrow;

  /* reset to known_stolen=0 levels where known_stolen = 1 but highscore owner = i_id_profile */
  try {
    v_result = readDB("SELECT a.id_profile, a.id_room, a.id_level "
                      "FROM webhighscores AS a "
		      "INNER JOIN levels_mywebhighscores AS b ON (a.id_room    = b.id_room     "
                                           		     "AND a.id_level   = b.id_level    "
		                                             "AND a.id_profile = b.id_profile) "
		      "WHERE a.id_profile = \"" + protectString(i_id_profile) + "\" "
		      "AND   b.known_stolen = 1;",
		      nrow);

    if(nrow > 0) {
      try {
	simpleSql("BEGIN TRANSACTION;");
	for(unsigned int i=0; i<nrow; i++) {
	  simpleSql("UPDATE levels_mywebhighscores "
		    "SET known_stolen=0 "
		    "WHERE id_profile=\"" + protectString(getResult(v_result, 3, i, 0)) + "\" "
		    "AND   id_room="      + getResult(v_result, 3, i, 1) + " "
		    "AND   id_level=\""   + protectString(getResult(v_result, 3, i, 2)) + "\";");
	}
	simpleSql("COMMIT;");
      } catch(Exception &e) {
	simpleSql("ROLLBACK;");
      }
    }

    read_DB_free(v_result);
  } catch(Exception &e) {
    /* ok, no pb */
    LogWarning("Unable to update known stolen on my highscores");
  }
}

bool xmDatabase::markMyHighscoresKnownStolen(const std::string& i_id_profile, std::string& o_stolen_msg) {
  char **v_result;
  unsigned int nrow;
  bool v_res;
  char v_line_info[256];
  
  v_res = false;
  o_stolen_msg = "";

  try {
    v_result = readDB("SELECT a.id_room, a.id_level, c.name, a.id_profile, d.name "
                      "FROM webhighscores AS a "
		      "INNER JOIN levels_mywebhighscores AS b ON (a.id_room    = b.id_room     "
                                           		     "AND a.id_level   = b.id_level    "
		                                             "AND b.id_profile = \"" + protectString(i_id_profile) + "\") "
                      "INNER JOIN levels AS c ON a.id_level = c.id_level "
                      "INNER JOIN webrooms AS d ON a.id_room=d.id_room "
		      "WHERE a.id_profile <> \"" + protectString(i_id_profile) + "\" "
		      "AND   b.known_stolen = 0 "
		      "ORDER BY a.date DESC;",
		      nrow);

    if(nrow > 0) {
      v_res = true;

      try {
	simpleSql("BEGIN TRANSACTION;");
	for(unsigned int i=0; i<nrow; i++) {
	  
	  if(i<XM_NB_THIEFS_MAX) {
	    if(o_stolen_msg != "") {
	      o_stolen_msg += "\n";
	    }

	    snprintf(v_line_info, 256, GAMETEXT_HIGHSCORE_STOLEN,
		     getResult(v_result, 5, i, 4),
		     getResult(v_result, 5, i, 3),
		     getResult(v_result, 5, i, 2));
	    o_stolen_msg += v_line_info;

	  } else if(i == XM_NB_THIEFS_MAX) {
	    o_stolen_msg += "\n...";
	  }

	  simpleSql("UPDATE levels_mywebhighscores "
		    "SET known_stolen=1, known_stolen_date=datetime('now', 'localtime') "
		    "WHERE id_profile=\"" + protectString(i_id_profile) + "\" "
		    "AND   id_room="      + getResult(v_result, 5, i, 0) + " "
		    "AND   id_level=\""   + protectString(getResult(v_result, 5, i, 1)) + "\";");
	}
	simpleSql("COMMIT;");
      } catch(Exception &e) {
	simpleSql("ROLLBACK;");
      }
    }

    read_DB_free(v_result);
  } catch(Exception &e) {
    /* ok, no pb */
    LogWarning("Unable to update known stolen on my highscores");
  }

  return v_res;
}