serverpassword.cpp
1 //------------------------------------------------------------------------------
2 // serverpassword.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2013 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "serverpassword.h"
24 
25 class ServerSimilarity
26 {
27 public:
28  ServerPasswordSummary server;
29  float similarity;
30 
31  ServerSimilarity(const ServerPasswordSummary &server, float similarity)
32  {
33  this->server = server;
34  this->similarity = similarity;
35  }
36 
37  bool operator<(const ServerSimilarity &other) const
38  {
39  return similarity < other.similarity;
40  }
41 };
42 
43 void ServerPassword::addServer(const ServerPasswordSummary &v)
44 {
45  if (v.isValid())
46  {
47  removeServer(v.game(), v.address(), v.port());
48  d.servers.append(v);
49  }
50 }
51 
52 ServerPassword ServerPassword::deserializeQVariant(const QVariant &v)
53 {
54  QVariantMap m = v.toMap();
56  o.setPhrase(m["phrase"].toString());
57  QVariantList variantServers = m["servers"].toList();
58  for (QVariant server : variantServers)
59  {
60  o.addServer(ServerPasswordSummary::deserializeQVariant(server));
61  }
62  return o;
63 }
64 
65 QString ServerPassword::lastGame() const
66 {
67  return lastServer().game();
68 }
69 
70 ServerPasswordSummary ServerPassword::lastServer() const
71 {
72  ServerPasswordSummary lastServer;
73  for (const ServerPasswordSummary &s : d.servers)
74  {
75  if (!lastServer.isValid())
76  {
77  lastServer = s;
78  continue;
79  }
80 
81  if (s.isValid() && s.time() > lastServer.time())
82  lastServer = s;
83  }
84  return lastServer;
85 }
86 
87 QString ServerPassword::lastServerName() const
88 {
89  return lastServer().name();
90 }
91 
92 QDateTime ServerPassword::lastTime() const
93 {
94  return lastServer().time();
95 }
96 
97 ServerPasswordSummary ServerPassword::mostSimilarServer(const ServerPasswordSummary &other, float *outSimilarity) const
98 {
99  QList<ServerSimilarity> similarities;
100  for (const ServerPasswordSummary &candidate : d.servers)
101  {
102  if (candidate.similarity(other) > 0.0f)
103  similarities << ServerSimilarity(candidate, candidate.similarity(other));
104  }
105  if (!similarities.empty())
106  {
107  std::sort(similarities.begin(), similarities.end());
108  if (outSimilarity != nullptr)
109  *outSimilarity = similarities.last().similarity;
110  return similarities.last().server;
111  }
112  else
113  {
114  if (outSimilarity != nullptr)
115  *outSimilarity = 0.0f;
116  return ServerPasswordSummary(); // Invalid value.
117  }
118 }
119 
120 void ServerPassword::removeServer(const QString &game, const QString &address, unsigned short port)
121 {
122  QMutableListIterator<ServerPasswordSummary> it(d.servers);
123  while (it.hasNext())
124  {
125  ServerPasswordSummary server = it.next();
126  if (server.game() == game && server.address() == address && server.port() == port)
127  it.remove();
128  }
129 }
130 
131 QVariant ServerPassword::serializeQVariant() const
132 {
133  QVariantMap m;
134  m.insert("phrase", d.phrase);
135  QVariantList variantServers;
136  for (const ServerPasswordSummary &s : d.servers)
137  {
138  variantServers << s.serializeQVariant();
139  }
140  m.insert("servers", variantServers);
141  return m;
142 }