娃娃机状态

This commit is contained in:
by 2024-08-13 14:13:01 +08:00
parent b8765251cc
commit 1795fdedfa
6 changed files with 177 additions and 68 deletions

View File

@ -11,9 +11,10 @@ var MachineMap = make(map[int]*DollMachine)
var MachineMapLock = sync.Mutex{}
type DollMachine struct {
Id int
Status bool
VideoAddr string
Id int
MachineStatus int32 //娃娃机链接状态 0:离线 1:在线
Status bool //是否空闲
VideoAddr string
}
func MSDollMachineList(session *netlib.Session, packetId int, data interface{}) error {
@ -22,9 +23,10 @@ func MSDollMachineList(session *netlib.Session, packetId int, data interface{})
if msg, ok := data.(*machine.MSDollMachineList); ok {
for i, info := range msg.Data {
MachineMap[i+1] = &DollMachine{
Id: i + 1,
Status: false,
VideoAddr: info.VideoAddr,
Id: i + 1,
Status: false,
VideoAddr: info.VideoAddr,
MachineStatus: 1,
}
logger.Logger.Tracef("MachineMap[%v] = %v", i, info.VideoAddr)
}
@ -46,6 +48,21 @@ func GetFreeDollMachineId() int {
return 0
}
// 获取指定娃娃机链接状态
func GetDollMachineStatus(id int) int32 {
return MachineMap[id].MachineStatus
}
func MSUpdateDollMachineStatusHandler(session *netlib.Session, packetId int, data interface{}) error {
logger.Logger.Tracef("MSUpdateDollMachineStatusHandler %v", data)
if msg, ok := data.(*machine.MSUpdateDollMachineStatus); ok {
MachineMap[int(msg.Id)].MachineStatus = msg.Status
logger.Logger.Tracef("更新娃娃机连接状态 id = %d,status= %d", msg.Id, msg.GetStatus())
}
return nil
}
func init() {
netlib.Register(int(machine.DollMachinePacketID_PACKET_MSDollMachineList), &machine.MSDollMachineList{}, MSDollMachineList)
//更新娃娃机链接状态
netlib.Register(int(machine.DollMachinePacketID_PACKET_MSUpdateDollMachineStatus), &machine.MSUpdateDollMachineStatus{}, MSUpdateDollMachineStatusHandler)
}

View File

@ -42,11 +42,12 @@ type SceneEx struct {
PlayerBackup map[int32]*PlayerData // 本局离场玩家数据备份
seats []*PlayerEx // 本局游戏中的玩家状态数据
RoundId int // 局数,第几局
robotNum int // 参与游戏的机器人数量
logid string
machineId int //娃娃机ID
machineConn *netlib.Session //娃娃机链接
RoundId int // 局数,第几局
robotNum int // 参与游戏的机器人数量
logid string
machineId int //娃娃机ID
machineConn *netlib.Session //娃娃机链接
machineStatus int32 //娃娃机链接状态 0:离线 1:在线
}
// 游戏是否能开始

View File

@ -68,6 +68,23 @@ func (this *PolicyClawdoll) OnTick(s *base.Scene) {
if sceneEx.machineId == 0 {
sceneEx.machineId = action.GetFreeDollMachineId()
}
if sceneEx.machineId != 0 {
machineStatus := action.GetDollMachineStatus(sceneEx.machineId)
if machineStatus == 0 {
//链接状态不可用 踢出所有玩家
for _, p := range sceneEx.players {
sceneEx.delPlayer(p.Player)
}
sceneEx.machineStatus = 0
logger.Logger.Trace("娃娃机离线,当前场景暂停服务!")
//通知客户单房间不可用
} else {
if sceneEx.machineStatus == 0 {
sceneEx.machineStatus = machineStatus
//通知客户端房间可用
}
}
}
}
}
@ -76,9 +93,7 @@ func (this *PolicyClawdoll) OnPlayerEnter(s *base.Scene, p *base.Player) {
if s == nil || p == nil {
return
}
logger.Logger.Trace("(this *PolicyClawdoll) OnPlayerEnter, sceneId=", s.GetSceneId(), " player=", p.SnId)
if sceneEx, ok := s.ExtraData.(*SceneEx); ok {
pos := -1
for i := 0; i < sceneEx.GetPlayerNum(); i++ {

View File

@ -89,7 +89,7 @@ func (this *MachineManager) StartHeartbeat(id int, conn *net.Conn, addr string)
delete(ConnMap, id)
this.DelConnMap[id] = addr
//通知游戏服
this.UpdateToGameServer()
this.UpdateToGameServer(id, 0)
fmt.Println("删除链接addr = ", addr)
go timer.StartTimer(timer.TimerActionWrapper(func(h timer.TimerHandle, ud interface{}) bool {
this.ReConnect()
@ -105,7 +105,6 @@ func (this *MachineManager) StartHeartbeat(id int, conn *net.Conn, addr string)
func (this *MachineManager) ReConnect() bool {
fmt.Println("================重连============")
delIds := []int{}
status := false
for id, addr := range this.DelConnMap {
conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
if err != nil {
@ -113,28 +112,20 @@ func (this *MachineManager) ReConnect() bool {
}
ConnMap[id] = conn
delIds = append(delIds, id)
status = true
this.UpdateToGameServer(id, 1)
}
for _, id := range delIds {
delete(this.DelConnMap, id)
fmt.Println("重新链接成功id = ", id)
}
if status {
this.UpdateToGameServer()
return true
}
return false
}
func (this *MachineManager) UpdateToGameServer() {
msg := &machine.MSDollMachineList{}
for i, _ := range ConnMap {
info := &machine.DollMachine{}
info.Id = int32(i)
info.VideoAddr = "www.baidu.com"
msg.Data = append(msg.Data, info)
}
SendToGameServer(int(machine.DollMachinePacketID_PACKET_MSDollMachineList), msg)
func (this *MachineManager) UpdateToGameServer(id int, status int32) {
msg := &machine.MSUpdateDollMachineStatus{}
msg.Status = status
msg.Id = int32(id)
SendToGameServer(int(machine.DollMachinePacketID_PACKET_MSUpdateDollMachineStatus), msg)
}
func SendToGameServer(pid int, msg interface{}) {

View File

@ -24,12 +24,13 @@ const (
type DollMachinePacketID int32
const (
DollMachinePacketID_PACKET_SMDollMachineZero DollMachinePacketID = 0
DollMachinePacketID_PACKET_SMGameLinkSucceed DollMachinePacketID = 20000
DollMachinePacketID_PACKET_SMDollMachinePerate DollMachinePacketID = 20001
DollMachinePacketID_PACKET_SMDollMachineGrab DollMachinePacketID = 20002
DollMachinePacketID_PACKET_MSDollMachineGrab DollMachinePacketID = 20003
DollMachinePacketID_PACKET_MSDollMachineList DollMachinePacketID = 20004
DollMachinePacketID_PACKET_SMDollMachineZero DollMachinePacketID = 0
DollMachinePacketID_PACKET_SMGameLinkSucceed DollMachinePacketID = 20000
DollMachinePacketID_PACKET_SMDollMachinePerate DollMachinePacketID = 20001
DollMachinePacketID_PACKET_SMDollMachineGrab DollMachinePacketID = 20002
DollMachinePacketID_PACKET_MSDollMachineGrab DollMachinePacketID = 20003
DollMachinePacketID_PACKET_MSDollMachineList DollMachinePacketID = 20004
DollMachinePacketID_PACKET_MSUpdateDollMachineStatus DollMachinePacketID = 20005
)
// Enum value maps for DollMachinePacketID.
@ -41,14 +42,16 @@ var (
20002: "PACKET_SMDollMachineGrab",
20003: "PACKET_MSDollMachineGrab",
20004: "PACKET_MSDollMachineList",
20005: "PACKET_MSUpdateDollMachineStatus",
}
DollMachinePacketID_value = map[string]int32{
"PACKET_SMDollMachineZero": 0,
"PACKET_SMGameLinkSucceed": 20000,
"PACKET_SMDollMachinePerate": 20001,
"PACKET_SMDollMachineGrab": 20002,
"PACKET_MSDollMachineGrab": 20003,
"PACKET_MSDollMachineList": 20004,
"PACKET_SMDollMachineZero": 0,
"PACKET_SMGameLinkSucceed": 20000,
"PACKET_SMDollMachinePerate": 20001,
"PACKET_SMDollMachineGrab": 20002,
"PACKET_MSDollMachineGrab": 20003,
"PACKET_MSDollMachineList": 20004,
"PACKET_MSUpdateDollMachineStatus": 20005,
}
)
@ -413,6 +416,62 @@ func (x *DollMachine) GetVideoAddr() string {
return ""
}
//更新娃娃机状态
type MSUpdateDollMachineStatus struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"`
Status int32 `protobuf:"varint,2,opt,name=Status,proto3" json:"Status,omitempty"` //1-空闲 0-无法使用
}
func (x *MSUpdateDollMachineStatus) Reset() {
*x = MSUpdateDollMachineStatus{}
if protoimpl.UnsafeEnabled {
mi := &file_machine_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *MSUpdateDollMachineStatus) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*MSUpdateDollMachineStatus) ProtoMessage() {}
func (x *MSUpdateDollMachineStatus) ProtoReflect() protoreflect.Message {
mi := &file_machine_proto_msgTypes[6]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use MSUpdateDollMachineStatus.ProtoReflect.Descriptor instead.
func (*MSUpdateDollMachineStatus) Descriptor() ([]byte, []int) {
return file_machine_proto_rawDescGZIP(), []int{6}
}
func (x *MSUpdateDollMachineStatus) GetId() int32 {
if x != nil {
return x.Id
}
return 0
}
func (x *MSUpdateDollMachineStatus) GetStatus() int32 {
if x != nil {
return x.Status
}
return 0
}
var File_machine_proto protoreflect.FileDescriptor
var file_machine_proto_rawDesc = []byte{
@ -441,24 +500,31 @@ var file_machine_proto_rawDesc = []byte{
0x74, 0x61, 0x22, 0x3b, 0x0a, 0x0b, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e,
0x65, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49,
0x64, 0x12, 0x1c, 0x0a, 0x09, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x41, 0x64, 0x64, 0x72, 0x18, 0x02,
0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x41, 0x64, 0x64, 0x72, 0x2a,
0xd5, 0x01, 0x0a, 0x13, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x50,
0x61, 0x63, 0x6b, 0x65, 0x74, 0x49, 0x44, 0x12, 0x1c, 0x0a, 0x18, 0x50, 0x41, 0x43, 0x4b, 0x45,
0x54, 0x5f, 0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x5a,
0x65, 0x72, 0x6f, 0x10, 0x00, 0x12, 0x1e, 0x0a, 0x18, 0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f,
0x53, 0x4d, 0x47, 0x61, 0x6d, 0x65, 0x4c, 0x69, 0x6e, 0x6b, 0x53, 0x75, 0x63, 0x63, 0x65, 0x65,
0x64, 0x10, 0xa0, 0x9c, 0x01, 0x12, 0x20, 0x0a, 0x1a, 0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f,
0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x50, 0x65, 0x72,
0x61, 0x74, 0x65, 0x10, 0xa1, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18, 0x50, 0x41, 0x43, 0x4b, 0x45,
0x54, 0x5f, 0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x47,
0x72, 0x61, 0x62, 0x10, 0xa2, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18, 0x50, 0x41, 0x43, 0x4b, 0x45,
0x54, 0x5f, 0x4d, 0x53, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x47,
0x72, 0x61, 0x62, 0x10, 0xa3, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18, 0x50, 0x41, 0x43, 0x4b, 0x45,
0x54, 0x5f, 0x4d, 0x53, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x4c,
0x69, 0x73, 0x74, 0x10, 0xa4, 0x9c, 0x01, 0x42, 0x27, 0x5a, 0x25, 0x6d, 0x6f, 0x6e, 0x67, 0x6f,
0x2e, 0x67, 0x61, 0x6d, 0x65, 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x61, 0x6d, 0x65, 0x2f,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x2f, 0x6d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x41, 0x64, 0x64, 0x72, 0x22,
0x43, 0x0a, 0x19, 0x4d, 0x53, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x44, 0x6f, 0x6c, 0x6c, 0x4d,
0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x0e, 0x0a, 0x02,
0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06,
0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x53, 0x74,
0x61, 0x74, 0x75, 0x73, 0x2a, 0xfd, 0x01, 0x0a, 0x13, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63,
0x68, 0x69, 0x6e, 0x65, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x49, 0x44, 0x12, 0x1c, 0x0a, 0x18,
0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63,
0x68, 0x69, 0x6e, 0x65, 0x5a, 0x65, 0x72, 0x6f, 0x10, 0x00, 0x12, 0x1e, 0x0a, 0x18, 0x50, 0x41,
0x43, 0x4b, 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x47, 0x61, 0x6d, 0x65, 0x4c, 0x69, 0x6e, 0x6b, 0x53,
0x75, 0x63, 0x63, 0x65, 0x65, 0x64, 0x10, 0xa0, 0x9c, 0x01, 0x12, 0x20, 0x0a, 0x1a, 0x50, 0x41,
0x43, 0x4b, 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69,
0x6e, 0x65, 0x50, 0x65, 0x72, 0x61, 0x74, 0x65, 0x10, 0xa1, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18,
0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63,
0x68, 0x69, 0x6e, 0x65, 0x47, 0x72, 0x61, 0x62, 0x10, 0xa2, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18,
0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x4d, 0x53, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63,
0x68, 0x69, 0x6e, 0x65, 0x47, 0x72, 0x61, 0x62, 0x10, 0xa3, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18,
0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x4d, 0x53, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63,
0x68, 0x69, 0x6e, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x10, 0xa4, 0x9c, 0x01, 0x12, 0x26, 0x0a, 0x20,
0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x4d, 0x53, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x44,
0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73,
0x10, 0xa5, 0x9c, 0x01, 0x42, 0x27, 0x5a, 0x25, 0x6d, 0x6f, 0x6e, 0x67, 0x6f, 0x2e, 0x67, 0x61,
0x6d, 0x65, 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x61, 0x6d, 0x65, 0x2f, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x2f, 0x6d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x62, 0x06, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -474,15 +540,16 @@ func file_machine_proto_rawDescGZIP() []byte {
}
var file_machine_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_machine_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
var file_machine_proto_msgTypes = make([]protoimpl.MessageInfo, 7)
var file_machine_proto_goTypes = []interface{}{
(DollMachinePacketID)(0), // 0: machine.DollMachinePacketID
(*SMGameLinkSucceed)(nil), // 1: machine.SMGameLinkSucceed
(*SMDollMachineoPerate)(nil), // 2: machine.SMDollMachineoPerate
(*SMDollMachineGrab)(nil), // 3: machine.SMDollMachineGrab
(*MSDollMachineGrab)(nil), // 4: machine.MSDollMachineGrab
(*MSDollMachineList)(nil), // 5: machine.MSDollMachineList
(*DollMachine)(nil), // 6: machine.DollMachine
(DollMachinePacketID)(0), // 0: machine.DollMachinePacketID
(*SMGameLinkSucceed)(nil), // 1: machine.SMGameLinkSucceed
(*SMDollMachineoPerate)(nil), // 2: machine.SMDollMachineoPerate
(*SMDollMachineGrab)(nil), // 3: machine.SMDollMachineGrab
(*MSDollMachineGrab)(nil), // 4: machine.MSDollMachineGrab
(*MSDollMachineList)(nil), // 5: machine.MSDollMachineList
(*DollMachine)(nil), // 6: machine.DollMachine
(*MSUpdateDollMachineStatus)(nil), // 7: machine.MSUpdateDollMachineStatus
}
var file_machine_proto_depIdxs = []int32{
6, // 0: machine.MSDollMachineList.data:type_name -> machine.DollMachine
@ -571,6 +638,18 @@ func file_machine_proto_init() {
return nil
}
}
file_machine_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*MSUpdateDollMachineStatus); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
@ -578,7 +657,7 @@ func file_machine_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_machine_proto_rawDesc,
NumEnums: 1,
NumMessages: 6,
NumMessages: 7,
NumExtensions: 0,
NumServices: 0,
},

View File

@ -12,6 +12,7 @@ enum DollMachinePacketID {
PACKET_SMDollMachineGrab = 20002;
PACKET_MSDollMachineGrab = 20003;
PACKET_MSDollMachineList = 20004;
PACKET_MSUpdateDollMachineStatus = 20005;
}
//
message SMGameLinkSucceed{
@ -45,4 +46,9 @@ message MSDollMachineList{
message DollMachine{
int32 Id = 1;
string VideoAddr = 2;
}
//
message MSUpdateDollMachineStatus{
int32 Id = 1;
int32 Status = 2; //1- 0-使
}