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
83d6c18d sago007 2018-03-31 16:28 20
https://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"
7a956470 sago007 2016-02-14 17:09 26
#include "cereal/cereal.hpp"
7a956470 sago007 2016-02-14 17:09 27
#include "cereal/types/vector.hpp"
7a956470 sago007 2016-02-14 17:09 28
#include "cereal/archives/json.hpp"
7a956470 sago007 2016-02-14 17:09 29
#include "sago/SagoMisc.hpp"
c7f2082f sago007 2016-03-13 11:48 30
#include <algorithm>
b5321636 sago007 2019-02-09 18:47 31
#include "common.h"
16476649 Poul Sander 2022-01-09 10:29 32
#include <fmt/core.h>
ecef5838 sago007 2015-11-24 20:01 33
7a956470 sago007 2016-02-14 17:09 34
namespace cereal {
c7f2082f sago007 2016-03-13 11:48 35
7a956470 sago007 2016-02-14 17:09 36
template<class Archive>
c7f2082f sago007 2016-03-13 11:48 37
void save(Archive& archive, record const& m) {
7a956470 sago007 2016-02-14 17:09 38
	archive( cereal::make_nvp("Name", m.name), cereal::make_nvp("Score", m.score) );
7a956470 sago007 2016-02-14 17:09 39
}
ecef5838 sago007 2015-11-24 20:01 40
7a956470 sago007 2016-02-14 17:09 41
template<class Archive>
c7f2082f sago007 2016-03-13 11:48 42
void load(Archive& archive, record& m) {
7a956470 sago007 2016-02-14 17:09 43
	archive( cereal::make_nvp("Name", m.name), cereal::make_nvp("Score", m.score) );
7a956470 sago007 2016-02-14 17:09 44
}
7a956470 sago007 2016-02-14 17:09 45
7a956470 sago007 2016-02-14 17:09 46
}
7a956470 sago007 2016-02-14 17:09 47
7a956470 sago007 2016-02-14 17:09 48
/*
7a956470 sago007 2016-02-14 17:09 49
 This sorts in reverse order. So the highest will be first
7a956470 sago007 2016-02-14 17:09 50
 */
