| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652 |
- package proto
- import (
- "bet24.com/log"
- "bet24.com/servers/micros/common"
- status_client "bet24.com/servers/micros/privateroom/status/client"
- status_server "bet24.com/servers/micros/privateroom/status/server"
- "context"
- "fmt"
- "github.com/smallnest/rpcx/client"
- "os"
- "time"
- )
- const ServiceName = "privateroom"
- var consulAddr = common.Default_Consul_Addr
- func getClient() client.XClient {
- return common.GetClientPool().GetClient(ServiceName, consulAddr)
- }
- type Request struct {
- Name string
- UserId int
- GameId int
- GameName string
- Addr string
- TableId int
- RoomNo int
- Status int
- YyfUid int
- RuleName string
- RuleDesc string
- RuleData string
- Online int
- Target int
- UserCount int
- Fee int
- Prize int
- RoomType string
- PlayTimeout int
- TableUserCount int
- NickName string
- FaceId int
- FaceUrl string
- ChairId int
- Score int
- BaseScore int
- SetCount int
- Winners []int
- SecAfter int
- IsPublic bool
- ExtraInfo string
- }
- type Response struct {
- Data string
- RoomNo int
- ErrorMsg string
- Status int
- Success bool
- RetCode int
- User *RoomUser
- Fee int
- Prize int
- }
- func SetConsulAddr(addr string) {
- consulAddr = addr
- }
- func SayHello(name string) string {
- xclient := getClient()
- //defer xclient.Close()
- args := &Request{
- Name: name,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "SayHello", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return ""
- }
- log.Debug("SayHello return %s", reply.Data)
- return reply.Data
- }
- func CreatePrivateRoomByGameServer(userId int, gameId int, gameName string, addr string, tableId int) (int, string) {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- GameId: gameId,
- GameName: gameName,
- Addr: addr,
- TableId: tableId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "CreatePrivateRoomByGameServer", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return 0, "server error"
- }
- return reply.RoomNo, reply.ErrorMsg
- }
- func GetPrivateRoomInfo(roomNo int) string {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetPrivateRoomInfo", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return ""
- }
- return reply.Data
- }
- func GetUserRooms(userId int) string {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetUserRooms", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- return ""
- }
- return reply.Data
- }
- func SetRoomStatus(roomNo, status int) {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- Status: status,
- }
- err := xclient.Call(context.Background(), "SetRoomStatus", args, nil)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- }
- }
- func GetRoomStatus(roomNo int) int {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetRoomStatus", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return 0
- }
- return reply.Status
- }
- func RegisterGameRule(addr string, gameId int, gameName string, ruleName, ruleDesc, ruleData string) {
- //log.Debug("RegisterGameRule %s", addr)
- xclient := getClient()
- args := &Request{
- Addr: addr,
- GameId: gameId,
- GameName: gameName,
- RuleName: ruleName,
- RuleDesc: ruleDesc,
- RuleData: ruleData,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "RegisterGameRule", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- }
- }
- func UpdateServerOnline(addr string, online int) {
- xclient := getClient()
- args := &Request{
- Addr: addr,
- Online: online,
- }
- err := xclient.Call(context.Background(), "UpdateServerOnline", args, nil)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- }
- }
- func UnregisterServer(addr string) {
- xclient := getClient()
- args := &Request{
- Addr: addr,
- }
- err := xclient.Call(context.Background(), "UnregisterServer", args, nil)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- }
- }
- func GetPrivateRoomRules(gameId int) string {
- xclient := getClient()
- args := &Request{
- GameId: gameId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetPrivateRoomRules", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return ""
- }
- return reply.Data
- }
- func CreatePrivateRoomByUser(userId int, gameId int, ruleName string, target int, userCount int, fee int,
- prize int, roomType string, playTimeout int, isPublic bool, extraInfo string,yyfUid int) (int, string) {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- GameId: gameId,
- RuleName: ruleName,
- Target: target,
- UserCount: userCount,
- Fee: fee,
- Prize: prize,
- RoomType: roomType,
- PlayTimeout: playTimeout,
- IsPublic: isPublic,
- ExtraInfo: extraInfo,
- YyfUid: yyfUid,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "CreatePrivateRoomByUser", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return 0, "server error"
- }
- return reply.RoomNo, reply.ErrorMsg
- }
- func ClosePrivateRoom(roomNo int) bool {
- if roomNo == 0 {
- return false
- }
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "ClosePrivateRoom", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return false
- }
- return reply.Success
- }
- func DismissPrivateRoom(roomNo int) bool {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "DismissPrivatRoom", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return false
- }
- return reply.Success
- }
- func UserRequestSit(roomNo int, userId int, nickName string, faceId int, faceUrl string, chairId int, score, baseScore, setCount int,yyfUid int) string {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- UserId: userId,
- NickName: nickName,
- FaceId: faceId,
- FaceUrl: faceUrl,
- ChairId: chairId,
- Score: score,
- BaseScore: baseScore,
- SetCount: setCount,
- YyfUid: yyfUid,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "UserRequestSit", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- return "server error"
- }
- return reply.Data
- }
- func UserSit(roomNo int, userId int, chairId int) int {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- UserId: userId,
- ChairId: chairId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "UserSit", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return -1
- }
- return reply.RetCode
- }
- func UserChangeChair(roomNo int, userId int, chairId int) bool {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- UserId: userId,
- ChairId: chairId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "UserChangeChair", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return false
- }
- return reply.Success
- }
- func UserLeave(roomNo int, userId int) bool {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "UserLeave", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return false
- }
- return reply.Success
- }
- func UpdateUserGameScore(roomNo, userId, scoreDelta int) {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- UserId: userId,
- Score: scoreDelta,
- }
- err := xclient.Call(context.Background(), "UpdateUserGameScore", args, nil)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- }
- }
- func SetWinners(roomNo int, winners []int) {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- Winners: winners,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "SetWinners", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- }
- }
- func GetHistory(userId int) string {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetHistory", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return ""
- }
- return reply.Data
- }
- func GetPlayingRoomNo(userId int) string {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetPlayingRoomNo", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return ""
- }
- return reply.Data
- }
- func IsGameRuleValid(gameId int, gameRule string, target int, tableUserCount int, playTime int) bool {
- xclient := getClient()
- args := &Request{
- GameId: gameId,
- RuleName: gameRule,
- Target: target,
- TableUserCount: tableUserCount,
- PlayTimeout: playTime,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "IsGameRuleValid", args, reply)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- return false
- }
- return reply.Success
- }
- func ForceUserEnter(userId int, roomNo int, chairId int, secAfter int) {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- UserId: userId,
- ChairId: chairId,
- SecAfter: secAfter,
- }
- err := xclient.Call(context.Background(), "ForceUserEnter", args, nil)
- if err != nil {
- common.GetClientPool().RemoveClient(ServiceName)
- log.Debug("privateroom failed to call: %v", err)
- }
- }
- func GetPrivateRoomsByGameId(gameId int, userId int) string {
- xclient := getClient()
- args := &Request{
- GameId: gameId,
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetPrivateRoomsByGameId", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return ""
- }
- return reply.Data
- }
- func RegisterServerStatus(receiver status_client.IPrivateServerStatusReceiver, roomstatus_receiver IPrivateRoomStatusReceiver, port int, moduleName string) {
- if port == 0 {
- port = os.Getpid()
- }
- addr := fmt.Sprintf("%s_%d_%s", common.GetLocalIp(), port, moduleName)
- //log.Debug("privateroom.RegisterServerStatus %s", addr)
- go status_server.Run(addr, consulAddr, receiver, roomstatus_receiver)
- for {
- xclient := getClient()
- args := &Request{
- Addr: addr,
- }
- //log.Release("notification.Subscribe %s", addr)
- err := xclient.Call(context.Background(), "RegisterServerStatus", args, nil)
- if err != nil {
- log.Debug("Subscribe failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- }
- time.Sleep(10 * time.Second)
- }
- }
- func UserSubscribeRoomStatus(userId int) {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "UserSubscribeRoomStatus", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- }
- }
- func UserDesubscribeRoomStatus(userId int) {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- }
- err := xclient.Call(context.Background(), "UserDesubscribeRoomStatus", args, nil)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- }
- }
- func GetCallList() string {
- xclient := getClient()
- args := &Request{}
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetCallList", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return ""
- }
- return reply.Data
- }
- func PrivateRoomCall(userId int, roomNo int) {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- RoomNo: roomNo,
- }
- err := xclient.Call(context.Background(), "PrivateRoomCall", args, nil)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- }
- }
- func GetUserRoomInfo(userId int, roomNo int) *RoomUser {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- RoomNo: roomNo,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetUserRoomInfo", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return nil
- }
- return reply.User
- }
- func GetRoomType(roomNo int) string {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetRoomType", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return ""
- }
- return reply.Data
- }
- func GetFeeAndPrize(roomNo int, userId int) (int, int) {
- xclient := getClient()
- args := &Request{
- RoomNo: roomNo,
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetUserFeeAndPrize", args, reply)
- if err != nil {
- log.Debug("privateroom failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return 0, 0
- }
- return reply.Fee, reply.Prize
- }
|