summaryrefslogtreecommitdiff
path: root/src/server/heightScore.c
blob: e29fad7d7dbd0fa96f1ffb29b63cd936de83eb70 (plain)
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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "main.h"
#include "list.h"
#include "textFile.h"
#include "heightScore.h"

static textFile_t *textFile;

void initHeightScore(char *file)
{
	int i;

	textFile = loadTextFile(file);

	if( textFile == NULL )
	{
		fprintf(stderr, "I can't load %s !\n", file);
		fprintf(stderr, "I create %s\n", file);
		textFile = newTextFile(file);
	}
	else
	{
		printf("I use %s as scorefile\n", file);
		return;
	}

	if( textFile == NULL )
	{
		fprintf(stderr, "I can't create %s!\n", file);
		return;
	}

	for( i = 0 ; i < HEIGHTSCORE_MAX_PLAYERS ; i++ )
	{
		addList(textFile->text, strdup("no_name 0") );
	}

	saveTextFile(textFile);
}

int addPlayerInHighScore(char *name, int score)
{
	int i;

	if( score <= 0 )
	{
		return -1; // Ha ha ha
	}

	for( i = 0 ; i < HEIGHTSCORE_MAX_PLAYERS ; i++ )
	{
		char *line;
		char thisName[STR_NAME_SIZE];
		int thisCore;

		line = (char *) textFile->text->list[i];
		sscanf(line, "%s %d", thisName, &thisCore);

		if( score >= thisCore )
		{
			char new[STR_SIZE];
			
			sprintf(new, "%s %d", name, score);
			insList(textFile->text, i, strdup(new) );
			delListItem(textFile->text, HEIGHTSCORE_MAX_PLAYERS, free);
			//printTextFile(textFile);
			saveTextFile(textFile);

			return 0;
		}
	}

	return -1;
}

char* getTableItem(int index)
{
	if( textFile != NULL && 
	    index >= 0 &&
	    index < textFile->text->count )
	{
		return (char *)textFile->text->list[index];
	}

	return NULL;
}

void quitHeightScore()
{
	if( textFile != NULL )
	{
		saveTextFile(textFile);
		destroyTextFile(textFile);
	}
}