From 7fb4be5f6a959e34e83574ef99e87b5c1cae02a0 Mon Sep 17 00:00:00 2001 From: tangmingyou <234767776@qq.com> Date: Fri, 17 Feb 2023 11:36:23 +0800 Subject: [PATCH] avatar some bug --- .gitignore | 6 ++---- Makefile | 4 ++-- api/poker.proto | 2 +- {etc => cmd}/config.toml | 2 ++ go.mod | 1 + go.sum | 8 +++++++ internal/conf/conf.go | 8 ++++--- internal/game/player.go | 5 +++-- internal/game/table.go | 2 +- internal/model/entity/user.go | 2 +- internal/server/route.go | 4 ++-- internal/server/ws.go | 14 ++++++------ internal/service/auth.go | 38 +++++++++++++++++++++++++-------- internal/service/service.go | 7 ++++++ internal/service/store/store.go | 2 ++ internal/service/table.go | 2 +- internal/session/net_client.go | 2 +- 17 files changed, 75 insertions(+), 34 deletions(-) rename {etc => cmd}/config.toml (89%) diff --git a/.gitignore b/.gitignore index b32653d..ecfae6a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,10 +21,8 @@ Thumbs.db # build -/cmd/comet/comet -/cmd/logic/logic -/cmd/job/job /target /configs /dist -/etc/texas-poker.db \ No newline at end of file +/etc/texas-poker.db +/cmd/avatar \ No newline at end of file diff --git a/Makefile b/Makefile index 21863a4..c0381f0 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,9 @@ GOTEST=$(GOCMD) test all: build build: - rm -rf target/ + rm -rf target/texas mkdir target/ - cp etc/config.toml target/config.toml + #cp etc/config.toml target/config.toml $(GOBUILD) -o target/texas cmd/main.go clean: diff --git a/api/poker.proto b/api/poker.proto index 8764671..9657c60 100644 --- a/api/poker.proto +++ b/api/poker.proto @@ -126,7 +126,7 @@ message ReqGameFullStatus { } message ResGameFullStatus { // 玩家当前游戏状态: 断线,重新登录.... - bool inGame = 1; // 游戏是否进行中 +// bool inGame = 1; // 游戏是否进行中 int32 tableNo = 2; // 所在桌面编号 int32 gameStage = 3; // 游戏阶段? int32 chip = 4; // 牌桌筹码 diff --git a/etc/config.toml b/cmd/config.toml similarity index 89% rename from etc/config.toml rename to cmd/config.toml index f347132..9de84f0 100644 --- a/etc/config.toml +++ b/cmd/config.toml @@ -11,6 +11,7 @@ ignoreUrl = [ "/api/auth/captcha", "/api/auth/authorize", "/api/user/findByName", + "/api/gm/avatar", ] # 需要输入图形验证码的url captchaUrl = [ @@ -23,3 +24,4 @@ dbPath = "etc/texas-poker.db" [game] # 新用户赠送筹码 GiftChip = 10000 +AvatarPath = "cmd/avatar/" diff --git a/go.mod b/go.mod index 969ed12..6fe3c66 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/o1egl/govatar v0.4.1 // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/ugorji/go/codec v1.2.7 // indirect golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect diff --git a/go.sum b/go.sum index b90a7a7..6a94411 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -62,6 +64,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/o1egl/govatar v0.4.1 h1:RRzAxm52WpZMSEoWgAXrTcXWKhIUPpgpI54KP+UI0Ew= +github.com/o1egl/govatar v0.4.1/go.mod h1:cSBJjpgYiKmQ8E+C4zNBcsbuDwy9UH4HS8BwE4m6JmQ= github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -70,6 +74,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -83,6 +89,7 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= +github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= @@ -112,6 +119,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 1db4949..ee8bf71 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -2,6 +2,7 @@ package conf import ( "flag" + "fmt" "github.com/BurntSushi/toml" ) @@ -12,14 +13,14 @@ var ( func init() { // go run xx -conf=xx.toml - flag.StringVar(&confPath, "conf", "etc/config.toml", "default config path.") + flag.StringVar(&confPath, "conf", "cmd/config.toml", "default config path.") + fmt.Printf("confPath: %v\n", confPath) Conf = Default() _, err := toml.DecodeFile(confPath, Conf) if err != nil { panic(err) } - // fmt.Printf("%v", Conf) } func Default() *Config { @@ -52,5 +53,6 @@ type Sqlite struct { } type Game struct { - GiftChip int32 + GiftChip int32 + AvatarPath string } diff --git a/internal/game/player.go b/internal/game/player.go index c6c59dd..191de45 100644 --- a/internal/game/player.go +++ b/internal/game/player.go @@ -41,11 +41,12 @@ func (p *Player) Init() { // RoundInit 下一回合初始化 func (p *Player) RoundInit() { p.SetStatus(1) + p.BetOpts = emptyOpts + p.Hand = nil p.BetMin = 0 p.BetMax = 0 - p.BetOpts = emptyOpts + p.RoundBetTimes = 0 p.TotalBetChip = 0 - p.Hand = nil p.Cards[0] = nil p.Cards[1] = nil } diff --git a/internal/game/table.go b/internal/game/table.go index df68243..3687f9e 100644 --- a/internal/game/table.go +++ b/internal/game/table.go @@ -131,7 +131,7 @@ func (t *Table) JoinPlayer(player *Player) (int, error) { // BuildResGameFullStatus 构建牌桌当前游戏状态消息 func (t *Table) BuildResGameFullStatus() *api.ResGameFullStatus { resGame := &api.ResGameFullStatus{ - InGame: collect.In(t.Stage, 2, 3, 4, 5, 7), + //InGame: collect.In(t.Stage, 2, 3, 4, 5), TableNo: t.TableNo, GameStage: t.Stage, Chip: t.Chip, diff --git a/internal/model/entity/user.go b/internal/model/entity/user.go index 9c965af..37d477a 100644 --- a/internal/model/entity/user.go +++ b/internal/model/entity/user.go @@ -5,8 +5,8 @@ type User struct { Id int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` Username string `gorm:"column:username" json:"username"` // comment:用户名 Password string `gorm:"column:password" json:"password"` // comment:密码 - Nickname string `gorm:"column:nickname" json:"nickname"` // comment:昵称 Balance int32 `gorm:"column:balance" json:"balance"` // comment:余额 + Avatar string `gorm:"column:avatar" json:"avatar"` // comment:头像 Version int64 `gorm:"column:version" json:"version"` // comment:更新版本锁 } diff --git a/internal/server/route.go b/internal/server/route.go index 802a795..980758d 100644 --- a/internal/server/route.go +++ b/internal/server/route.go @@ -33,7 +33,7 @@ func NewServer() *gin.Engine { user := base.Group("/user") user.GET("/findByName", dao.FindUserByName) - _ = base.Group("/game") - + gm := base.Group("/gm") + gm.GET("/avatar/:avatar", service.Avatar) return server } diff --git a/internal/server/ws.go b/internal/server/ws.go index 9f514c9..6e606ae 100644 --- a/internal/server/ws.go +++ b/internal/server/ws.go @@ -85,13 +85,13 @@ const ( // 处理新建的websocket func handleNetClient(client *session.NetClient) { - //defer (func() { - // client.Close("conn finish") - //})() - - //var account *session.NetAccount - //var player *session.Player - + defer func() { + // 捕获aes解析错误 + if r := recover(); r != nil { + fmt.Println(r) + client.Write(&api.ResFail{Code: 401, Msg: r.(error).Error()}) + } + }() // https://github.com/gorilla/websocket/blob/a68708917c6a4f06314ab4e52493cc61359c9d42/examples/chat/conn.go#L50 client.Conn.SetReadLimit(maxMessageSize) // TODO readDeadLine 读取超时后关闭连接 diff --git a/internal/service/auth.go b/internal/service/auth.go index 107eb4b..f780431 100644 --- a/internal/service/auth.go +++ b/internal/service/auth.go @@ -6,7 +6,11 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/golang/protobuf/proto" + "github.com/o1egl/govatar" + "math/rand" "net/http" + "os" + "strings" "sync" "texas-poker-bk/api" "texas-poker-bk/internal/conf" @@ -33,6 +37,9 @@ func init() { panic(err) } aesTokenKeyBytes = keyBytes + + // 头像文件夹 + _ = os.MkdirAll(conf.Conf.Game.AvatarPath, os.ModePerm) } // Authorize 登录或注册 @@ -48,8 +55,13 @@ func Authorize(ctx *gin.Context) { } user := dao.UserDao.FindUserByName(username) if user == nil { + var err error = nil // 注册 - user = registerUser(username, password) + user, err = registerUser(username, password) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()}) + return + } } else { // 校验密码 if user.Password != password { @@ -59,9 +71,10 @@ func Authorize(ctx *gin.Context) { } sub := &session.Subject{ - Id: user.Id, - Name: user.Username, - Time: time.Now().UnixMilli(), + Id: user.Id, + Name: user.Username, + Time: time.Now().UnixMilli(), + Avatar: user.Avatar, } token, err := EncodeSubject(sub) if err != nil { @@ -74,22 +87,29 @@ func Authorize(ctx *gin.Context) { "user": gin.H{ "id": user.Id, "username": user.Username, - // TODO avatar... + "avatar": user.Avatar, }, }}) } // 注册用户到DB -func registerUser(username string, password string) *entity.User { +func registerUser(username string, password string) (*entity.User, error) { + // 生成头像 + avatar := fmt.Sprintf("%d%d%s", time.Now().UnixMilli(), rand.Intn(99), ".jpg") + err := govatar.GenerateFile(govatar.MALE, conf.Conf.Game.AvatarPath+avatar) + if err != nil { + return nil, err + } user := &entity.User{ Username: username, Password: password, - Nickname: username, Balance: conf.Conf.Game.GiftChip, + Avatar: avatar, + Version: 0, } // TODO error dao.UserDao.DB.Save(user) - return user + return user, nil } // SubjectAuthFilter 认证过滤器 @@ -99,7 +119,7 @@ func SubjectAuthFilter(ctx *gin.Context) { // ignore auth urls if collect.IsNotEmptySlice(conf.Conf.Auth.IgnoreUrl) { for _, ignoreUrl := range conf.Conf.Auth.IgnoreUrl { - if ignoreUrl == ctx.Request.URL.Path { + if strings.HasPrefix(ctx.Request.URL.Path, ignoreUrl) { return } } diff --git a/internal/service/service.go b/internal/service/service.go index 14fb090..27ebc14 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -6,9 +6,16 @@ import ( "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" "net/http" + "texas-poker-bk/internal/conf" "time" ) +func Avatar(ctx *gin.Context) { + avatar := ctx.Param("avatar") + ctx.Header("Cache-Control", "private, max-age=86400") + ctx.File(conf.Conf.Game.AvatarPath + avatar) +} + // Captcha 获取验证码 func Captcha(ctx *gin.Context) { l, w, h := 4, 107, 36 diff --git a/internal/service/store/store.go b/internal/service/store/store.go index e8838b5..92eeb57 100644 --- a/internal/service/store/store.go +++ b/internal/service/store/store.go @@ -5,6 +5,7 @@ import ( "fmt" "sync" "sync/atomic" + "texas-poker-bk/api" "texas-poker-bk/internal/game" "texas-poker-bk/internal/session" ) @@ -49,6 +50,7 @@ func SaveAndTryRecoverNetAccounts(account *session.NetAccount) *session.NetAccou return account } // 掉线重连的用户, TODO 重复登录的用户 + current.Client.Write(&api.ResFail{Code: 403, Msg: "该账号在其他地方登录,您已下线"}) current.Client = account.Client if current.Player != nil { current.Player.ProtoWriter = account.Client diff --git a/internal/service/table.go b/internal/service/table.go index 57ed440..1148a2c 100644 --- a/internal/service/table.go +++ b/internal/service/table.go @@ -140,7 +140,7 @@ func HandleReqJoinTable(account *session.NetAccount, msg *api.ReqJoinTable) (pro if table == nil { return &api.ResFail{Msg: "牌桌不存在"}, nil } - if table.Stage != 1 { + if !collect.In(table.Stage, 1, 7) { return &api.ResFail{Msg: "牌桌正在进行中"}, nil } // 账户余额限制 diff --git a/internal/session/net_client.go b/internal/session/net_client.go index 4837c90..d7881f1 100644 --- a/internal/session/net_client.go +++ b/internal/session/net_client.go @@ -40,7 +40,7 @@ func (c *NetClient) WriteSeq(success bool, seq int32, msg proto.Message) { // 序列化 protobuf 消息 op, err := api.GetProtoOp(msg) if err != nil { - fmt.Println("write msg match op err:", err) + fmt.Println("write msg match op err:", err, msg.String()) return } bytes, err := proto.Marshal(msg)