You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
4.8 KiB
Go

2 years ago
package user
import (
2 years ago
"app/network"
2 years ago
"app/service/main/in_obj"
"app/service/main/message/pb"
"app/service/main/redis"
2 years ago
"encoding/json"
2 years ago
"fmt"
2 years ago
"sync"
2 years ago
"time"
2 years ago
)
func NewUser(mgr in_obj.IGameMgr) *User {
return &User{mgr: mgr}
}
type User struct {
network.BaseNetHandler
2 years ago
mgr in_obj.IGameMgr
appId string
roomId string
uid string
nickName string
isDebug bool
isRunning bool
audienceSet sync.Map
msgChan chan func()
closeChan chan struct{}
pushCloseChan chan struct{}
2 years ago
}
func (s *User) OnSessionCreated() {
2 years ago
fmt.Println("[SessionCreated]")
2 years ago
//主协程
s.msgChan = make(chan func())
s.closeChan = make(chan struct{})
2 years ago
s.pushCloseChan = make(chan struct{})
s.audienceSet = sync.Map{}
2 years ago
go func() {
for {
select {
case f := <-s.msgChan:
f()
case <-s.closeChan:
return
}
}
}()
}
func (s *User) OnSessionClosed() {
2 years ago
fmt.Println("[SessionClosed]")
2 years ago
if !s.isRunning {
return
}
s.setPushActive(false)
s.isRunning = false
2 years ago
s.pushCloseChan <- struct{}{}
2 years ago
s.closeChan <- struct{}{}
}
func (s *User) OnRecv(msgId int32, data interface{}) {
2 years ago
if msgId == int32(pb.MSG_TYPE__Ping) {
s.SendMsg(&pb.Pong{
ServerTime: time.Now().Unix(),
ClientTime: data.(*pb.Ping).ClientTime,
})
return
}
s.Log("Recv", data)
2 years ago
s.msgChan <- func() {
switch msgId {
case int32(pb.MSG_TYPE__Login):
s.OnLogin(data.(*pb.Login))
case int32(pb.MSG_TYPE__PlayStart):
s.OnPlayStart(data.(*pb.PlayStart))
case int32(pb.MSG_TYPE__PlayEnd):
s.OnPlayEnd(data.(*pb.PlayEnd))
case int32(pb.MSG_TYPE__Report):
s.OnReport(data.(*pb.Report))
case int32(pb.MSG_TYPE__GetRank):
s.OnGetRank(data.(*pb.GetRank))
}
}
}
2 years ago
func (s *User) OnRecvPush(data string) {
1 year ago
s.Log("OnRecvPush", data)
2 years ago
msg := &pb.NotifyAudienceAction{}
err := json.Unmarshal([]byte(data), msg)
if err != nil {
return
}
if _, ok := s.audienceSet.Load(msg.OpenId); !ok {
1 year ago
audienceData := redis.GetAudience(s.appId, msg.OpenId)
2 years ago
if audienceData != nil {
s.audienceSet.Store(msg.OpenId, 1)
audienceMsg := &pb.NotifyNewAudience{
Audience: audienceData,
}
s.msgChan <- func() {
s.SendUserMsg(audienceMsg)
}
}
}
s.msgChan <- func() {
s.SendUserMsg(msg)
}
}
2 years ago
func (s *User) SendUserMsg(msg interface{}) {
s.Log("Send", msg)
s.SendMsg(msg)
}
func (s *User) Log(logType string, msg interface{}) {
2 years ago
fmt.Printf("[%s][%s](%s,%s,%s)%v\n", logType, network.MsgName(msg), s.roomId, s.uid, s.nickName, msg)
2 years ago
}
2 years ago
func (s *User) OnLogin(msg *pb.Login) {
2 years ago
var (
roomId string
uid string
nickName string
result pb.ERROR_CODE
)
if !msg.IsDebug {
roomId, uid, nickName, result = s.mgr.Login(msg.AppId, msg.Token)
} else {
1 year ago
roomId, uid, nickName, result = "1111111111111111111", "debug", "debug", pb.ERROR_CODE_SUCCESS
2 years ago
}
2 years ago
s.appId = msg.AppId
s.roomId = roomId
2 years ago
s.uid = uid
s.nickName = nickName
2 years ago
s.isDebug = msg.IsDebug
s.SendUserMsg(&pb.LoginResult{
2 years ago
Result: result,
RoomId: roomId,
UID: uid,
NickName: nickName,
})
2 years ago
//test
s.OnPlayStart(nil)
2 years ago
}
func (s *User) OnPlayStart(msg *pb.PlayStart) {
if s.isRunning {
2 years ago
s.SendUserMsg(&pb.PlayStartResult{Result: pb.ERROR_CODE_GAME_IS_RUNNING})
2 years ago
return
}
2 years ago
s.isRunning = true
2 years ago
s.setPushActive(true)
2 years ago
s.SendUserMsg(&pb.PlayStartResult{Result: pb.ERROR_CODE_SUCCESS})
2 years ago
}
func (s *User) OnPlayEnd(msg *pb.PlayEnd) {
if !s.isRunning {
2 years ago
s.SendUserMsg(&pb.PlayEndResult{Result: pb.ERROR_CODE_GAME_IS_STOPPED})
2 years ago
return
}
2 years ago
s.isRunning = false
2 years ago
s.setPushActive(false)
2 years ago
s.SendUserMsg(&pb.PlayEndResult{Result: pb.ERROR_CODE_SUCCESS})
2 years ago
}
func (s *User) OnReport(msg *pb.Report) {
scoreMap := map[string]int32{}
for _, info := range msg.Info {
scoreMap[info.OpenId] = info.Score
}
err := redis.AddScore(s.appId, scoreMap)
if err != nil {
2 years ago
s.SendUserMsg(&pb.ReportResult{Result: pb.ERROR_CODE_FAIL})
2 years ago
return
}
2 years ago
s.SendUserMsg(&pb.ReportResult{Result: pb.ERROR_CODE_SUCCESS})
2 years ago
}
func (s *User) OnGetRank(msg *pb.GetRank) {
rankList, err := redis.GetRank(s.appId, msg.TopCount)
if err != nil {
2 years ago
s.SendUserMsg(&pb.GetRankResult{Result: pb.ERROR_CODE_FAIL})
2 years ago
return
}
var ret []*pb.ReportInfo
for _, info := range rankList {
ret = append(ret, &pb.ReportInfo{
OpenId: info.OpenId,
Score: info.Score,
})
}
2 years ago
s.SendUserMsg(&pb.GetRankResult{Result: pb.ERROR_CODE_SUCCESS, Info: ret})
2 years ago
}
func (s *User) setPushActive(isActive bool) {
if isActive {
1 year ago
s.Log("SetPushActive1", s.appId)
s.Log("SetPushActive2", s.roomId)
2 years ago
redis.Subscribe(s.appId, s.roomId, s.OnRecvPush, s.pushCloseChan)
if !s.isDebug {
s.mgr.StartTask(s.appId, s.roomId, "1")
s.mgr.StartTask(s.appId, s.roomId, "2")
s.mgr.StartTask(s.appId, s.roomId, "3")
}
2 years ago
} else {
2 years ago
s.pushCloseChan <- struct{}{}
if !s.isDebug {
s.mgr.StopTask(s.appId, s.roomId, "1")
s.mgr.StopTask(s.appId, s.roomId, "2")
s.mgr.StopTask(s.appId, s.roomId, "3")
}
2 years ago
}
}