summaryrefslogtreecommitdiff
path: root/src/server/highScore.c
blob: 37c87b4a88e97f6d5c6c00b9b497c41f0efa93ba (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

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

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

static textFile_t *textFile;

void initHighScore(char *file)
{
	int i;

	textFile = loadTextFile(file);

	if (textFile == NULL) {
		fprintf(stderr, _("I am unable to load: \"%s\"!\n"), file);
		fprintf(stderr, _("Creating: \"%s\"\n"), file);
		textFile = newTextFile(file);
	} else {
#ifdef DEBUG
		printf(_("Scorefile: \"%s\"\n"), file);
#endif
		return;
	}

	if (textFile == NULL) {
		fprintf(stderr, _("I was unable to create: \"%s\"!\n"), file);
		return;
	}

	for (i = 0; i < HIGHSCORE_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 < HIGHSCORE_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, HIGHSCORE_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 quitHighScore()
{
	if (textFile != NULL) {
		saveTextFile(textFile);
		destroyTextFile(textFile);
	}
}