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
|
/* $Id: xmlscores.c,v 1.10 2001/12/21 04:27:44 riq Exp $ */
/* Tenes Empanadas Graciela
*
* Copyright (C) 2001 Ricardo Quesada
*
* Author: Ricardo Calixto Quesada <riq@corest.com>
*
* This program 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; only version 2 of the License
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* @file xmlscores.c
* server scores from/to xml
*/
/*
* File based in the job example from libxml
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <glib.h>
#include <time.h>
#include "server.h"
#include "xmlscores.h"
#include "scores.h"
#ifndef xmlChildrenNode
#define xmlChildrenNode childs
#define xmlRootNode root
#endif
static PSCORES parseScore(xmlDocPtr doc, xmlNodePtr cur)
{
PSCORES ret = NULL;
xmlChar *val;
ret = (PSCORES) malloc(sizeof(SCORES));
if (ret == NULL) {
fprintf(stderr,_("Out of memory\n"));
return(NULL);
}
memset(ret, 0, sizeof(*ret));
val = xmlGetProp(cur, (const xmlChar *) "name");
if (val == NULL)
fprintf(stderr, ("Score has no name\n"));
else
strncpy( ret->name, val, sizeof(ret->name) -1 );
val = xmlGetProp(cur, (const xmlChar *) "color");
if ( val == NULL)
fprintf(stderr, ("Score has no color\n"));
else
ret->color = atoi( val );
val = xmlGetProp(cur, (const xmlChar *) "points");
if ( val == NULL)
fprintf(stderr, ("Score has no score\n"));
else
ret->stats.score = atoi( val );
val = xmlGetProp(cur, (const xmlChar *) "date");
if (val == NULL)
fprintf(stderr, ("Score has no date\n"));
else
strncpy( ret->date, val, sizeof(ret->date) -1 );
val = xmlGetProp(cur, (const xmlChar *) "human");
if (val == NULL)
fprintf(stderr, ("Score has no human\n"));
else
ret->human = atoi( val );
return(ret);
}
TEG_STATUS xmlscores_load( void )
{
char filename[512];
xmlDocPtr doc;
xmlNodePtr cur;
PSCORES curscore;
/*
* build an XML tree from a the file;
*/
snprintf( filename, sizeof(filename)-1,"%s/%s/server_scores.xml",g_get_home_dir(),TEG_DIRRC);
filename[ sizeof(filename)-1 ] = 0;
doc = xmlParseFile( filename );
if (doc == NULL)
return TEG_STATUS_ERROR;
/*
* Check the document is of the right kind
*/
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,("Empty document\n"));
goto error;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "teg_scores")) {
fprintf(stderr,("Wrong type. root node != teg_scores\n"));
goto error;
}
/*
* Allocate the structure to be returned.
*/
cur = cur->xmlChildrenNode;
while( cur != NULL ) {
if ( !xmlStrcmp(cur->name, (const xmlChar *) "score") ) {
curscore = parseScore( doc, cur );
if (curscore != NULL)
scores_insert_score(curscore);
}
else {
fprintf(stderr,("Wrong type (%s). score was expected\n"), cur->name);
goto error;
}
cur = cur->next;
}
xmlFreeDoc(doc);
return TEG_STATUS_SUCCESS;
error:
xmlFreeDoc(doc);
return TEG_STATUS_ERROR;
}
static TEG_STATUS xmlscores_add( xmlNodePtr parent, PSCORES pS )
{
xmlNodePtr child;
char buffer[512];
child = xmlNewTextChild( parent , NULL, "score", NULL );
xmlSetProp( child, "name", pS->name );
snprintf(buffer,sizeof(buffer)-1,"%d", pS->stats.score );
buffer[ sizeof(buffer) -1 ] = 0;
xmlSetProp( child, "points", buffer);
snprintf(buffer,sizeof(buffer)-1,"%d", pS->color);
buffer[ sizeof(buffer) -1 ] = 0;
xmlSetProp( child, "color", buffer );
xmlSetProp( child, "date", pS->date );
snprintf(buffer,sizeof(buffer)-1,"%d", pS->human);
buffer[ sizeof(buffer) -1 ] = 0;
xmlSetProp( child, "human", buffer );
return TEG_STATUS_SUCCESS;
}
TEG_STATUS xmlscores_save( PLIST_ENTRY pL_orig )
{
xmlDocPtr doc;
xmlNodePtr child;
PLIST_ENTRY pL = pL_orig->Flink;
char filename[512];
PSCORES pS;
doc = xmlNewDoc("1.0");
child = xmlNewDocRawNode( doc, NULL, "teg_scores", NULL );
/* whats the difference between xmlNewDocRawNode & SetRootElement */
xmlDocSetRootElement( doc, child );
while( !IsListEmpty( pL_orig ) && (pL != pL_orig) ) {
pS = (PSCORES) pL;
xmlscores_add( child, pS );
pL = LIST_NEXT(pL);
}
snprintf( filename, sizeof(filename)-1,"%s/%s/server_scores.xml",g_get_home_dir(),TEG_DIRRC);
filename[ sizeof(filename)-1 ] = 0;
xmlSaveFile( filename , doc );
return TEG_STATUS_SUCCESS;
}
/* creates a new node with the correct values */
static PSCORES new_score_node( PJUGADOR pJ )
{
PSCORES pS = NULL;
time_t tt;
/* char *char_time; */
/* char *newline; */
struct tm *t;
pS = (PSCORES) malloc( sizeof(*pS) );
if( !pS )
return NULL;
memset( pS,0,sizeof(*pS) );
InitializeListHead( &pS->next );
/* copy the stats */
pS->stats = pJ->player_stats;
pS->color = pJ->color;
strncpy( pS->name, pJ->nombre, sizeof( pS->name ) -1 );
pS->human = pJ->human;
time(&tt);
t = localtime(&tt);
#if 0
char_time = ctime(&tt);
/* remove newline */
if( (newline = strchr( char_time, (int) '\n' )) != NULL )
*newline = '\0';
snprintf( pS->date, sizeof(pS->date) -1, "%s", char_time );
#else
snprintf( pS->date, sizeof(pS->date) -1, "%.2d/%.2d/%.2d %.2d:%.2d"
,t->tm_mon +1
,t->tm_mday
,t->tm_year + 1900
,t->tm_hour
,t->tm_min
);
#endif
return pS;
}
TEG_STATUS scores_insert_player( PJUGADOR pJ )
{
PSCORES pS_new;
TEG_STATUS s;
if( ! pJ->is_player )
return TEG_STATUS_ERROR;
stats_score( &pJ->player_stats );
if( pJ->player_stats.score == 0 && pJ->player_stats.armies_won == 0 )
return TEG_STATUS_ERROR;
if( (pS_new = new_score_node( pJ )) == NULL)
return TEG_STATUS_NOMEM;
s = scores_insert_score( pS_new );
xmlscores_save( scores_get_list() );
return s;
}
TEG_STATUS scores_dump(char *strout)
{
int n;
int max;
char strtmp[ PLAYERNAME_MAX_LEN + 200];
PLIST_ENTRY l = scores_get_list()->Flink;
PSCORES pS;
strout[0]=0;
n=0;
max=0;
while( !IsListEmpty( scores_get_list() ) && (l != scores_get_list()) ) {
pS = (PSCORES) l;
if(n==0) {
snprintf(strtmp,sizeof(strtmp)-1,"%s,%d,%s,%d,%d",pS->name,pS->color,pS->date,pS->stats.score,pS->human );
n=1;
} else
snprintf(strtmp,sizeof(strtmp)-1,"\\%s,%d,%s,%d,%d",pS->name,pS->color,pS->date,pS->stats.score,pS->human );
strtmp[ sizeof(strtmp) -1 ] = 0;
strcat(strout,strtmp);
/* only the top ten */
if( ++max == 10 )
break;
l = LIST_NEXT(l);
}
return TEG_STATUS_SUCCESS;
}
|