Fix inconsistencies in guest nick name generation
authorWuzzy <Wuzzy2@mail.ru>
Tue, 02 Apr 2019 01:03:39 +0200
changeset 14761 7b2bd37d7503
parent 14760 5d5d7db9f417
child 14762 37a781d28e43
Fix inconsistencies in guest nick name generation When the setting net/nick is empty, HW generates a guest name. But there were several problems: - Team name did not match guest name - Random guest number was re-rolled multiple times, causing inconsistencies - Guest number was often too large (now capped at 99999)
QTfrontend/gameuiconfig.cpp
QTfrontend/gameuiconfig.h
QTfrontend/hwform.cpp
--- a/QTfrontend/gameuiconfig.cpp	Tue Apr 02 00:13:25 2019 +0200
+++ b/QTfrontend/gameuiconfig.cpp	Tue Apr 02 01:03:39 2019 +0200
@@ -115,7 +115,7 @@
     Form->ui.pageOptions->CBDampenAudio->setChecked(value("audio/dampen", true).toBool());
     Form->ui.pageOptions->SLVolume->setValue(value("audio/volume", 100).toUInt());
 
-    QString netNick = value("net/nick", tr("Guest")+QString("%1").arg(rand())).toString();
+    QString netNick = value("net/nick", getRandomNick()).toString();
     Form->ui.pageOptions->editNetNick->setText(netNick);
     bool savePwd = value("net/savepassword",true).toBool();
     Form->ui.pageOptions->CBSavePassword->setChecked(savePwd);
@@ -517,6 +517,16 @@
     Form->ui.pageOptions->editNetNick->setText(value("net/nick", "").toString());
 }
 
+QString GameUIConfig::getRandomNick()
+{
+    // Generate random nick name or pick old one if one was already generated.
+    QString nick;
+    if (cachedRandomNick.isNull())
+        // "Guest" + number between 1 and 99999
+        cachedRandomNick = tr("Guest") + QString("%1").arg(rand() % 99999 + 1);
+    return cachedRandomNick;
+}
+
 QByteArray GameUIConfig::netPasswordHash()
 {
     return QCryptographicHash::hash(Form->ui.pageOptions->editNetPassword->text().toUtf8(), QCryptographicHash::Md5).toHex();
--- a/QTfrontend/gameuiconfig.h	Tue Apr 02 00:13:25 2019 +0200
+++ b/QTfrontend/gameuiconfig.h	Tue Apr 02 01:03:39 2019 +0200
@@ -54,6 +54,7 @@
         quint8 volume();
         quint8 timerInterval();
         QString netNick();
+        QString getRandomNick();
         QByteArray netPasswordHash();
         int netPasswordLength();
         void clearPasswordHash();
@@ -104,6 +105,8 @@
         QList<BindAction> m_binds;
 
         void applyProxySettings();
+
+        QString cachedRandomNick;
 };
 
 #endif
--- a/QTfrontend/hwform.cpp	Tue Apr 02 00:13:25 2019 +0200
+++ b/QTfrontend/hwform.cpp	Tue Apr 02 01:03:39 2019 +0200
@@ -168,7 +168,7 @@
 
     config = new GameUIConfig(this, DataManager::instance().settingsFileName());
     frontendEffects = config->value("frontend/effects", true).toBool();
-    playerHash = QString(QCryptographicHash::hash(config->value("net/nick",tr("Guest")+QString("%1").arg(rand())).toString().toUtf8(), QCryptographicHash::Md5).toHex());
+    playerHash = QString(QCryptographicHash::hash(config->value("net/nick", config->getRandomNick()).toString().toUtf8(), QCryptographicHash::Md5).toHex());
 
     // Icons for finished missions
     finishedIcon.addFile(":/res/missionFinished.png", QSize(), QIcon::Normal, QIcon::On);
@@ -521,7 +521,7 @@
 
     if(teamslist.empty())
     {
-        QString currentNickName = config->value("net/nick",tr("Guest")+QString("%1").arg(rand())).toString();
+        QString currentNickName = config->value("net/nick", config->getRandomNick()).toString();
         QString teamName;
         int firstHumanTeam = 1;
         int lastHumanTeam = 2;
@@ -1523,7 +1523,7 @@
     if (hwnet->m_private_game == false && AskForNickAndPwd() != 0)
         return;
 
-    QString nickname = config->value("net/nick",tr("Guest")+QString("%1").arg(rand())).toString();
+    QString nickname = config->value("net/nick", config->getRandomNick()).toString();
     ui.pageRoomsList->setUser(nickname);
     ui.pageNetGame->setUser(nickname);
 
@@ -1542,7 +1542,7 @@
     QString password;
 
     do {
-        nickname = config->value("net/nick",tr("Guest")+QString("%1").arg(rand())).toString();
+        nickname = config->value("net/nick", config->getRandomNick()).toString();
         hash = config->passwordHash();
         temphash = config->tempHash();