Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Donald Haase
Cockatrice
Commits
53d25e73
Commit
53d25e73
authored
Apr 15, 2015
by
poixen
Browse files
Merge pull request #969 from poixen/game_created
Game created code improvements
parents
698e6cce
25dbfb37
Changes
2
Hide whitespace changes
Inline
Side-by-side
cockatrice/src/gamesmodel.cpp
View file @
53d25e73
...
@@ -10,62 +10,33 @@
...
@@ -10,62 +10,33 @@
enum
GameListColumn
{
ROOM
,
CREATED
,
DESCRIPTION
,
CREATOR
,
GAME_TYPE
,
RESTRICTIONS
,
PLAYERS
,
SPECTATORS
};
enum
GameListColumn
{
ROOM
,
CREATED
,
DESCRIPTION
,
CREATOR
,
GAME_TYPE
,
RESTRICTIONS
,
PLAYERS
,
SPECTATORS
};
namespace
{
const
QString
GamesModel
::
getGameCreatedString
(
const
int
secs
)
const
{
const
int
SECS_PER_MIN
=
60
;
const
int
SECS_PER_HOUR
=
60
*
60
;
QString
ret
;
if
(
secs
<
SECS_PER_MIN
)
/**
ret
=
tr
(
"<1m ago"
);
* Pretty print an integer number of seconds ago. Accurate to only one unit,
else
if
(
secs
<
SECS_PER_MIN
*
5
)
* rounded; <5 minutes and >5 hours are displayed as such. As a special case,
ret
=
tr
(
"<5m ago"
);
* time between 60 and 90 minutes will display both the hours and minutes.
else
if
(
secs
<
SECS_PER_HOUR
)
*
ret
=
QString
::
number
(
secs
/
SECS_PER_MIN
).
append
(
tr
(
"m ago"
));
* For example...
else
if
(
secs
<
SECS_PER_MIN
*
90
)
{
* 0-300 seconds will return "<5m ago"
ret
=
tr
(
"1hr "
).
append
(
QString
::
number
((
secs
/
SECS_PER_MIN
)
-
60
)).
append
(
tr
(
"m ago"
));
* 5-59 minutes will return "Xm ago"
}
else
if
(
secs
<
SECS_PER_HOUR
*
4
)
{
* 60-90 minutes will return "Xhr Ym ago"
unsigned
int
hours
=
secs
/
SECS_PER_HOUR
;
* 91-300 minutes will return "Xhr ago"
if
(
secs
%
SECS_PER_HOUR
>=
SECS_PER_MIN
*
30
)
* 300+ minutes will return "5+ hr ago"
hours
++
;
ret
=
QString
::
number
(
hours
).
append
(
tr
(
"hr ago"
));
}
else
ret
=
tr
(
"5+ hrs ago"
);
return
ret
;
/*
todo
. would like less if()
. would like less code repition
*/
*/
QString
prettyPrintSecsAgo
(
int
secs
)
{
if
(
secs
<
SECS_PER_MIN
)
{
return
QObject
::
tr
(
"<1m ago"
);
}
if
(
secs
<
SECS_PER_MIN
*
5
)
{
return
QObject
::
tr
(
"<5m ago"
);
}
if
(
secs
<
SECS_PER_HOUR
)
{
unsigned
int
mins
=
secs
/
SECS_PER_MIN
;
if
(
secs
%
SECS_PER_MIN
>=
30
)
mins
++
;
//: This will have a number prepended, like "10m ago"
return
QString
::
number
(
mins
).
append
(
QObject
::
tr
(
"m ago"
));
}
// Here, we want to display both the hours and minutes.
//
// There are two small "corner" cases which could be rectified with
// some more knotty iffy-elsey code:
// Between 1:00:00 and 1:00:29 will display "1hr 0m ago"
// Between 1:29:30 and 1:29:59 will display "1hr 31m ago"
//
// Personally, I prefer to keep the code cleaner, and allow these.
if
(
secs
<
SECS_PER_MIN
*
90
)
{
unsigned
int
mins
=
secs
/
SECS_PER_MIN
-
60
;
if
(
secs
%
SECS_PER_MIN
>=
30
)
mins
++
;
return
QObject
::
tr
(
"1hr "
)
.
append
(
QString
::
number
(
mins
))
//: This will have a number prepended, like "5m ago"
.
append
(
QObject
::
tr
(
"m ago"
));
}
if
(
secs
<
SECS_PER_HOUR
*
5
)
{
unsigned
int
hours
=
secs
/
SECS_PER_HOUR
;
if
(
secs
%
SECS_PER_HOUR
>=
SECS_PER_MIN
*
30
)
hours
++
;
//: This will have a number prepended, like "2h ago"
return
QString
::
number
(
hours
).
append
(
QObject
::
tr
(
"hr ago"
));
}
return
QObject
::
tr
(
"5+ hrs ago"
);
}
}
}
GamesModel
::
GamesModel
(
const
QMap
<
int
,
QString
>
&
_rooms
,
const
QMap
<
int
,
GameTypeMap
>
&
_gameTypes
,
QObject
*
parent
)
GamesModel
::
GamesModel
(
const
QMap
<
int
,
QString
>
&
_rooms
,
const
QMap
<
int
,
GameTypeMap
>
&
_gameTypes
,
QObject
*
parent
)
...
@@ -94,7 +65,7 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
...
@@ -94,7 +65,7 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
int
secs
=
then
.
secsTo
(
QDateTime
::
currentDateTime
());
int
secs
=
then
.
secsTo
(
QDateTime
::
currentDateTime
());
switch
(
role
)
{
switch
(
role
)
{
case
Qt
::
DisplayRole
:
return
prettyPrintSecsAgo
(
secs
);
case
Qt
::
DisplayRole
:
return
getGameCreatedString
(
secs
);
case
SORT_ROLE
:
return
QVariant
(
secs
);
case
SORT_ROLE
:
return
QVariant
(
secs
);
default:
return
QVariant
();
default:
return
QVariant
();
}
}
...
...
cockatrice/src/gamesmodel.h
View file @
53d25e73
...
@@ -18,6 +18,8 @@ private:
...
@@ -18,6 +18,8 @@ private:
QMap
<
int
,
GameTypeMap
>
gameTypes
;
QMap
<
int
,
GameTypeMap
>
gameTypes
;
static
const
int
NUM_COLS
=
8
;
static
const
int
NUM_COLS
=
8
;
static
const
int
SECS_PER_MIN
=
60
;
static
const
int
SECS_PER_HOUR
=
3600
;
public:
public:
static
const
int
SORT_ROLE
=
Qt
::
UserRole
+
1
;
static
const
int
SORT_ROLE
=
Qt
::
UserRole
+
1
;
...
@@ -26,7 +28,7 @@ public:
...
@@ -26,7 +28,7 @@ public:
int
columnCount
(
const
QModelIndex
&
/*parent*/
=
QModelIndex
())
const
{
return
NUM_COLS
;
}
int
columnCount
(
const
QModelIndex
&
/*parent*/
=
QModelIndex
())
const
{
return
NUM_COLS
;
}
QVariant
data
(
const
QModelIndex
&
index
,
int
role
)
const
;
QVariant
data
(
const
QModelIndex
&
index
,
int
role
)
const
;
QVariant
headerData
(
int
section
,
Qt
::
Orientation
orientation
,
int
role
=
Qt
::
DisplayRole
)
const
;
QVariant
headerData
(
int
section
,
Qt
::
Orientation
orientation
,
int
role
=
Qt
::
DisplayRole
)
const
;
const
QString
getGameCreatedString
(
const
int
secs
)
const
;
const
ServerInfo_Game
&
getGame
(
int
row
);
const
ServerInfo_Game
&
getGame
(
int
row
);
/**
/**
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment