finish world initialization, set up init on startup and on shutdown, move all models to main directory
This commit is contained in:
@@ -4,293 +4,125 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
ID string
|
||||
|
||||
Name string
|
||||
Role int
|
||||
Avatar string
|
||||
Character string
|
||||
Color string
|
||||
Pronouns string
|
||||
CreatedAt time.Time
|
||||
Hotbar map[string]string
|
||||
Stats UserStats
|
||||
Role int
|
||||
Stats Stats
|
||||
Hotbar []UserHotbar
|
||||
}
|
||||
|
||||
type UserStats struct {
|
||||
Id int64
|
||||
CoreVersion string
|
||||
SystemId string
|
||||
SystemVersion string
|
||||
CreatedTime int64
|
||||
ModifiedTime int64
|
||||
LastModifiedBy string
|
||||
func (u *User) Query(data *InsertId[uint]) {
|
||||
data.query = `
|
||||
INSERT INTO user (game_id, id, name, avatar, character, color, pronouns, role)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
game_id = EXCLUDED.game_id,
|
||||
updated_at = datetime('now')
|
||||
RETURNING (created_at == updated_at) AS is_inserted`
|
||||
}
|
||||
|
||||
type Users []User
|
||||
func (u *User) InsertObjects(tx *sqlx.Tx) error {
|
||||
group, ctx := errgroup.WithContext(context.Background())
|
||||
|
||||
func (users Users) GetById(id int) *User {
|
||||
return &users[id]
|
||||
relId := InsertId[string]{id: u.ID, fieldName: "user_id"}
|
||||
InsertWithCtxParallel(group, ctx, tx, u.Stats, relId)
|
||||
InsertSliceParallel(group, tx, u.Hotbar, relId)
|
||||
|
||||
err := group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertUser(user *User, stateId int64) error {
|
||||
query := `
|
||||
INSERT INTO users (state_id, name, role, character, color, pronouns)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, created_at`
|
||||
func (u *User) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{stateId, user.Name, user.Role, user.Character, user.Color, user.Pronouns}
|
||||
args := []any{data.id, u.ID, u.Name, u.Avatar, u.Character, u.Color, u.Pronouns, u.Role}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&user.Id, &user.CreatedAt)
|
||||
var isInserted bool
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&isInserted)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for k, v := range user.Hotbar {
|
||||
err = m.InsertUserHotbar(k, v, user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isInserted {
|
||||
return u.InsertObjects(tx)
|
||||
}
|
||||
|
||||
return m.InsertUserStats(&user.Stats, user.Id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertUserHotbar(key string, value string, userId int64) error {
|
||||
query := `
|
||||
INSERT INTO users_hotbar (user_id, key, value)
|
||||
func (u *User) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, u.ID, u.Name, u.Avatar, u.Character, u.Color, u.Pronouns, u.Role}
|
||||
|
||||
var isInserted bool
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&isInserted)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if isInserted {
|
||||
return u.InsertObjects(tx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type UserHotbar struct {
|
||||
ID uint
|
||||
|
||||
Key int
|
||||
Value string
|
||||
}
|
||||
|
||||
func (u UserHotbar) Query(data *InsertId[string]) {
|
||||
data.query = `
|
||||
INSERT INTO hotbar (user_id, key_, value)
|
||||
VALUES ($1, $2, $3)`
|
||||
|
||||
args := []any{userId, key, value}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := m.DB.ExecContext(ctx, query, args...)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertUserStats(stats *UserStats, userId int64) error {
|
||||
query := `
|
||||
INSERT INTO users_stats (user_id, core_version, system_id, system_version, created_time, modified_time, last_modified_by)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id`
|
||||
|
||||
args := []any{userId, stats.CoreVersion, stats.SystemId, stats.SystemVersion, stats.CreatedTime, stats.ModifiedTime, stats.LastModifiedBy}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&stats.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetUsers(idState int64) (Users, error) {
|
||||
if idState < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
func (u UserHotbar) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, created_at, name, role, character, color, pronouns
|
||||
FROM users
|
||||
WHERE state_id = $1`
|
||||
args := []any{data.id, u.Key, u.Value}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query, idState)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
users := make(Users, 0, 1)
|
||||
|
||||
for rows.Next() {
|
||||
var user User
|
||||
err := rows.Scan(
|
||||
&user.Id,
|
||||
&user.CreatedAt,
|
||||
&user.Name,
|
||||
&user.Role,
|
||||
&user.Character,
|
||||
&user.Color,
|
||||
&user.Pronouns,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.Hotbar, err = m.GetUserHotbar(user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userState, err := m.GetUserStats(user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user.Stats = *userState
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetUserHotbar(idUser int64) (map[string]string, error) {
|
||||
if idUser < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT key, value
|
||||
FROM users_hotbar
|
||||
WHERE user_id = $1`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query, idUser)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
hotbar := make(map[string]string)
|
||||
for rows.Next() {
|
||||
var key string
|
||||
var val string
|
||||
|
||||
err := rows.Scan(
|
||||
&key,
|
||||
&val,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hotbar[key] = val
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return hotbar, nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetUserStats(idSystem int64) (*UserStats, error) {
|
||||
if idSystem < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, core_version, system_id, system_version, created_time, modified_time, last_modified_by
|
||||
FROM users_stats
|
||||
WHERE user_id = $1`
|
||||
|
||||
var userStats UserStats
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, idSystem).Scan(
|
||||
&userStats.Id,
|
||||
&userStats.CoreVersion,
|
||||
&userStats.SystemId,
|
||||
&userStats.SystemVersion,
|
||||
&userStats.CreatedTime,
|
||||
&userStats.ModifiedTime,
|
||||
&userStats.LastModifiedBy,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &userStats, nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) DeleteUsers(idState int64) error {
|
||||
if idState < 1 {
|
||||
return ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
DELETE FROM users
|
||||
WHERE state_id = $1`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err := m.DB.ExecContext(ctx, query, idState)
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&u.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return ErrorRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) DeleteUser(idUser int64) error {
|
||||
if idUser < 1 {
|
||||
return ErrorRecordNotFound
|
||||
func (u UserHotbar) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
query := `
|
||||
DELETE FROM users
|
||||
WHERE id = $1`
|
||||
args := []any{data.id, u.Key, u.Value}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err := m.DB.ExecContext(ctx, query, idUser)
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&u.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return ErrorRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user