user_center_func.go 9.05 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
package rpc

import (
	"encoding/json"
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/protocol/userProxy"
	"google.golang.org/protobuf/proto"
)

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

hujiebin's avatar
hujiebin committed
69
// 发送钻石变化通知
hujiebin's avatar
hujiebin committed
70 71 72 73 74 75 76 77 78 79 80
func SendDiamondChange(userId uint64, diamond uint32) error {
	msg := &userProxy.DiamondChange{
		RemainDiamond: diamond,
	}
	if buffer, err := proto.Marshal(msg); err == nil {
		rspUids, err := multicast([]uint64{userId}, MsgDiamondChange, buffer)
		//记录socket,注意闭包问题
		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)
81

hujiebin's avatar
hujiebin committed
82 83 84 85 86 87
		if err != nil {
			mylogrus.MyLog.Errorf("grpc SendDiamondChange send fail")
			return err
		} else {
			mylogrus.MyLog.Info("grpc SendDiamondChange send success")
		}
88
	} else {
hujiebin's avatar
hujiebin committed
89
		return err
90 91 92
	}
	return nil
}
hujiebin's avatar
hujiebin committed
93 94 95

// 发送游戏横幅
// param winUserId:胜利的用户id
hujiebin's avatar
hujiebin committed
96
// param gameType 0:slot 5:luckybox 6:fruit
hujiebin's avatar
hujiebin committed
97
// param gameId 7:slot 8:candy 5:luckybox 6:fruit
iamhujiebin's avatar
iamhujiebin committed
98
func SendGlobalGameBanner(winUserId uint64, diamond uint64, avatar string, gameId uint64, gameType uint32) error {
hujiebin's avatar
hujiebin committed
99 100 101 102 103 104 105 106 107
	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"
	}
hujiebin's avatar
hujiebin committed
108
	if gameId == uint64(8) {
hujiebin's avatar
hujiebin committed
109
		bannerUrl = "https://image.whoisamy.shop/hilo/resource/game/game_banner_candy.png"
hujiebin's avatar
hujiebin committed
110
	}
hujiebin's avatar
hujiebin committed
111
	msg := &userProxy.GlobalGameBanner{
iamhujiebin's avatar
iamhujiebin committed
112
		GameId:    gameId,
hujiebin's avatar
hujiebin committed
113 114 115 116 117
		GameType:  gameType,
		UserId:    winUserId,
		Avatar:    avatar,
		Diamond:   diamond,
		BannerUrl: bannerUrl,
hujiebin's avatar
hujiebin committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	}
	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
}
hujiebin's avatar
hujiebin committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

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 {
hujiebin's avatar
hujiebin committed
213
			mylogrus.MyLog.Errorf("grpc SendGroupChatNotice send fail,userId:%v", fromUserId)
hujiebin's avatar
hujiebin committed
214 215
			return err
		} else {
hujiebin's avatar
hujiebin committed
216
			mylogrus.MyLog.Infof("grpc SendGroupChatNotice send success,userId:%v", fromUserId)
hujiebin's avatar
hujiebin committed
217 218 219 220 221 222
		}
	} else {
		return err
	}
	return nil
}
hujiebin's avatar
hujiebin committed
223 224

// 羊羊匹配成功
hujiebin's avatar
hujiebin committed
225
func SendSheepMatchSuccess(matchId, myUserId, otherUserId uint64, nick1, nick2, avatar1, avatar2 string) error {
hujiebin's avatar
hujiebin committed
226
	msg := &userProxy.SheepMatchSuccess{
hujiebin's avatar
hujiebin committed
227 228 229
		MatchId:   matchId,
		User:      &userProxy.User{Id: myUserId, Nick: nick1, Avatar: avatar1},
		OtherUser: &userProxy.User{Id: otherUserId, Nick: nick2, Avatar: avatar2},
hujiebin's avatar
hujiebin committed
230 231
	}
	if buffer, err := proto.Marshal(msg); err == nil {
hujiebin's avatar
hujiebin committed
232
		userIds := []uint64{myUserId}
hujiebin's avatar
hujiebin committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
		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
}

// 羊羊游戏结果
hujiebin's avatar
hujiebin committed
254 255 256 257
func SendSheepGameResult(matchId, winId, userId1, userId2 uint64, nick1, nick2, avatar1, avatar2 string, isAiMap map[uint64]bool) error {
	if isAiMap == nil {
		isAiMap = make(map[uint64]bool)
	}
hujiebin's avatar
hujiebin committed
258 259 260 261 262 263 264
	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,
	}})
hujiebin's avatar
hujiebin committed
265 266 267 268 269 270
	if !players[0].IsWin {
		players[0], players[1] = players[1], players[0]
	}
	for i := range players {
		players[i].Rank = int32(i) + 1
	}
hujiebin's avatar
hujiebin committed
271 272 273 274 275
	msg := &userProxy.SheepGameResult{
		MatchId: matchId,
		Players: players,
	}
	if buffer, err := proto.Marshal(msg); err == nil {
hujiebin's avatar
hujiebin committed
276
		var userIds []uint64
hujiebin's avatar
hujiebin committed
277
		if !isAiMap[userId1] {
hujiebin's avatar
hujiebin committed
278 279
			userIds = append(userIds, userId1)
		}
hujiebin's avatar
hujiebin committed
280
		if !isAiMap[userId2] {
hujiebin's avatar
hujiebin committed
281 282
			userIds = append(userIds, userId2)
		}
hujiebin's avatar
hujiebin committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
		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
}