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

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

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9012) Sorting the metaserver / LAN servers list
From: "Com013" <kg.guests@xxxxxx>
Date: Thu, 17 Jun 2004 05:28:22 -0700
Reply-to: rt@xxxxxxxxxxx

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

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.

Greetings,

David Kolf

diff -ruN -Xfreeciv/diff_ignore freeciv/client/clinet.c 
freeciv+metasort/client/clinet.c
--- freeciv/client/clinet.c     2004-05-19 22:14:26.000000000 +0200
+++ freeciv+metasort/client/clinet.c    2004-06-15 22:42:23.000000000 +0200
@@ -483,6 +483,121 @@
 
 
 /**************************************************************************
+ 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)
+{
+  struct server *s1, *s2;
+
+  /* p1 and p2 are pointers to pointers */
+  s1 = (struct server *) *(struct server **)p1;
+  s2 = (struct server *) *(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. */
+
+  {
+    char *cur_ver = VERSION_STRING;
+    int dot = 0, incompatible1 = 0, incompatible2 = 0;
+    int i;
+
+    for (i = 0; cur_ver[i] != 0 && dot < 2; i++) {
+      if (!incompatible1 && s1->version[i] != cur_ver[i]) {
+       incompatible1 = 1;
+      }
+
+      if (!incompatible2 && s2->version[i] != cur_ver[i]) {
+       incompatible2 = 1;
+      }
+
+      if (cur_ver[i] == '.') {
+       dot++;
+      }
+    }
+
+    if (incompatible1 && !incompatible2) {
+      return 1;
+    } else if (!incompatible1 && incompatible2) {
+      return -1;
+    }
+  }
+
+  /* Game status. */
+
+  {
+    char *status_strings[] = {
+      "Pregame",
+      "Waiting",
+      "Running",
+      "Game over"
+    };
+
+    int status1 = 4, status2 = 4;
+    int i;
+
+    for (i = 0; i < 4; i++) {
+      if (strcmp(s1->status, status_strings[i]) == 0) {
+       status1 = i;
+      }
+      if (strcmp(s2->status, status_strings[i]) == 0) {
+       status2 = i;
+      }
+    }
+
+    if (status1 < status2) {
+      return -1;
+    } else if (status1 > status2) {
+      return 1;
+    }
+  }
+
+  /* Players.
+     The servers with the most players should apear first */
+  {
+    int c1, c2;
+
+    c1 = atoi(s1->players);
+    c2 = atoi(s2->players);
+
+    if (c1 > c2) {
+      return -1;
+    } else if (c2 > c1) {
+      return 1;
+    }
+  }
+
+  /* Servername */
+  {
+    int c = strcmp(s1->name, s2->name);
+
+    if (c != 0) return c;
+  }
+
+  /* Port */
+  {
+    int p1, p2;
+
+    p1 = atoi(s1->port);
+    p2 = atoi(s2->port);
+
+    if (p1 < p2) {
+      return -1;
+    } else if (p1 > p2) {
+      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
@@ -634,6 +749,7 @@
   }
   fz_fclose(f);
 
+  server_list_sort (server_list, compare_servers);
   return server_list;
 }
 
@@ -843,6 +959,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) Sorting the metaserver / LAN servers list, Com013 <=