2016-07-06 03:55:05 +05:30
|
|
|
|
package data
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
2016-09-22 22:02:00 +05:30
|
|
|
|
"fmt"
|
2016-07-06 03:55:05 +05:30
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
|
|
"elyby/minecraft-skinsystem/lib/services"
|
|
|
|
|
"elyby/minecraft-skinsystem/lib/tools"
|
2016-08-27 02:15:55 +05:30
|
|
|
|
|
|
|
|
|
"github.com/mediocregopher/radix.v2/redis"
|
2016-07-06 03:55:05 +05:30
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SkinItem struct {
|
2016-09-15 03:49:16 +05:30
|
|
|
|
UserId int `json:"userId"`
|
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
SkinId int `json:"skinId"`
|
|
|
|
|
Url string `json:"url"`
|
|
|
|
|
Is1_8 bool `json:"is1_8"`
|
|
|
|
|
IsSlim bool `json:"isSlim"`
|
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
|
oldUsername string
|
2016-07-06 03:55:05 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 03:49:16 +05:30
|
|
|
|
const accountIdToUsernameKey string = "hash:username-to-account-id"
|
|
|
|
|
|
2016-07-06 03:55:05 +05:30
|
|
|
|
func (s *SkinItem) Save() {
|
|
|
|
|
str, _ := json.Marshal(s)
|
2016-09-15 03:49:16 +05:30
|
|
|
|
pool, _ := services.RedisPool.Get()
|
|
|
|
|
pool.Cmd("MULTI")
|
|
|
|
|
|
|
|
|
|
// Если пользователь сменил ник, то мы должны удать его ключ
|
|
|
|
|
if (s.oldUsername != "" && s.oldUsername != s.Username) {
|
|
|
|
|
pool.Cmd("DEL", tools.BuildKey(s.oldUsername))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Если это новая запись или если пользователь сменил ник, то обновляем значение в хэш-таблице
|
|
|
|
|
if (s.oldUsername != "" || s.oldUsername != s.Username) {
|
|
|
|
|
pool.Cmd("HSET", accountIdToUsernameKey, s.UserId, s.Username)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool.Cmd("SET", tools.BuildKey(s.Username), str)
|
|
|
|
|
|
|
|
|
|
pool.Cmd("EXEC")
|
|
|
|
|
|
|
|
|
|
s.oldUsername = s.Username
|
2016-07-06 03:55:05 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 03:49:16 +05:30
|
|
|
|
func (s *SkinItem) Delete() {
|
|
|
|
|
if (s.oldUsername == "") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool, _ := services.RedisPool.Get()
|
|
|
|
|
pool.Cmd("MULTI")
|
|
|
|
|
|
|
|
|
|
pool.Cmd("DEL", tools.BuildKey(s.oldUsername))
|
|
|
|
|
pool.Cmd("HDEL", accountIdToUsernameKey, s.UserId)
|
|
|
|
|
|
|
|
|
|
pool.Cmd("EXEC")
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-22 22:02:00 +05:30
|
|
|
|
func FindSkinByUsername(username string) (SkinItem, error) {
|
2016-07-06 03:55:05 +05:30
|
|
|
|
var record SkinItem;
|
2016-12-03 04:27:55 +05:30
|
|
|
|
services.Logger.IncCounter("skins.storage.query", 1)
|
2016-08-27 02:15:55 +05:30
|
|
|
|
response := services.RedisPool.Cmd("GET", tools.BuildKey(username));
|
|
|
|
|
if (response.IsType(redis.Nil)) {
|
2016-12-03 04:27:55 +05:30
|
|
|
|
services.Logger.IncCounter("skins.storage.not_found", 1)
|
2016-09-22 22:02:00 +05:30
|
|
|
|
return record, SkinNotFound{username}
|
2016-08-27 02:15:55 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := response.Str()
|
2016-07-06 03:55:05 +05:30
|
|
|
|
if (err == nil) {
|
2016-12-03 04:27:55 +05:30
|
|
|
|
services.Logger.IncCounter("skins.storage.found", 1)
|
2016-07-06 03:55:05 +05:30
|
|
|
|
decodeErr := json.Unmarshal([]byte(result), &record)
|
|
|
|
|
if (decodeErr != nil) {
|
|
|
|
|
log.Println("Cannot decode record data")
|
|
|
|
|
}
|
2016-09-15 03:49:16 +05:30
|
|
|
|
|
|
|
|
|
record.oldUsername = record.Username
|
2016-07-06 03:55:05 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return record, err
|
|
|
|
|
}
|
2016-09-15 03:49:16 +05:30
|
|
|
|
|
2016-09-22 22:02:00 +05:30
|
|
|
|
func FindSkinById(id int) (SkinItem, error) {
|
2016-09-15 03:49:16 +05:30
|
|
|
|
response := services.RedisPool.Cmd("HGET", accountIdToUsernameKey, id);
|
|
|
|
|
if (response.IsType(redis.Nil)) {
|
2016-09-22 22:02:00 +05:30
|
|
|
|
return SkinItem{}, SkinNotFound{"unknown"}
|
2016-09-15 03:49:16 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
username, _ := response.Str()
|
|
|
|
|
|
2016-09-22 22:02:00 +05:30
|
|
|
|
return FindSkinByUsername(username)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SkinNotFound struct {
|
|
|
|
|
Who string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e SkinNotFound) Error() string {
|
|
|
|
|
return fmt.Sprintf("Skin data not found. Required username \"%v\"", e.Who)
|
2016-09-15 03:49:16 +05:30
|
|
|
|
}
|