You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.9 KiB
Go

package store
import (
"context"
"encoding/json"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/store"
"log"
)
type StoreModel struct {
Ctx context.Context
Store *cache.Cache[string]
}
type PlayerInfo struct {
IsOnline bool `json:"online"`
BoundUser string `json:"bound_user"`
PairCode string `json:"pair_code"`
Name string `json:"name"`
}
type UserInfo struct {
DisplayName string `json:"display_name"`
BoundPlayer []string `json:"bound_player"`
}
func NewStoreModel() *StoreModel {
storeModel := &StoreModel{
Ctx: context.Background(),
Store: NewManager[string](),
}
return storeModel
}
func (m *StoreModel) GetPlayerInfo(token string) *PlayerInfo {
dataStr, err := m.Store.Get(m.Ctx, MakeKey("player", token))
if err != nil && err.Error() != store.NOT_FOUND_ERR && err.Error() != "Entry not found" {
log.Panicln("Cannot get player info for token ["+token+"]: ", err)
}
if dataStr == "" {
return nil
}
data := &PlayerInfo{}
err = json.Unmarshal([]byte(dataStr), data)
if err != nil {
return nil
}
return data
}
func (m *StoreModel) SetPlayerInfo(token string, data *PlayerInfo) {
dataStr, err := json.Marshal(data)
if err != nil {
log.Panicln("Cannot convert player info to json: ", err)
}
err = m.Store.Set(m.Ctx, MakeKey("player", token), string(dataStr), store.WithExpiration(PersistTTL))
if err != nil {
log.Panicln("Cannot set player info for token ["+token+"]: ", err)
}
}
func (m *StoreModel) GetUserInfo(userName string) *UserInfo {
dataStr, err := m.Store.Get(m.Ctx, MakeKey("user", userName))
if err != nil && err.Error() != store.NOT_FOUND_ERR && err.Error() != "Entry not found" {
log.Panicln("Cannot get user info from userName ["+userName+"]: ", err)
}
if dataStr == "" {
return nil
}
data := &UserInfo{}
err = json.Unmarshal([]byte(dataStr), data)
if err != nil {
return nil
}
return data
}
func (m *StoreModel) SetUserInfo(userName string, data *UserInfo) {
dataStr, err := json.Marshal(data)
if err != nil {
log.Panicln("Cannot convert info to json: ", err)
}
err = m.Store.Set(m.Ctx, MakeKey("user", userName), string(dataStr), store.WithExpiration(PersistTTL))
if err != nil {
log.Panicln("Cannot set player info for userName ["+userName+"]: ", err)
}
}
func (m *StoreModel) RemovePlayerInfo(token string) {
m.Store.Delete(m.Ctx, "player:"+token)
}
func (m *StoreModel) GetCodeInfo(code string) string {
token, err := m.Store.Get(m.Ctx, MakeKey("code", code))
if err != nil && err.Error() != store.NOT_FOUND_ERR && err.Error() != "Entry not found" {
log.Panicln("Cannot get code info from code ["+code+"]: ", err)
}
return token
}
func (m *StoreModel) SetCodeInfo(code string, token string) {
err := m.Store.Set(m.Ctx, MakeKey("code", code), token, store.WithExpiration(CacheTTL))
if err != nil {
log.Panicln("Cannot set code info: ", err)
}
}
func (m *StoreModel) RemoveCodeInfo(code string) {
m.Store.Delete(m.Ctx, MakeKey("code", code))
}