Browse Source

avatar some bug

master
tangmingyou 4 years ago
parent
commit
7fb4be5f6a
  1. 4
      .gitignore
  2. 4
      Makefile
  3. 2
      api/poker.proto
  4. 2
      cmd/config.toml
  5. 1
      go.mod
  6. 8
      go.sum
  7. 8
      internal/conf/conf.go
  8. 5
      internal/game/player.go
  9. 2
      internal/game/table.go
  10. 2
      internal/model/entity/user.go
  11. 4
      internal/server/route.go
  12. 14
      internal/server/ws.go
  13. 38
      internal/service/auth.go
  14. 7
      internal/service/service.go
  15. 2
      internal/service/store/store.go
  16. 2
      internal/service/table.go
  17. 2
      internal/session/net_client.go

4
.gitignore vendored

@ -21,10 +21,8 @@
Thumbs.db
# build
/cmd/comet/comet
/cmd/logic/logic
/cmd/job/job
/target
/configs
/dist
/etc/texas-poker.db
/cmd/avatar

4
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:

2
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; //

2
etc/config.toml → 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/"

1
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

8
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=

8
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
}

5
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
}

2
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,

2
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:更新版本锁
}

4
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
}

14
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 读取超时后关闭连接

38
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
}
}

7
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

2
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

2
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
}
// 账户余额限制

2
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)

Loading…
Cancel
Save