package rpc import ( "encoding/json" "git.hilo.cn/hilo-common/mylogrus" "git.hilo.cn/hilo-common/protocol/userProxy" "git.hilo.cn/hilo-common/utils" "google.golang.org/protobuf/proto" "time" ) func SendFruitMachine(date string, round uint32) error { msg := &userProxy.FruitMachine{ Date: date, Round: round, } if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := broadcast(MsgFruitMachine, buffer) //记录socket,注意闭包问题 go func(userId uint64, msg *userProxy.FruitMachine, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgFruitMachine, userId, string(buf[:]), rspUids, err) }(0, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendFruitMachine send fail") return err } else { mylogrus.MyLog.Info("grpc SendFruitMachine send success") } } else { return err } return nil } func SendFruitMachineToUser(date string, round uint32, userIds []uint64) error { msg := &userProxy.FruitMachine{ Date: date, Round: round, } if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := multicast(userIds, MsgFruitMachine, buffer) //记录socket,注意闭包问题 go func(userId uint64, msg *userProxy.FruitMachine, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgFruitMachine, userId, string(buf[:]), rspUids, err) }(0, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendFruitMachine send fail") return err } else { mylogrus.MyLog.Info("grpc SendFruitMachine send success") } } else { return err } return nil } func SendGlobalRocketNotice(groupId string, period string, round uint32, stage uint32, fromUserId uint64, topUserIcon string, nick string, code string, avatar string) error { msg := &userProxy.GlobalRocketNotice{ GroupId: groupId, Period: period, Round: round, Stage: stage, TopUserIcon: topUserIcon, Nick: nick, Code: code, Avatar: avatar, } if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := broadcast(MsgTypeGlobalRocketNotice, buffer) //记录socket,注意闭包问题 go func(userId uint64, msg *userProxy.GlobalRocketNotice, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgTypeGlobalRocketNotice, userId, string(buf[:]), rspUids, err) }(fromUserId, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc GlobalRocketNotice send fail") return err } else { mylogrus.MyLog.Info("grpc GlobalRocketNotice send success") } } else { return err } return nil } // 发送钻石变化通知 func SendDiamondChange(userId uint64, diamond, pinkDiamond uint32) error { msg := &userProxy.DiamondChange{ RemainDiamond: diamond, RemainPinkDiamond: pinkDiamond, } if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := multicast([]uint64{userId}, MsgDiamondChange, buffer) // 记录socket // 有err||uid不在线才入库 if err != nil || len(rspUids) > 0 { go func(userId uint64, msg *userProxy.DiamondChange, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgDiamondChange, userId, string(buf[:]), rspUids, err) }(userId, msg, rspUids, err) } if err != nil { mylogrus.MyLog.Errorf("grpc SendDiamondChange send fail") return err } else { mylogrus.MyLog.Info("grpc SendDiamondChange send success") } } else { return err } return nil } // 发送游戏横幅 // param winUserId:胜利的用户id // param gameType 0:slot 5:luckybox 6:fruit // param gameId 7:slot 8:candy 5:luckybox 6:fruit func SendGlobalGameBanner(winUserId uint64, diamond uint64, avatar string, gameId uint64, gameType uint32) error { defer utils.CheckGoPanic() bannerUrl := "" switch gameType { case 0: bannerUrl = "https://image.whoisamy.shop/hilo/resource/game/game_banner_slot.png" case 5: bannerUrl = "https://image.whoisamy.shop/hilo/resource/game/game_banner_luckybox.png" case 6: bannerUrl = "https://image.whoisamy.shop/hilo/resource/game/game_banner_fruit.png" } if gameId == uint64(8) { bannerUrl = "https://image.whoisamy.shop/hilo/resource/game/game_banner_candy.png" } msg := &userProxy.GlobalGameBanner{ GameId: gameId, GameType: gameType, UserId: winUserId, Avatar: avatar, Diamond: diamond, BannerUrl: bannerUrl, } if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := broadcast(MsgTypeGlobalGameBanner, buffer) //记录socket,注意闭包问题 go func(userId uint64, msg *userProxy.GlobalGameBanner, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgTypeGlobalGameBanner, userId, string(buf[:]), rspUids, err) }(winUserId, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendGlobalGameBanner send fail") return err } else { mylogrus.MyLog.Info("grpc SendGlobalGameBanner send success") } } else { return err } return nil } func SendJoinGroup(userId uint64, externalId string, groupId string) error { msg := &userProxy.JoinGroup{GroupId: groupId, ExternalId: externalId} if buffer, err := proto.Marshal(msg); err == nil { userIds := []uint64{userId} rspUids, err := multicast(userIds, MsgTypeJoinGroup, buffer) //记录socket,注意闭包问题 go func(userIds []uint64, msg *userProxy.JoinGroup, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLogs(MsgTypeJoinGroup, userIds, string(buf[:]), rspUids, err) }(userIds, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendJoinGroup send fail") return err } else { mylogrus.MyLog.Info("grpc SendJoinGroup send success") } } else { return err } return nil } func SendConfigChange(operUserId uint64, configType uint32) error { msg := &userProxy.ConfigChange{ Type: configType, } if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := broadcast(MsgTypeConfigChange, buffer) //记录socket,注意闭包问题 go func(configType uint32, msg *userProxy.ConfigChange, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgTypeConfigChange, operUserId, string(buf[:]), rspUids, err) }(configType, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendConfigChange send fail") return err } else { mylogrus.MyLog.Info("grpc SendConfigChange send success") } } else { return err } return nil } func SendGroupChatNotice(fromUserId uint64, userIds []uint64, senderExtId string, senderCode string, senderSex uint32, senderAvatar string, text string, groupId, groupName, groupCode, groupAvatar string, userInNum uint32) error { msg := &userProxy.GroupSendNotice{ SenderExtId: senderExtId, SenderCode: senderCode, SenderSex: senderSex, SenderAvatar: senderAvatar, Text: text, GroupName: groupName, GroupCode: groupCode, GroupAvatar: groupAvatar, UserInNum: userInNum, GroupId: groupId, } if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := multicast(userIds, MsgTypeGroupChatNotice, buffer) //记录socket,注意闭包问题 go func(userId uint64, msg *userProxy.GroupSendNotice, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgTypeGroupChatNotice, userId, string(buf[:]), rspUids, err) }(fromUserId, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendGroupChatNotice send fail,userId:%v", fromUserId) return err } else { mylogrus.MyLog.Infof("grpc SendGroupChatNotice send success,userId:%v", fromUserId) } } else { return err } return nil } // 羊羊匹配成功 // Deprecated: missing Params func SendSheepMatchSuccess(matchId, myUserId, otherUserId uint64, nick1, nick2, avatar1, avatar2 string, gameIds ...uint64) error { gameId := uint64(0) if len(gameIds) > 0 { gameId = gameIds[0] } msg := &userProxy.SheepMatchSuccess{ MatchId: matchId, User: &userProxy.User{Id: myUserId, Nick: nick1, Avatar: avatar1}, OtherUser: &userProxy.User{Id: otherUserId, Nick: nick2, Avatar: avatar2}, GameId: gameId, } if buffer, err := proto.Marshal(msg); err == nil { userIds := []uint64{myUserId} rspUids, err := multicast(userIds, MsgTypeSheepGameMatchSuccess, buffer) //记录socket,注意闭包问题 go func(userIds []uint64, msg *userProxy.SheepMatchSuccess, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLogs(MsgTypeSheepGameMatchSuccess, userIds, string(buf[:]), rspUids, err) }(userIds, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendSheepMatchSuccess send fail") return err } else { mylogrus.MyLog.Info("grpc SendSheepMatchSuccess send success") } } else { return err } return nil } // 羊羊匹配成功 func SendSheepMatchSuccessV2(matchId, myUserId, otherUserId uint64, nick1, nick2, avatar1, avatar2 string, gameId uint64, channelId, token string, agoraId, otherAgoraId uint32, provider int) error { msg := &userProxy.SheepMatchSuccess{ MatchId: matchId, User: &userProxy.User{Id: myUserId, Nick: nick1, Avatar: avatar1}, OtherUser: &userProxy.User{Id: otherUserId, Nick: nick2, Avatar: avatar2}, GameId: gameId, ChannelId: channelId, Token: token, AgoraId: agoraId, OtherAgoraId: otherAgoraId, Provider: uint32(provider), } if buffer, err := proto.Marshal(msg); err == nil { userIds := []uint64{myUserId} rspUids, err := multicast(userIds, MsgTypeSheepGameMatchSuccess, buffer) //记录socket,注意闭包问题 go func(userIds []uint64, msg *userProxy.SheepMatchSuccess, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLogs(MsgTypeSheepGameMatchSuccess, userIds, string(buf[:]), rspUids, err) }(userIds, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendSheepMatchSuccess send fail") return err } else { mylogrus.MyLog.Info("grpc SendSheepMatchSuccess send success") } } else { return err } return nil } // 羊羊游戏结果 func SendSheepGameResult(matchId, winId, userId1, userId2 uint64, nick1, nick2, avatar1, avatar2 string, isAiMap map[uint64]bool, gameIds ...uint64) error { if isAiMap == nil { isAiMap = make(map[uint64]bool) } var players []*userProxy.SheepGamePlayer players = append(players, &userProxy.SheepGamePlayer{IsWin: winId == userId1, User: &userProxy.User{ Id: userId1, Nick: nick1, Avatar: avatar1, }}) players = append(players, &userProxy.SheepGamePlayer{IsWin: winId == userId2, User: &userProxy.User{ Id: userId2, Nick: nick2, Avatar: avatar2, }}) if !players[0].IsWin { players[0], players[1] = players[1], players[0] } for i := range players { players[i].Rank = int32(i) + 1 } gameId := uint64(0) if len(gameIds) > 0 { gameId = gameIds[0] } msg := &userProxy.SheepGameResult{ MatchId: matchId, Players: players, GameId: gameId, } if buffer, err := proto.Marshal(msg); err == nil { var userIds []uint64 if !isAiMap[userId1] { userIds = append(userIds, userId1) } if !isAiMap[userId2] { userIds = append(userIds, userId2) } rspUids, err := multicast(userIds, MsgTypeSheepGameResult, buffer) //记录socket,注意闭包问题 go func(userIds []uint64, msg *userProxy.SheepGameResult, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLogs(MsgTypeSheepGameResult, userIds, string(buf[:]), rspUids, err) }(userIds, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendSheepGameResult send fail") return err } else { mylogrus.MyLog.Info("grpc SendSheepGameResult send success") } } else { return err } return nil } // cp邀请 func SendCpInviteNotice(userId uint64, code, name, avatar, content, extId string) error { defer utils.CheckGoPanic() msg := &userProxy.CpInvite{ User: &userProxy.User{Id: userId, Code: code, Nick: name, Avatar: avatar, ExternalId: extId}, Msg: content, } if buffer, err := proto.Marshal(msg); err == nil { userIds := []uint64{userId} rspUids, err := multicast(userIds, MsgTypeCpInvite, buffer) //记录socket,注意闭包问题 go func(userIds []uint64, msg *userProxy.CpInvite, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLogs(MsgTypeCpInvite, userIds, string(buf[:]), rspUids, err) }(userIds, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendCpInviteNotice send fail") return err } else { mylogrus.MyLog.Info("grpc SendCpInviteNotice send success") } } else { return err } return nil } // cp升级 func SendCpUpgrade(nick1, nick2, avatar1, avatar2 string, cpLevel uint32, groupId string) error { msg := &userProxy.CpUpgrade{ User1: &userProxy.User{Nick: nick1, Avatar: avatar1}, User2: &userProxy.User{Nick: nick2, Avatar: avatar2}, CpLevel: cpLevel, GroupId: groupId, } if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := broadcast(MsgTypeCpUpgrade, buffer) //记录socket,注意闭包问题 go func(userId uint64, msg *userProxy.CpUpgrade, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgTypeCpUpgrade, userId, string(buf[:]), rspUids, err) }(0, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendCpUpgrade send fail") return err } else { mylogrus.MyLog.Info("grpc SendCpUpgrade send success") } } else { return err } return nil } // svip升级 func SendSvipUpgrade(nick, avatar string, svipLevel uint32, txGroupId string) error { msg := &userProxy.SvipUpgrade{ User: &userProxy.User{Nick: nick, Avatar: avatar}, SvipLevel: svipLevel, GroupId: txGroupId, } if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := broadcast(MsgTypeSvipUpgrade, buffer) //记录socket,注意闭包问题 go func(userId uint64, msg *userProxy.SvipUpgrade, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgTypeSvipUpgrade, userId, string(buf[:]), rspUids, err) }(0, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendSvipUpgrade send fail") return err } else { mylogrus.MyLog.Info("grpc SendSvipUpgrade send success") } } else { return err } return nil } // 发送socket的麦位变化 func SendSocketMicChange(seqId string, userId uint64, micUserExternalId, txGroupId string, i uint32, lock, forbid, micForbid bool, agoraId uint32, micUserData *userProxy.MicUserData) error { var msg = &userProxy.GroupMicChange{ SeqId: seqId, GroupId: txGroupId, I: i, Lock: lock, Forbid: forbid, MicForbid: micForbid, ExternalId: micUserExternalId, AgoraId: agoraId, Timestamp: time.Now().UnixNano(), User: micUserData, } n := 0 if buffer, err := proto.Marshal(msg); err == nil { userIds := []uint64{userId} var rspUids []uint64 for { n++ rspUids, err = multicast(userIds, MsgTypeGroupMicChange, buffer, time.Duration(10)) if n >= 3 || len(rspUids) <= 0 { break } time.Sleep(time.Millisecond * 200) } // 记录socket // 有err||uid不在线才入库 if err != nil || len(rspUids) > 0 { go func(userId uint64, msg *userProxy.GroupMicChange, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgTypeGroupMicChange, userId, string(buf[:]), rspUids, err) }(userId, msg, rspUids, err) } if err != nil { mylogrus.MyLog.Errorf("grpc SendSocketMicChange send fail,seqId:%v,retry:%v,data:%v,err:%v", seqId, n, *msg, err) return err } else { mylogrus.MyLog.Infof("grpc SendSocketMicChange send success,seqId:%v,retry:%v,data:%v", seqId, n, *msg) } } else { return err } return nil } // 游戏大厅匹配成功 func SendLobbyMatchSuccess(myUserId, otherUserId uint64, nick1, nick2, avatar1, avatar2 string, gameId uint64, txGroupId string, mode int, gameCode string) error { msg := &userProxy.LobbyMatchSuccess{ GameId: gameId, GroupId: txGroupId, Mode: uint64(mode), User: &userProxy.User{Id: myUserId, Nick: nick1, Avatar: avatar1}, OtherUser: &userProxy.User{Id: otherUserId, Nick: nick2, Avatar: avatar2}, GameCode: gameCode, } if buffer, err := proto.Marshal(msg); err == nil { userIds := []uint64{myUserId} rspUids, err := multicast(userIds, MsgTypeLobbyMatchSuccess, buffer) //记录socket,注意闭包问题 go func(userIds []uint64, msg *userProxy.LobbyMatchSuccess, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLogs(MsgTypeLobbyMatchSuccess, userIds, string(buf[:]), rspUids, err) }(userIds, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc LobbyMatchSuccess send fail") return err } else { mylogrus.MyLog.Info("grpc LobbyMatchSuccess send success") } } else { return err } return nil } // h5游戏静音 func SendH5GameMute(userId uint64) error { msg := &userProxy.H5GameVoiceMute{} if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := multicast([]uint64{userId}, MsgTypeH5GameMute, buffer) //记录socket,注意闭包问题 go func(userId uint64, msg *userProxy.H5GameVoiceMute, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgTypeH5GameMute, userId, string(buf[:]), rspUids, err) }(userId, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendH5GameMute send fail") return err } else { mylogrus.MyLog.Info("grpc SendH5GameMute send success") } } else { return err } return nil } // h5游戏开音 func SendH5GameUnMute(userId uint64) error { msg := &userProxy.H5GameVoiceUnMute{} if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := multicast([]uint64{userId}, MsgTypeH5GameUnMute, buffer) //记录socket,注意闭包问题 go func(userId uint64, msg *userProxy.H5GameVoiceUnMute, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgTypeH5GameUnMute, userId, string(buf[:]), rspUids, err) }(userId, msg, rspUids, err) if err != nil { mylogrus.MyLog.Errorf("grpc SendH5GameUnMute send fail") return err } else { mylogrus.MyLog.Info("grpc SendH5GameUnMute send success") } } else { return err } return nil } // 发送退房通知 原因1.被拉黑;2.被踢出 func SendQuitRoom(userId uint64, reason uint32, txGroupId string) error { msg := &userProxy.QuitRoom{Reason: reason, GroupId: txGroupId} if buffer, err := proto.Marshal(msg); err == nil { rspUids, err := multicast([]uint64{userId}, MsgTypeQuitRoom, buffer) // 记录socket // 有err||uid不在线才入库 if err != nil || len(rspUids) > 0 { go func(userId uint64, msg *userProxy.QuitRoom, rspUids []uint64, err error) { buf, _ := json.Marshal(msg) AddRpcLog(MsgTypeQuitRoom, userId, string(buf[:]), rspUids, err) }(userId, msg, rspUids, err) } if err != nil { mylogrus.MyLog.Errorf("grpc SendQuitRoom send fail") return err } else { mylogrus.MyLog.Info("grpc SendQuitRoom send success") } } else { return err } return nil }