Updated to latest GTK2 bindings API and moved to Gitlab.

This commit is contained in:
2018-11-10 13:21:53 +05:00
parent 070aa50762
commit 127e1b8ab9
27 changed files with 3347 additions and 3351 deletions

50
cache/cache_object.go vendored
View File

@@ -10,41 +10,41 @@
package cache
import (
// stdlib
"fmt"
"sync"
// stdlib
"fmt"
"sync"
// local
"github.com/pztrn/urtrator/cachemodels"
// local
"gitlab.com/pztrn/urtrator/cachemodels"
)
type Cache struct {
// Profiles cache.
Profiles map[string]*cachemodels.Profile
// Profiles cache mutex.
ProfilesMutex sync.Mutex
// Servers cache.
Servers map[string]*cachemodels.Server
// Servers cache mutex.
ServersMutex sync.Mutex
// Profiles cache.
Profiles map[string]*cachemodels.Profile
// Profiles cache mutex.
ProfilesMutex sync.Mutex
// Servers cache.
Servers map[string]*cachemodels.Server
// Servers cache mutex.
ServersMutex sync.Mutex
}
func (c *Cache) Initialize() {
fmt.Println("Initializing cache...")
c.initializeStorages()
c.LoadServers(map[string]string{})
fmt.Println("Initializing cache...")
c.initializeStorages()
c.LoadServers(map[string]string{})
Eventer.AddEventHandler("deleteProfile", c.deleteProfile)
Eventer.AddEventHandler("flushProfiles", c.FlushProfiles)
Eventer.AddEventHandler("loadProfiles", c.LoadProfiles)
Eventer.AddEventHandler("deleteProfile", c.deleteProfile)
Eventer.AddEventHandler("flushProfiles", c.FlushProfiles)
Eventer.AddEventHandler("loadProfiles", c.LoadProfiles)
Eventer.AddEventHandler("flushServers", c.FlushServers)
Eventer.AddEventHandler("loadServersIntoCache", c.LoadServers)
Eventer.AddEventHandler("flushServers", c.FlushServers)
Eventer.AddEventHandler("loadServersIntoCache", c.LoadServers)
}
func (c *Cache) initializeStorages() {
// Profiles cache.
c.Profiles = make(map[string]*cachemodels.Profile)
// Servers cache.
c.Servers = make(map[string]*cachemodels.Server)
// Profiles cache.
c.Profiles = make(map[string]*cachemodels.Profile)
// Servers cache.
c.Servers = make(map[string]*cachemodels.Server)
}

View File

