From 4d466a9d2bac9e69ae3f53cd4d46c1dfc733ddf3 Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Tue, 22 Nov 2016 07:36:27 +0500 Subject: [PATCH] Servers sorting by ping and players count. For now only on "Servers" tab, and REQUIRES custom go-gtk build (see https://github.com/mattn/go-gtk/issues/290#issuecomment-262127956) --- ui/mainwindow_init.go | 4 +++ ui/mainwindow_servers_sorting.go | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 ui/mainwindow_servers_sorting.go diff --git a/ui/mainwindow_init.go b/ui/mainwindow_init.go index 3490181..dddb622 100644 --- a/ui/mainwindow_init.go +++ b/ui/mainwindow_init.go @@ -442,6 +442,10 @@ func (m *MainWindow) InitializeTabs() { // ToDo: remembering it to configuration storage. m.all_servers_store_sortable.SetSortColumnId(m.column_pos["Servers"]["Name"], gtk.SORT_ASCENDING) + // Sorting functions. + m.all_servers_store_sortable.SetSortFunc(m.column_pos["Servers"]["Players"], m.sortServersByPlayers) + m.all_servers_store_sortable.SetSortFunc(m.column_pos["Servers"]["Ping"], m.sortServersByPing) + // Selection changed signal, which will update server's short info pane. m.all_servers.Connect("cursor-changed", m.showShortServerInformation) diff --git a/ui/mainwindow_servers_sorting.go b/ui/mainwindow_servers_sorting.go new file mode 100644 index 0000000..cfb569d --- /dev/null +++ b/ui/mainwindow_servers_sorting.go @@ -0,0 +1,55 @@ +package ui + +import ( + // stdlib + "strconv" + "strings" + + // other + "github.com/mattn/go-gtk/glib" + "github.com/mattn/go-gtk/gtk" +) + +func (m *MainWindow) sortServersByName() { + +} + +func (m *MainWindow) sortServersByPlayers(model *gtk.TreeModel, a *gtk.TreeIter, b *gtk.TreeIter) int { + var players1_raw glib.GValue + var players2_raw glib.GValue + model.GetValue(a, m.column_pos["Servers"]["Players"], &players1_raw) + model.GetValue(b, m.column_pos["Servers"]["Players"], &players2_raw) + + players1_online := strings.Split(players1_raw.GetString(), "/")[0] + players2_online := strings.Split(players2_raw.GetString(), "/")[0] + + if len(players1_online) > 0 && len(players2_online) > 0 { + players1, _ := strconv.Atoi(players1_online) + players2, _ := strconv.Atoi(players2_online) + if players1 > players2 { + return -1 + } else { + return 1 + } + } + + return -1 +} + +func (m *MainWindow) sortServersByPing(model *gtk.TreeModel, a *gtk.TreeIter, b *gtk.TreeIter) int { + var ping1_raw glib.GValue + var ping2_raw glib.GValue + model.GetValue(a, m.column_pos["Servers"]["Ping"], &ping1_raw) + model.GetValue(b, m.column_pos["Servers"]["Ping"], &ping2_raw) + + ping1, _ := strconv.Atoi(ping1_raw.GetString()) + ping2, _ := strconv.Atoi(ping2_raw.GetString()) + + if ping1 < ping2 { + return 1 + } else { + return -1 + } + + return -1 +}