package internal

/*
 玩家数据可能保存在多个表中，主表user_playerinfo，如果新加字段比较多可以建立新表
 然后使用 PlayerLoader 加载玩家数据，实现这个接口
 使用 RegisterPlayerLoad 注册到框架中
*/

type PlayerLoadReplay struct {
	Platform string // 平台id
	Snid     int32  // 玩家id
	Err      error  // 查询错误信息
	Data     interface{}
}

// IPlayerLoad 玩家登录时首次加载数据
// 重连不会执行
type IPlayerLoad interface {
	// Load 查询数据库，在task中执行
	Load(platform string, snid int32, player any) *PlayerLoadReplay
	// Callback 数据查询成功的回掉方法
	Callback(player any, ret *PlayerLoadReplay)
}

// IPlayerLogined 玩家登录之后加载其他玩家数据
// 重连不会执行
// 在 IPlayerLoad 之后执行
type IPlayerLogined interface {
	// LoadAfter 查询数据库，在task中执行
	LoadAfter(platform string, snid int32) *PlayerLoadReplay
	// CallbackAfter 数据查询成功的回掉方法
	CallbackAfter(ret *PlayerLoadReplay)
}

type IPlayerRelease interface {
	// Release 内存释放
	Release(platform string, snid int32)
}

type IPlayerSave interface {
	// Save 保存到数据库
	// isSync 是否同步保存
	// force 是否强制保存
	Save(platform string, snid int32, isSync, force bool)
}

type PlayerLoader interface {
	IPlayerLoad
	IPlayerLogined
	IPlayerSave
	IPlayerRelease
}

type BasePlayerLoader struct {
}

func (b *BasePlayerLoader) Load(platform string, snid int32, player any) *PlayerLoadReplay {
	return nil
}

func (b *BasePlayerLoader) Callback(player any, ret *PlayerLoadReplay) {
}

func (b *BasePlayerLoader) LoadAfter(platform string, snid int32) *PlayerLoadReplay {
	return nil
}

func (b *BasePlayerLoader) CallbackAfter(ret *PlayerLoadReplay) {
}

func (b *BasePlayerLoader) Save(platform string, snid int32, isSync, force bool) {
}

func (b *BasePlayerLoader) Release(platform string, snid int32) {
}

var playerLoads []PlayerLoader

func RegisterPlayerLoad(i PlayerLoader) {
	playerLoads = append(playerLoads, i)
}

func GetPlayerLoads() []PlayerLoader {
	return playerLoads
}
