serverpassword.cpp
1 //------------------------------------------------------------------------------
2 // serverpassword.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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  foreach (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  foreach (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  {
83  lastServer = s;
84  }
85  }
86  return lastServer;
87 }
88 
89 QString ServerPassword::lastServerName() const
90 {
91  return lastServer().name();
92 }
93 
94 QDateTime ServerPassword::lastTime() const
95 {
96  return lastServer().time();
97 }
98 
99 ServerPasswordSummary ServerPassword::mostSimilarServer(const ServerPasswordSummary& other, float* outSimilarity) const
100 {
101  QList<ServerSimilarity> similarities;
102  foreach (const ServerPasswordSummary& candidate, d.servers)
103  {
104  if (candidate.similarity(other) > 0.0f)
105  {
106  similarities << ServerSimilarity(candidate, candidate.similarity(other));
107  }
108  }
109  if (!similarities.empty())
110  {
111  qSort(similarities);
112  if (outSimilarity != NULL)
113  {
114  *outSimilarity = similarities.last().similarity;
115  }
116  return similarities.last().server;
117  }
118  else
119  {
120  if (outSimilarity != NULL)
121  {
122  *outSimilarity = 0.0f;
123  }
124  return ServerPasswordSummary(); // Invalid value.
125  }
126 }
127 
128 void ServerPassword::removeServer(const QString& game, const QString& address, unsigned short port)
129 {
130  QMutableListIterator<ServerPasswordSummary> it(d.servers);
131  while (it.hasNext())
132  {
133  ServerPasswordSummary server = it.next();
134  if (server.game() == game && server.address() == address && server.port() == port)
135  {
136  it.remove();
137  }
138  }
139 }
140 
141 QVariant ServerPassword::serializeQVariant() const
142 {
143  QVariantMap m;
144  m.insert("phrase", d.phrase);
145  QVariantList variantServers;
146  foreach (const ServerPasswordSummary& s, d.servers)
147  {
148  variantServers << s.serializeQVariant();
149  }
150  m.insert("servers", variantServers);
151  return m;
152 }
float similarity(const ServerPasswordSummary &other) const
Similarity to the &#39;other&#39; server; between 0.0 and 1.0.