From e46d203a3cb0916fe5574f116322327769c9a267 Mon Sep 17 00:00:00 2001 From: pztrn Date: Fri, 7 Oct 2016 22:21:19 +0500 Subject: [PATCH] Many fixes and debug prints removed. Removed debug prints, as we don't really need them now. Fixed database updating from cache, until now only inserts was working fine, updates failing. All because of forgotten comma :(. Reworked handling of icons in servers lists, now they didn't recreated, but reused from created on application start. Can also save us much memory for long runs. Moved gamemode detector to mainwindow_helpers, where it should be. --- cache/cache_object.go | 13 +++++++-- database/database_object.go | 7 +++++ requester/pooler.go | 1 - ui/mainwindow.go | 55 ++++++++++++++----------------------- ui/mainwindow_helpers.go | 10 +++++++ ui/mainwindow_init.go | 12 ++++++-- 6 files changed, 58 insertions(+), 40 deletions(-) diff --git a/cache/cache_object.go b/cache/cache_object.go index 0d1d241..54c1bf9 100644 --- a/cache/cache_object.go +++ b/cache/cache_object.go @@ -84,17 +84,24 @@ func (c *Cache) FlushServers() { } } + Database.Unlock() tx := Database.Db.MustBegin() fmt.Println("Adding new servers...") - for _, srv := range new_servers { - tx.NamedExec("INSERT INTO servers (ip, port, name, ping, players, maxplayers, gamemode, map, version, extended_config, players_info, is_private) VALUES (:ip, :port, :name, :ping, :players, :maxplayers, :gamemode, :map, :version, :extended_config, :players_info, :is_private)", srv) + if len(new_servers) > 0 { + for _, srv := range new_servers { + tx.NamedExec("INSERT INTO servers (ip, port, name, ping, players, maxplayers, gamemode, map, version, extended_config, players_info, is_private) VALUES (:ip, :port, :name, :ping, :players, :maxplayers, :gamemode, :map, :version, :extended_config, :players_info, :is_private)", srv) + } } fmt.Println("Updating cached servers...") for _, srv := range cached_servers { - tx.NamedExec("UPDATE servers SET name=:name, players=:players, maxplayers=:maxplayers, gamemode=:gamemode, map=:map, ping=:ping, version=:version, extended_config=:extended_config, players_info=:players_info is_private=:is_private WHERE ip=:ip AND port=:port", &srv) + _, err := tx.NamedExec("UPDATE servers SET name=:name, players=:players, maxplayers=:maxplayers, gamemode=:gamemode, map=:map, ping=:ping, version=:version, extended_config=:extended_config, players_info=:players_info, is_private=:is_private WHERE ip=:ip AND port=:port", &srv) + if err != nil { + fmt.Println(err.Error()) + } } tx.Commit() + Database.Lock() fmt.Println("Done") } diff --git a/database/database_object.go b/database/database_object.go index 47b8f0a..466064d 100644 --- a/database/database_object.go +++ b/database/database_object.go @@ -75,6 +75,10 @@ func (d *Database) Initialize(cfg *configuration.Config) { } } +func (d *Database) Lock() { + runtime.LockOSThread() +} + func (d *Database) Migrate() { // Getting current database version. dbver := 0 @@ -91,3 +95,6 @@ func (d *Database) Migrate() { migrate_full(d, dbver) } +func (d *Database) Unlock() { + runtime.UnlockOSThread() +} diff --git a/requester/pooler.go b/requester/pooler.go index 01cde52..9a001c7 100644 --- a/requester/pooler.go +++ b/requester/pooler.go @@ -233,7 +233,6 @@ func (p *Pooler) updateSpecificServer(server *datamodels.Server) error { if len(received_lines) >= 2 { // Here we go, players information. players := received_lines[2:] - fmt.Println(players, len(players), len(players[0])) // Calculate players! if len(players) == 1 && len(players[0]) > 255 { server.Players = "0" diff --git a/ui/mainwindow.go b/ui/mainwindow.go index 8eb1b5b..47c98e6 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -21,6 +21,7 @@ import ( "github.com/pztrn/urtrator/ioq3dataparser" // Other + "github.com/mattn/go-gtk/gdkpixbuf" "github.com/mattn/go-gtk/glib" "github.com/mattn/go-gtk/gtk" ) @@ -111,6 +112,17 @@ type MainWindow struct { // Real columns positions on servers tabs. column_pos map[string]map[string]int + // Resources. + // Pixbufs. + // For unavailable (e.g. offline) server. + server_offline_pic *gdkpixbuf.Pixbuf + // For online server. + server_online_pic *gdkpixbuf.Pixbuf + // For private (passworded) server. + server_passworded_pic *gdkpixbuf.Pixbuf + // For public server + server_public_pic *gdkpixbuf.Pixbuf + // Flags. // Window is hidden? @@ -252,16 +264,6 @@ func (m *MainWindow) editFavorite() { } } -func (m *MainWindow) getGameModeName(name string) string { - val, ok := m.gamemodes[name] - - if !ok { - return "Unknown or custom" - } - - return val -} - // Executes when "Hide offline servers" checkbox changed it's state on // "Servers" tab. func (m *MainWindow) hideOfflineAllServers() { @@ -439,14 +441,14 @@ func (m *MainWindow) loadAllServers() { } if server.Server.Name == "" && server.Server.Players == "" && server.Server.Maxplayers == "" { - m.all_servers_store.SetValue(iter, 0, gtk.NewImage().RenderIcon(gtk.STOCK_NO, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + m.all_servers_store.SetValue(iter, 0, m.server_offline_pic.GPixbuf) m.all_servers_store.SetValue(iter, m.column_pos["Servers"]["IP"], server.Server.Ip + ":" + server.Server.Port) } else { - m.all_servers_store.SetValue(iter, 0, gtk.NewImage().RenderIcon(gtk.STOCK_OK, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + m.all_servers_store.SetValue(iter, 0, m.server_online_pic.GPixbuf) if server.Server.IsPrivate == "1" { - m.all_servers_store.SetValue(iter, 1, gtk.NewImage().RenderIcon(gtk.STOCK_CLOSE, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + m.all_servers_store.SetValue(iter, 1, m.server_passworded_pic.GPixbuf) } else { - m.all_servers_store.SetValue(iter, 1, gtk.NewImage().RenderIcon(gtk.STOCK_OK, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + m.all_servers_store.SetValue(iter, 1, m.server_public_pic.GPixbuf) } server_name := ctx.Colorizer.Fix(server.Server.Name) m.all_servers_store.SetValue(iter, m.column_pos["Servers"]["Name"], server_name) @@ -493,14 +495,14 @@ func (m *MainWindow) loadFavoriteServers() { } if server.Server.Name == "" && server.Server.Players == "" { - m.fav_servers_store.SetValue(iter, 0, gtk.NewImage().RenderIcon(gtk.STOCK_NO, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + m.fav_servers_store.SetValue(iter, 0, m.server_offline_pic.GPixbuf) m.fav_servers_store.SetValue(iter, m.column_pos["Favorites"]["IP"], server.Server.Ip + ":" + server.Server.Port) } else { - m.fav_servers_store.SetValue(iter, 0, gtk.NewImage().RenderIcon(gtk.STOCK_OK, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + m.fav_servers_store.SetValue(iter, 0, m.server_online_pic.GPixbuf) if server.Server.IsPrivate == "1" { - m.fav_servers_store.SetValue(iter, 1, gtk.NewImage().RenderIcon(gtk.STOCK_CLOSE, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + m.fav_servers_store.SetValue(iter, 1, m.server_passworded_pic.GPixbuf) } else { - m.fav_servers_store.SetValue(iter, 1, gtk.NewImage().RenderIcon(gtk.STOCK_OK, gtk.ICON_SIZE_SMALL_TOOLBAR, "").GPixbuf) + m.fav_servers_store.SetValue(iter, 1, m.server_public_pic.GPixbuf) } server_name := ctx.Colorizer.Fix(server.Server.Name) m.fav_servers_store.SetValue(iter, m.column_pos["Favorites"]["Name"], server_name) @@ -673,22 +675,7 @@ func (m *MainWindow) unlockInterface() { func (m *MainWindow) updateOneServer() { current_tab := m.tab_widget.GetTabLabelText(m.tab_widget.GetNthPage(m.tab_widget.GetCurrentPage())) - sel := m.all_servers.GetSelection() - model := m.all_servers.GetModel() - iter := new(gtk.TreeIter) - _ = sel.GetSelected(iter) - - // Getting server address. - var srv_addr string - srv_address_gval := glib.ValueFromNative(srv_addr) - model.GetValue(iter, m.column_pos["Servers"]["IP"], srv_address_gval) - if strings.Contains(current_tab, "Favorites") { - sel = m.fav_servers.GetSelection() - model = m.fav_servers.GetModel() - model.GetValue(iter, m.column_pos["Favorites"]["IP"], srv_address_gval) - } - - srv_address := srv_address_gval.GetString() + srv_address := m.getIpFromServersList(current_tab) if len(srv_address) > 0 { go ctx.Requester.UpdateOneServer(srv_address) diff --git a/ui/mainwindow_helpers.go b/ui/mainwindow_helpers.go index da406c7..6ae382c 100644 --- a/ui/mainwindow_helpers.go +++ b/ui/mainwindow_helpers.go @@ -9,6 +9,16 @@ import ( "github.com/mattn/go-gtk/gtk" ) +func (m *MainWindow) getGameModeName(name string) string { + val, ok := m.gamemodes[name] + + if !ok { + return "Unknown or custom" + } + + return val +} + func (m *MainWindow) getIpFromServersList(current_tab string) string { // Getting server's address from list. sel := m.all_servers.GetSelection() diff --git a/ui/mainwindow_init.go b/ui/mainwindow_init.go index 2785dfb..d82b27f 100644 --- a/ui/mainwindow_init.go +++ b/ui/mainwindow_init.go @@ -324,6 +324,16 @@ func (m *MainWindow) initializeStorages() { // Window hidden flag. m.hidden = false + + // Pixbufs. + // Offline server. + m.server_offline_pic = gtk.NewImage().RenderIcon(gtk.STOCK_NO, gtk.ICON_SIZE_SMALL_TOOLBAR, "") + // Online server. + m.server_online_pic = gtk.NewImage().RenderIcon(gtk.STOCK_OK, gtk.ICON_SIZE_SMALL_TOOLBAR, "") + // Passworded server. + m.server_passworded_pic = gtk.NewImage().RenderIcon(gtk.STOCK_CLOSE, gtk.ICON_SIZE_SMALL_TOOLBAR, "") + // Public server. + m.server_public_pic = gtk.NewImage().RenderIcon(gtk.STOCK_OK, gtk.ICON_SIZE_SMALL_TOOLBAR, "") } // Tabs widget initialization, including all child widgets. @@ -348,7 +358,6 @@ func (m *MainWindow) InitializeTabs() { // +2 because we have 2 static columns. all_servers_columns_to_append := make([]*gtk.TreeViewColumn, len(m.column_names) + 2) for pos, name := range m.column_names { - fmt.Println(pos, name) // Check if we have column position saved. If so - use it. // Otherwise use default position. // Should be actual only for first launch. @@ -424,7 +433,6 @@ func (m *MainWindow) InitializeTabs() { // +2 because we have 2 static columns. fav_servers_columns_to_append := make([]*gtk.TreeViewColumn, len(m.column_names) + 2) for pos, name := range m.column_names { - fmt.Println(pos, name) // Check if we have column position saved. If so - use it. // Otherwise use default position. // Should be actual only for first launch.