git repos / blockattack-game

blame: source/code/highscore.cpp

normal view · raw

ecef5838 sago007 2015-11-24 20:01 1
/*
ecef5838 sago007 2015-11-24 20:01 2
===========================================================================
ecef5838 sago007 2015-11-24 20:01 3
blockattack - Block Attack - Rise of the Blocks
ecef5838 sago007 2015-11-24 20:01 4
Copyright (C) 2005-2012 Poul Sander
ecef5838 sago007 2015-11-24 20:01 5
ecef5838 sago007 2015-11-24 20:01 6
This program is free software: you can redistribute it and/or modify
ecef5838 sago007 2015-11-24 20:01 7
it under the terms of the GNU General Public License as published by
ecef5838 sago007 2015-11-24 20:01 8
the Free Software Foundation, either version 2 of the License, or
ecef5838 sago007 2015-11-24 20:01 9
(at your option) any later version.
ecef5838 sago007 2015-11-24 20:01 10
ecef5838 sago007 2015-11-24 20:01 11
This program is distributed in the hope that it will be useful,
ecef5838 sago007 2015-11-24 20:01 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
ecef5838 sago007 2015-11-24 20:01 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ecef5838 sago007 2015-11-24 20:01 14
GNU General Public License for more details.
ecef5838 sago007 2015-11-24 20:01 15
ecef5838 sago007 2015-11-24 20:01 16
You should have received a copy of the GNU General Public License
ecef5838 sago007 2015-11-24 20:01 17
along with this program.  If not, see http://www.gnu.org/licenses/
ecef5838 sago007 2015-11-24 20:01 18
ecef5838 sago007 2015-11-24 20:01 19
Source information and contacts persons can be found at
021de090 sago007 2015-12-30 18:56 20
http://blockattack.net
ecef5838 sago007 2015-11-24 20:01 21
===========================================================================
ecef5838 sago007 2015-11-24 20:01 22
*/
ecef5838 sago007 2015-11-24 20:01 23
ecef5838 sago007 2015-11-24 20:01 24
#include "highscore.h"
1f50b970 sago007 2015-12-30 21:00 25
#include "os.hpp"
3647d72c sago007 2016-02-12 22:24 26
#include "Libs/physfs.hpp"
ecef5838 sago007 2015-11-24 20:01 27
ecef5838 sago007 2015-11-24 20:01 28
using namespace std;
ecef5838 sago007 2015-11-24 20:01 29
ecef5838 sago007 2015-11-24 20:01 30
Highscore::Highscore(int type) {
3647d72c sago007 2016-02-12 22:24 31
	string filename1 = "endless.dat"; 
3647d72c sago007 2016-02-12 22:24 32
	string filename2 = "timetrial.dat"; 
ecef5838 sago007 2015-11-24 20:01 33
	ourType = type;
ecef5838 sago007 2015-11-24 20:01 34
	if (type == 1) {
ecef5838 sago007 2015-11-24 20:01 35
		filename = filename1;
ecef5838 sago007 2015-11-24 20:01 36
	}
ecef5838 sago007 2015-11-24 20:01 37
	if (type == 2) {
ecef5838 sago007 2015-11-24 20:01 38
		filename = filename2;
ecef5838 sago007 2015-11-24 20:01 39
	}
3647d72c sago007 2016-02-12 22:24 40
	PhysFS::ifstream scorefile(filename.c_str());
ecef5838 sago007 2015-11-24 20:01 41
	if (scorefile) {
ecef5838 sago007 2015-11-24 20:01 42
		for (int i = 0; i<top; i++) {
ecef5838 sago007 2015-11-24 20:01 43
			scorefile.read(tabel[i].name,30*sizeof(char));
ecef5838 sago007 2015-11-24 20:01 44
			scorefile.read(reinterpret_cast<char*>(&tabel[i].score), sizeof(int));
ecef5838 sago007 2015-11-24 20:01 45
		}
ecef5838 sago007 2015-11-24 20:01 46
	}
ecef5838 sago007 2015-11-24 20:01 47
	else {
ecef5838 sago007 2015-11-24 20:01 48
		for (int i = 0; i<top; i++) {
ecef5838 sago007 2015-11-24 20:01 49
			strcpy(tabel[i].name,"Poul Sander                  \0");
ecef5838 sago007 2015-11-24 20:01 50
			tabel[i].score = 2000 - i*100;
ecef5838 sago007 2015-11-24 20:01 51
		}
ecef5838 sago007 2015-11-24 20:01 52
	}
ecef5838 sago007 2015-11-24 20:01 53
	writeFile();
ecef5838 sago007 2015-11-24 20:01 54
}
ecef5838 sago007 2015-11-24 20:01 55
ecef5838 sago007 2015-11-24 20:01 56
void Highscore::writeFile() {
3647d72c sago007 2016-02-12 22:24 57
	string filename1 = "endless.dat";
3647d72c sago007 2016-02-12 22:24 58
	string filename2 = "timetrial.dat"; 
ecef5838 sago007 2015-11-24 20:01 59
	if (ourType == 1) {
ecef5838 sago007 2015-11-24 20:01 60
		filename = filename1;
ecef5838 sago007 2015-11-24 20:01 61
	}
ecef5838 sago007 2015-11-24 20:01 62
	if (ourType == 2) {
ecef5838 sago007 2015-11-24 20:01 63
		filename = filename2;
ecef5838 sago007 2015-11-24 20:01 64
	}
ecef5838 sago007 2015-11-24 20:01 65
3647d72c sago007 2016-02-12 22:24 66
	PhysFS::ofstream outfile(filename);
ecef5838 sago007 2015-11-24 20:01 67
	if (!outfile) {
ecef5838 sago007 2015-11-24 20:01 68
		cout << "Error writing to file: " << filename << endl;
ecef5838 sago007 2015-11-24 20:01 69
		exit(1);
ecef5838 sago007 2015-11-24 20:01 70
	}
ecef5838 sago007 2015-11-24 20:01 71
	for (int i = 0; i<top; i++) {
ecef5838 sago007 2015-11-24 20:01 72
		outfile.write(tabel[i].name,30*sizeof(char));
ecef5838 sago007 2015-11-24 20:01 73
		outfile.write(reinterpret_cast<char*>(&tabel[i].score),sizeof(int));
ecef5838 sago007 2015-11-24 20:01 74
	}
ecef5838 sago007 2015-11-24 20:01 75
}
ecef5838 sago007 2015-11-24 20:01 76
ecef5838 sago007 2015-11-24 20:01 77
bool Highscore::isHighScore(int newScore) {
ecef5838 sago007 2015-11-24 20:01 78
	if (newScore>tabel[top-1].score) {
ecef5838 sago007 2015-11-24 20:01 79
		return true;
ecef5838 sago007 2015-11-24 20:01 80
	}
ecef5838 sago007 2015-11-24 20:01 81
	else {
ecef5838 sago007 2015-11-24 20:01 82
		return false;
ecef5838 sago007 2015-11-24 20:01 83
	}
ecef5838 sago007 2015-11-24 20:01 84
}
ecef5838 sago007 2015-11-24 20:01 85
ecef5838 sago007 2015-11-24 20:01 86
void Highscore::addScore(const string& newName, int newScore) {
ecef5838 sago007 2015-11-24 20:01 87
	int ranking = top-1;
dfdb7d66 sago007 2015-11-29 19:21 88
	while ( (ranking != 0) && (tabel[ranking-1].score<newScore)) {
ecef5838 sago007 2015-11-24 20:01 89
		ranking--;
ecef5838 sago007 2015-11-24 20:01 90
	}
ecef5838 sago007 2015-11-24 20:01 91
	for (int i=top-1; i>ranking; i--) {
ecef5838 sago007 2015-11-24 20:01 92
		tabel[i].score = tabel[i-1].score;
ecef5838 sago007 2015-11-24 20:01 93
		strcpy(tabel[i].name,tabel[i-1].name);
ecef5838 sago007 2015-11-24 20:01 94
	}
ecef5838 sago007 2015-11-24 20:01 95
	tabel[ranking].score = newScore;
ecef5838 sago007 2015-11-24 20:01 96
	snprintf(tabel[ranking].name, sizeof(tabel[ranking].name), "%s", newName.c_str());
ecef5838 sago007 2015-11-24 20:01 97
	Highscore::writeFile();
ecef5838 sago007 2015-11-24 20:01 98
}
ecef5838 sago007 2015-11-24 20:01 99
ecef5838 sago007 2015-11-24 20:01 100
int Highscore::getScoreNumber(int room) {
ecef5838 sago007 2015-11-24 20:01 101
	return tabel[room].score;
ecef5838 sago007 2015-11-24 20:01 102
}
ecef5838 sago007 2015-11-24 20:01 103
02428d87 sago007 2016-02-02 17:53 104
const char* Highscore::getScoreName(int room) {
ecef5838 sago007 2015-11-24 20:01 105
	return &tabel[room].name[0];
ecef5838 sago007 2015-11-24 20:01 106
}
1970-01-01 00:00 107