serversummary.cpp
1 //------------------------------------------------------------------------------
2 // serversummary.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 "serversummary.h"
24 
25 #include "serverapi/server.h"
26 
27 ServerSummary::ServerSummary(const Server *server)
28 {
29  setAddress(server->address().toString());
30  setPort(server->port());
31  setGame(server->engineName());
32  setName(server->name());
33 }
34 
35 ServerSummary ServerSummary::deserializeQVariant(const QVariant& v)
36 {
37  QVariantMap m = v.toMap();
38  ServerSummary o;
39  o.setAddress(m["address"].toString());
40  o.setGame(m["game"].toString());
41  o.setName(m["name"].toString());
42  o.setPort(m["port"].toUInt());
43  o.setTime(m["time"].toDateTime());
44  return o;
45 }
46 
47 QVariant ServerSummary::serializeQVariant() const
48 {
49  QVariantMap m;
50  m.insert("address", d.address);
51  m.insert("game", d.game);
52  m.insert("name", d.name);
53  m.insert("port", d.port);
54  m.insert("time", d.time);
55  return m;
56 }
57 
58 float ServerSummary::similarity(const ServerSummary& other) const
59 {
60  if (!isValid() || !other.isValid())
61  {
62  return 0.0f;
63  }
64 
65  const float MAX_SIMILARITY = 3.0f;
66  float similarity = 0.0f;
67  if (address() == other.address() && port() == other.port() && game() == other.game())
68  {
69  similarity += 2.0f;
70  }
71  if (name() == other.name())
72  {
73  similarity += 1.0f;
74  }
75  return similarity / MAX_SIMILARITY;
76 }
QString engineName() const
Returns name of the engine for this server, for example: "Skulltag".
Definition: server.cpp:282
A representation of a server for a given game.
Definition: server.h:93
const QString & name() const
Server&#39;s name.
Definition: server.cpp:430
float similarity(const ServerSummary &other) const
Similarity to the &#39;other&#39; server; between 0.0 and 1.0.
unsigned short port() const
Network port on which this server is hosted.
Definition: server.cpp:468
const QHostAddress & address() const
Address of this server.
Definition: server.cpp:193