c7f2082f sago007 2016-03-13 11:48 51
bool record_sorter (const record& i,const record& j) {
c7f2082f sago007 2016-03-13 11:48 52
	return (i.score > j.score);
c7f2082f sago007 2016-03-13 11:48 53
}
7a956470 sago007 2016-02-14 17:09 54
4379ec19 Poul Sander 2019-01-30 18:27 55
Highscore::Highscore(const std::string& type, double speed) : filename(type+".json.dat"), type(type), speed(speed) {
b5321636 sago007 2019-02-09 18:47 56
	if (speed < 0.4) {
b5321636 sago007 2019-02-09 18:47 57
		//Use special filenames for higher speeds (higher speed = lower number)
16476649 Poul Sander 2022-01-09 10:29 58
		filename = fmt::format("{}_{:.4f}.json.dat", type, speed);
b5321636 sago007 2019-02-09 18:47 59
	}
7a956470 sago007 2016-02-14 17:09 60
	std::string readFileContent = sago::GetFileContent(filename.c_str());
e567627f Poul Sander 2016-02-17 18:47 61
	if (readFileContent.length() > 0) {
ad2d11ab Poul Sander 2016-02-17 19:06 62
		try {
ad2d11ab Poul Sander 2016-02-17 19:06 63
			std::stringstream ss(readFileContent);
ad2d11ab Poul Sander 2016-02-17 19:06 64
			{
ad2d11ab Poul Sander 2016-02-17 19:06 65
				cereal::JSONInputArchive archive(ss);
ad2d11ab Poul Sander 2016-02-17 19:06 66
				archive(cereal::make_nvp("highscore", table));
ad2d11ab Poul Sander 2016-02-17 19:06 67
			}
ad2d11ab Poul Sander 2016-02-17 19:06 68
		}
c7f2082f sago007 2016-03-13 11:48 69
		catch (cereal::Exception& e) {
ad2d11ab Poul Sander 2016-02-17 19:06 70
			std::cerr << "Failed to read highscore " << filename << " due to formatting errors. Resetting the file. Reason: " <<
161a010c sago007 2016-05-06 14:00 71
			          e.what() << "\n";
ad2d11ab Poul Sander 2016-02-17 19:06 72
			table.clear();
e567627f Poul Sander 2016-02-17 18:47 73
		}
ecef5838 sago007 2015-11-24 20:01 74
	}
7a956470 sago007 2016-02-14 17:09 75
	if (table.size() < top) {
ecef5838 sago007 2015-11-24 20:01 76
		for (int i = 0; i<top; i++) {
7a956470 sago007 2016-02-14 17:09 77
			record r;
7a956470 sago007 2016-02-14 17:09 78
			r.name = "Poul Sander";
7a956470 sago007 2016-02-14 17:09 79
			r.score = 2000 - i*100;
7a956470 sago007 2016-02-14 17:09 80
			table.push_back(r);
ecef5838 sago007 2015-11-24 20:01 81
		}
ecef5838 sago007 2015-11-24 20:01 82
	}
7a956470 sago007 2016-02-14 17:09 83
	std::stable_sort(table.begin(), table.end(), record_sorter);
7a956470 sago007 2016-02-14 17:09 84
	table.resize(top);
ecef5838 sago007 2015-11-24 20:01 85
	writeFile();
ecef5838 sago007 2015-11-24 20:01 86
}
ecef5838 sago007 2015-11-24 20:01 87
ecef5838 sago007 2015-11-24 20:01 88
7a956470 sago007 2016-02-14 17:09 89
void Highscore::writeFile() {
ad2d11ab Poul Sander 2016-02-17 19:06 90
	std::stringstream ss;
7a956470 sago007 2016-02-14 17:09 91
	{
7a956470 sago007 2016-02-14 17:09 92
		cereal::JSONOutputArchive archive(ss);
7a956470 sago007 2016-02-14 17:09 93
		archive(cereal::make_nvp("highscore", table));
ecef5838 sago007 2015-11-24 20:01 94
	}
7a956470 sago007 2016-02-14 17:09 95
	sago::WriteFileContent(filename.c_str(), ss.str());
ecef5838 sago007 2015-11-24 20:01 96
}
ecef5838 sago007 2015-11-24 20:01 97
ecef5838 sago007 2015-11-24 20:01 98
bool Highscore::isHighScore(int newScore) {
7a956470 sago007 2016-02-14 17:09 99
	if (newScore>table.back().score) {
ecef5838 sago007 2015-11-24 20:01 100
		return true;
ecef5838 sago007 2015-11-24 20:01 101
	}
30412ca6 sago007 2016-03-25 19:35 102
	return false;
ecef5838 sago007 2015-11-24 20:01 103
}
ecef5838 sago007 2015-11-24 20:01 104
7a956470 sago007 2016-02-14 17:09 105
void Highscore::addScore(const std::string& newName, int newScore) {
7a956470 sago007 2016-02-14 17:09 106
	record r;
7a956470 sago007 2016-02-14 17:09 107
	r.name = newName;
7a956470 sago007 2016-02-14 17:09 108
	r.score = newScore;
7a956470 sago007 2016-02-14 17:09 109
	table.push_back(r);
7a956470 sago007 2016-02-14 17:09 110
	std::stable_sort(table.begin(), table.end(), record_sorter);
7a956470 sago007 2016-02-14 17:09 111
	table.resize(top);
ecef5838 sago007 2015-11-24 20:01 112
	Highscore::writeFile();
ecef5838 sago007 2015-11-24 20:01 113
}
ecef5838 sago007 2015-11-24 20:01 114
7a956470 sago007 2016-02-14 17:09 115
record Highscore::getScoreNumber(int room) {
7a956470 sago007 2016-02-14 17:09 116
	record ret;
c7f2082f sago007 2016-03-13 11:48 117
	if (room < static_cast<int>(table.size()) ) {
7a956470 sago007 2016-02-14 17:09 118
		ret = table.at(room);
7a956470 sago007 2016-02-14 17:09 119
	}
7a956470 sago007 2016-02-14 17:09 120
	return ret;
ecef5838 sago007 2015-11-24 20:01 121
}
ecef5838 sago007 2015-11-24 20:01 122
1970-01-01 00:00 123