You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.5 KiB
133 lines
3.5 KiB
package redis |
|
|
|
import ( |
|
"context" |
|
"errors" |
|
"server/common" |
|
"server/util" |
|
|
|
"github.com/liangdas/mqant/log" |
|
) |
|
|
|
// 更新玩家字段 |
|
func (u *RedisClient) SetToken(uid int, token string) (err error) { |
|
key := common.GetRedisKeyToken(token) |
|
err = u.client.Set(context.Background(), key, uid, common.RedisExpireToken).Err() |
|
return |
|
} |
|
|
|
// 获取用户信息 |
|
func (r *RedisClient) GetUserData(uid int) (player *common.PlayerDBInfo) { |
|
key := common.GetRedisKeyUser(uid) |
|
|
|
res := r.client.HGetAll(context.Background(), key) |
|
if res.Err() != nil { |
|
log.Error("redis hgetall error %v", res.Err()) |
|
return |
|
} |
|
|
|
player = &common.PlayerDBInfo{} |
|
|
|
if err := res.Scan(player); err != nil { |
|
log.Error("RedisClient GetUserData redis hgetall scan error %v", err) |
|
return nil |
|
} |
|
|
|
if player.Id == 0 { |
|
return nil |
|
} |
|
|
|
return |
|
} |
|
|
|
// 获取玩家session |
|
func (u *RedisClient) GetUserSession(uid int) (s *common.PlayerSession) { |
|
s = &common.PlayerSession{} |
|
if err := u.GetUserXInfo(uid, s, "sessionID", "gateID"); err != nil || s.SessionID == "" { |
|
return nil |
|
} |
|
return |
|
} |
|
|
|
// 获取玩家session |
|
func (u *RedisClient) GetUserToken(uid int) string { |
|
user := &common.PlayerDBInfo{} |
|
if err := u.GetUserXInfo(uid, user, "token"); err != nil { |
|
return "" |
|
} |
|
return user.Token |
|
} |
|
|
|
// 获取用户自定义信息 |
|
func (u *RedisClient) GetUserXInfo(uid int, ret interface{}, fields ...string) error { |
|
key := common.GetRedisKeyUser(uid) |
|
res := u.client.HMGet(context.Background(), key, fields...) |
|
if res.Err() != nil { |
|
log.Error("RedisClient GetUserBriefInfo redis HMGet error %v", res.Err()) |
|
return res.Err() |
|
} |
|
if res.Val() == nil || res.Val()[0] == nil { |
|
return errors.New("not found") |
|
} |
|
|
|
if err := res.Scan(ret); err != nil { |
|
log.Error("RedisClient GetUserBriefInfo redis HMGet scan error %v", res.Err()) |
|
return err |
|
} |
|
|
|
return nil |
|
} |
|
|
|
// 设置机器人 |
|
func (u *RedisClient) SetRobotData(data common.PlayerDBInfo) error { |
|
m := util.StructToMap(data, "json") |
|
key := common.GetRedisKeyUser(data.Id) |
|
if err := u.client.HSet(context.Background(), key, m).Err(); err != nil { |
|
log.Error("err:%v", err) |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
// 登陆成功后更新用户信息 |
|
func (u *RedisClient) UpdateUserData(data common.PlayerDBInfo) error { |
|
m := util.StructToMap(data, "json") |
|
// sessionID 和 gateID 不需要在这里更新 |
|
delete(m, "gateID") |
|
delete(m, "sessionID") |
|
key := common.GetRedisKeyUser(data.Id) |
|
log.Debug("m:%v", m) |
|
if err := u.client.HSet(context.Background(), key, m).Err(); err != nil { |
|
log.Error("err:%v", err) |
|
return err |
|
} |
|
u.client.Expire(context.Background(), key, common.RedisExpireToken) |
|
//log.Debug("cache update user:%d cache, v:%v, err:%v", data.Id, v, e) |
|
return nil |
|
} |
|
|
|
// 更新玩家字段 |
|
func (u *RedisClient) UpdateUserFields(uid int, values map[string]interface{}) (err error) { |
|
key := common.GetRedisKeyUser(uid) |
|
if exists, _ := u.client.Exists(context.Background(), key).Result(); exists == 0 { |
|
return |
|
} |
|
err = u.client.HSet(context.Background(), key, values).Err() |
|
if err != nil { |
|
log.Error("err:%v", err) |
|
} |
|
return |
|
} |
|
|
|
// AddUserExpire 增加玩家token过期时间 |
|
func (u *RedisClient) AddUserExpire(uid int, token string) error { |
|
if err := u.client.Expire(context.Background(), common.GetRedisKeyUser(uid), common.RedisExpireToken).Err(); err != nil { |
|
log.Error("err:%v", err) |
|
return err |
|
} |
|
if err := u.client.Expire(context.Background(), common.GetRedisKeyToken(token), common.RedisExpireToken).Err(); err != nil { |
|
log.Error("err:%v", err) |
|
return err |
|
} |
|
return nil |
|
}
|
|
|