@@ -10,105 +10,105 @@
package cache
import (
// stdlib
"fmt"
"strconv"
// stdlib
"fmt"
"strconv"
// local
"github.com/pztrn/urtrator/cachemodels"
"github.com/pztrn/urtrator/datamodels"
// local
"gitlab.com/pztrn/urtrator/cachemodels"
"gitlab.com/pztrn/urtrator/datamodels"
)
func (c *Cache) CreateProfile(name string) {
fmt.Println("Creating profile " + name)
_, ok := c.Profiles[name]
fmt.Println("Creating profile " + name)
_, ok := c.Profiles[name]
if !ok {
c.ProfilesMutex.Lock()
c.Profiles[name] = &cachemodels.Profile{}
c.Profiles[name].Profile = &datamodels.Profile{}
c.ProfilesMutex.Unlock()
}
if !ok {
c.ProfilesMutex.Lock()
c.Profiles[name] = &cachemodels.Profile{}
c.Profiles[name].Profile = &datamodels.Profile{}
c.ProfilesMutex.Unlock()
}
}
func (c *Cache) deleteProfile(data map[string]string) {
fmt.Println("Deleting profile " + data["profile_name"])
fmt.Println("Deleting profile " + data["profile_name"])
c.ProfilesMutex.Lock()
_, ok := c.Profiles[data["profile_name"]]
c.ProfilesMutex.Unlock()
if ok {
c.ProfilesMutex.Lock()
delete(c.Profiles, data["profile_name"])
c.ProfilesMutex.Unlock()
}
c.ProfilesMutex.Lock()
_, ok := c.Profiles[data["profile_name"]]
c.ProfilesMutex.Unlock()
if ok {
c.ProfilesMutex.Lock()
delete(c.Profiles, data["profile_name"])
c.ProfilesMutex.Unlock()
}
c.ProfilesMutex.Lock()
_, ok1 := c.Profiles[data["profile_name"]]
c.ProfilesMutex.Unlock()
if !ok1 {
Database.Db.MustExec(Database.Db.Rebind("DELETE FROM urt_profiles WHERE name=?"), data["profile_name"])
fmt.Println("Profile deleted")
} else {
fmt.Println("Something goes wrong! Profile is still here!")
}
c.ProfilesMutex.Lock()
_, ok1 := c.Profiles[data["profile_name"]]
c.ProfilesMutex.Unlock()
if !ok1 {
Database.Db.MustExec(Database.Db.Rebind("DELETE FROM urt_profiles WHERE name=?"), data["profile_name"])
fmt.Println("Profile deleted")
} else {
fmt.Println("Something goes wrong! Profile is still here!")
}
}
func (c *Cache) FlushProfiles(data map[string]string) {
fmt.Println("Flushing profiles to database...")
fmt.Println("Flushing profiles to database...")
raw_profiles := []datamodels.Profile{}
err := Database.Db.Select(&raw_profiles, "SELECT * FROM urt_profiles")
if err != nil {
fmt.Println(err.Error())
}
raw_profiles := []datamodels.Profile{}
err := Database.Db.Select(&raw_profiles, "SELECT * FROM urt_profiles")
if err != nil {
fmt.Println(err.Error())
}
cached_profiles := make(map[string]*datamodels.Profile)
for i := range raw_profiles {
cached_profiles[raw_profiles[i].Name] = c.Profiles[raw_profiles[i].Name].Profile
}
cached_profiles := make(map[string]*datamodels.Profile)
for i := range raw_profiles {
cached_profiles[raw_profiles[i].Name] = c.Profiles[raw_profiles[i].Name].Profile
}
new_profiles := make(map[string]*datamodels.Profile)
new_profiles := make(map[string]*datamodels.Profile)
c.ProfilesMutex.Lock()
for _, profile := range c.Profiles {
_, ok := cached_profiles[profile.Profile.Name]
if !ok {
fmt.Println("Flushing new profile " + profile.Profile.Name)
new_profiles[profile.Profile.Name] = profile.Profile
}
}
c.ProfilesMutex.Unlock()
c.ProfilesMutex.Lock()
for _, profile := range c.Profiles {
_, ok := cached_profiles[profile.Profile.Name]
if !ok {
fmt.Println("Flushing new profile " + profile.Profile.Name)
new_profiles[profile.Profile.Name] = profile.Profile
}
}
c.ProfilesMutex.Unlock()
tx := Database.Db.MustBegin()
fmt.Println("Adding new profiles...")
for _, profile := range new_profiles {
tx.NamedExec("INSERT INTO urt_profiles (name, version, binary, second_x_session, additional_parameters, profile_path) VALUES (:name, :version, :binary, :second_x_session, :additional_parameters, :profile_path)", &profile)
}
fmt.Println("Updating existing profiles...")
for _, profile := range cached_profiles {
fmt.Println(fmt.Sprintf("%+v", profile))
tx.NamedExec("UPDATE urt_profiles SET name=:name, version=:version, binary=:binary, second_x_session=:second_x_session, additional_parameters=:additional_parameters, profile_path=:profile_path WHERE name=:name", &profile)
}
tx.Commit()
fmt.Println("Done")
tx := Database.Db.MustBegin()
fmt.Println("Adding new profiles...")
for _, profile := range new_profiles {
tx.NamedExec("INSERT INTO urt_profiles (name, version, binary, second_x_session, additional_parameters, profile_path) VALUES (:name, :version, :binary, :second_x_session, :additional_parameters, :profile_path)", &profile)
}
fmt.Println("Updating existing profiles...")
for _, profile := range cached_profiles {
fmt.Println(fmt.Sprintf("%+v", profile))
tx.NamedExec("UPDATE urt_profiles SET name=:name, version=:version, binary=:binary, second_x_session=:second_x_session, additional_parameters=:additional_parameters, profile_path=:profile_path WHERE name=:name", &profile)
}
tx.Commit()
fmt.Println("Done")
}
func (c *Cache) LoadProfiles(data map[string]string) {
fmt.Println("Loading profiles to cache...")
fmt.Println("Loading profiles to cache...")
raw_profiles := []datamodels.Profile{}
err := Database.Db.Select(&raw_profiles, "SELECT * FROM urt_profiles")
if err != nil {
fmt.Println(err.Error())
}
raw_profiles := []datamodels.Profile{}
err := Database.Db.Select(&raw_profiles, "SELECT * FROM urt_profiles")
if err != nil {
fmt.Println(err.Error())
}
c.ProfilesMutex.Lock()
for _, profile := range raw_profiles {
c.Profiles[profile.Name] = &cachemodels.Profile{}
c.Profiles[profile.Name].Profile = &profile
}
c.ProfilesMutex.Unlock()
c.ProfilesMutex.Lock()
for _, profile := range raw_profiles {
c.Profiles[profile.Name] = &cachemodels.Profile{}
c.Profiles[profile.Name].Profile = &profile
}
c.ProfilesMutex.Unlock()
fmt.Println("Load completed. Loaded " + strconv.Itoa(len(c.Profiles)) + " profiles.")
fmt.Println("Load completed. Loaded " + strconv.Itoa(len(c.Profiles)) + " profiles.")
}

