|
|
@@ -2,16 +2,19 @@ package mod
|
|
|
|
|
|
import (
|
|
|
pb "git.wanbits.io/joe/franklin/protos"
|
|
|
+ "git.wanbits.io/joe/kettle/rds"
|
|
|
"git.wanbits.io/joe/kettle/utl"
|
|
|
"github.com/go-redis/redis"
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
"reflect"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- rds *redis.Client
|
|
|
+ r *redis.Client
|
|
|
+ lsm *rds.LuaScriptManager
|
|
|
)
|
|
|
|
|
|
// convert ANY struct( or its pointer) to map[string]interface{}
|
|
|
@@ -47,16 +50,17 @@ func StructToMap(stru interface{}, lowerCase ...bool) (map[string]interface{}, e
|
|
|
}
|
|
|
|
|
|
func Install(client *redis.Client) {
|
|
|
- rds = client
|
|
|
+ r = client
|
|
|
+ lsm = rds.NewLuaScriptManager(r)
|
|
|
}
|
|
|
|
|
|
func ExistUser(userId uint64) bool {
|
|
|
- ival, err := rds.Exists(kh_user(userId)).Result()
|
|
|
+ ival, err := r.Exists(kh_user(userId)).Result()
|
|
|
return err == nil && ival != 0
|
|
|
}
|
|
|
|
|
|
func LoadUser(userId uint64) (*pb.User, error) {
|
|
|
- mp, err := rds.HGetAll(kh_user(userId)).Result()
|
|
|
+ mp, err := r.HGetAll(kh_user(userId)).Result()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
@@ -70,21 +74,57 @@ func LoadUser(userId uint64) (*pb.User, error) {
|
|
|
}
|
|
|
|
|
|
func UpdateUserAttr(userId uint64, field string, val interface{}) error {
|
|
|
- _, err := rds.HSet(kh_user(userId), field, val).Result()
|
|
|
+ _, err := r.HSet(kh_user(userId), field, val).Result()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func UpdateUserAttrs(userId uint64, fields map[string]interface{}) error {
|
|
|
- _, err := rds.HMSet(kh_user(userId), fields).Result()
|
|
|
+ _, err := r.HMSet(kh_user(userId), fields).Result()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func SaveKick(userId uint64, secs int) error {
|
|
|
- _, err := rds.Set(ks_kick(userId), 1, time.Duration(secs)*time.Second).Result()
|
|
|
+ _, err := r.Set(ks_kick(userId), 1, time.Duration(secs)*time.Second).Result()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func LoadKick(userId uint64) bool {
|
|
|
- _, err := rds.Get(ks_kick(userId)).Result()
|
|
|
+ _, err := r.Get(ks_kick(userId)).Result()
|
|
|
return err == nil
|
|
|
}
|
|
|
+
|
|
|
+func SaveOffline(userId, logicId uint64) error {
|
|
|
+ _, err := r.HSet(kh_offlines, strconv.FormatUint(userId, 10), logicId).Result()
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func LoadOffline(userId uint64) (uint64, error) {
|
|
|
+ sid, err := r.HGet(kh_offlines, strconv.FormatUint(userId, 10)).Result()
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ return strconv.ParseUint(sid, 10, 64)
|
|
|
+}
|
|
|
+
|
|
|
+func SaveUserLogin(userId, agentId uint64) error {
|
|
|
+ _, err := lsm.Exec("user_login", []string{}, userId, agentId).Result()
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func SaveUserLogout(userId uint64) error {
|
|
|
+ _, err := lsm.Exec("user_logout", []string{}, userId).Result()
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func SaveUserChangeLogic(userId, logicId uint64) error {
|
|
|
+ _, err := lsm.Exec("user_change_logic", []string{}, userId, logicId).Result()
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func LoadUserLogic(userId uint64) (uint64, error) {
|
|
|
+ sid, err := r.HGet(kh_user_logic, strconv.FormatUint(userId, 10)).Result()
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ return strconv.ParseUint(sid, 10, 64)
|
|
|
+}
|