Add authentication server, dev CLI, Docker multi-service setup, and cross-platform improvements

This commit is contained in:
Ilya Groshev
2026-04-21 16:49:44 +03:00
parent 43d6527b42
commit a3fbb1aeba
121 changed files with 4523 additions and 2888 deletions
+3 -3
View File
@@ -16,13 +16,13 @@ const (
defaultChargeMoneyThisMonth = int64(0)
)
func SeedUserState(userId int64, uuid string, nowMillis int64) *UserState {
func SeedUserState(userId int64, uuid string, nowMillis int64, platform model.ClientPlatform) *UserState {
user := &UserState{
UserId: userId,
Uuid: uuid,
PlayerId: userId,
OsType: 2,
PlatformType: 2,
OsType: platform.OsType,
PlatformType: platform.PlatformType,
UserRestrictionType: 0,
RegisterDatetime: nowMillis,
GameStartDatetime: nowMillis,
+4 -2
View File
@@ -10,19 +10,21 @@ import (
func (s *SQLiteStore) LoadUser(userId int64) (store.UserState, error) {
var u store.UserState
var fbId sql.NullInt64
err := s.db.QueryRow(`SELECT user_id, uuid, player_id, os_type, platform_type, user_restriction_type,
register_datetime, game_start_datetime, latest_version, birth_year, birth_month,
backup_token, charge_money_this_month FROM users WHERE user_id = ?`, userId).Scan(
backup_token, charge_money_this_month, facebook_id FROM users WHERE user_id = ?`, userId).Scan(
&u.UserId, &u.Uuid, &u.PlayerId, &u.OsType, &u.PlatformType, &u.UserRestrictionType,
&u.RegisterDatetime, &u.GameStartDatetime, &u.LatestVersion, &u.BirthYear, &u.BirthMonth,
&u.BackupToken, &u.ChargeMoneyThisMonth)
&u.BackupToken, &u.ChargeMoneyThisMonth, &fbId)
if err == sql.ErrNoRows {
return u, store.ErrNotFound
}
if err != nil {
return u, fmt.Errorf("load users: %w", err)
}
u.FacebookId = fbId.Int64
initMaps(&u)
+51 -4
View File
@@ -4,10 +4,11 @@ import (
"database/sql"
"fmt"
"lunar-tear/server/internal/model"
"lunar-tear/server/internal/store"
)
func (s *SQLiteStore) CreateUser(uuid string) (int64, error) {
func (s *SQLiteStore) CreateUser(uuid string, platform model.ClientPlatform) (int64, error) {
tx, err := s.db.Begin()
if err != nil {
return 0, fmt.Errorf("begin tx: %w", err)
@@ -24,8 +25,8 @@ func (s *SQLiteStore) CreateUser(uuid string) (int64, error) {
res, err := tx.Exec(`INSERT INTO users (uuid, player_id, os_type, platform_type, user_restriction_type,
register_datetime, game_start_datetime, latest_version, birth_year, birth_month,
backup_token, charge_money_this_month) VALUES (?, 0, 2, 2, 0, ?, ?, 0, 2000, 1, 'mock-backup-token', 0)`,
uuid, nowMillis, nowMillis)
backup_token, charge_money_this_month) VALUES (?, 0, ?, ?, 0, ?, ?, 0, 2000, 1, 'mock-backup-token', 0)`,
uuid, platform.OsType, platform.PlatformType, nowMillis, nowMillis)
if err != nil {
return 0, fmt.Errorf("insert user: %w", err)
}
@@ -39,7 +40,7 @@ func (s *SQLiteStore) CreateUser(uuid string) (int64, error) {
return 0, fmt.Errorf("update player_id: %w", err)
}
user := store.SeedUserState(userId, uuid, nowMillis)
user := store.SeedUserState(userId, uuid, nowMillis, platform)
if err := writeUserState(tx, userId, user); err != nil {
return 0, fmt.Errorf("write seed state: %w", err)
}
@@ -188,6 +189,52 @@ func (s *SQLiteStore) ImportUser(u *store.UserState) error {
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit: %w", err)
}
return nil
}
func (s *SQLiteStore) SetFacebookId(userId int64, facebookId int64) error {
_, err := s.db.Exec(`UPDATE users SET facebook_id = ? WHERE user_id = ?`, facebookId, userId)
if err != nil {
return fmt.Errorf("set facebook_id: %w", err)
}
return nil
}
func (s *SQLiteStore) GetUserByFacebookId(facebookId int64) (int64, error) {
var userId int64
err := s.db.QueryRow(`SELECT user_id FROM users WHERE facebook_id = ?`, facebookId).Scan(&userId)
if err == sql.ErrNoRows {
return 0, store.ErrNotFound
}
if err != nil {
return 0, fmt.Errorf("query user by facebook_id: %w", err)
}
return userId, nil
}
func (s *SQLiteStore) GetFacebookId(userId int64) (int64, error) {
var fbId sql.NullInt64
err := s.db.QueryRow(`SELECT facebook_id FROM users WHERE user_id = ?`, userId).Scan(&fbId)
if err != nil {
return 0, store.ErrNotFound
}
return fbId.Int64, nil
}
func (s *SQLiteStore) ClearFacebookId(userId int64) error {
_, err := s.db.Exec(`UPDATE users SET facebook_id = NULL WHERE user_id = ?`, userId)
if err != nil {
return fmt.Errorf("clear facebook_id: %w", err)
}
return nil
}
func (s *SQLiteStore) UpdateUUID(userId int64, newUuid string) error {
_, err := s.db.Exec(`UPDATE users SET uuid = ? WHERE user_id = ?`, newUuid, userId)
if err != nil {
return fmt.Errorf("update uuid: %w", err)
}
return nil
}
+8 -1
View File
@@ -3,6 +3,8 @@ package store
import (
"errors"
"time"
"lunar-tear/server/internal/model"
)
var ErrNotFound = errors.New("store: not found")
@@ -10,11 +12,16 @@ var ErrNotFound = errors.New("store: not found")
type Clock func() time.Time
type UserRepository interface {
CreateUser(uuid string) (int64, error)
CreateUser(uuid string, platform model.ClientPlatform) (int64, error)
GetUserByUUID(uuid string) (int64, error)
LoadUser(userId int64) (UserState, error)
UpdateUser(userId int64, mutate func(*UserState)) (UserState, error)
DefaultUserId() (int64, error)
SetFacebookId(userId int64, facebookId int64) error
GetUserByFacebookId(facebookId int64) (int64, error)
GetFacebookId(userId int64) (int64, error)
ClearFacebookId(userId int64) error
UpdateUUID(userId int64, newUuid string) error
}
type SessionRepository interface {
+1
View File
@@ -31,6 +31,7 @@ type UserState struct {
BirthMonth int32
BackupToken string
ChargeMoneyThisMonth int64
FacebookId int64
Setting UserSettingState
Status UserStatusState