Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
RogueReborn
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mikhail Andrenkov
RogueReborn
Commits
a657e592
Commit
a657e592
authored
8 years ago
by
Ian Prins
Browse files
Options
Downloads
Patches
Plain Diff
Begin tracking/storing/displaying scores
parent
8297c756
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/include/ripscreen.h
+8
-0
8 additions, 0 deletions
src/include/ripscreen.h
src/ripscreen.cpp
+86
-1
86 additions, 1 deletion
src/ripscreen.cpp
with
94 additions
and
1 deletion
src/include/ripscreen.h
+
8
−
0
View file @
a657e592
...
...
@@ -2,11 +2,19 @@
#include
"uistate.h"
#include
"playerchar.h"
#include
"../libtcod/include/libtcod.hpp"
#include
<vector>
#include
<string>
#include
<sstream>
struct
ScoreItem
;
class
RIPScreen
:
public
UIState
{
public:
RIPScreen
(
PlayerChar
*
);
virtual
void
draw
(
TCODConsole
*
);
virtual
UIState
*
handleInput
(
TCOD_key_t
);
private:
PlayerChar
*
player
;
std
::
vector
<
ScoreItem
>
scores
;
const
std
::
string
SCORE_FILE
=
"scores.txt"
;
};
This diff is collapsed.
Click to expand it.
src/ripscreen.cpp
+
86
−
1
View file @
a657e592
...
...
@@ -2,11 +2,96 @@
#include
"include/playerchar.h"
#include
"libtcod/include/libtcod.hpp"
#include
<string>
#include
<iostream>
#include
<fstream>
#include
<sstream>
#include
<exception>
struct
ScoreItem
{
ScoreItem
(
int
gold
,
int
depth
,
std
::
string
name
,
std
::
string
death
)
:
gold
(
gold
)
,
depth
(
depth
)
,
name
(
name
)
,
death
(
death
)
{}
int
gold
,
depth
;
std
::
string
name
,
death
;
static
const
char
DELIM
=
','
;
static
ScoreItem
decode
(
std
::
string
line
)
{
std
::
stringstream
ss
(
line
);
std
::
string
temp
;
if
(
!
readItem
(
ss
,
temp
))
throw
std
::
invalid_argument
(
"bad format"
);
int
gold
=
std
::
stoi
(
temp
);
if
(
!
readItem
(
ss
,
temp
))
throw
std
::
invalid_argument
(
"bad format"
);
int
depth
=
std
::
stoi
(
temp
);
if
(
!
readItem
(
ss
,
temp
))
throw
std
::
invalid_argument
(
"bad format"
);
std
::
string
name
=
temp
;
if
(
!
readItem
(
ss
,
temp
))
throw
std
::
invalid_argument
(
"bad format"
);
std
::
string
death
=
temp
;
return
ScoreItem
(
gold
,
depth
,
name
,
death
);
}
std
::
string
encode
()
{
return
std
::
to_string
(
gold
)
+
DELIM
+
std
::
to_string
(
depth
)
+
DELIM
+
name
+
DELIM
+
death
;
}
static
bool
readItem
(
std
::
stringstream
&
ss
,
std
::
string
&
str
)
{
if
(
!
std
::
getline
(
ss
,
str
,
DELIM
))
{
std
::
cerr
<<
"error parsing score file
\n
"
;
return
false
;
}
return
true
;
}
};
RIPScreen
::
RIPScreen
(
PlayerChar
*
player
)
:
player
(
player
)
{}
{
std
::
ifstream
scoreFile
(
SCORE_FILE
);
std
::
string
line
;
if
(
scoreFile
.
is_open
())
{
while
(
std
::
getline
(
scoreFile
,
line
))
{
try
{
scores
.
push_back
(
ScoreItem
::
decode
(
line
));
}
catch
(
std
::
string
param
)
{
std
::
cerr
<<
param
<<
" while reading "
<<
SCORE_FILE
<<
"
\n
"
;
}
}
scoreFile
.
close
();
}
else
{
std
::
cerr
<<
"could not open "
<<
SCORE_FILE
<<
"
\n
"
;
}
scores
.
push_back
(
ScoreItem
(
player
->
getGold
(),
-
1
,
player
->
getName
(),
"unknown"
));
// 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
();
}
void
RIPScreen
::
draw
(
TCODConsole
*
con
)
{
int
i
=
0
;
for
(
ScoreItem
item
:
scores
)
{
con
->
print
(
0
,
i
,
(
std
::
to_string
(
item
.
gold
)
+
":"
+
item
.
name
+
" "
+
item
.
death
+
" on level "
+
std
::
to_string
(
item
.
depth
)).
c_str
());
i
++
;
}
con
->
print
(
10
,
10
,
std
::
string
(
"rip"
).
c_str
());
}
UIState
*
RIPScreen
::
handleInput
(
TCOD_key_t
key
)
{
if
(
key
.
vk
==
TCODK_ESCAPE
)
{
delete
player
;
player
=
NULL
;
return
NULL
;
}
return
this
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment