-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiscore.c
154 lines (126 loc) · 3.52 KB
/
hiscore.c
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
/*****************************************************************************
*
* lines -- a game
*
* (c) 1999 Albertas Agejevas <[email protected]>
*
* This program is free software. You can use it under the conditions of
* the GNU General Public Licence version 2
*
*
*****************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "lines.h"
#include "hiscore.h"
void hiscore_ok(GtkWidget *widget, gpointer data);
void show_hiscore();
int read_hiscore();
int write_hiscore();
void add_hiscore(int score);
static GtkWidget *list;
void game_over()
{
show_hiscore();
add_hiscore(score);
write_hiscore();
reset();
}
void hiscore_ok(GtkWidget *widget, gpointer data){
gtk_widget_destroy(GTK_WIDGET(data));
}
void show_hiscore()
{
GtkWidget *hiscore, *ok;
hiscore = gtk_dialog_new();
ok = gtk_button_new_with_label(_("OK"));
list = gtk_clist_new(3);
gtk_clist_set_column_title(GTK_CLIST(list), 0, _("Username"));
gtk_clist_set_column_title(GTK_CLIST(list), 1, _("Date"));
gtk_clist_set_column_title(GTK_CLIST(list), 2, _("Score"));
gtk_clist_column_titles_show(GTK_CLIST(list));
gtk_clist_set_column_width(GTK_CLIST(list),0,6 * strlen(_("Username")));
gtk_clist_set_column_width(GTK_CLIST(list),1,170);
gtk_clist_set_column_width(GTK_CLIST(list),2,8 * strlen(_("Score")));
gtk_container_add(GTK_CONTAINER(GTK_DIALOG(hiscore)->vbox), list);
gtk_container_add(GTK_CONTAINER(GTK_DIALOG(hiscore)->action_area), ok);
gtk_window_set_modal(GTK_WINDOW(hiscore), TRUE);
gtk_signal_connect(GTK_OBJECT(ok), "clicked", (GCallback) hiscore_ok, hiscore);
read_hiscore();
gtk_widget_show_all(hiscore);
}
int read_hiscore()
{
FILE *f;
int count = 0;
gchar *buffer[3];
char buf[80];
int i;
if(!(f = fopen(SCOREFILE, "r")))
return 0;
for (i=0; i < HISCORESIZE; i++) {
fgets(buf, sizeof buf, f);
buffer[0] = strtok(buf, "\t\n");
buffer[1] = strtok(NULL, "\t\n");
buffer[2] = strtok(NULL, "\t\n");
if (!buffer[0] || !buffer[1] || !buffer[2]) {
buffer[0] = buffer[1] = buffer[2] = "-";
}
if (buffer[0] && *buffer[0]) {
gtk_clist_append(GTK_CLIST(list), buffer);
}
*buf = 0;
}
fclose(f);
return count;
}
void add_hiscore(int score)
{
gchar scorebuf[80];
gchar *scorestr;
time_t t;
GtkStyle *style;
GdkColor color;
gchar *buffer[3];
int i;
for (i=0; i < HISCORESIZE; i++) {
gtk_clist_get_text(GTK_CLIST(list), i, 2, &scorestr);
if (score > atoi(scorestr)) {
color.red = 65535;
color.green = 0;
color.blue = 0;
style = gtk_style_new();
style->fg[GTK_STATE_NORMAL] = color;
sprintf(scorebuf, "%d", score);
buffer[2] = scorebuf;
t = time(NULL);
buffer[1] = ctime(&t);
buffer[0] = getenv("USER");
buffer[1][strlen(buffer[1]) - 1] = 0;
gtk_clist_insert(GTK_CLIST(list), i, buffer);
gtk_clist_set_row_style(GTK_CLIST(list), i, style);
gtk_clist_remove(GTK_CLIST(list), HISCORESIZE);
return;
}
}
}
int write_hiscore()
{
FILE *f;
int i;
gchar *name, *date, *score;
if(!(f = fopen(SCOREFILE, "w")))
return 0;
for(i = 0; i < GTK_CLIST(list)->rows; i++)
{
gtk_clist_get_text(GTK_CLIST(list), i, 0, &name);
gtk_clist_get_text(GTK_CLIST(list), i, 1, &date);
gtk_clist_get_text(GTK_CLIST(list), i, 2, &score);
fprintf( f, "%s\t%s\t%s\n", name, date, score);
}
fclose(f);
return i;
}