Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2004:
[Freeciv-Dev] (PR#9012) Patch: Sorting the metaserver / LAN servers list
Home

[Freeciv-Dev] (PR#9012) Patch: Sorting the metaserver / LAN servers list

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: kg.guests@xxxxxx
Subject: [Freeciv-Dev] (PR#9012) Patch: Sorting the metaserver / LAN servers list
From: "Vasco Alexandre da Silva Costa" <vasc@xxxxxxxxxxxxxx>
Date: Sat, 18 Sep 2004 18:45:22 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=9012 >

> [kg.guests@xxxxxx - Thu Jun 17 12:28:21 2004]:
> 
> This is a small patch that sorts the list of servers so that the server 
> with a compatible version, in pregame state and with the most players 
> apear at the start.
> 
> I wrote this because I got bored of scrolling through the whole list 
> just to find the "best" server.
> 
> I am sorry, I already sent this to the freeciv-dev list, but noticed it 
> didn't create a ticket - I hope it works now.

I modified it a bit so sorting by version works like it should, but I'm
still not very happy with the sorting results.

Index: client/clinet.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/clinet.c,v
retrieving revision 1.100
diff -u -r1.100 clinet.c
--- client/clinet.c     20 Jul 2004 17:13:52 -0000      1.100
+++ client/clinet.c     19 Sep 2004 01:43:52 -0000
@@ -488,6 +488,85 @@
 
 
 /**************************************************************************
+ Compare two metaserver entries so we can sort the list.
+ Currently this is just a quick hack - it might be better to
+ shift this to the user interface, so that the user can change the
+ sorting. - D. Kolf
+**************************************************************************/
+static int compare_servers(const void *p1, const void *p2)
+{
+  const struct server *s1, *s2;
+
+  int major1, minor1, major2, minor2;
+  bool incompatible1, incompatible2;
+
+  int players1, players2;
+
+  int cmp;
+
+  int port1, port2;
+
+  /* p1 and p2 are pointers to pointers */
+  s1 = (const struct server *) *(const struct server **)p1;
+  s2 = (const struct server *) *(const struct server **)p2;
+
+  /* First we check whether one of the servers has a compatible version
+     while the other has not. The one with the 'better' version should
+     come first.
+     The versions are considered compatible when they match up to the
+     second dot or to the end of the string. */
+  if (sscanf(s1->version, "%d.%d.", &major1, &minor1) == 2) {
+    incompatible1 = (major1 != MAJOR_VERSION || minor1 != MINOR_VERSION);
+  } else {
+    incompatible1 = TRUE;
+  }
+  if (sscanf(s2->version, "%d.%d.", &major2, &minor2) == 2) {
+    incompatible2 = (major2 != MAJOR_VERSION || minor2 != MINOR_VERSION);
+  } else {
+    incompatible2 = TRUE;
+  }
+
+  if (incompatible1 && !incompatible2) {
+    return 1;
+  } else if (!incompatible1 && incompatible2) {
+    return -1;
+  }
+
+  /* Skip game status comparison. */
+
+  /* Players.
+     The servers with the most players should apear first */
+  players1 = atoi(s1->players);
+  players2 = atoi(s2->players);
+
+  if (players1 > players2) {
+    return -1;
+  } else if (players2 > players1) {
+    return 1;
+  }
+
+  /* Servername */
+  cmp = strcmp(s1->name, s2->name);
+
+  if (cmp != 0) {
+    return cmp;
+  }
+
+  /* Port */
+  port1 = atoi(s1->port);
+  port2 = atoi(s2->port);
+
+  if (port1 < port2) {
+    return -1;
+  } else if (port1 > port2) {
+    return 1;
+  }
+
+  /* Still couldn't decide? */
+  return 0;
+}
+
+/**************************************************************************
  Create the list of servers from the metaserver
  The result must be free'd with delete_server_list() when no
  longer used
@@ -639,6 +718,7 @@
   }
   fz_fclose(f);
 
+  server_list_sort(server_list, compare_servers);
   return server_list;
 }
 
@@ -848,6 +928,7 @@
     pserver->metastring = mystrdup(metastring);
 
     server_list_insert(lan_servers, pserver);
+    server_list_sort(lan_servers, compare_servers);
   } else {
     return lan_servers;
   }                                       

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#9012) Patch: Sorting the metaserver / LAN servers list, Vasco Alexandre da Silva Costa <=