knowyt/server/src/user/user.go
2024-02-18 10:24:40 +01:00

212 lines
3.6 KiB
Go

package user
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
"time"
"sirlab.de/go/knowyt/fileutil"
)
func NewUserFromFile(fileName string) (*User, error) {
jsonBytes, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
// var usr User
var userJson UserJson
if err := json.Unmarshal(jsonBytes, &userJson); err != nil {
return nil, fmt.Errorf("%s: %v", fileName, err)
} else {
_, fileNameShort := path.Split(fileName)
id := strings.TrimSuffix(fileNameShort, ".json")
return &User{
id: id,
name: userJson.Name,
filename: fileName,
role: userJson.Role,
authcode: userJson.Authcode,
gameId: userJson.GameId,
lastLoggedIn: userJson.LastLoggedIn,
created: userJson.Created,
}, nil
}
}
func CreateUser(fileName, gameId string) *User {
_, fileNameShort := path.Split(fileName)
id := strings.TrimSuffix(fileNameShort, ".json")
return &User{
id: id,
filename: fileName,
gameId: gameId,
lastLoggedIn: 0,
created: time.Now().Unix(),
}
}
func (usr *User) DeepCloneUserObj() *User {
usr.mu.Lock()
defer usr.mu.Unlock()
usrNew := User{
id: usr.id,
filename: usr.filename,
authcode: usr.authcode,
name: usr.name,
role: usr.role,
gameId: usr.gameId,
created: usr.created,
lastLoggedIn: usr.lastLoggedIn,
cameo: usr.cameo,
}
return &usrNew
}
func (usr *User) SaveUser() error {
usr.mu.Lock()
defer usr.mu.Unlock()
userJson := UserJson{
Name: usr.name,
Authcode: usr.authcode,
Role: usr.role,
GameId: usr.gameId,
Created: usr.created,
LastLoggedIn: usr.lastLoggedIn,
}
if jsonBytes, err := json.Marshal(userJson); err != nil {
return err
} else {
if err := fileutil.SaveFile(usr.filename, jsonBytes); err != nil {
return err
}
}
return nil
}
func (usr *User) RemovePlayer() error {
return os.Remove(usr.filename)
}
func (usr *User) GetId() string {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.id
}
func (usr *User) GetAuthCode() string {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.authcode
}
func (usr *User) SetAuthcode(authcode string) {
usr.mu.Lock()
defer usr.mu.Unlock()
usr.authcode = authcode
}
func (usr *User) GetName() string {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.name
}
func (usr *User) SetName(name string) {
usr.mu.Lock()
defer usr.mu.Unlock()
usr.name = name
}
func (usr *User) GetRole() string {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.role
}
func (usr *User) SetRole(role string) {
usr.mu.Lock()
defer usr.mu.Unlock()
usr.role = role
}
func (usr *User) GetGameId() string {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.gameId
}
func (usr *User) IsPlayer() bool {
return true
}
func (usr *User) IsGamemaster() bool {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.role == ROLE_GAMEMASTER || usr.role == ROLE_ADMIN
}
func (usr *User) IsAdmin() bool {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.role == ROLE_ADMIN
}
func (usr *User) IsAdminOrCameo() bool {
usr.mu.Lock()
defer usr.mu.Unlock()
if usr.role == ROLE_ADMIN {
return true
}
if usr.cameo != nil {
if usr.cameo.role == ROLE_ADMIN {
return true
}
}
return false
}
func (usr *User) SetCameo(usrCameo *User) {
usr.mu.Lock()
defer usr.mu.Unlock()
usr.cameo = usrCameo
}
func (usr *User) GetCameo() *User {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.cameo
}
func (usr *User) GetCreated() int64 {
return usr.created
}
func (usr *User) GetLastLoggedIn() int64 {
return usr.lastLoggedIn
}
func (usr *User) UpdateHeartbeat() {
usr.lastLoggedIn = time.Now().Unix()
}