2016-10-08 19:57:33 +05:00
|
|
|
// 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 cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
// stdlib
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
// local
|
|
|
|
"github.com/pztrn/urtrator/cachemodels"
|
|
|
|
"github.com/pztrn/urtrator/datamodels"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Cache) CreateProfile(name string) {
|
|
|
|
fmt.Println("Creating profile " + name)
|
|
|
|
_, ok := c.Profiles[name]
|
|
|
|
|
|
|
|
if !ok {
|
2016-12-13 04:58:31 +05:00
|
|
|
c.ProfilesMutex.Lock()
|
2016-10-08 19:57:33 +05:00
|
|
|
c.Profiles[name] = &cachemodels.Profile{}
|
|
|
|
c.Profiles[name].Profile = &datamodels.Profile{}
|
2016-12-13 04:58:31 +05:00
|
|
|
c.ProfilesMutex.Unlock()
|
2016-10-08 19:57:33 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) deleteProfile(data map[string]string) {
|
|
|
|
fmt.Println("Deleting profile " + data["profile_name"])
|
|
|
|
|
2017-05-10 12:04:01 +05:00
|
|
|
c.ProfilesMutex.Lock()
|
2016-10-08 19:57:33 +05:00
|
|
|
_, ok := c.Profiles[data["profile_name"]]
|
2017-05-10 12:04:01 +05:00
|
|
|
c.ProfilesMutex.Unlock()
|
2016-10-08 19:57:33 +05:00
|
|
|
if ok {
|
2016-12-13 04:58:31 +05:00
|
|
|
c.ProfilesMutex.Lock()
|
2016-10-08 19:57:33 +05:00
|
|
|
delete(c.Profiles, data["profile_name"])
|
2016-12-13 04:58:31 +05:00
|
|
|
c.ProfilesMutex.Unlock()
|
2016-10-08 19:57:33 +05:00
|
|
|
}
|
|
|
|
|
2017-05-10 12:04:01 +05:00
|
|
|
c.ProfilesMutex.Lock()
|
2016-10-08 19:57:33 +05:00
|
|
|
_, ok1 := c.Profiles[data["profile_name"]]
|
2017-05-10 12:04:01 +05:00
|
|
|
c.ProfilesMutex.Unlock()
|
2016-10-08 19:57:33 +05:00
|
|
|
if !ok1 {
|
|
|
|
Database.Db.MustExec(Database.Db.Rebind("DELETE FROM urt_profiles WHERE name=?"), data["profile_name"])
|
2017-05-13 01:08:56 +05:00
|
|
|
fmt.Println("Profile deleted")
|
2016-10-08 19:57:33 +05:00
|
|
|
} else {
|
|
|
|
fmt.Println("Something goes wrong! Profile is still here!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) FlushProfiles(data map[string]string) {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
cached_profiles := make(map[string]*datamodels.Profile)
|
|
|
|
for i := range raw_profiles {
|
2017-05-10 12:04:01 +05:00
|
|
|
cached_profiles[raw_profiles[i].Name] = c.Profiles[raw_profiles[i].Name].Profile
|
2016-10-08 19:57:33 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
new_profiles := make(map[string]*datamodels.Profile)
|
|
|
|
|
2017-05-10 12:04:01 +05:00
|
|
|
c.ProfilesMutex.Lock()
|
2016-10-08 19:57:33 +05:00
|
|
|
for _, profile := range c.Profiles {
|
|
|
|
_, ok := cached_profiles[profile.Profile.Name]
|
|
|
|
if !ok {
|
|
|
|
fmt.Println("Flushing new profile " + profile.Profile.Name)
|
2017-05-10 12:04:01 +05:00
|
|
|
new_profiles[profile.Profile.Name] = profile.Profile
|
2016-10-08 19:57:33 +05:00
|
|
|
}
|
|
|
|
}
|
2017-05-10 12:04:01 +05:00
|
|
|
c.ProfilesMutex.Unlock()
|
2016-10-08 19:57:33 +05:00
|
|
|
|
|
|
|
tx := Database.Db.MustBegin()
|
|
|
|
fmt.Println("Adding new profiles...")
|
|
|
|
for _, profile := range new_profiles {
|
2017-05-10 12:04:01 +05:00
|
|
|
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)
|
2016-10-08 19:57:33 +05:00
|
|
|
}
|
|
|
|
fmt.Println("Updating existing profiles...")
|
|
|
|
for _, profile := range cached_profiles {
|
2017-05-10 12:04:01 +05:00
|
|
|
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)
|
2016-10-08 19:57:33 +05:00
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
fmt.Println("Done")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) LoadProfiles(data map[string]string) {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2017-05-10 12:04:01 +05:00
|
|
|
c.ProfilesMutex.Lock()
|
2016-10-08 19:57:33 +05:00
|
|
|
for _, profile := range raw_profiles {
|
|
|
|
c.Profiles[profile.Name] = &cachemodels.Profile{}
|
2017-05-10 12:04:01 +05:00
|
|
|
c.Profiles[profile.Name].Profile = &profile
|
2016-10-08 19:57:33 +05:00
|
|
|
}
|
2017-05-10 12:04:01 +05:00
|
|
|
c.ProfilesMutex.Unlock()
|
2016-10-08 19:57:33 +05:00
|
|
|
|
|
|
|
fmt.Println("Load completed. Loaded " + strconv.Itoa(len(c.Profiles)) + " profiles.")
|
|
|
|
}
|