package mod

import (
	"../nt"
)

type RedisLua struct {
	Lua string
	Sha string
}

var (
	// 玩家切换游戏，切换玩家所在的逻辑服务器
	Scripts map[string]*RedisLua
)

func LoadScripts() {
	Scripts = make(map[string]*RedisLua)

	// 0 arg1 arg2
	// 0 guid, agentId
	Scripts["user_agent_update"] = &RedisLua{
		Lua: `local key2=string.format('map:agent_user:%d',ARGV[2]); return {redis.call('hset','map:user_agent',ARGV[1], ARGV[2]),redis.call('hset',key2, ARGV[1],1)}
		`,
	}
	Scripts["user_agent_remove"] = &RedisLua{
		Lua: `local key2=string.format('map:agent_user:%d',ARGV[2]);return{redis.call('hdel','map:user_agent',ARGV[1]),redis.call('hdel',key2,ARGV[1])}
		`,
	}
	// 0 guid, gameId
	Scripts["user_logic_update"] = &RedisLua{
		Lua: `local key2=string.format('map:logic_user:%d',ARGV[2]); return {redis.call('hset','map:user_logic',ARGV[1], ARGV[2]),redis.call('hset',key2, ARGV[1],1)}
		`,
	}
	Scripts["user_logic_remove"] = &RedisLua{
		Lua: `local key2=string.format('map:logic_user:%d',ARGV[2]);return{redis.call('hdel','map:user_logic',ARGV[1]),redis.call('hdel',key2,ARGV[1])}
		`,
	}

	for _, item := range Scripts {
		sha, err := nt.Rdb.ScriptLoad(item.Lua).Result()
		if nil != err {
			panic(err)
		}
		item.Sha = sha
	}
}