228
cache/cache_servers.go vendored
View File

@@ -10,135 +10,135 @@
package cache
import (
// stdlib
"fmt"
// stdlib
"fmt"
// local
"github.com/pztrn/urtrator/cachemodels"
"github.com/pztrn/urtrator/datamodels"
// local
"gitlab.com/pztrn/urtrator/cachemodels"
"gitlab.com/pztrn/urtrator/datamodels"
)
func (c *Cache) CreateServer(addr string) {
_, ok := c.Servers[addr]
if !ok {
c.ServersMutex.Lock()
c.Servers[addr] = &cachemodels.Server{}
c.Servers[addr].Server = &datamodels.Server{}
c.ServersMutex.Unlock()
} else {
fmt.Println("Server " + addr + " already exist.")
}
_, ok := c.Servers[addr]
if !ok {
c.ServersMutex.Lock()
c.Servers[addr] = &cachemodels.Server{}
c.Servers[addr].Server = &datamodels.Server{}
c.ServersMutex.Unlock()
} else {
fmt.Println("Server " + addr + " already exist.")
}
}
// Flush servers to database.
func (c *Cache) FlushServers(data map[string]string) {
fmt.Println("Updating servers information in database...")
raw_cached := []datamodels.Server{}
Database.Db.Select(&raw_cached, "SELECT * FROM servers")
fmt.Println("Updating servers information in database...")
raw_cached := []datamodels.Server{}
Database.Db.Select(&raw_cached, "SELECT * FROM servers")
// Create map[string]*datamodels.Server once, so we won't iterate
// over slice of datamodels.Server everytime.
cached_servers := make(map[string]*datamodels.Server)
for s := range raw_cached {
mapping_item_name := raw_cached[s].Ip + ":" + raw_cached[s].Port
cached_servers[mapping_item_name] = &raw_cached[s]
}
// Create map[string]*datamodels.Server once, so we won't iterate
// over slice of datamodels.Server everytime.
cached_servers := make(map[string]*datamodels.Server)
for s := range raw_cached {
mapping_item_name := raw_cached[s].Ip + ":" + raw_cached[s].Port
cached_servers[mapping_item_name] = &raw_cached[s]
}
new_servers := make(map[string]*datamodels.Server)
new_servers := make(map[string]*datamodels.Server)
// Update our cached mapping.
for _, s := range c.Servers {
mapping_item_name := s.Server.Ip + ":" + s.Server.Port
_, ok := cached_servers[mapping_item_name]
if !ok {
fmt.Println(mapping_item_name + " not found!")
new_servers[mapping_item_name] = &datamodels.Server{}
new_servers[mapping_item_name].Ip = s.Server.Ip
new_servers[mapping_item_name].Port = s.Server.Port
new_servers[mapping_item_name].Name = s.Server.Name
new_servers[mapping_item_name].Players = s.Server.Players
new_servers[mapping_item_name].Bots = s.Server.Bots
new_servers[mapping_item_name].Maxplayers = s.Server.Maxplayers
new_servers[mapping_item_name].Ping = s.Server.Ping
new_servers[mapping_item_name].Map = s.Server.Map
new_servers[mapping_item_name].Gamemode = s.Server.Gamemode
new_servers[mapping_item_name].Version = s.Server.Version
new_servers[mapping_item_name].ExtendedConfig = s.Server.ExtendedConfig
new_servers[mapping_item_name].PlayersInfo = s.Server.PlayersInfo
new_servers[mapping_item_name].IsPrivate = s.Server.IsPrivate
new_servers[mapping_item_name].Favorite = s.Server.Favorite
new_servers[mapping_item_name].ProfileToUse = s.Server.ProfileToUse
new_servers[mapping_item_name].Password = s.Server.Password
} else {
cached_servers[mapping_item_name].Ip = s.Server.Ip
cached_servers[mapping_item_name].Port = s.Server.Port
cached_servers[mapping_item_name].Name = s.Server.Name
cached_servers[mapping_item_name].Players = s.Server.Players
cached_servers[mapping_item_name].Bots = s.Server.Bots
cached_servers[mapping_item_name].Maxplayers = s.Server.Maxplayers
cached_servers[mapping_item_name].Ping = s.Server.Ping
cached_servers[mapping_item_name].Map = s.Server.Map
cached_servers[mapping_item_name].Gamemode = s.Server.Gamemode
cached_servers[mapping_item_name].Version = s.Server.Version
cached_servers[mapping_item_name].ExtendedConfig = s.Server.ExtendedConfig
cached_servers[mapping_item_name].PlayersInfo = s.Server.PlayersInfo
cached_servers[mapping_item_name].IsPrivate = s.Server.IsPrivate
cached_servers[mapping_item_name].Favorite = s.Server.Favorite
cached_servers[mapping_item_name].ProfileToUse = s.Server.ProfileToUse
cached_servers[mapping_item_name].Password = s.Server.Password
}
}
// Update our cached mapping.
for _, s := range c.Servers {
mapping_item_name := s.Server.Ip + ":" + s.Server.Port
_, ok := cached_servers[mapping_item_name]
if !ok {
fmt.Println(mapping_item_name + " not found!")
new_servers[mapping_item_name] = &datamodels.Server{}
new_servers[mapping_item_name].Ip = s.Server.Ip
new_servers[mapping_item_name].Port = s.Server.Port
new_servers[mapping_item_name].Name = s.Server.Name
new_servers[mapping_item_name].Players = s.Server.Players
new_servers[mapping_item_name].Bots = s.Server.Bots
new_servers[mapping_item_name].Maxplayers = s.Server.Maxplayers
new_servers[mapping_item_name].Ping = s.Server.Ping
new_servers[mapping_item_name].Map = s.Server.Map
new_servers[mapping_item_name].Gamemode = s.Server.Gamemode
new_servers[mapping_item_name].Version = s.Server.Version
new_servers[mapping_item_name].ExtendedConfig = s.Server.ExtendedConfig
new_servers[mapping_item_name].PlayersInfo = s.Server.PlayersInfo
new_servers[mapping_item_name].IsPrivate = s.Server.IsPrivate
new_servers[mapping_item_name].Favorite = s.Server.Favorite
new_servers[mapping_item_name].ProfileToUse = s.Server.ProfileToUse
new_servers[mapping_item_name].Password = s.Server.Password
} else {
cached_servers[mapping_item_name].Ip = s.Server.Ip
cached_servers[mapping_item_name].Port = s.Server.Port
cached_servers[mapping_item_name].Name = s.Server.Name
cached_servers[mapping_item_name].Players = s.Server.Players
cached_servers[mapping_item_name].Bots = s.Server.Bots
cached_servers[mapping_item_name].Maxplayers = s.Server.Maxplayers
cached_servers[mapping_item_name].Ping = s.Server.Ping
cached_servers[mapping_item_name].Map = s.Server.Map
cached_servers[mapping_item_name].Gamemode = s.Server.Gamemode
cached_servers[mapping_item_name].Version = s.Server.Version
cached_servers[mapping_item_name].ExtendedConfig = s.Server.ExtendedConfig
cached_servers[mapping_item_name].PlayersInfo = s.Server.PlayersInfo
cached_servers[mapping_item_name].IsPrivate = s.Server.IsPrivate
cached_servers[mapping_item_name].Favorite = s.Server.Favorite
cached_servers[mapping_item_name].ProfileToUse = s.Server.ProfileToUse
cached_servers[mapping_item_name].Password = s.Server.Password
}
}
tx := Database.Db.MustBegin()
fmt.Println("Adding new servers...")
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, favorite, profile_to_use, bots) VALUES (:ip, :port, :name, :ping, :players, :maxplayers, :gamemode, :map, :version, :extended_config, :players_info, :is_private, :favorite, :profile_to_use, :bots)", srv)
}
}
fmt.Println("Updating cached servers...")
for _, srv := range cached_servers {
_, err := tx.NamedExec("UPDATE servers SET name=:name, players=:players, maxplayers=:maxplayers, gamemode=:gamemode, map=:map, ping=:ping, version=:version, extended_config=:extended_config, favorite=:favorite, password=:password, players_info=:players_info, is_private=:is_private, profile_to_use=:profile_to_use, bots=:bots WHERE ip=:ip AND port=:port", &srv)
if err != nil {
fmt.Println(err.Error())
}
}
tx := Database.Db.MustBegin()
fmt.Println("Adding new servers...")
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, favorite, profile_to_use, bots) VALUES (:ip, :port, :name, :ping, :players, :maxplayers, :gamemode, :map, :version, :extended_config, :players_info, :is_private, :favorite, :profile_to_use, :bots)", srv)
}
}
fmt.Println("Updating cached servers...")
for _, srv := range cached_servers {
_, err := tx.NamedExec("UPDATE servers SET name=:name, players=:players, maxplayers=:maxplayers, gamemode=:gamemode, map=:map, ping=:ping, version=:version, extended_config=:extended_config, favorite=:favorite, password=:password, players_info=:players_info, is_private=:is_private, profile_to_use=:profile_to_use, bots=:bots WHERE ip=:ip AND port=:port", &srv)
if err != nil {
fmt.Println(err.Error())
}
}
tx.Commit()
fmt.Println("Done")
tx.Commit()
fmt.Println("Done")
}
func (c *Cache) LoadServers(data map[string]string) {
fmt.Println("Loading servers into cache...")
c.Servers = make(map[string]*cachemodels.Server)
// Getting servers from database.
raw_servers := []datamodels.Server{}
err := Database.Db.Select(&raw_servers, "SELECT * FROM servers")
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("Loading servers into cache...")
c.Servers = make(map[string]*cachemodels.Server)
// Getting servers from database.
raw_servers := []datamodels.Server{}
err := Database.Db.Select(&raw_servers, "SELECT * FROM servers")
if err != nil {
fmt.Println(err.Error())
}
// Due to nature of pointers and goroutines thing (?) this should
// be done in this way.
for _, server := range raw_servers {
key := server.Ip + ":" + server.Port
c.CreateServer(key)
c.Servers[key].Server.Name = server.Name
c.Servers[key].Server.Ip = server.Ip
c.Servers[key].Server.Port = server.Port
c.Servers[key].Server.Players = server.Players
c.Servers[key].Server.Bots = server.Bots
c.Servers[key].Server.Maxplayers = server.Maxplayers
c.Servers[key].Server.Ping = server.Ping
c.Servers[key].Server.Gamemode = server.Gamemode
c.Servers[key].Server.Map = server.Map
c.Servers[key].Server.Version = server.Version
c.Servers[key].Server.Favorite = server.Favorite
c.Servers[key].Server.Password = server.Password
c.Servers[key].Server.ProfileToUse = server.ProfileToUse
c.Servers[key].Server.ExtendedConfig = server.ExtendedConfig
c.Servers[key].Server.PlayersInfo = server.PlayersInfo
c.Servers[key].Server.IsPrivate = server.IsPrivate
}
fmt.Println("Load completed.")
// Due to nature of pointers and goroutines thing (?) this should
// be done in this way.
for _, server := range raw_servers {
key := server.Ip + ":" + server.Port
c.CreateServer(key)
c.Servers[key].Server.Name = server.Name
c.Servers[key].Server.Ip = server.Ip
c.Servers[key].Server.Port = server.Port
c.Servers[key].Server.Players = server.Players
c.Servers[key].Server.Bots = server.Bots
c.Servers[key].Server.Maxplayers = server.Maxplayers
c.Servers[key].Server.Ping = server.Ping
c.Servers[key].Server.Gamemode = server.Gamemode
c.Servers[key].Server.Map = server.Map
c.Servers[key].Server.Version = server.Version
c.Servers[key].Server.Favorite = server.Favorite
c.Servers[key].Server.Password = server.Password
c.Servers[key].Server.ProfileToUse = server.ProfileToUse
c.Servers[key].Server.ExtendedConfig = server.ExtendedConfig
c.Servers[key].Server.PlayersInfo = server.PlayersInfo
c.Servers[key].Server.IsPrivate = server.IsPrivate
}
fmt.Println("Load completed.")
}

18
cache/exported.go vendored
View File

@@ -10,19 +10,19 @@
package cache
import (
// local
event "github.com/pztrn/urtrator/eventer"
"github.com/pztrn/urtrator/database"
// local
"gitlab.com/pztrn/urtrator/database"
event "gitlab.com/pztrn/urtrator/eventer"
)
var (
Database *database.Database
Eventer *event.Eventer
Database *database.Database
Eventer *event.Eventer
)
func New(d *database.Database, e *event.Eventer) *Cache {
Database = d
Eventer = e
c := Cache{}
return &c
Database = d
Eventer = e
c := Cache{}
return &c
}