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
b06b8189
Commit
b06b8189
authored
Jan 22, 2015
by
poixen
Browse files
Merge pull request #580 from poixen/room_user_level
Updated game view
parents
71a06703
8fdaadf4
Changes
2
Hide whitespace changes
Inline
Side-by-side
cockatrice/src/gamesmodel.cpp
View file @
b06b8189
#include
"gamesmodel.h"
#include
"gamesmodel.h"
#include
"pb/serverinfo_game.pb.h"
#include
"pb/serverinfo_game.pb.h"
#include
"pixmapgenerator.h"
#include
<QDebug>
#include
<QDebug>
#include
<QIcon>
#include
<QStringList>
#include
<QStringList>
#include
<QDateTime>
#include
<QDateTime>
#include
<QSettings>
#include
<QSettings>
#include
<QCryptographicHash>
#include
<QCryptographicHash>
enum
GameListColumn
{
ROOM
,
CREATED
,
DESCRIPTION
,
CREATOR
,
GAME_TYPE
,
RESTRICTIONS
,
PLAYERS
,
SPECTATORS
};
namespace
{
namespace
{
const
unsigned
SECS_PER_MIN
=
60
;
const
unsigned
SECS_PER_MIN
=
60
;
const
unsigned
SECS_PER_HOUR
=
60
*
60
;
const
unsigned
SECS_PER_HOUR
=
60
*
60
;
/**
/**
* Pretty print an integer number of seconds ago. Accurate to only one unit,
* Pretty print an integer number of seconds ago. Accurate to only one unit,
* rounded; <5 minutes and >5 hours are displayed as such. As a special case,
* rounded; <5 minutes and >5 hours are displayed as such. As a special case,
* time between 60 and 90 minutes will display both the hours and minutes.
* time between 60 and 90 minutes will display both the hours and minutes.
*
*
* For example...
* For example...
* 0-300 seconds will return "<5m ago"
* 0-300 seconds will return "<5m ago"
* 5-59 minutes will return "Xm ago"
* 5-59 minutes will return "Xm ago"
* 60-90 minutes will return "Xhr Ym ago"
* 60-90 minutes will return "Xhr Ym ago"
* 91-300 minutes will return "Xhr ago"
* 91-300 minutes will return "Xhr ago"
* 300+ minutes will return "5+ hr ago"
* 300+ minutes will return "5+ hr ago"
*/
*/
QString
prettyPrintSecsAgo
(
unsigned
int
secs
)
{
QString
prettyPrintSecsAgo
(
unsigned
int
secs
)
{
if
(
secs
<
SECS_PER_MIN
)
{
if
(
secs
<
SECS_PER_MIN
)
{
return
QObject
::
tr
(
"<1m ago"
);
return
QObject
::
tr
(
"<1m ago"
);
...
@@ -32,7 +36,7 @@ namespace {
...
@@ -32,7 +36,7 @@ namespace {
if
(
secs
<
SECS_PER_HOUR
)
{
if
(
secs
<
SECS_PER_HOUR
)
{
unsigned
int
mins
=
secs
/
SECS_PER_MIN
;
unsigned
int
mins
=
secs
/
SECS_PER_MIN
;
if
(
secs
%
SECS_PER_MIN
>=
30
)
if
(
secs
%
SECS_PER_MIN
>=
30
)
mins
++
;
mins
++
;
//: This will have a number prepended, like "10m ago"
//: This will have a number prepended, like "10m ago"
return
QString
::
number
(
mins
).
append
(
QObject
::
tr
(
"m ago"
));
return
QString
::
number
(
mins
).
append
(
QObject
::
tr
(
"m ago"
));
}
}
...
@@ -45,18 +49,18 @@ namespace {
...
@@ -45,18 +49,18 @@ namespace {
//
//
// Personally, I prefer to keep the code cleaner, and allow these.
// Personally, I prefer to keep the code cleaner, and allow these.
if
(
secs
<
SECS_PER_MIN
*
90
)
{
if
(
secs
<
SECS_PER_MIN
*
90
)
{
unsigned
int
mins
=
secs
/
SECS_PER_MIN
-
60
;
unsigned
int
mins
=
secs
/
SECS_PER_MIN
-
60
;
if
(
secs
%
SECS_PER_MIN
>=
30
)
if
(
secs
%
SECS_PER_MIN
>=
30
)
mins
++
;
mins
++
;
return
QObject
::
tr
(
"1hr "
)
return
QObject
::
tr
(
"1hr "
)
.
append
(
QString
::
number
(
mins
))
.
append
(
QString
::
number
(
mins
))
//: This will have a number prepended, like "5m ago"
//: This will have a number prepended, like "5m ago"
.
append
(
QObject
::
tr
(
"m ago"
));
.
append
(
QObject
::
tr
(
"m ago"
));
}
}
if
(
secs
<
SECS_PER_HOUR
*
5
)
{
if
(
secs
<
SECS_PER_HOUR
*
5
)
{
unsigned
int
hours
=
secs
/
SECS_PER_HOUR
;
unsigned
int
hours
=
secs
/
SECS_PER_HOUR
;
if
(
secs
%
SECS_PER_HOUR
>=
SECS_PER_MIN
*
30
)
if
(
secs
%
SECS_PER_HOUR
>=
SECS_PER_MIN
*
30
)
hours
++
;
hours
++
;
//: This will have a number prepended, like "2h ago"
//: This will have a number prepended, like "2h ago"
return
QString
::
number
(
hours
).
append
(
QObject
::
tr
(
"hr ago"
));
return
QString
::
number
(
hours
).
append
(
QObject
::
tr
(
"hr ago"
));
}
}
...
@@ -75,15 +79,16 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
...
@@ -75,15 +79,16 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
return
QVariant
();
return
QVariant
();
if
(
role
==
Qt
::
UserRole
)
if
(
role
==
Qt
::
UserRole
)
return
index
.
row
();
return
index
.
row
();
if
(
role
!=
Qt
::
DisplayRole
&&
role
!=
SORT_ROLE
)
if
(
role
!=
Qt
::
DisplayRole
&&
role
!=
SORT_ROLE
&&
role
!=
Qt
::
DecorationRole
&&
role
!=
Qt
::
TextAlignmentRole
)
return
QVariant
();
return
QVariant
();
if
((
index
.
row
()
>=
gameList
.
size
())
||
(
index
.
column
()
>=
columnCount
()))
if
((
index
.
row
()
>=
gameList
.
size
())
||
(
index
.
column
()
>=
columnCount
()))
return
QVariant
();
return
QVariant
();
const
ServerInfo_Game
&
g
=
gameList
[
index
.
row
()];
const
ServerInfo_Game
&
g
=
gameList
[
index
.
row
()];
switch
(
index
.
column
())
{
switch
(
index
.
column
())
{
case
0
:
return
rooms
.
value
(
g
.
room_id
());
case
ROOM
:
case
1
:
{
return
rooms
.
value
(
g
.
room_id
());
case
CREATED
:
{
QDateTime
then
;
QDateTime
then
;
then
.
setTime_t
(
g
.
start_time
());
then
.
setTime_t
(
g
.
start_time
());
unsigned
int
secs
=
then
.
secsTo
(
QDateTime
::
currentDateTime
());
unsigned
int
secs
=
then
.
secsTo
(
QDateTime
::
currentDateTime
());
...
@@ -91,32 +96,89 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
...
@@ -91,32 +96,89 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
switch
(
role
)
{
switch
(
role
)
{
case
Qt
::
DisplayRole
:
return
prettyPrintSecsAgo
(
secs
);
case
Qt
::
DisplayRole
:
return
prettyPrintSecsAgo
(
secs
);
case
SORT_ROLE
:
return
QVariant
(
secs
);
case
SORT_ROLE
:
return
QVariant
(
secs
);
default:
{
default:
return
QVariant
();
qDebug
()
<<
"Returning data for col 1 of games model when role != display, role != sort"
;
return
QVariant
();
// Shouldn't ever be reached.
}
}
}
}
}
case
2
:
return
QString
::
fromStdString
(
g
.
description
());
case
DESCRIPTION
:
case
3
:
return
QString
::
fromStdString
(
g
.
creator_info
().
name
());
switch
(
role
)
{
case
4
:
{
case
SORT_ROLE
:
QStringList
result
;
case
Qt
::
DisplayRole
:
GameTypeMap
gameTypeMap
=
gameTypes
.
value
(
g
.
room_id
());
return
QString
::
fromStdString
(
g
.
description
());
for
(
int
i
=
g
.
game_types_size
()
-
1
;
i
>=
0
;
--
i
)
case
Qt
::
TextAlignmentRole
:
result
.
append
(
gameTypeMap
.
value
(
g
.
game_types
(
i
)));
Qt
::
AlignLeft
;
return
result
.
join
(
", "
);
default:
return
QVariant
();
}
case
CREATOR
:
{
switch
(
role
)
{
case
SORT_ROLE
:
case
Qt
::
DisplayRole
:
return
QString
::
fromStdString
(
g
.
creator_info
().
name
());
case
Qt
::
DecorationRole
:
{
QPixmap
avatarPixmap
=
UserLevelPixmapGenerator
::
generatePixmap
(
13
,
(
UserLevelFlags
)
g
.
creator_info
().
user_level
());
return
QIcon
(
avatarPixmap
);
}
default:
return
QVariant
();
}
}
}
case
5
:
return
g
.
with_password
()
?
((
g
.
spectators_need_password
()
||
!
g
.
spectators_allowed
())
?
tr
(
"yes"
)
:
tr
(
"yes, free for spectators"
))
:
tr
(
"no"
);
case
GAME_TYPE
:
case
6
:
{
switch
(
role
)
{
QStringList
result
;
case
SORT_ROLE
:
if
(
g
.
only_buddies
())
case
Qt
::
DisplayRole
:
{
result
.
append
(
tr
(
"buddies only"
));
QStringList
result
;
if
(
g
.
only_registered
())
GameTypeMap
gameTypeMap
=
gameTypes
.
value
(
g
.
room_id
());
result
.
append
(
tr
(
"reg. users only"
));
for
(
int
i
=
g
.
game_types_size
()
-
1
;
i
>=
0
;
--
i
)
return
result
.
join
(
", "
);
result
.
append
(
gameTypeMap
.
value
(
g
.
game_types
(
i
)));
return
result
.
join
(
", "
);
}
case
Qt
::
TextAlignmentRole
:
return
Qt
::
AlignLeft
;
default:
return
QVariant
();
}
}
case
7
:
return
QString
(
"%1/%2"
).
arg
(
g
.
player_count
()).
arg
(
g
.
max_players
());
case
RESTRICTIONS
:
case
8
:
return
g
.
spectators_allowed
()
?
QVariant
(
g
.
spectators_count
())
:
QVariant
(
tr
(
"not allowed"
));
switch
(
role
)
{
case
SORT_ROLE
:
case
Qt
::
DisplayRole
:
{
QStringList
result
;
if
(
g
.
with_password
())
result
.
append
(
tr
(
"password"
));
if
(
g
.
only_buddies
())
result
.
append
(
tr
(
"buddies only"
));
if
(
g
.
only_registered
())
result
.
append
(
tr
(
"reg. users only"
));
return
result
.
join
(
", "
);
}
case
Qt
::
DecorationRole
:{
return
g
.
with_password
()
?
QIcon
(
":/resources/lock.svg"
)
:
QVariant
();
case
Qt
::
TextAlignmentRole
:
return
Qt
::
AlignLeft
;
default:
return
QVariant
();
}
}
case
PLAYERS
:
switch
(
role
)
{
case
SORT_ROLE
:
case
Qt
::
DisplayRole
:
return
QString
(
"%1/%2"
).
arg
(
g
.
player_count
()).
arg
(
g
.
max_players
());
case
Qt
::
TextAlignmentRole
:
return
Qt
::
AlignLeft
;
default:
return
QVariant
();
}
case
SPECTATORS
:
switch
(
role
)
{
case
SORT_ROLE
:
case
Qt
::
DisplayRole
:
return
g
.
spectators_allowed
()
?
QVariant
(
g
.
spectators_count
())
:
QVariant
(
tr
(
"not allowed"
));
case
Qt
::
TextAlignmentRole
:
return
Qt
::
AlignLeft
;
default:
return
QVariant
();
}
default:
return
QVariant
();
default:
return
QVariant
();
}
}
}
}
...
@@ -126,16 +188,15 @@ QVariant GamesModel::headerData(int section, Qt::Orientation orientation, int ro
...
@@ -126,16 +188,15 @@ QVariant GamesModel::headerData(int section, Qt::Orientation orientation, int ro
if
((
role
!=
Qt
::
DisplayRole
)
||
(
orientation
!=
Qt
::
Horizontal
))
if
((
role
!=
Qt
::
DisplayRole
)
||
(
orientation
!=
Qt
::
Horizontal
))
return
QVariant
();
return
QVariant
();
switch
(
section
)
{
switch
(
section
)
{
case
0
:
return
tr
(
"Room"
);
case
ROOM
:
return
tr
(
"Room"
);
case
1
:
return
tr
(
"Game Created"
);
case
CREATED
:
return
tr
(
"Game Created"
);
case
2
:
return
tr
(
"Description"
);
case
DESCRIPTION
:
return
tr
(
"Description"
);
case
3
:
return
tr
(
"Creator"
);
case
CREATOR
:
return
tr
(
"Creator"
);
case
4
:
return
tr
(
"Game Type"
);
case
GAME_TYPE
:
return
tr
(
"Game Type"
);
case
5
:
return
tr
(
"Password"
);
case
RESTRICTIONS
:
return
tr
(
"Restrictions"
);
case
6
:
return
tr
(
"Restrictions"
);
case
PLAYERS
:
return
tr
(
"Players"
);
case
7
:
return
tr
(
"Players"
);
case
SPECTATORS
:
return
tr
(
"Spectators"
);
case
8
:
return
tr
(
"Spectators"
);
default:
return
QVariant
();
default:
return
QVariant
();
}
}
}
}
...
@@ -169,11 +230,11 @@ void GamesModel::updateGameList(const ServerInfo_Game &game)
...
@@ -169,11 +230,11 @@ void GamesModel::updateGameList(const ServerInfo_Game &game)
GamesProxyModel
::
GamesProxyModel
(
QObject
*
parent
,
ServerInfo_User
*
_ownUser
)
GamesProxyModel
::
GamesProxyModel
(
QObject
*
parent
,
ServerInfo_User
*
_ownUser
)
:
QSortFilterProxyModel
(
parent
),
:
QSortFilterProxyModel
(
parent
),
ownUser
(
_ownUser
),
ownUser
(
_ownUser
),
unavailableGamesVisible
(
false
),
unavailableGamesVisible
(
false
),
passwordProtectedGamesVisible
(
false
),
passwordProtectedGamesVisible
(
false
),
maxPlayersFilterMin
(
-
1
),
maxPlayersFilterMin
(
-
1
),
maxPlayersFilterMax
(
-
1
)
maxPlayersFilterMax
(
-
1
)
{
{
setSortRole
(
GamesModel
::
SORT_ROLE
);
setSortRole
(
GamesModel
::
SORT_ROLE
);
setDynamicSortFilter
(
true
);
setDynamicSortFilter
(
true
);
...
@@ -261,7 +322,7 @@ void GamesProxyModel::saveFilterParameters(const QMap<int, QString> &allGameType
...
@@ -261,7 +322,7 @@ void GamesProxyModel::saveFilterParameters(const QMap<int, QString> &allGameType
settings
.
setValue
(
settings
.
setValue
(
"password_protected_games_visible"
,
"password_protected_games_visible"
,
passwordProtectedGamesVisible
passwordProtectedGamesVisible
);
);
settings
.
setValue
(
"game_name_filter"
,
gameNameFilter
);
settings
.
setValue
(
"game_name_filter"
,
gameNameFilter
);
settings
.
setValue
(
"creator_name_filter"
,
creatorNameFilter
);
settings
.
setValue
(
"creator_name_filter"
,
creatorNameFilter
);
...
@@ -272,7 +333,7 @@ void GamesProxyModel::saveFilterParameters(const QMap<int, QString> &allGameType
...
@@ -272,7 +333,7 @@ void GamesProxyModel::saveFilterParameters(const QMap<int, QString> &allGameType
settings
.
setValue
(
settings
.
setValue
(
"game_type/"
+
hashGameType
(
gameTypeIterator
.
value
()),
"game_type/"
+
hashGameType
(
gameTypeIterator
.
value
()),
gameTypeFilter
.
contains
(
gameTypeIterator
.
key
())
gameTypeFilter
.
contains
(
gameTypeIterator
.
key
())
);
);
}
}
settings
.
setValue
(
"min_players"
,
maxPlayersFilterMin
);
settings
.
setValue
(
"min_players"
,
maxPlayersFilterMin
);
...
...
cockatrice/src/gamesmodel.h
View file @
b06b8189
...
@@ -17,7 +17,7 @@ private:
...
@@ -17,7 +17,7 @@ private:
QMap
<
int
,
QString
>
rooms
;
QMap
<
int
,
QString
>
rooms
;
QMap
<
int
,
GameTypeMap
>
gameTypes
;
QMap
<
int
,
GameTypeMap
>
gameTypes
;
static
const
int
NUM_COLS
=
9
;
static
const
int
NUM_COLS
=
8
;
public:
public:
static
const
int
SORT_ROLE
=
Qt
::
UserRole
+
1
;
static
const
int
SORT_ROLE
=
Qt
::
UserRole
+
1
;
...
...
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