This repository has been archived on 2022-06-29. You can view files and clone it, but cannot push or open issues or pull requests.
urtrator/database/database_object.go
pztrn 063f6a622a Favorites and fixes.
Favorites functionality done, except editing favorite server.
Note, that game profile defined for favorite server have
higher priority on normal profile selected in combobox near
"Launch!" button. Later WILL be ignored.

Bunch of fixes is added.
2016-10-04 23:39:32 +05:00

113 lines
3.5 KiB
Go

// URTator - Urban Terror server browser and game launcher, written in
// Go.
//
// Copyright (c) 2016, Stanslav N. a.k.a pztrn (or p0z1tr0n)
// All rights reserved.
//
// Licensed under Terms and Conditions of GNU General Public License
// version 3 or any higher.
// ToDo: put full text of license here.
package database
import (
// stdlib
//"database/sql"
"fmt"
"path"
"strconv"
// local
"github.com/pztrn/urtrator/datamodels"
// Other
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
type Database struct {
// Pointer to initialized database connection.
Db *sqlx.DB
}
func (d *Database) Close() {
fmt.Println("Closing database...")
d.Db.Close()
}
func (d *Database) Initialize() {
fmt.Println("Initializing database...")
// Connect to database.
db_path := path.Join(cfg.TEMP["DATA"], "database.sqlite3")
fmt.Println("Database path: " + db_path)
db, err := sqlx.Connect("sqlite3", db_path)
if err != nil {
fmt.Println(err.Error())
}
d.Db = db
}
func (d *Database) Migrate() {
// Getting current database version.
dbver := 0
database := []datamodels.Database{}
d.Db.Select(&database, "SELECT * FROM database")
if len(database) > 0 {
fmt.Println("Current database version: " + database[0].Version)
dbver, _ = strconv.Atoi(database[0].Version)
} else {
fmt.Println("No database found, will create new one")
}
migrate_full(d, dbver)
}
func (d *Database) UpdateServers(data map[string]*datamodels.Server) {
fmt.Println("Updating servers information in database...")
raw_cached := []datamodels.Server{}
d.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]
}
new_servers := make(map[string]*datamodels.Server)
// Update our cached mapping.
for _, s := range data {
mapping_item_name := s.Ip + ":" + s.Port
_, ok := cached_servers[mapping_item_name]
if !ok {
fmt.Println(mapping_item_name + " not found!")
new_servers[mapping_item_name] = s
} else {
cached_servers[mapping_item_name].Ip = s.Ip
cached_servers[mapping_item_name].Port = s.Port
cached_servers[mapping_item_name].Name = s.Name
cached_servers[mapping_item_name].Players = s.Players
cached_servers[mapping_item_name].Maxplayers = s.Maxplayers
cached_servers[mapping_item_name].Ping = s.Ping
cached_servers[mapping_item_name].Map = s.Map
cached_servers[mapping_item_name].Gamemode = s.Gamemode
}
}
tx := d.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) VALUES (:ip, :port, :name, :ping, :players, :maxplayers, :gamemode, :map, :version)", 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, version=:version WHERE ip=:ip AND port=:port", &srv)
}
tx.Commit()
fmt.Println("Done")
}