Commit 29a163bec708a503440123dd6cbdb0b580aed793

Authored by zhangqijia
1 parent 0ce6c418

fix: CreateReq uid->token; random name

.gitignore
... ... @@ -5,5 +5,6 @@ protos
5 5 csvdata
6 6 *.csv
7 7 *.out
  8 +imgui
8 9  
9 10 *.log
10 11 \ No newline at end of file
... ...
cmd/gameserver/action/RoleAction.go
1 1 package action
2 2  
3 3 import (
  4 + "fmt"
4 5 "github.com/golang/protobuf/proto"
5 6 "pro2d/common"
6 7 "pro2d/common/components"
  8 + "pro2d/common/db/redisproxy"
7 9 "pro2d/common/logger"
8 10 "pro2d/models"
9 11 "pro2d/pb"
... ... @@ -14,19 +16,39 @@ func HeartRpc(msg components.IMessage) (int32, interface{}) {
14 16 return 0, nil
15 17 }
16 18  
  19 +func getRandomName() string {
  20 + name := ""
  21 + for {
  22 + name = common.RandomName(common.DefaultName)
  23 + if name == "" {
  24 + continue
  25 + }
  26 +
  27 + relay, err := redisproxy.SETNX(fmt.Sprintf(common.NickNames, name), "1")
  28 + if err != nil {
  29 + continue
  30 + }
  31 + if relay.(int64) == 1 {
  32 + break
  33 + }
  34 + }
  35 + return name
  36 +}
  37 +
17 38 func CreateRpc(msg components.IMessage) (int32, interface{}) {
18 39 req := pb.CreateReq{}
19 40 if err := proto.Unmarshal(msg.GetData(), &req); err != nil {
20 41 logger.Error("CreateRpc err: %v", err)
21 42 return 1, nil
22 43 }
23   - role := models.RoleExistByUid(req.Uid)
  44 + role := models.RoleExistByUid(req.Token)
24 45 if role != nil {
25 46 return 2, nil
26 47 }
27 48  
28 49 roleId := common.SnowFlack.NextValStr()
29 50 role = models.NewRole(roleId)
  51 + role.Role.Nick = getRandomName()
30 52 if err := role.Create(); err != nil {
31 53 logger.Error("CreateRpc role create err: %v", err)
32 54 return 3, nil
... ...
cmd/gameserver/action/RoleAction_test.go 0 โ†’ 100644
... ... @@ -0,0 +1,22 @@
  1 +package action
  2 +
  3 +import (
  4 + "fmt"
  5 + "math/rand"
  6 + "pro2d/common"
  7 + "pro2d/common/db/redisproxy"
  8 + "pro2d/common/logger"
  9 + "testing"
  10 +)
  11 +
  12 +func TestGetActionMap(t *testing.T) {
  13 + rand.Seed(common.Timex())
  14 +
  15 + var redisConf = common.GlobalConf.GameConf.RedisConf
  16 + if err := redisproxy.ConnectRedis(redisConf.DB, redisConf.Auth, redisConf.Address); err != nil {
  17 + logger.Error(err)
  18 + return
  19 + }
  20 + name := getRandomName()
  21 + fmt.Println(name)
  22 +}
... ...
cmd/gameserver/agent.go
... ... @@ -9,7 +9,6 @@ import (
9 9 "pro2d/common/logger"
10 10 "pro2d/models"
11 11 "pro2d/pb"
12   - "pro2d/utils"
13 12 "sync"
14 13 "sync/atomic"
15 14 )
... ... @@ -31,7 +30,7 @@ func NewAgent(s components.IServer) *Agent {
31 30 a.Server = s
32 31  
33 32 a.nextCheckTime = 0
34   - a.lastHeartCheckTime = utils.Timex()
  33 + a.lastHeartCheckTime = common.Timex()
35 34 a.heartTimeoutCount= 0
36 35 return a
37 36 }
... ... @@ -65,7 +64,7 @@ func (c *Agent) OnLogin(msg components.IMessage) {
65 64 }
66 65  
67 66 func (c *Agent) OnMessage(msg components.IMessage) {
68   - atomic.StoreInt64(&c.lastHeartCheckTime, utils.Timex())
  67 + atomic.StoreInt64(&c.lastHeartCheckTime, common.Timex())
69 68  
70 69 if msg.GetHeader().GetMsgID() == uint32(pb.ProtoCode_LoginReq) {
71 70 c.OnLogin(msg)
... ... @@ -98,7 +97,7 @@ func (c *Agent) OnMessage(msg components.IMessage) {
98 97  
99 98 func (c *Agent) OnTimer() {
100 99 nextCheckTime := atomic.LoadInt64(&c.nextCheckTime)
101   - now := utils.Timex()
  100 + now := common.Timex()
102 101 if now >= nextCheckTime {
103 102 //ๆฃ€ๆŸฅๅฟƒ่ทณ
104 103 c.checkHeartBeat(now)
... ...
cmd/gameserver/game.go
... ... @@ -2,12 +2,15 @@ package main
2 2  
3 3 import (
4 4 "fmt"
  5 + "math/rand"
5 6 _ "net/http/pprof"
6 7 "pro2d/cmd/gameserver/action"
7 8 "pro2d/common"
8 9 "pro2d/common/components"
9 10 "pro2d/common/db/mongoproxy"
  11 + "pro2d/common/db/redisproxy"
10 12 "pro2d/models"
  13 + "time"
11 14  
12 15 "pro2d/common/etcd"
13 16 )
... ... @@ -40,6 +43,11 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) {
40 43 }
41 44 models.InitGameModels()
42 45  
  46 + //redis init
  47 + if err = redisproxy.ConnectRedis(sconf.RedisConf.DB, sconf.RedisConf.Auth, sconf.RedisConf.Address); err != nil {
  48 + return nil, err
  49 + }
  50 +
43 51 //Etcd ๅˆๅง‹ๅŒ–
44 52 s.EtcdClient, err = etcd.NewEtcdClient(common.GlobalConf.Etcd)
45 53 if err != nil {
... ... @@ -51,6 +59,9 @@ func NewGameServer(sconf *common.SConf) (*GameServer, error) {
51 59 }
52 60  
53 61 func (s *GameServer) Start() error {
  62 + //่ฎพ็ฝฎ้šๆœบ็งๅญ
  63 + rand.Seed(time.Now().Unix())
  64 +
54 65 return s.IServer.Start()
55 66 }
56 67  
... ... @@ -58,6 +69,7 @@ func (s *GameServer) Stop() {
58 69 s.IServer.Stop()
59 70  
60 71 mongoproxy.CloseMongo()
  72 + redisproxy.CloseRedis()
61 73 s.EtcdClient.Close()
62 74 }
63 75  
... ...
cmd/httpserver/AccountAction.go
... ... @@ -5,7 +5,6 @@ import (
5 5 "pro2d/common"
6 6 "pro2d/models"
7 7 "pro2d/pb"
8   - "pro2d/utils"
9 8 )
10 9  
11 10 type AccountAction struct {
... ... @@ -28,7 +27,7 @@ func (h *AccountAction) Register(c *gin.Context) (int, interface{}){
28 27 }
29 28  
30 29 account.Uid = common.SnowFlack.NextValStr()
31   - account.Password = utils.Md5V(register.Password)
  30 + account.Password = common.Md5V(register.Password)
32 31 if err := account.Create(); err != nil{
33 32 return 4, "account register err: " + err.Error()
34 33 }
... ... @@ -46,7 +45,7 @@ func (h *AccountAction) Login(c *gin.Context) (int,interface{}) {
46 45 return 2, err.Error()
47 46 }
48 47  
49   - if utils.Md5V(login.Password) != account.Password {
  48 + if common.Md5V(login.Password) != account.Password {
50 49 return 3, "password error"
51 50 }
52 51  
... ...
common/conf.go
... ... @@ -38,6 +38,7 @@ type SConf struct {
38 38 Port int `yaml:"port"`
39 39 DebugPort int `yaml:"debugport"`
40 40 MongoConf *MongoConf `yaml:"mongo"`
  41 + RedisConf *RedisConf `yaml:"redis"`
41 42 WorkerPoolSize int `yaml:"pool_size"`
42 43 PluginPath string `yaml:"plugin_path"`
43 44 }
... ... @@ -85,7 +86,6 @@ type ServerConf struct {
85 86 DatacenterID int64 `yaml:"datacenterid"`
86 87 AccountConf *SConf `yaml:"server_account"`
87 88 GameConf *SConf `yaml:"server_game"`
88   - RedisConf *RedisConf `yaml:"redis"`
89 89 LogConf *LogConf `yaml:"logconf" json:"logconf"`
90 90 TestClient *TestClient `yaml:"test_client"`
91 91 Etcd *Etcd `yaml:"etcd"`
... ...
common/const.go
... ... @@ -12,3 +12,8 @@ const (
12 12 //ไฟๅญ˜ๆ•ฐๆฎๆ—ถ้—ด
13 13 SaveDataInterval = 5 //s
14 14 )
  15 +
  16 +//redis keys
  17 +const (
  18 + NickNames = "name:%s"
  19 +)
... ...
common/db/redisproxy/redis.go
... ... @@ -30,14 +30,28 @@ func CloseRedis() {
30 30 RedisPool.Close()
31 31 }
32 32  
33   -func HKEYS(args ...interface{}) (reply interface{}, err error) {
  33 +func redisCommand(command string, args ...interface{}) (reply interface{}, err error) {
34 34 conn := RedisPool.Get()
35 35 defer conn.Close()
36   - return conn.Do("HKEYS", args)
  36 + return conn.Do(command , args...)
  37 +}
  38 +
  39 +func SETNX(args ...interface{}) (reply interface{}, err error) {
  40 + return redisCommand("SETNX", args...)
  41 +}
  42 +
  43 +func SET(args ...interface{}) (reply interface{}, err error) {
  44 + return redisCommand("SET", args...)
  45 +}
  46 +
  47 +func GET(args ...interface{}) (reply interface{}, err error) {
  48 + return redisCommand("GET", args...)
  49 +}
  50 +
  51 +func HKEYS(args ...interface{}) (reply interface{}, err error) {
  52 + return redisCommand("HKEYS", args...)
37 53 }
38 54  
39 55 func HMSET(args ...interface{}) (reply interface{}, err error) {
40   - conn := RedisPool.Get()
41   - defer conn.Close()
42   - return conn.Do("HMSET", args)
  56 + return redisCommand("HMSET", args...)
43 57 }
44 58 \ No newline at end of file
... ...
common/name.go 0 โ†’ 100644
... ... @@ -0,0 +1,30 @@
  1 +package common
  2 +
  3 +var DefaultName [][]string = [][]string {
  4 +[]string{
  5 + "ไผŠ็‘Ÿๅฐผไบš",
  6 + "็ป“ๆ™ถ่’ๆผ ",
  7 + "ๅทจ็‚ฎ",
  8 + "ๅฐผๅธƒๅฐ”",
  9 + "่™นๅ…‰ๅฑฑ่„‰",
  10 + "ไธœ้ƒจๆนฟๅœฐ",
  11 + "ๆ˜Ÿไน‹็ปฟๆดฒ",
  12 + "็ฅžๅœฃไน‹ๆณ‰",
  13 + "ๅบŸๅผƒๅ†›ๆธฏ",
  14 +},
  15 +
  16 +[]string{
  17 + "ๅนปๅฝฑ",
  18 + "ๆ˜Ÿ่พฐ",
  19 + "้“ๅฃ",
  20 + "ๅ…‰ๆ˜Ž",
  21 + "ๆ— ๅธธ",
  22 + "็ตๆœจ",
  23 + "่’ๆตท",
  24 + "้ป„้‡‘",
  25 + "็œŸ็†",
  26 + "็ŒŽๆฝฎ",
  27 +
  28 +},
  29 +}
  30 +
... ...
common/utils.go 0 โ†’ 100644
... ... @@ -0,0 +1,49 @@
  1 +package common
  2 +
  3 +import (
  4 + "crypto/md5"
  5 + "encoding/hex"
  6 + "math/rand"
  7 + "strings"
  8 + "time"
  9 +)
  10 +
  11 +func Md5V(str string) string {
  12 + h := md5.New()
  13 + h.Write([]byte(str))
  14 + return hex.EncodeToString(h.Sum(nil))
  15 +}
  16 +
  17 +func Timex() int64 {
  18 + return time.Now().Unix()
  19 +}
  20 +
  21 +var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  22 +func RandomString(n int, allowedChars ...[]rune) string {
  23 + var letters []rune
  24 +
  25 + if len(allowedChars) == 0 {
  26 + letters = defaultLetters
  27 + } else {
  28 + letters = allowedChars[0]
  29 + }
  30 +
  31 + b := make([]rune, n)
  32 + for i := range b {
  33 + b[i] = letters[rand.Intn(len(letters))]
  34 + }
  35 +
  36 + return string(b)
  37 +}
  38 +
  39 +
  40 +func RandomName(name [][]string) string {
  41 + idx1 := rand.Intn(len(name[0]))
  42 + idx2 := rand.Intn(len(name[1]))
  43 +
  44 + var builder strings.Builder
  45 + builder.WriteString(name[0][idx1])
  46 + builder.WriteString(name[1][idx2])
  47 + return builder.String()
  48 +}
  49 +
... ...
common/utils_test.go 0 โ†’ 100644
... ... @@ -0,0 +1,21 @@
  1 +package common
  2 +
  3 +import (
  4 + "fmt"
  5 + "math/rand"
  6 + "pro2d/common/db/redisproxy"
  7 + "pro2d/common/logger"
  8 + "testing"
  9 +)
  10 +
  11 +func TestRandomString(t *testing.T) {
  12 + rand.Seed(Timex())
  13 + fmt.Println(RandomName(DefaultName))
  14 +}
  15 +
  16 +func TestRandomName(t *testing.T) {
  17 + if err := redisproxy.ConnectRedis(GlobalConf.GameConf.RedisConf.DB, GlobalConf.GameConf.RedisConf.Auth, GlobalConf.GameConf.RedisConf.Address); err != nil {
  18 + logger.Error(err)
  19 + return
  20 + }
  21 +}
... ...
conf/conf.yaml
... ... @@ -11,6 +11,10 @@ mongo: &default-mongo
11 11 timeout: 2
12 12 maxnum: 50
13 13  
  14 +redis: &default-redis
  15 + address: "127.0.0.1:6100"
  16 + auth: ""
  17 +
14 18 etcd:
15 19 dialtimeout: 5
16 20 endpoints:
... ... @@ -38,6 +42,9 @@ server_game:
38 42 mongo:
39 43 <<: *default-mongo
40 44 dbname: "game"
  45 + redis:
  46 + <<: *default-redis
  47 + db: 0
41 48  
42 49 test_client:
43 50 ip: "127.0.0.1"
... ...
models/role.go
... ... @@ -6,7 +6,6 @@ import (
6 6 "pro2d/common/db/mongoproxy"
7 7 "pro2d/common/logger"
8 8 "pro2d/pb"
9   - "pro2d/utils"
10 9 "sync/atomic"
11 10 )
12 11  
... ... @@ -167,7 +166,7 @@ func (m *RoleModel) OnRecoverTimer(now int64) {
167 166  
168 167 func (m *RoleModel) OnOfflineEvent() {
169 168 // ่ฎพ็ฝฎๆœ€ๆ–ฐ็š„็™ปๅฝ•ๆ—ถ้—ด
170   - m.saveRoleData(utils.Timex())
  169 + m.saveRoleData(common.Timex())
171 170 }
172 171  
173 172 func (m *RoleModel) saveRoleData(now int64) {
... ...
pb/game.pb.go
... ... @@ -175,7 +175,7 @@ type CreateReq struct {
175 175 sizeCache protoimpl.SizeCache
176 176 unknownFields protoimpl.UnknownFields
177 177  
178   - Uid string `protobuf:"bytes,1,opt,name=uid,proto3" json:"uid,omitempty"`
  178 + Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
179 179 Device string `protobuf:"bytes,2,opt,name=device,proto3" json:"device,omitempty"`
180 180 }
181 181  
... ... @@ -211,9 +211,9 @@ func (*CreateReq) Descriptor() ([]byte, []int) {
211 211 return file_game_proto_rawDescGZIP(), []int{3}
212 212 }
213 213  
214   -func (x *CreateReq) GetUid() string {
  214 +func (x *CreateReq) GetToken() string {
215 215 if x != nil {
216   - return x.Uid
  216 + return x.Token
217 217 }
218 218 return ""
219 219 }
... ... @@ -300,19 +300,19 @@ var file_game_proto_rawDesc = []byte{
300 300 0x22, 0x38, 0x0a, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x12, 0x14, 0x0a, 0x05,
301 301 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b,
302 302 0x65, 0x6e, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01,
303   - 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x22, 0x35, 0x0a, 0x09, 0x43, 0x72,
304   - 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x01,
305   - 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x64, 0x65, 0x76,
306   - 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63,
307   - 0x65, 0x22, 0x6f, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, 0x12, 0x20, 0x0a, 0x04,
308   - 0x72, 0x6f, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64,
309   - 0x65, 0x6c, 0x73, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x12, 0x20,
310   - 0x0a, 0x04, 0x68, 0x65, 0x72, 0x6f, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x6d,
311   - 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x48, 0x65, 0x72, 0x6f, 0x52, 0x04, 0x68, 0x65, 0x72, 0x6f,
312   - 0x12, 0x20, 0x0a, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c,
313   - 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x54, 0x65, 0x61, 0x6d, 0x52, 0x04, 0x74, 0x65,
314   - 0x61, 0x6d, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06,
315   - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
  303 + 0x28, 0x09, 0x52, 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x22, 0x39, 0x0a, 0x09, 0x43, 0x72,
  304 + 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e,
  305 + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x16, 0x0a,
  306 + 0x06, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x64,
  307 + 0x65, 0x76, 0x69, 0x63, 0x65, 0x22, 0x6f, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70,
  308 + 0x12, 0x20, 0x0a, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c,
  309 + 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x04, 0x72, 0x6f,
  310 + 0x6c, 0x65, 0x12, 0x20, 0x0a, 0x04, 0x68, 0x65, 0x72, 0x6f, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b,
  311 + 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x48, 0x65, 0x72, 0x6f, 0x52, 0x04,
  312 + 0x68, 0x65, 0x72, 0x6f, 0x12, 0x20, 0x0a, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x18, 0x04, 0x20, 0x03,
  313 + 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x54, 0x65, 0x61, 0x6d,
  314 + 0x52, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b,
  315 + 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
316 316 }
317 317  
318 318 var (
... ...
1   -Subproject commit a2f16acb43e1a3272760452efe0d504a11927261
  1 +Subproject commit 5071be163d7bf5389320f21255f43f69ecf0166b
... ...
utils/utils.go deleted
... ... @@ -1,17 +0,0 @@
1   -package utils
2   -
3   -import (
4   - "crypto/md5"
5   - "encoding/hex"
6   - "time"
7   -)
8   -
9   -func Md5V(str string) string {
10   - h := md5.New()
11   - h.Write([]byte(str))
12   - return hex.EncodeToString(h.Sum(nil))
13   -}
14   -
15   -func Timex() int64 {
16   - return time.Now().Unix()
17   -}
18 0 \ No newline at end of file