summaryrefslogtreecommitdiffstats
path: root/kmahjongg/HighScore.cpp
blob: 637fdf259b575439a195db7d9c3d370c71eb9fc8 (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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include <unistd.h>
#include "HighScore.h"
#include "HighScore.moc"


#include <tqlabel.h>
#include <tqfileinfo.h>
#include <kmessagebox.h>
#include "klocale.h"
#include <kstandarddirs.h>                       
#include <kiconloader.h>
#include <tqlineedit.h>
#include <tqcombobox.h>
#include <kapplication.h>
#include <kpushbutton.h>
#include <kstdguiitem.h>

static const TQString highScoreMagic1_0 = "kmahjongg-scores-v1.0";
static const TQString highScoreMagic1_1 = "kmahjongg-scores-v1.1";  

static const char * highScoreFilename = "/kmahjonggHiscores";

const char * defNames[numScores] = {
    "David Black",
    "Mathias Mueller",
    "Osvaldo Stark",
    "Steve Taylor",
    "Clare Brizzolara",
    "Angela Simpson",
    "Michael O'Brien",
    "Kelvin Bell",
    "Jenifferi O'Keeffe",
    "Phil Lamdin"
};

int defScores[numScores] = 
	{400, 350, 300, 250, 200, 150, 100, 50, 20, 10}; 
 
const int ages = 59+(59*60)+(2*60*60);    
int defTimes[numScores] = {ages, ages-1, ages-2, ages-3,  
				ages-4, ages-5, ages-6, ages-7, ages-8, ages-9};


HighScore::HighScore
(
	TQWidget* parent,
	const char* name
)
	:
	TQDialog( parent, name, true, 0 )
{

	// form the target name


	filename = locateLocal("appdata", highScoreFilename);

	TQFont fnt;
	// Number
	TQLabel* qtarch_Label_3;
	qtarch_Label_3 = new TQLabel( this, "Label_3" );
	qtarch_Label_3->setGeometry( 10, 45, 30, 30 );
	qtarch_Label_3->setFrameStyle( 50 );
	qtarch_Label_3->setText( i18n("Pos") );
	qtarch_Label_3->setAlignment( AlignCenter );
	fnt = qtarch_Label_3->font();
	fnt.setBold(true);
	qtarch_Label_3->setFont(fnt);


	// name

	TQLabel* qtarch_Label_4;
	qtarch_Label_4 = new TQLabel( this, "Label_4" );
	qtarch_Label_4->setGeometry( 40, 45, 150, 30 );
	qtarch_Label_4->setFrameStyle( 50 );
	qtarch_Label_4->setText( i18n("Name") );
	qtarch_Label_4->setFont(fnt);


	// board number
	TQLabel* boardTitle;
	boardTitle= new TQLabel( this, "" );
	boardTitle->setGeometry( 190, 45, 80, 30 );
	boardTitle->setFrameStyle( 50 );
	boardTitle->setText( i18n("Board") );
	boardTitle->setFont(fnt);

	// score
	TQLabel* qtarch_Label_5;
	qtarch_Label_5 = new TQLabel( this, "Label_5" );
	qtarch_Label_5->setGeometry( 270, 45, 70, 30 );
	qtarch_Label_5->setFrameStyle( 50 );
	qtarch_Label_5->setText( i18n("Score") );
	qtarch_Label_5->setFont(fnt);

        // time
        TQLabel* qtarch_Label_6;
        qtarch_Label_6 = new TQLabel( this, "Label_6" );
        qtarch_Label_6->setGeometry( 340, 45, 70, 30 );
        qtarch_Label_6->setFrameStyle( 50 );
        qtarch_Label_6->setText( i18n("Time") );
        qtarch_Label_6->setFont(fnt); 



	for (int row=0; row<numScores; row++)
		addRow(row);

	TQPushButton* qtarch_PushButton_1;
	qtarch_PushButton_1 = new KPushButton( KStdGuiItem::ok(), this, "PushButton_1" );
	qtarch_PushButton_1->setGeometry( 110+35, 340+50, 100, 30 );
	qtarch_PushButton_1->setMinimumSize( 0, 0 );
	qtarch_PushButton_1->setMaximumSize( 32767, 32767 );
	qtarch_PushButton_1->setFocusPolicy( TQ_TabFocus );
	qtarch_PushButton_1->setAutoRepeat( false );
        qtarch_PushButton_1->setAutoResize( false );
        qtarch_PushButton_1->setDefault(true);

	TQPushButton* resetBtn;
	resetBtn= new TQPushButton( this, "resetBtn" );
	resetBtn->setGeometry( 10, 5, 25, 25);
	resetBtn->setMinimumSize( 0, 0 );
	resetBtn->setMaximumSize( 32767, 32767 );
	resetBtn->setFocusPolicy( TQ_TabFocus );
	//resetBtn->setText(i18n( "Reset" ));
	resetBtn->setAutoRepeat( false );
	resetBtn->setAutoResize( false );

 	KIconLoader *loader = KGlobal::iconLoader();
        resetBtn->setPixmap(loader->loadIcon("editdelete", KIcon::Toolbar));
       


	/* We create the ediat area for the hi score name entry and move it */
	/* off screen. it is moved over and placed in position when a */
	/* new name is added */

	lineEdit = new TQLineEdit(this, "");
	lineEdit->setGeometry( 50, 40+(20*30), 190, 30 );
	lineEdit->setFocusPolicy(TQ_StrongFocus);
	lineEdit->setFrame(true);
	lineEdit->setEchoMode(TQLineEdit::Normal);
	lineEdit->setText("");

	// the drop down for the board names

        combo = new TQComboBox( false, this, "combo" );
        combo->setGeometry( 65, 5, 220, 25 );
        combo->setMinimumSize( 0, 0 );
        combo->setMaximumSize( 32767, 32767 );
        combo->setFocusPolicy( TQ_StrongFocus );
        combo->setSizeLimit( 10 );
        combo->setAutoResize( false );
        connect( combo, TQT_SIGNAL(activated(int)), TQT_SLOT(selectionChanged(int)) );


	resize( 350+70,390+45 );
	setFixedSize(350+70,390+45);

	tables = NULL; 
	loadTables();
	currTable = tables;

	setCaption(kapp->makeStdCaption(i18n("Scores")));

	selectedLine = -1;	

	connect(lineEdit, TQT_SIGNAL( textChanged(const TQString &)),
					  TQT_SLOT( nameChanged(const TQString &)));


	connect(qtarch_PushButton_1, TQT_SIGNAL(clicked()), TQT_SLOT(reject()));
	connect(resetBtn, TQT_SIGNAL(clicked()), TQT_SLOT(reset()));
}

// free up the table structures
	
HighScore::~HighScore()
{
	TableInstance *t, *t1;

	if (tables != NULL) {
		t = tables;
		while(t != NULL)
		{
			t1 = t;
			t = t -> next;
			delete t1;
		}
	}
	tables = NULL;
}



// return a pointer to a linked list of table entries from the
// saved hi score tables file. If no such file exists then
// return a single default hiscore table.

void HighScore::loadTables() {
	char buff[1024];

	// open the file, on error set up the default table
	FILE *fp = fopen( TQFile::encodeName(highScoreFile()), "r");
	if (fp == NULL) 
		goto error;	

	// check magic
 	fscanf(fp, "%1023s\n", buff);
	if (highScoreMagic1_1 != buff) {
		goto error;	
	}

	int num;
	fscanf(fp, "%d\n", &num);

	for (int p=0; p<num; p++) {
		TableInstance *t = new TableInstance;
		t->next = tables;


		tables = t;

		fgets(buff, sizeof(buff), fp);
		if (buff[strlen(buff)-1] == '\n')
			buff[strlen(buff)-1] = '\0';
		t->name = buff;
		combo->insertItem(t->name);
		setComboTo(t->name);
		for (int e=0; e<numScores; e++) {
			fscanf(fp, "%ld\n", &(t->entries[e].score)); 
			fscanf(fp, "%ld\n", &(t->entries[e].elapsed));
			fscanf(fp, "%ld\n", &(t->entries[e].board));
			fgets(buff, sizeof(buff), fp);
			if (buff[strlen(buff)-1] == '\n')
				buff[strlen(buff)-1] = '\0';
			t->entries[e].name=TQString::fromUtf8(buff,-1);
		}
	}	


	fclose(fp);
	return;

	
error:
	selectTable("default");
	saveTables();
	return;
}

void HighScore::saveTables() {

	TableInstance *p;
	int	      num = 0;

	// Nothing to do
	if (tables == NULL)
		return;


	// open the outrput file, with naff error handling
	FILE *fp = fopen( TQFile::encodeName(highScoreFile()), "w");
	if (fp == NULL)
		return;
	
	// count up the number of tables to save
 	for (p=tables; p != NULL; p = p->next)
		num++;

	// output the file magic
	fprintf(fp,"%s\n", highScoreMagic1_1.utf8().data());
	
	// output the count of tables to process
	fprintf(fp, "%d\n", num);

	// output each table
	for (p=tables; p != NULL; p = p->next) {
		fprintf(fp, "%s\n", p->name.utf8().data());
		for (int e=0; e<numScores; e++) {
			fprintf(fp,"%ld\n%ld\n%ld\n%s\n", 
				p->entries[e].score,
				p->entries[e].elapsed,
				p->entries[e].board,
				p->entries[e].name.utf8().data());
		}
	}
	fclose(fp);	

}

// traverse the list of hi score tables and set the
// current table to the specified board. Create it if it does not 
// exist.

void HighScore::selectTable(const TQString &board) {

	TableInstance *pos = tables;


	while (pos != NULL) {
		if (pos->name == board)
			break;
		pos = pos->next;
	}	



	if (pos == NULL) {
		// not found, add new board to the front of the list
	        TableInstance *n = new TableInstance;
        	n->next = tables;
       	 	n->name = board; 



        	for (int p =0; p < numScores; p ++) {
                	n->entries[p].name = defNames[p];
                	n->entries[p].score = defScores[p];
                	n->entries[p].board = 928364243l+(p *3); 
			n->entries[p].elapsed = defTimes[p];
        	}
		tables = n;
		currTable = n;
		combo->insertItem(board);
		setComboTo(board);
		return;
	}
	currTable = pos;
	setComboTo(board);
	return;
}



void HighScore::addRow(int num) {
	TQFont tmp;

	// game number
	numbersWidgets[num] = new TQLabel( this);
	numbersWidgets[num]->setGeometry( 10, 75+(num*30), 30, 30 );
	numbersWidgets[num]->setFrameStyle( 50 );
	numbersWidgets[num]->setAlignment( AlignRight | AlignVCenter );
	numbersWidgets[num]->setNum(num+1);


	// name
	namesWidgets[num] = new TQLabel( this);
	namesWidgets[num]->setGeometry( 40, 75+(num*30), 150, 30 );
	namesWidgets[num]->setFrameStyle( 50 );
	namesWidgets[num]->setAlignment( 289 );
	
	// board 
	boardWidgets[num] = new TQLabel( this);
	boardWidgets[num]->setGeometry( 190, 75+(num*30), 80, 30 );
	boardWidgets[num]->setFrameStyle( 50 );
	boardWidgets[num]->setAlignment( 289 );

	// score
	scoresWidgets[num] = new TQLabel( this);
	scoresWidgets[num]->setGeometry( 270, 75+(num*30), 70, 30 );
	scoresWidgets[num]->setFrameStyle( 50 );
	tmp = scoresWidgets[num]->font();
	tmp.setItalic(true);
	scoresWidgets[num]->setFont(tmp);

        // elapsed time
        elapsedWidgets[num] = new TQLabel( this);
        elapsedWidgets[num]->setGeometry( 270+70, 75+(num*30), 70, 30 );
        elapsedWidgets[num]->setFrameStyle( 50 );
        tmp = elapsedWidgets[num]->font();
        tmp.setItalic(true);
        elapsedWidgets[num]->setFont(tmp);    

	
}

void HighScore::copyTableToScreen(const TQString &name) {
	char buff[256];
	TQString base;
	getBoardName(name, base);
	selectTable(base);
	for (int p=0; p<numScores;p++) {
		scoresWidgets[p]->setNum((int)currTable->entries[p].score);
		namesWidgets[p]->setText(currTable->entries[p].name);
		boardWidgets[p]->setNum((int)currTable->entries[p].board);

                int e = currTable->entries[p].elapsed;
                int s = e % 60;
                e = e-s;
                int m = (e % (60*60)) / 60;
                e = e - (e % (60*60));
                int h = (e % (60*60*60)) / (60*60);
                sprintf(buff, "%2.2d:%2.2d:%2.2d", h, m , s);
                elapsedWidgets[p]->setText(buff);   

	}
	repaint(false);
}

int HighScore::exec(TQString &layout) {
	copyTableToScreen(layout);	
	return(TQDialog::exec());
}

void HighScore::checkHighScore(int s, int e, long gameNum, TQString &name) {
	int pos;

	TQString board;
	getBoardName(name, board);

	// make this board name the current one!
	// creates it if it does not exist
	selectTable(board);

	
	for (pos=0; pos <numScores; pos++) {
		if (s > currTable->entries[pos].score) {
			break;
		}
	}
	if (pos >= numScores) {
		return;
	}
	for (int move= numScores-1; move >pos; move--) {
	    currTable->entries[move].score = currTable->entries[move-1].score;
	    currTable->entries[move].name = currTable->entries[move-1].name;
	    currTable->entries[move].board = currTable->entries[move-1].board;
	    currTable->entries[move].elapsed = currTable->entries[move-1].elapsed;
	}

	currTable->entries[pos].score = s;
	currTable->entries[pos].board = gameNum;
	currTable->entries[pos].elapsed = e;

	lineEdit->setEnabled(true);
	lineEdit->setGeometry( 40, 75+(pos*30), 150, 30 ); 	
	lineEdit->setFocus();
	lineEdit->setText("");
	selectedLine = pos;
	nameChanged("");

	// no board change when entering data
	combo->setEnabled(false);
	exec(board);
	combo->setEnabled(true);

	selectedLine = -1;
	lineEdit->setGeometry( 40, 75+(20*30), 150, 30);
	lineEdit->setEnabled(false);
	
	// sync the hiscore table to disk now
	saveTables();
	
}

void HighScore::nameChanged(const TQString &s) {

	if (selectedLine == -1)
		return;
		
	if (s.isEmpty())
		currTable->entries[selectedLine].name = 
			i18n("Anonymous");
	else
		currTable->entries[selectedLine].name = s;
}


void HighScore::getBoardName(TQString in, TQString &out) {

            TQFileInfo fi( in );
            out = fi.baseName();
}

void HighScore::setComboTo(const TQString &to) {
	for (int p=0; p<combo->count(); p++) {
		if (combo->text(p) == to)
			combo->setCurrentItem(p);
	}
}


void HighScore::selectionChanged(int ) {
	copyTableToScreen(combo->currentText());

}


// reset the high score table. Caution the user
// before acting


void HighScore::reset() {

        int res=KMessageBox::warningContinueCancel(this,
                        i18n("Resetting the high scores will "
                                           "remove all high score entries "
                                           "both in memory and on disk. Do "
					   "you wish to proceed?"),
                        		   i18n("Reset High Scores"),i18n("Reset"));
        if (res != KMessageBox::Continue)
                return ;   

	// delete the file
	res = unlink( TQFile::encodeName(highScoreFile()));
	
	// wipe ou the in memory list of tables	
	TableInstance *t, *d;

	if (tables != NULL) {
	    t = tables;
	    while (t != NULL) {
		d = t;
		t = t->next;
		d->next=0;
		delete d;
	    }

	}
	
	// set the list empty
	tables = NULL;
	currTable=NULL;	

	// clear out the combobox
	combo->clear();
	
	// stick in a default
	selectTable("default");

	// make sure tha the on screen data does not
	// point to deleted data
	copyTableToScreen("default");
}

TQString &HighScore::highScoreFile() {
	return filename;

}