package main

import (
	"../log"
	"../mod"
	"../nt"
	"../utils"
	"go.uber.org/zap"
	"time"
)

///  被踢的用户在一定的时间内不能重复登录，不管什么原因被踢

var (
	defKickoffPolicy = &KickoffPolicy{}
)

type KickoffPolicy struct{}

// 检测用户是否被踢
func (self *KickoffPolicy) UserKickoffBlocked(guid uint64) bool {
	off_time, err := mod.GetKickoff(guid)
	if nil != err {
		return false
	}

	var conf_lasts int64 = 3 // default 5 min
	now := time.Now().Unix()
	kickoff_lasts, ok := nt.DynConf.Kv["kickoff_lasts"]
	if ok {
		conf_lasts = utils.If_to_int64(kickoff_lasts, 0)
	}
	conf_lasts *= 60

	// if in the window
	if now-off_time > conf_lasts {
		_ = mod.RemKickoff(guid)
		return false
	}

	log.Debug("kickoff forbidden", zap.Uint64("guid", guid), zap.Int64("lasts", now-off_time),
		zap.Int64("conf", conf_lasts))

	return true
}

func (self *KickoffPolicy) OnUserKickedoff(guid uint64) {
	_ = mod.UpdateKickoff(guid)
}
