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"
6e1cb0b4 sago007 2016-02-13 09:26 26
#include "physfs_stream.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
	}
6e1cb0b4 sago007 2016-02-13 09:26 53
	scorefile.close();
ecef5838 sago007 2015-11-24 20:01 54
	writeFile();
ecef5838 sago007 2015-11-24 20:01 55
}
ecef5838 sago007 2015-11-24 20:01 56
ecef5838 sago007 2015-11-24 20:01 57
void Highscore::writeFile() {
3647d72c sago007 2016-02-12 22:24 58
	string filename1 = "endless.dat";
3647d72c sago007 2016-02-12 22:24 59
	string filename2 = "timetrial.dat"; 
ecef5838 sago007 2015-11-24 20:01 60
	if (ourType == 1) {
ecef5838 sago007 2015-11-24 20:01 61
		filename = filename1;
ecef5838 sago007 2015-11-24 20:01 62
	}
ecef5838 sago007 2015-11-24 20:01 63
	if (ourType == 2) {
ecef5838 sago007 2015-11-24 20:01 64
		filename = filename2;
ecef5838 sago007 2015-11-24 20:01 65
	}
ecef5838 sago007 2015-11-24 20:01 66
59dcb571 sago007 2016-02-14 15:34 67
	PhysFS::ofstream outfile(filename.c_str());
ecef5838 sago007 2015-11-24 20:01 68
	if (!outfile) {
59dcb571 sago007 2016-02-14 15:34 69
		cout << "Error writing to file: " << filename << ", Error: " << PHYSFS_getLastError() << endl;
ecef5838 sago007 2015-11-24 20:01 70
		exit(1);
ecef5838 sago007 2015-11-24 20:01 71
	}
ecef5838 sago007 2015-11-24 20:01 72
	for (int i = 0; i<top; i++) {
ecef5838 sago007 2015-11-24 20:01 73
		outfile.write(tabel[i].name,30*sizeof(char));
ecef5838 sago007 2015-11-24 20:01 74
		outfile.write(reinterpret_cast<char*>(&tabel[i].score),sizeof(int));
ecef5838 sago007 2015-11-24 20:01 75
	}
6e1cb0b4 sago007 2016-02-13 09:26 76
	outfile.close();
ecef5838 sago007 2015-11-24 20:01 77
}
ecef5838 sago007 2015-11-24 20:01 78
ecef5838 sago007 2015-11-24 20:01 79
bool Highscore::isHighScore(int newScore) {
ecef5838 sago007 2015-11-24 20:01 80
	if (newScore>tabel[top-1].score) {
ecef5838 sago007 2015-11-24 20:01 81
		return true;
ecef5838 sago007 2015-11-24 20:01 82
	}
ecef5838 sago007 2015-11-24 20:01 83
	else {
ecef5838 sago007 2015-11-24 20:01 84
		return false;
ecef5838 sago007 2015-11-24 20:01 85
	}
ecef5838 sago007 2015-11-24 20:01 86
}
ecef5838 sago007 2015-11-24 20:01 87
ecef5838 sago007 2015-11-24 20:01 88
void Highscore::addScore(const string& newName, int newScore) {
ecef5838 sago007 2015-11-24 20:01 89
	int ranking = top-1;
dfdb7d66 sago007 2015-11-29 19:21 90
	while ( (ranking != 0) && (tabel[ranking-1].score<newScore)) {
ecef5838 sago007 2015-11-24 20:01 91
		ranking--;
ecef5838 sago007 2015-11-24 20:01 92
	}
ecef5838 sago007 2015-11-24 20:01 93
	for (int i=top-1; i>ranking; i--) {
ecef5838 sago007 2015-11-24 20:01 94
		tabel[i].score = tabel[i-1].score;
ecef5838 sago007 2015-11-24 20:01 95
		strcpy(tabel[i].name,tabel[i-1].name);
ecef5838 sago007 2015-11-24 20:01 96
	}
ecef5838 sago007 2015-11-24 20:01 97
	tabel[ranking].score = newScore;
ecef5838 sago007 2015-11-24 20:01 98
	snprintf(tabel[ranking].name, sizeof(tabel[ranking].name), "%s", newName.c_str());
ecef5838 sago007 2015-11-24 20:01 99
	Highscore::writeFile();
ecef5838 sago007 2015-11-24 20:01 100
}
ecef5838 sago007 2015-11-24 20:01 101
ecef5838 sago007 2015-11-24 20:01 102
int Highscore::getScoreNumber(int room) {
ecef5838 sago007 2015-11-24 20:01 103
	return tabel[room].score;
ecef5838 sago007 2015-11-24 20:01 104
}
ecef5838 sago007 2015-11-24 20:01 105
02428d87 sago007 2016-02-02 17:53 106
const char* Highscore::getScoreName(int room) {
ecef5838 sago007 2015-11-24 20:01 107
	return &tabel[room].name[0];
ecef5838 sago007 2015-11-24 20:01 108
}
1970-01-01 00:00 109