From 29a163bec708a503440123dd6cbdb0b580aed793 Mon Sep 17 00:00:00 2001 From: zqj <582132116@qq.com> Date: Fri, 25 Mar 2022 17:08:12 +0800 Subject: [PATCH] fix: CreateReq uid->token; random name --- .gitignore | 1 + cmd/gameserver/action/RoleAction.go | 24 +++++++++++++++++++++++- cmd/gameserver/action/RoleAction_test.go | 22 ++++++++++++++++++++++ cmd/gameserver/agent.go | 7 +++---- cmd/gameserver/game.go | 12 ++++++++++++ cmd/httpserver/AccountAction.go | 5 ++--- common/conf.go | 2 +- common/const.go | 5 +++++ common/db/redisproxy/redis.go | 24 +++++++++++++++++++----- common/name.go | 30 ++++++++++++++++++++++++++++++ common/utils.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ common/utils_test.go | 21 +++++++++++++++++++++ conf/conf.yaml | 7 +++++++ models/role.go | 3 +-- pb/game.pb.go | 32 ++++++++++++++++---------------- protos | 2 +- utils/utils.go | 17 ----------------- 17 files changed, 213 insertions(+), 50 deletions(-) create mode 100644 cmd/gameserver/action/RoleAction_test.go create mode 100644 common/name.go create mode 100644 common/utils.go create mode 100644 common/utils_test.go delete mode 100644 utils/utils.go diff --git a/.gitignore b/.gitignore index 24ded17..ff20d49 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ protos csvdata *.csv *.out +imgui *.log \ No newline at end of file diff --git a/cmd/gameserver/action/RoleAction.go b/cmd/gameserver/action/RoleAction.go index fd0f3c6..681d36c 100644 --- a/cmd/gameserver/action/RoleAction.go +++ b/cmd/gameserver/action/RoleAction.go @@ -1,9 +1,11 @@ package action import ( + "fmt" "github.com/golang/protobuf/proto" "pro2d/common" "pro2d/common/components" + "pro2d/common/db/redisproxy" "pro2d/common/logger" "pro2d/models" "pro2d/pb" @@ -14,19 +16,39 @@ func HeartRpc(msg components.IMessage) (int32, interface{}) { return 0, nil } +func getRandomName() string { + name := "" + for { + name = common.RandomName(common.DefaultName) + if name == "" { + continue + } + + relay, err := redisproxy.SETNX(fmt.Sprintf(common.NickNames, name), "1") + if err != nil { + continue + } + if relay.(int64) == 1 { + break + } + } + return name +} + func CreateRpc(msg components.IMessage) (int32, interface{}) { req := pb.CreateReq{} if err := proto.Unmarshal(msg.GetData(), &req); err != nil { logger.Error("CreateRpc err: %v", err) return 1, nil } - role := models.RoleExistByUid(req.Uid) + role := models.RoleExistByUid(req.Token) if role != nil { return 2, nil } roleId := common.SnowFlack.NextValStr() role = models.NewRole(roleId) + role.Role.Nick = getRandomName() if err := role.Create(); err != nil { logger.Error("CreateRpc role create err: %v", err) return 3, nil diff --git a/cmd/gameserver/action/RoleAction_test.go b/cmd/gameserver/action/RoleAction_test.go new file mode 100644 index 0000000..8716bb8 --- /dev/null +++ b/cmd/gameserver/action/RoleAction_test.go @@ -0,0 +1,22 @@ +package action + +import ( + "fmt" + "math/rand" + "pro2d/common" + "pro2d/common/db/redisproxy" + "pro2d/common/logger" + "testing" +) + +func TestGetActionMap(t *testing.T) { + rand.Seed(common.Timex()) + + var redisConf = common.GlobalConf.GameConf.RedisConf + if err := redisproxy.ConnectRedis(redisConf.DB, redisConf.Auth, redisConf.Address); err != nil { + logger.Error(err) + return + } + name := getRandomName() + fmt.Println(name) +} diff --git a/cmd/gameserver/agent.go b/cmd/gameserver/agent.go index 2058878..67c32b0 100644 --- a/cmd/gameserver/agent.go +++ b/cmd/gameserver/agent.go @@ -9,7 +9,6 @@ import ( "pro2d/common/logger" "pro2d/models" "pro2d/pb" - "pro2d/utils" "sync" "sync/atomic" ) @@ -31,7 +30,7 @@ func NewAgent(s components.IServer) *Agent { a.Server = s a.nextCheckTime = 0 - a.lastHeartCheckTime = utils.Timex() + a.lastHeartCheckTime = common.Timex() a.heartTimeoutCount= 0 return a } @@ -65,7 +64,7 @@ func (c *Agent) OnLogin(msg components.IMessage) { } func (c *Agent) OnMessage(msg components.IMessage) { - atomic.StoreInt64(&c.lastHeartCheckTime, utils.Timex()) + atomic.StoreInt64(&c.lastHeartCheckTime, common.Timex()) if msg.GetHeader().GetMsgID() == uint32(pb.ProtoCode_LoginReq) { c.OnLogin(msg) @@ -98,7 +97,7 @@ func (c *Agent) OnMessage(msg components.IMessage) { func (c *Agent) OnTimer() { nextCheckTime := atomic.LoadInt64(&c.nextCheckTime) - now := utils.Timex() + now := common.Timex() if now >= nextCheckTime { //检查心跳 c.checkHeartBeat(now) diff --git a/cmd/gameserver/game.go b/cmd/gameserver/game.go index dce6b1a..b106efa 100644 --- a/cmd/gameserver/game.go +++ b/cmd/gameserver/game.go @@ -2,12 +2,15 @@ package main import ( "fmt" + "math/rand" _ "net/http/pprof" "pro2d/cmd/gameserver/action" "pro2d/common" "pro2d/common/components" "pro2d/common/db/mongoproxy" + "pro2d/common/db/redisproxy" "pro2d/models" + "time" "pro2d/common/etcd" ) @@ -40,6 +43,11 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) { } models.InitGameModels() + //redis init + if err = redisproxy.ConnectRedis(sconf.RedisConf.DB, sconf.RedisConf.Auth, sconf.RedisConf.Address); err != nil { + return nil, err + } + //Etcd 初始化 s.EtcdClient, err = etcd.NewEtcdClient(common.GlobalConf.Etcd) if err != nil { @@ -51,6 +59,9 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) { } func (s *GameServer) Start() error { + //设置随机种子 + rand.Seed(time.Now().Unix()) + return s.IServer.Start() } @@ -58,6 +69,7 @@ func (s *GameServer) Stop() { s.IServer.Stop() mongoproxy.CloseMongo() + redisproxy.CloseRedis() s.EtcdClient.Close() } diff --git a/cmd/httpserver/AccountAction.go b/cmd/httpserver/AccountAction.go index ee29d02..74a751d 100644 --- a/cmd/httpserver/AccountAction.go +++ b/cmd/httpserver/AccountAction.go @@ -5,7 +5,6 @@ import ( "pro2d/common" "pro2d/models" "pro2d/pb" - "pro2d/utils" ) type AccountAction struct { @@ -28,7 +27,7 @@ func (h *AccountAction) Register(c *gin.Context) (int, interface{}){ } account.Uid = common.SnowFlack.NextValStr() - account.Password = utils.Md5V(register.Password) + account.Password = common.Md5V(register.Password) if err := account.Create(); err != nil{ return 4, "account register err: " + err.Error() } @@ -46,7 +45,7 @@ func (h *AccountAction) Login(c *gin.Context) (int,interface{}) { return 2, err.Error() } - if utils.Md5V(login.Password) != account.Password { + if common.Md5V(login.Password) != account.Password { return 3, "password error" } diff --git a/common/conf.go b/common/conf.go index 5927ae6..5a43c3a 100644 --- a/common/conf.go +++ b/common/conf.go @@ -38,6 +38,7 @@ type SConf struct { Port int `yaml:"port"` DebugPort int `yaml:"debugport"` MongoConf *MongoConf `yaml:"mongo"` + RedisConf *RedisConf `yaml:"redis"` WorkerPoolSize int `yaml:"pool_size"` PluginPath string `yaml:"plugin_path"` } @@ -85,7 +86,6 @@ type ServerConf struct { DatacenterID int64 `yaml:"datacenterid"` AccountConf *SConf `yaml:"server_account"` GameConf *SConf `yaml:"server_game"` - RedisConf *RedisConf `yaml:"redis"` LogConf *LogConf `yaml:"logconf" json:"logconf"` TestClient *TestClient `yaml:"test_client"` Etcd *Etcd `yaml:"etcd"` diff --git a/common/const.go b/common/const.go index 72dc3e3..68bb8d7 100644 --- a/common/const.go +++ b/common/const.go @@ -12,3 +12,8 @@ const ( //保存数据时间 SaveDataInterval = 5 //s ) + +//redis keys +const ( + NickNames = "name:%s" +) diff --git a/common/db/redisproxy/redis.go b/common/db/redisproxy/redis.go index bbab04f..1d19bfb 100644 --- a/common/db/redisproxy/redis.go +++ b/common/db/redisproxy/redis.go @@ -30,14 +30,28 @@ func CloseRedis() { RedisPool.Close() } -func HKEYS(args ...interface{}) (reply interface{}, err error) { +func redisCommand(command string, args ...interface{}) (reply interface{}, err error) { conn := RedisPool.Get() defer conn.Close() - return conn.Do("HKEYS", args) + return conn.Do(command , args...) +} + +func SETNX(args ...interface{}) (reply interface{}, err error) { + return redisCommand("SETNX", args...) +} + +func SET(args ...interface{}) (reply interface{}, err error) { + return redisCommand("SET", args...) +} + +func GET(args ...interface{}) (reply interface{}, err error) { + return redisCommand("GET", args...) +} + +func HKEYS(args ...interface{}) (reply interface{}, err error) { + return redisCommand("HKEYS", args...) } func HMSET(args ...interface{}) (reply interface{}, err error) { - conn := RedisPool.Get() - defer conn.Close() - return conn.Do("HMSET", args) + return redisCommand("HMSET", args...) } \ No newline at end of file diff --git a/common/name.go b/common/name.go new file mode 100644 index 0000000..92e6ca0 --- /dev/null +++ b/common/name.go @@ -0,0 +1,30 @@ +package common + +var DefaultName [][]string = [][]string { +[]string{ + "伊瑟尼亚", + "结晶荒漠", + "巨炮", + "尼布尔", + "虹光山脉", + "东部湿地", + "星之绿洲", + "神圣之泉", + "废弃军港", +}, + +[]string{ + "幻影", + "星辰", + "铁壁", + "光明", + "无常", + "灵木", + "荒海", + "黄金", + "真理", + "猎潮", + +}, +} + diff --git a/common/utils.go b/common/utils.go new file mode 100644 index 0000000..29118c8 --- /dev/null +++ b/common/utils.go @@ -0,0 +1,49 @@ +package common + +import ( + "crypto/md5" + "encoding/hex" + "math/rand" + "strings" + "time" +) + +func Md5V(str string) string { + h := md5.New() + h.Write([]byte(str)) + return hex.EncodeToString(h.Sum(nil)) +} + +func Timex() int64 { + return time.Now().Unix() +} + +var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") +func RandomString(n int, allowedChars ...[]rune) string { + var letters []rune + + if len(allowedChars) == 0 { + letters = defaultLetters + } else { + letters = allowedChars[0] + } + + b := make([]rune, n) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] + } + + return string(b) +} + + +func RandomName(name [][]string) string { + idx1 := rand.Intn(len(name[0])) + idx2 := rand.Intn(len(name[1])) + + var builder strings.Builder + builder.WriteString(name[0][idx1]) + builder.WriteString(name[1][idx2]) + return builder.String() +} + diff --git a/common/utils_test.go b/common/utils_test.go new file mode 100644 index 0000000..0e34527 --- /dev/null +++ b/common/utils_test.go @@ -0,0 +1,21 @@ +package common + +import ( + "fmt" + "math/rand" + "pro2d/common/db/redisproxy" + "pro2d/common/logger" + "testing" +) + +func TestRandomString(t *testing.T) { + rand.Seed(Timex()) + fmt.Println(RandomName(DefaultName)) +} + +func TestRandomName(t *testing.T) { + if err := redisproxy.ConnectRedis(GlobalConf.GameConf.RedisConf.DB, GlobalConf.GameConf.RedisConf.Auth, GlobalConf.GameConf.RedisConf.Address); err != nil { + logger.Error(err) + return + } +} diff --git a/conf/conf.yaml b/conf/conf.yaml index 5e1218e..38cbbdf 100644 --- a/conf/conf.yaml +++ b/conf/conf.yaml @@ -11,6 +11,10 @@ mongo: &default-mongo timeout: 2 maxnum: 50 +redis: &default-redis + address: "127.0.0.1:6100" + auth: "" + etcd: dialtimeout: 5 endpoints: @@ -38,6 +42,9 @@ server_game: mongo: <<: *default-mongo dbname: "game" + redis: + <<: *default-redis + db: 0 test_client: ip: "127.0.0.1" diff --git a/models/role.go b/models/role.go index de257ee..914f524 100644 --- a/models/role.go +++ b/models/role.go @@ -6,7 +6,6 @@ import ( "pro2d/common/db/mongoproxy" "pro2d/common/logger" "pro2d/pb" - "pro2d/utils" "sync/atomic" ) @@ -167,7 +166,7 @@ func (m *RoleModel) OnRecoverTimer(now int64) { func (m *RoleModel) OnOfflineEvent() { // 设置最新的登录时间 - m.saveRoleData(utils.Timex()) + m.saveRoleData(common.Timex()) } func (m *RoleModel) saveRoleData(now int64) { diff --git a/pb/game.pb.go b/pb/game.pb.go index 37587e6..330ce9d 100644 --- a/pb/game.pb.go +++ b/pb/game.pb.go @@ -175,7 +175,7 @@ type CreateReq struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Uid string `protobuf:"bytes,1,opt,name=uid,proto3" json:"uid,omitempty"` + Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"` Device string `protobuf:"bytes,2,opt,name=device,proto3" json:"device,omitempty"` } @@ -211,9 +211,9 @@ func (*CreateReq) Descriptor() ([]byte, []int) { return file_game_proto_rawDescGZIP(), []int{3} } -func (x *CreateReq) GetUid() string { +func (x *CreateReq) GetToken() string { if x != nil { - return x.Uid + return x.Token } return "" } @@ -300,19 +300,19 @@ var file_game_proto_rawDesc = []byte{ 0x22, 0x38, 0x0a, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x22, 0x35, 0x0a, 0x09, 0x43, 0x72, - 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x76, - 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, - 0x65, 0x22, 0x6f, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, 0x12, 0x20, 0x0a, 0x04, - 0x72, 0x6f, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64, - 0x65, 0x6c, 0x73, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x12, 0x20, - 0x0a, 0x04, 0x68, 0x65, 0x72, 0x6f, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x6d, - 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x48, 0x65, 0x72, 0x6f, 0x52, 0x04, 0x68, 0x65, 0x72, 0x6f, - 0x12, 0x20, 0x0a, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, - 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x54, 0x65, 0x61, 0x6d, 0x52, 0x04, 0x74, 0x65, - 0x61, 0x6d, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x22, 0x39, 0x0a, 0x09, 0x43, 0x72, + 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x16, 0x0a, + 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, + 0x65, 0x76, 0x69, 0x63, 0x65, 0x22, 0x6f, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, + 0x12, 0x20, 0x0a, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, + 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x04, 0x72, 0x6f, + 0x6c, 0x65, 0x12, 0x20, 0x0a, 0x04, 0x68, 0x65, 0x72, 0x6f, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x48, 0x65, 0x72, 0x6f, 0x52, 0x04, + 0x68, 0x65, 0x72, 0x6f, 0x12, 0x20, 0x0a, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x18, 0x04, 0x20, 0x03, + 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x54, 0x65, 0x61, 0x6d, + 0x52, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, + 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/protos b/protos index a2f16ac..5071be1 160000 --- a/protos +++ b/protos @@ -1 +1 @@ -Subproject commit a2f16acb43e1a3272760452efe0d504a11927261 +Subproject commit 5071be163d7bf5389320f21255f43f69ecf0166b diff --git a/utils/utils.go b/utils/utils.go deleted file mode 100644 index c944050..0000000 --- a/utils/utils.go +++ /dev/null @@ -1,17 +0,0 @@ -package utils - -import ( - "crypto/md5" - "encoding/hex" - "time" -) - -func Md5V(str string) string { - h := md5.New() - h.Write([]byte(str)) - return hex.EncodeToString(h.Sum(nil)) -} - -func Timex() int64 { - return time.Now().Unix() -} \ No newline at end of file -- libgit2 0.21.2