修改命名,增加自动填写播放器名称

main
落雨楓 2 years ago
parent d3d214a6b2
commit ab6215d611

@ -126,10 +126,10 @@ func (c *ClientSocketController) SetToken(socket socketio.Conn, token string) {
return
}
if playerInfo.BindingUser != "" {
ctx.User = playerInfo.BindingUser
userName := playerInfo.BindingUser
userInfo := c.storeModel.GetUserInfo(playerInfo.BindingUser)
if playerInfo.BoundUser != "" {
ctx.User = playerInfo.BoundUser
userName := playerInfo.BoundUser
userInfo := c.storeModel.GetUserInfo(playerInfo.BoundUser)
if userInfo != nil {
userName = userInfo.DisplayName
}
@ -137,7 +137,7 @@ func (c *ClientSocketController) SetToken(socket socketio.Conn, token string) {
// 通知客户端
socket.Emit("user:update", ctx.User, userName)
// 加入房间
c.SwitchRoom(socket, "user", playerInfo.BindingUser)
c.SwitchRoom(socket, "user", playerInfo.BoundUser)
}
if playerInfo.PairCode != "" { // 删除存储中的code
@ -168,6 +168,7 @@ func (c *ClientSocketController) RefreshToken(socket socketio.Conn) {
if playerInfo == nil {
playerInfo = new(store.PlayerInfo)
playerInfo.Name = utils.GetPlayerNameFromUserAgent(socket.RemoteHeader().Get("User-Agent"))
} else {
c.storeModel.RemovePlayerInfo(oldToken)
}
@ -284,7 +285,7 @@ func (c *ClientSocketController) OnBindUser(eventObj ...interface{}) {
// 存储信息
userInfo.BoundPlayer = append(userInfo.BoundPlayer, event.Token)
playerInfo.BindingUser = event.User
playerInfo.BoundUser = event.User
c.storeModel.SetUserInfo(event.User, userInfo)
c.storeModel.SetPlayerInfo(event.Token, playerInfo)

@ -39,6 +39,7 @@ require (
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mileusna/useragent v1.2.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect

@ -227,6 +227,8 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mileusna/useragent v1.2.1 h1:p3RJWhi3LfuI6BHdddojREyK3p6qX67vIfOVMnUIVr0=
github.com/mileusna/useragent v1.2.1/go.mod h1:3d8TOmwL/5I8pJjyVDteHtgDGcefrFUX4ccGOMKNYYc=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

@ -3,10 +3,9 @@ package store
import (
"context"
"encoding/json"
"log"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/store"
"log"
)
type StoreModel struct {
@ -15,10 +14,10 @@ type StoreModel struct {
}
type PlayerInfo struct {
IsOnline bool `json:"online"`
BindingUser string `json:"binding_user"`
PairCode string `json:"pair_code"`
Name string `json:"name"`
IsOnline bool `json:"online"`
BoundUser string `json:"bound_user"`
PairCode string `json:"pair_code"`
Name string `json:"name"`
}
type UserInfo struct {

@ -1,6 +1,9 @@
package utils
import "strings"
import (
"github.com/mileusna/useragent"
"strings"
)
func AddAsterisks(text string, prefixLen int, suffixLen int) string {
strLen := len(text)
@ -10,3 +13,17 @@ func AddAsterisks(text string, prefixLen int, suffixLen int) string {
}
return text[0:prefixLen] + strings.Repeat("*", padLen) + text[strLen-suffixLen:strLen]
}
func GetPlayerNameFromUserAgent(userAgentString string) string {
ua := useragent.Parse(userAgentString)
name := ua.OS
if ua.Mobile {
name += " 手机"
} else if ua.Tablet {
name += " 平板电脑"
} else if ua.Desktop {
name += " 电脑"
}
name += "上的 " + ua.Name
return name
}

Loading…
Cancel
Save