summaryrefslogtreecommitdiff
path: root/src/server/highScore.c
blob: 75323fa5896546c0b7272386edd447a9d5e373ca (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
#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 high_score_init(char *file)
{
	int i;

	textFile = text_file_load(file);

	if (textFile == NULL) {
		error("Unable to load high score [%s]", file);
		debug("Creating high score file [%s]", file);
		textFile = text_file_new(file);
	} else {
		debug("Loading high score file [%s]", file);
		return;
	}

	if (textFile == NULL) {
		error("Unable to create high score file [%s]", file);
		return;
	}

	for (i = 0; i < HIGHSCORE_MAX_PLAYERS; i++) {
		list_add(textFile->text, strdup("--- 0"));
	}

	text_file_save(textFile);
}

int table_add(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);
			list_ins(textFile->text, i, strdup(new));
			list_del_item(textFile->text, HIGHSCORE_MAX_PLAYERS, free);
			/*text_file_print(textFile);*/
			text_file_save(textFile);

			return 0;
		}
	}

	return -1;
}

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

	return NULL;
}

void high_score_quit()
{
	if (textFile != NULL) {
		text_file_save(textFile);
		text_file_destroy(textFile);
	}
}