From 7d1ff3a1df47d07eb0912507c1b9d0dda9136c34 Mon Sep 17 00:00:00 2001
From: prinsij <prinsij@mcmaster.ca>
Date: Wed, 19 Oct 2016 17:22:38 -0500
Subject: [PATCH] record some extra information for the death screen

---
 src/include/ripscreen.h |  3 ++-
 src/playstate.cpp       |  2 +-
 src/ripscreen.cpp       | 21 +++++++++++++++------
 src/scores.txt          |  5 +----
 4 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/src/include/ripscreen.h b/src/include/ripscreen.h
index afcd314..af3497a 100644
--- a/src/include/ripscreen.h
+++ b/src/include/ripscreen.h
@@ -2,6 +2,7 @@
 #include "uistate.h"
 #include "playerchar.h"
 #include "../libtcod/include/libtcod.hpp"
+#include "level.h"
 #include <vector>
 #include <string>
 #include <sstream>
@@ -10,7 +11,7 @@ struct ScoreItem;
 
 class RIPScreen : public UIState {
 	public:
-		RIPScreen(PlayerChar*);
+		RIPScreen(PlayerChar*, Level*, std::string);
 		virtual void draw(TCODConsole*);
 		virtual UIState* handleInput(TCOD_key_t);
 	private:
diff --git a/src/playstate.cpp b/src/playstate.cpp
index 217973a..5f38bcc 100644
--- a/src/playstate.cpp
+++ b/src/playstate.cpp
@@ -53,7 +53,7 @@ class QuitPrompt : public Prompt {
 
 		virtual Transition handleInput(TCOD_key_t key) {
 			if (key.c == 'y' || key.c == 'Y') {
-				return Transition(NULL, new RIPScreen(player));
+				return Transition(NULL, new RIPScreen(player, level, "retired"));
 			}
 			if (key.c == 'n' || key.c == 'N') {
 				return Transition(NULL, NULL);
diff --git a/src/ripscreen.cpp b/src/ripscreen.cpp
index c872e02..1dd8b34 100644
--- a/src/ripscreen.cpp
+++ b/src/ripscreen.cpp
@@ -6,6 +6,7 @@
 #include <fstream>
 #include <sstream>
 #include <exception>
+#include <algorithm>
 
 struct ScoreItem {
 	ScoreItem(int gold, int depth, std::string name, std::string death)
@@ -48,10 +49,15 @@ struct ScoreItem {
 		}
 		return true;
 	}
+
+	bool operator<(const ScoreItem& other) const {
+		return gold < other.gold;
+	}
 };
 
 
-RIPScreen::RIPScreen(PlayerChar* player)
+RIPScreen::RIPScreen(PlayerChar* player,
+					 Level* level, std::string reason)
 	: player(player)
 {
 	std::ifstream scoreFile(SCORE_FILE);
@@ -68,23 +74,26 @@ RIPScreen::RIPScreen(PlayerChar* player)
 	} else {
 		std::cerr << "could not open " << SCORE_FILE << "\n";
 	}
-	scores.push_back(ScoreItem(player->getGold(), -1, player->getName(), "unknown"));
+	scores.push_back(ScoreItem(player->getGold(), level->getDepth(), player->getName(), reason));
 	// create if not exist, otherwise append
 	std::ofstream outStream(SCORE_FILE, std::ios::app | std::ios::out);
 	if (outStream.is_open()) {
 		outStream << scores.back().encode() << "\n";
 	}
 	outStream.close();
+	delete level;
+	std::sort(scores.begin(), scores.end());
+	std::reverse(scores.begin(), scores.end());
 }
 
 void RIPScreen::draw(TCODConsole* con) {
-	int i = 0;
+	int y = 1;
 	for (ScoreItem item : scores) {
-		con->print(0, i, (std::to_string(item.gold) + ":" + item.name + " " + item.death 
+		con->print(0, y, (std::to_string(item.gold) + ":" + item.name + " " + item.death 
 					+ " on level " + std::to_string(item.depth)).c_str());
-		i++;
+		y++;
 	}
-	con->print(10, 10, std::string("rip").c_str());
+	con->print(0, 0, std::string("rip").c_str());
 }
 
 UIState* RIPScreen::handleInput(TCOD_key_t key) {
diff --git a/src/scores.txt b/src/scores.txt
index 66fcc16..204dae1 100644
--- a/src/scores.txt
+++ b/src/scores.txt
@@ -1,5 +1,2 @@
-0,-1,quark,unknown,
-0,-1,qwerty,unknown
-0,-1,brucethebold,unknown
-0,-1,arglebargle,unknown
 1350,8,robert the mediocre,was killed by a grue
+0,0,jordy,retired
-- 
GitLab