From 8d6a4f4f90805770f2816c6a4e4b5c9afca2aca3 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Sun, 10 Oct 2010 00:06:05 +0200 Subject: [PATCH] preliminary avatar support --- cockatrice/src/tab_server.cpp | 61 +++++++++- cockatrice/src/tab_server.h | 13 +++ cockatrice/translations/cockatrice_de.ts | 53 +++++---- cockatrice/translations/cockatrice_en.ts | 53 +++++---- cockatrice/translations/cockatrice_es.ts | 53 +++++---- common/protocol.cpp | 9 ++ common/protocol.h | 18 ++- common/protocol_datastructures.cpp | 4 +- common/protocol_datastructures.h | 3 +- common/protocol_item_ids.h | 135 ++++++++++++----------- common/protocol_items.cpp | 6 + common/protocol_items.dat | 1 + common/protocol_items.h | 8 ++ common/serializable_item.cpp | 13 +++ common/serializable_item.h | 15 ++- common/server.cpp | 2 - common/server_protocolhandler.cpp | 20 ++++ common/server_protocolhandler.h | 1 + servatrice/src/servatrice.cpp | 19 +++- 19 files changed, 344 insertions(+), 143 deletions(-) diff --git a/cockatrice/src/tab_server.cpp b/cockatrice/src/tab_server.cpp index 9014d783..04eb7e1b 100644 --- a/cockatrice/src/tab_server.cpp +++ b/cockatrice/src/tab_server.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -338,12 +339,65 @@ void UserList::userClicked(QTreeWidgetItem *item, int /*column*/) emit openMessageDialog(item->data(2, Qt::UserRole).toString(), true); } +UserInfoBox::UserInfoBox(AbstractClient *_client, QWidget *parent) + : QWidget(parent) +{ + avatarLabel = new QLabel; + nameLabel = new QLabel; + QFont nameFont = nameLabel->font(); + nameFont.setBold(true); + nameFont.setPointSize(nameFont.pointSize() * 1.5); + nameLabel->setFont(nameFont); + countryLabel1 = new QLabel; + countryLabel2 = new QLabel; + userLevelLabel1 = new QLabel; + userLevelLabel2 = new QLabel; + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(avatarLabel, 0, 0, 3, 1); + mainLayout->addWidget(nameLabel, 0, 1, 1, 2); + mainLayout->addWidget(countryLabel1, 1, 1, 1, 1); + mainLayout->addWidget(countryLabel2, 1, 2, 1, 1); + mainLayout->addWidget(userLevelLabel1, 2, 1, 1, 1); + mainLayout->addWidget(userLevelLabel2, 2, 2, 1, 1); + + setLayout(mainLayout); + + Command_GetUserInfo *cmd = new Command_GetUserInfo; + connect(cmd, SIGNAL(finished(ProtocolResponse *)), this, SLOT(processResponse(ProtocolResponse *))); + _client->sendCommand(cmd); +} + +void UserInfoBox::retranslateUi() +{ + countryLabel1->setText(tr("Location:")); + userLevelLabel1->setText(tr("User level:")); +} + +void UserInfoBox::processResponse(ProtocolResponse *response) +{ + Response_GetUserInfo *resp = qobject_cast(response); + if (!resp) + return; + ServerInfo_User *user = resp->getUserInfo(); + + QPixmap avatarPixmap; + if (!avatarPixmap.loadFromData(user->getAvatarBmp())) + avatarPixmap = UserLevelPixmapGenerator::generatePixmap(64, user->getUserLevel()); + avatarLabel->setPixmap(avatarPixmap); + + nameLabel->setText(user->getName()); + countryLabel2->setPixmap(CountryPixmapGenerator::generatePixmap(15, user->getCountry())); + userLevelLabel2->setPixmap(UserLevelPixmapGenerator::generatePixmap(15, user->getUserLevel())); +} + TabServer::TabServer(AbstractClient *_client, QWidget *parent) : Tab(parent), client(_client) { gameSelector = new GameSelector(client); chatChannelSelector = new ChatChannelSelector(client); serverMessageLog = new ServerMessageLog(client); + userInfoBox = new UserInfoBox(client); userList = new UserList(client); connect(gameSelector, SIGNAL(gameJoined(int)), this, SIGNAL(gameJoined(int))); @@ -359,9 +413,13 @@ TabServer::TabServer(AbstractClient *_client, QWidget *parent) vbox->addWidget(gameSelector); vbox->addLayout(hbox); + QVBoxLayout *vbox2 = new QVBoxLayout; + vbox2->addWidget(userInfoBox); + vbox2->addWidget(userList); + QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addLayout(vbox, 3); - mainLayout->addWidget(userList, 1); + mainLayout->addLayout(vbox2, 1); setLayout(mainLayout); } @@ -371,5 +429,6 @@ void TabServer::retranslateUi() gameSelector->retranslateUi(); chatChannelSelector->retranslateUi(); serverMessageLog->retranslateUi(); + userInfoBox->retranslateUi(); userList->retranslateUi(); } diff --git a/cockatrice/src/tab_server.h b/cockatrice/src/tab_server.h index d9549e20..3ddd89cd 100644 --- a/cockatrice/src/tab_server.h +++ b/cockatrice/src/tab_server.h @@ -13,6 +13,7 @@ class QTreeWidgetItem; class QPushButton; class QCheckBox; class QTextEdit; +class QLabel; class GamesModel; class GamesProxyModel; @@ -101,6 +102,17 @@ public: void retranslateUi(); }; +class UserInfoBox : public QWidget { + Q_OBJECT +private: + QLabel *avatarLabel, *nameLabel, *countryLabel1, *countryLabel2, *userLevelLabel1, *userLevelLabel2; +private slots: + void processResponse(ProtocolResponse *response); +public: + UserInfoBox(AbstractClient *_client, QWidget *parent = 0); + void retranslateUi(); +}; + class TabServer : public Tab { Q_OBJECT signals: @@ -114,6 +126,7 @@ private: ChatChannelSelector *chatChannelSelector; ServerMessageLog *serverMessageLog; UserList *userList; + UserInfoBox *userInfoBox; public: TabServer(AbstractClient *_client, QWidget *parent = 0); void retranslateUi(); diff --git a/cockatrice/translations/cockatrice_de.ts b/cockatrice/translations/cockatrice_de.ts index 95836ea9..08a93af4 100644 --- a/cockatrice/translations/cockatrice_de.ts +++ b/cockatrice/translations/cockatrice_de.ts @@ -402,27 +402,27 @@ ChatChannelSelector - + Chat channels Chaträume - + Joi&n Teil&nehmen - + Channel Raum - + Description Beschreibung - + Players Spieler @@ -1183,20 +1183,20 @@ GameSelector - + C&reate Spiel e&rstellen - + &Join &Teilnehmen - + Error Fehler @@ -1205,47 +1205,47 @@ XXX - + Wrong password. Falsches Passwort. - + Spectators are not allowed in this game. In diesem Spiel sind keine Zuschauer zugelassen. - + The game is already full. Das Spiel ist bereits voll. - + The game does not exist any more. Dieses Spiel gibt es nicht mehr. - + Join game Spiel beitreten - + Password: Passwort: - + Games Spiele - + &Show full games &Volle Spiele anzeigen - + J&oin as spectator &Zuschauen @@ -2815,7 +2815,7 @@ ServerMessageLog - + Server messages Servernachrichten @@ -3066,7 +3066,7 @@ Bitte geben Sie einen Namen ein: TabServer - + Server Server @@ -3086,6 +3086,19 @@ Bitte geben Sie einen Namen ein: Spiel %1 + + UserInfoBox + + + Location: + Ort: + + + + User level: + Nutzerstatus: + + UserInterfaceSettingsPage @@ -3112,7 +3125,7 @@ Bitte geben Sie einen Namen ein: UserList - + Users online: %1 Benutzer online: %1 diff --git a/cockatrice/translations/cockatrice_en.ts b/cockatrice/translations/cockatrice_en.ts index 23c3d244..c04f4ab0 100644 --- a/cockatrice/translations/cockatrice_en.ts +++ b/cockatrice/translations/cockatrice_en.ts @@ -352,27 +352,27 @@ ChatChannelSelector - + Chat channels - + Joi&n - + Channel - + Description - + Players @@ -790,65 +790,65 @@ GameSelector - + C&reate - + &Join - + Error - + Wrong password. - + Spectators are not allowed in this game. - + The game is already full. - + The game does not exist any more. - + Join game - + Password: - + Games - + &Show full games - + J&oin as spectator @@ -1922,7 +1922,7 @@ ServerMessageLog - + Server messages @@ -2144,11 +2144,24 @@ Please enter a name: TabServer - + Server + + UserInfoBox + + + Location: + + + + + User level: + + + UserInterfaceSettingsPage @@ -2175,7 +2188,7 @@ Please enter a name: UserList - + Users online: %1 diff --git a/cockatrice/translations/cockatrice_es.ts b/cockatrice/translations/cockatrice_es.ts index f6e1e6bd..34cb314e 100644 --- a/cockatrice/translations/cockatrice_es.ts +++ b/cockatrice/translations/cockatrice_es.ts @@ -356,27 +356,27 @@ ChatChannelSelector - + Chat channels Canales de Chat - + Joi&n E&ntrar - + Channel Canal - + Description Descripción - + Players Jugadores @@ -806,65 +806,65 @@ GameSelector - + C&reate C&rear - + &Join E&ntrar - + Error Error - + Wrong password. Contraseña incorrecta. - + Spectators are not allowed in this game. No se permiten espectadores en esta partida. - + The game is already full. La partida no tiene plazas libres. - + The game does not exist any more. La partida ya no existe. - + Join game Entrar en la partida - + Password: Contraseña: - + Games Partidas - + &Show full games &Ver partidas sin plazas libres - + J&oin as spectator Entrar como e&spectador @@ -1950,7 +1950,7 @@ ServerMessageLog - + Server messages Mensajes del servidor @@ -2173,11 +2173,24 @@ Por favor, introduzca un nombre: TabServer - + Server Servidor + + UserInfoBox + + + Location: + + + + + User level: + + + UserInterfaceSettingsPage @@ -2204,7 +2217,7 @@ Por favor, introduzca un nombre: UserList - + Users online: %1 Usuarios online: %1 diff --git a/common/protocol.cpp b/common/protocol.cpp index 4facb234..dac2c55c 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -38,6 +38,7 @@ void ProtocolItem::initializeHash() registerSerializableItem("resp", ProtocolResponse::newItem); ProtocolResponse::initializeHash(); registerSerializableItem("resplist_users", Response_ListUsers::newItem); + registerSerializableItem("respget_user_info", Response_GetUserInfo::newItem); registerSerializableItem("respdeck_list", Response_DeckList::newItem); registerSerializableItem("respdeck_download", Response_DeckDownload::newItem); registerSerializableItem("respdeck_upload", Response_DeckUpload::newItem); @@ -232,6 +233,14 @@ Response_DeckList::Response_DeckList(int _cmdId, ResponseCode _responseCode, Dec insertItem(_root); } +Response_GetUserInfo::Response_GetUserInfo(int _cmdId, ResponseCode _responseCode, ServerInfo_User *_user) + : ProtocolResponse(_cmdId, _responseCode, "get_user_info") +{ + if (!_user) + _user = new ServerInfo_User; + insertItem(_user); +} + Response_DeckDownload::Response_DeckDownload(int _cmdId, ResponseCode _responseCode, DeckList *_deck) : ProtocolResponse(_cmdId, _responseCode, "deck_download") { diff --git a/common/protocol.h b/common/protocol.h index b7de90f3..53532e6e 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -38,10 +38,11 @@ enum ItemId { ItemId_Event_Join = ItemId_Other + 210, ItemId_Event_Ping = ItemId_Other + 211, ItemId_Response_ListUsers = ItemId_Other + 300, - ItemId_Response_DeckList = ItemId_Other + 301, - ItemId_Response_DeckDownload = ItemId_Other + 302, - ItemId_Response_DeckUpload = ItemId_Other + 303, - ItemId_Response_DumpZone = ItemId_Other + 304, + ItemId_Response_GetUserInfo = ItemId_Other + 301, + ItemId_Response_DeckList = ItemId_Other + 302, + ItemId_Response_DeckDownload = ItemId_Other + 303, + ItemId_Response_DeckUpload = ItemId_Other + 304, + ItemId_Response_DumpZone = ItemId_Other + 305, ItemId_Invalid = ItemId_Other + 1000 }; @@ -210,6 +211,15 @@ public: QList getUserList() const { return typecastItemList(); } }; +class Response_GetUserInfo : public ProtocolResponse { + Q_OBJECT +public: + Response_GetUserInfo(int _cmdId = -1, ResponseCode _responseCode = RespOk, ServerInfo_User *_userInfo = 0); + int getItemId() const { return ItemId_Response_GetUserInfo; } + static SerializableItem *newItem() { return new Response_GetUserInfo; } + ServerInfo_User *getUserInfo() const { return static_cast(itemMap.value("user")); } +}; + class Response_DeckList : public ProtocolResponse { Q_OBJECT public: diff --git a/common/protocol_datastructures.cpp b/common/protocol_datastructures.cpp index dbe927e2..afbf726a 100644 --- a/common/protocol_datastructures.cpp +++ b/common/protocol_datastructures.cpp @@ -12,12 +12,13 @@ ServerInfo_ChatChannel::ServerInfo_ChatChannel(const QString &_name, const QStri insertItem(new SerializableItem_Bool("auto_join", _autoJoin)); } -ServerInfo_User::ServerInfo_User(const QString &_name, int _userLevel, const QString &_country) +ServerInfo_User::ServerInfo_User(const QString &_name, int _userLevel, const QString &_country, const QByteArray &_avatarBmp) : SerializableItem_Map("user") { insertItem(new SerializableItem_String("name", _name)); insertItem(new SerializableItem_Int("userlevel", _userLevel)); insertItem(new SerializableItem_String("country", _country)); + insertItem(new SerializableItem_ByteArray("avatar_bmp", _avatarBmp)); } ServerInfo_User::ServerInfo_User(const ServerInfo_User *other) @@ -26,6 +27,7 @@ ServerInfo_User::ServerInfo_User(const ServerInfo_User *other) insertItem(new SerializableItem_String("name", other->getName())); insertItem(new SerializableItem_Int("userlevel", other->getUserLevel())); insertItem(new SerializableItem_String("country", other->getCountry())); + insertItem(new SerializableItem_ByteArray("avatar_bmp", other->getAvatarBmp())); } ServerInfo_Game::ServerInfo_Game(int _gameId, const QString &_description, bool _hasPassword, int _playerCount, int _maxPlayers, ServerInfo_User *_creatorInfo, bool _spectatorsAllowed, bool _spectatorsNeedPassword, int _spectatorCount) diff --git a/common/protocol_datastructures.h b/common/protocol_datastructures.h index 6e498dbe..ff16b667 100644 --- a/common/protocol_datastructures.h +++ b/common/protocol_datastructures.h @@ -39,13 +39,14 @@ public: IsJudge = 0x04, IsAdmin = 0x08 }; - ServerInfo_User(const QString &_name = QString(), int _userLevel = IsNothing, const QString &_country = QString()); + ServerInfo_User(const QString &_name = QString(), int _userLevel = IsNothing, const QString &_country = QString(), const QByteArray &_avatarBmp = QByteArray()); ServerInfo_User(const ServerInfo_User *other); static SerializableItem *newItem() { return new ServerInfo_User; } QString getName() const { return static_cast(itemMap.value("name"))->getData(); } int getUserLevel() const { return static_cast(itemMap.value("userlevel"))->getData(); } void setUserLevel(int _userLevel) { static_cast(itemMap.value("userlevel"))->setData(_userLevel); } QString getCountry() const { return static_cast(itemMap.value("country"))->getData(); } + QByteArray getAvatarBmp() const { return static_cast(itemMap.value("avatar_bmp"))->getData(); } }; class ServerInfo_Game : public SerializableItem_Map { diff --git a/common/protocol_item_ids.h b/common/protocol_item_ids.h index 211e03ac..6921eb81 100644 --- a/common/protocol_item_ids.h +++ b/common/protocol_item_ids.h @@ -2,71 +2,72 @@ enum AutoItemId { ItemId_Command_Ping = 1001, ItemId_Command_Login = 1002, ItemId_Command_Message = 1003, -ItemId_Command_DeckList = 1004, -ItemId_Command_DeckNewDir = 1005, -ItemId_Command_DeckDelDir = 1006, -ItemId_Command_DeckDel = 1007, -ItemId_Command_DeckDownload = 1008, -ItemId_Command_ListChatChannels = 1009, -ItemId_Command_ChatJoinChannel = 1010, -ItemId_Command_ChatLeaveChannel = 1011, -ItemId_Command_ChatSay = 1012, -ItemId_Command_ListGames = 1013, -ItemId_Command_ListUsers = 1014, -ItemId_Command_CreateGame = 1015, -ItemId_Command_JoinGame = 1016, -ItemId_Command_LeaveGame = 1017, -ItemId_Command_Say = 1018, -ItemId_Command_Shuffle = 1019, -ItemId_Command_Mulligan = 1020, -ItemId_Command_RollDie = 1021, -ItemId_Command_DrawCards = 1022, -ItemId_Command_MoveCard = 1023, -ItemId_Command_FlipCard = 1024, -ItemId_Command_AttachCard = 1025, -ItemId_Command_CreateToken = 1026, -ItemId_Command_CreateArrow = 1027, -ItemId_Command_DeleteArrow = 1028, -ItemId_Command_SetCardAttr = 1029, -ItemId_Command_SetCardCounter = 1030, -ItemId_Command_IncCardCounter = 1031, -ItemId_Command_ReadyStart = 1032, -ItemId_Command_Concede = 1033, -ItemId_Command_IncCounter = 1034, -ItemId_Command_CreateCounter = 1035, -ItemId_Command_SetCounter = 1036, -ItemId_Command_DelCounter = 1037, -ItemId_Command_NextTurn = 1038, -ItemId_Command_SetActivePhase = 1039, -ItemId_Command_DumpZone = 1040, -ItemId_Command_StopDumpZone = 1041, -ItemId_Event_Say = 1042, -ItemId_Event_Leave = 1043, -ItemId_Event_GameClosed = 1044, -ItemId_Event_Shuffle = 1045, -ItemId_Event_RollDie = 1046, -ItemId_Event_MoveCard = 1047, -ItemId_Event_FlipCard = 1048, -ItemId_Event_DestroyCard = 1049, -ItemId_Event_AttachCard = 1050, -ItemId_Event_CreateToken = 1051, -ItemId_Event_DeleteArrow = 1052, -ItemId_Event_SetCardAttr = 1053, -ItemId_Event_SetCardCounter = 1054, -ItemId_Event_SetCounter = 1055, -ItemId_Event_DelCounter = 1056, -ItemId_Event_SetActivePlayer = 1057, -ItemId_Event_SetActivePhase = 1058, -ItemId_Event_DumpZone = 1059, -ItemId_Event_StopDumpZone = 1060, -ItemId_Event_ServerMessage = 1061, -ItemId_Event_Message = 1062, -ItemId_Event_GameJoined = 1063, -ItemId_Event_UserLeft = 1064, -ItemId_Event_ChatLeaveChannel = 1065, -ItemId_Event_ChatSay = 1066, -ItemId_Context_ReadyStart = 1067, -ItemId_Context_Concede = 1068, -ItemId_Context_DeckSelect = 1069, -ItemId_Other = 1070 +ItemId_Command_GetUserInfo = 1004, +ItemId_Command_DeckList = 1005, +ItemId_Command_DeckNewDir = 1006, +ItemId_Command_DeckDelDir = 1007, +ItemId_Command_DeckDel = 1008, +ItemId_Command_DeckDownload = 1009, +ItemId_Command_ListChatChannels = 1010, +ItemId_Command_ChatJoinChannel = 1011, +ItemId_Command_ChatLeaveChannel = 1012, +ItemId_Command_ChatSay = 1013, +ItemId_Command_ListGames = 1014, +ItemId_Command_ListUsers = 1015, +ItemId_Command_CreateGame = 1016, +ItemId_Command_JoinGame = 1017, +ItemId_Command_LeaveGame = 1018, +ItemId_Command_Say = 1019, +ItemId_Command_Shuffle = 1020, +ItemId_Command_Mulligan = 1021, +ItemId_Command_RollDie = 1022, +ItemId_Command_DrawCards = 1023, +ItemId_Command_MoveCard = 1024, +ItemId_Command_FlipCard = 1025, +ItemId_Command_AttachCard = 1026, +ItemId_Command_CreateToken = 1027, +ItemId_Command_CreateArrow = 1028, +ItemId_Command_DeleteArrow = 1029, +ItemId_Command_SetCardAttr = 1030, +ItemId_Command_SetCardCounter = 1031, +ItemId_Command_IncCardCounter = 1032, +ItemId_Command_ReadyStart = 1033, +ItemId_Command_Concede = 1034, +ItemId_Command_IncCounter = 1035, +ItemId_Command_CreateCounter = 1036, +ItemId_Command_SetCounter = 1037, +ItemId_Command_DelCounter = 1038, +ItemId_Command_NextTurn = 1039, +ItemId_Command_SetActivePhase = 1040, +ItemId_Command_DumpZone = 1041, +ItemId_Command_StopDumpZone = 1042, +ItemId_Event_Say = 1043, +ItemId_Event_Leave = 1044, +ItemId_Event_GameClosed = 1045, +ItemId_Event_Shuffle = 1046, +ItemId_Event_RollDie = 1047, +ItemId_Event_MoveCard = 1048, +ItemId_Event_FlipCard = 1049, +ItemId_Event_DestroyCard = 1050, +ItemId_Event_AttachCard = 1051, +ItemId_Event_CreateToken = 1052, +ItemId_Event_DeleteArrow = 1053, +ItemId_Event_SetCardAttr = 1054, +ItemId_Event_SetCardCounter = 1055, +ItemId_Event_SetCounter = 1056, +ItemId_Event_DelCounter = 1057, +ItemId_Event_SetActivePlayer = 1058, +ItemId_Event_SetActivePhase = 1059, +ItemId_Event_DumpZone = 1060, +ItemId_Event_StopDumpZone = 1061, +ItemId_Event_ServerMessage = 1062, +ItemId_Event_Message = 1063, +ItemId_Event_GameJoined = 1064, +ItemId_Event_UserLeft = 1065, +ItemId_Event_ChatLeaveChannel = 1066, +ItemId_Event_ChatSay = 1067, +ItemId_Context_ReadyStart = 1068, +ItemId_Context_Concede = 1069, +ItemId_Context_DeckSelect = 1070, +ItemId_Other = 1071 }; diff --git a/common/protocol_items.cpp b/common/protocol_items.cpp index 369bd518..74f51c02 100644 --- a/common/protocol_items.cpp +++ b/common/protocol_items.cpp @@ -17,6 +17,11 @@ Command_Message::Command_Message(const QString &_userName, const QString &_text) insertItem(new SerializableItem_String("user_name", _userName)); insertItem(new SerializableItem_String("text", _text)); } +Command_GetUserInfo::Command_GetUserInfo(const QString &_userName) + : Command("get_user_info") +{ + insertItem(new SerializableItem_String("user_name", _userName)); +} Command_DeckList::Command_DeckList() : Command("deck_list") { @@ -432,6 +437,7 @@ void ProtocolItem::initializeHashAuto() itemNameHash.insert("cmdping", Command_Ping::newItem); itemNameHash.insert("cmdlogin", Command_Login::newItem); itemNameHash.insert("cmdmessage", Command_Message::newItem); + itemNameHash.insert("cmdget_user_info", Command_GetUserInfo::newItem); itemNameHash.insert("cmddeck_list", Command_DeckList::newItem); itemNameHash.insert("cmddeck_new_dir", Command_DeckNewDir::newItem); itemNameHash.insert("cmddeck_del_dir", Command_DeckDelDir::newItem); diff --git a/common/protocol_items.dat b/common/protocol_items.dat index 5d7b3b11..ec077b41 100644 --- a/common/protocol_items.dat +++ b/common/protocol_items.dat @@ -1,6 +1,7 @@ 0:ping 0:login:s,username:s,password 0:message:s,user_name:s,text +0:get_user_info:s,user_name 0:deck_list 0:deck_new_dir:s,path:s,dir_name 0:deck_del_dir:s,path diff --git a/common/protocol_items.h b/common/protocol_items.h index 277211e5..00c16260 100644 --- a/common/protocol_items.h +++ b/common/protocol_items.h @@ -28,6 +28,14 @@ public: static SerializableItem *newItem() { return new Command_Message; } int getItemId() const { return ItemId_Command_Message; } }; +class Command_GetUserInfo : public Command { + Q_OBJECT +public: + Command_GetUserInfo(const QString &_userName = QString()); + QString getUserName() const { return static_cast(itemMap.value("user_name"))->getData(); }; + static SerializableItem *newItem() { return new Command_GetUserInfo; } + int getItemId() const { return ItemId_Command_GetUserInfo; } +}; class Command_DeckList : public Command { Q_OBJECT public: diff --git a/common/serializable_item.cpp b/common/serializable_item.cpp index 9a091e45..4fcdf0e1 100644 --- a/common/serializable_item.cpp +++ b/common/serializable_item.cpp @@ -145,3 +145,16 @@ void SerializableItem_DateTime::writeElement(QXmlStreamWriter *xml) { xml->writeCharacters(QString::number(data.toTime_t())); } + +bool SerializableItem_ByteArray::readElement(QXmlStreamReader *xml) +{ + if (xml->isCharacters() && !xml->isWhitespace()) + data = qUncompress(QByteArray::fromBase64(xml->text().toString().toAscii())); + + return SerializableItem::readElement(xml); +} + +void SerializableItem_ByteArray::writeElement(QXmlStreamWriter *xml) +{ + xml->writeCharacters(QString(qCompress(data).toBase64())); +} diff --git a/common/serializable_item.h b/common/serializable_item.h index 4867d8a1..ef1e91ad 100644 --- a/common/serializable_item.h +++ b/common/serializable_item.h @@ -121,7 +121,7 @@ public: const Color &getData() { return data; } void setData(const Color &_data) { data = _data; } }; - + class SerializableItem_DateTime : public SerializableItem { private: QDateTime data; @@ -135,4 +135,17 @@ public: void setData(const QDateTime &_data) { data = _data; } }; +class SerializableItem_ByteArray : public SerializableItem { +private: + QByteArray data; +protected: + bool readElement(QXmlStreamReader *xml); + void writeElement(QXmlStreamWriter *xml); +public: + SerializableItem_ByteArray(const QString &_itemType, const QByteArray &_data) + : SerializableItem(_itemType), data(_data) { } + const QByteArray &getData() { return data; } + void setData(const QByteArray &_data) { data = _data; } +}; + #endif diff --git a/common/server.cpp b/common/server.cpp index 499dfa4d..dc4125b2 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -56,8 +56,6 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString } ServerInfo_User *data = getUserData(name); - if (authState == PasswordRight) - data->setUserLevel(data->getUserLevel() | ServerInfo_User::IsRegistered); session->setUserInfo(data); users.insert(name, session); diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 87054352..8a51dd3a 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -123,6 +123,7 @@ ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, Comm case ItemId_Command_DeckDel: return cmdDeckDel(qobject_cast(command), cont); case ItemId_Command_DeckUpload: return cmdDeckUpload(qobject_cast(command), cont); case ItemId_Command_DeckDownload: return cmdDeckDownload(qobject_cast(command), cont); + case ItemId_Command_GetUserInfo: return cmdGetUserInfo(qobject_cast(command), cont); case ItemId_Command_ListChatChannels: return cmdListChatChannels(qobject_cast(command), cont); case ItemId_Command_ChatJoinChannel: return cmdChatJoinChannel(qobject_cast(command), cont); case ItemId_Command_ListUsers: return cmdListUsers(qobject_cast(command), cont); @@ -246,6 +247,25 @@ ResponseCode Server_ProtocolHandler::cmdMessage(Command_Message *cmd, CommandCon return RespOk; } +ResponseCode Server_ProtocolHandler::cmdGetUserInfo(Command_GetUserInfo *cmd, CommandContainer *cont) +{ + if (authState == PasswordWrong) + return RespLoginNeeded; + + ServerInfo_User *result; + if (cmd->getUserName().isEmpty()) + result = new ServerInfo_User(userInfo); + else { + Server_ProtocolHandler *handler = server->getUsers().value(cmd->getUserName()); + if (!handler) + return RespNameNotFound; + result = handler->getUserInfo(); + } + + cont->setResponse(new Response_GetUserInfo(cont->getCmdId(), RespOk, result)); + return RespNothing; +} + ResponseCode Server_ProtocolHandler::cmdListChatChannels(Command_ListChatChannels * /*cmd*/, CommandContainer *cont) { if (authState == PasswordWrong) diff --git a/common/server_protocolhandler.h b/common/server_protocolhandler.h index 5a54ccde..0f6ccc8c 100644 --- a/common/server_protocolhandler.h +++ b/common/server_protocolhandler.h @@ -44,6 +44,7 @@ private: virtual ResponseCode cmdDeckDel(Command_DeckDel *cmd, CommandContainer *cont) = 0; virtual ResponseCode cmdDeckUpload(Command_DeckUpload *cmd, CommandContainer *cont) = 0; virtual ResponseCode cmdDeckDownload(Command_DeckDownload *cmd, CommandContainer *cont) = 0; + ResponseCode cmdGetUserInfo(Command_GetUserInfo *cmd, CommandContainer *cont); ResponseCode cmdListChatChannels(Command_ListChatChannels *cmd, CommandContainer *cont); ResponseCode cmdChatJoinChannel(Command_ChatJoinChannel *cmd, CommandContainer *cont); ResponseCode cmdChatLeaveChannel(Command_ChatLeaveChannel *cmd, CommandContainer *cont, Server_ChatChannel *channel); diff --git a/servatrice/src/servatrice.cpp b/servatrice/src/servatrice.cpp index f7d49336..e138f70b 100644 --- a/servatrice/src/servatrice.cpp +++ b/servatrice/src/servatrice.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "servatrice.h" #include "server_chatchannel.h" #include "serversocketinterface.h" @@ -79,8 +80,12 @@ bool Servatrice::openDatabase() sqldb.setPassword(settings->value("password").toString()); settings->endGroup(); - if (!sqldb.open()) + std::cerr << "Opening database..."; + if (!sqldb.open()) { + std::cerr << "error" << std::endl; return false; + } + std::cerr << "OK" << std::endl; if (!nextGameId) { QSqlQuery query; @@ -124,7 +129,7 @@ AuthenticationResult Servatrice::checkUserPassword(const QString &user, const QS checkSql(); QSqlQuery query; - query.prepare("select password from users where name = :name"); + query.prepare("select password from users where name = :name and active = 1"); query.bindValue(":name", user); if (!execSqlQuery(query)) return PasswordWrong; @@ -147,7 +152,7 @@ ServerInfo_User *Servatrice::getUserData(const QString &name) checkSql(); QSqlQuery query; - query.prepare("select admin, country from users where name = :name"); + query.prepare("select admin, country, avatar_bmp from users where name = :name and active = 1"); query.bindValue(":name", name); if (!execSqlQuery(query)) return new ServerInfo_User(name); @@ -155,15 +160,17 @@ ServerInfo_User *Servatrice::getUserData(const QString &name) if (query.next()) { bool is_admin = query.value(0).toInt(); QString country = query.value(1).toString(); + QByteArray avatarBmp = query.value(2).toByteArray(); - int userLevel = ServerInfo_User::IsUser; + int userLevel = ServerInfo_User::IsUser | ServerInfo_User::IsRegistered; if (is_admin) userLevel |= ServerInfo_User::IsAdmin; return new ServerInfo_User( name, userLevel, - country + country, + avatarBmp ); } else return new ServerInfo_User(name); @@ -171,4 +178,4 @@ ServerInfo_User *Servatrice::getUserData(const QString &name) return new ServerInfo_User(name); } -const QString Servatrice::versionString = "Servatrice 0.20100918"; +const QString Servatrice::versionString = "Servatrice 0.20101009";