github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/game/internal/user_room.go (about)

     1  package internal
     2  
     3  import (
     4  	"common"
     5  	"conf"
     6  	"game/poker"
     7  	"msg"
     8  	"time"
     9  
    10  	"github.com/name5566/leaf/log"
    11  )
    12  
    13  func (user *User) checkRoomMinChips(minChips int64, create bool) bool {
    14  	if minChips > user.baseData.userData.Chips {
    15  		//机器人需要对其金币进行修改
    16  		if user.isRobot() {
    17  			user.baseData.userData.Chips += minChips
    18  			return true
    19  		}
    20  		if create {
    21  			user.WriteMsg(&msg.S2C_CreateRoom{
    22  				Error:    msg.S2C_CreateRoom_LackOfChips,
    23  				MinChips: minChips,
    24  			})
    25  		} else {
    26  			user.WriteMsg(&msg.S2C_EnterRoom{
    27  				Error:    msg.S2C_EnterRoom_LackOfChips,
    28  				MinChips: minChips,
    29  			})
    30  		}
    31  		return false
    32  	}
    33  	return true
    34  }
    35  
    36  func (user *User) checkRoomMaxChips(maxChips int64, create bool) bool {
    37  	if user.baseData.userData.Chips > maxChips {
    38  		if user.isRobot() {
    39  			user.baseData.userData.Chips = maxChips
    40  			return true
    41  		}
    42  		if create {
    43  			user.WriteMsg(&msg.S2C_CreateRoom{
    44  				Error:    msg.S2C_CreateRoom_MaxChipsLimit,
    45  				MaxChips: maxChips,
    46  			})
    47  		} else {
    48  			user.WriteMsg(&msg.S2C_EnterRoom{
    49  				Error:    msg.S2C_EnterRoom_MaxChipsLimit,
    50  				MaxChips: maxChips,
    51  			})
    52  		}
    53  		return false
    54  	}
    55  	return true
    56  }
    57  
    58  func (user *User) createLandlordRoom(rule *poker.LandlordRule) {
    59  	roomNumber := getRoomNumber()
    60  	if _, ok := roomNumberRooms[roomNumber]; ok {
    61  		user.WriteMsg(&msg.S2C_CreateRoom{Error: msg.S2C_CreateRoom_InnerError})
    62  		return
    63  	}
    64  	room := newLandlordRoom(rule)
    65  	log.Debug("userID: %v 创建斗地主房间: %v, 类型: %v, 底分: %v, 红包: %v", user.baseData.userData.UserID, roomNumber, rule.RoomType, rule.BaseScore, rule.RedPacketType)
    66  	room.number = roomNumber
    67  	roomNumberRooms[roomNumber] = room
    68  	room.ownerUserID = user.baseData.userData.UserID
    69  	room.creatorUserID = user.baseData.userData.UserID
    70  	user.enterRoom(room)
    71  }
    72  
    73  func (user *User) createOrEnterPracticeRoom() {
    74  	for _, r := range roomNumberRooms {
    75  		room := r.(*LandlordRoom)
    76  		if room.rule.RoomType == roomPractice && !room.full() {
    77  			user.enterRoom(r)
    78  			return
    79  		}
    80  	}
    81  	rule := &poker.LandlordRule{
    82  		RoomType:   roomPractice,
    83  		MaxPlayers: 3,
    84  	}
    85  	user.createLandlordRoom(rule)
    86  }
    87  
    88  func (user *User) createOrEnterBaseScoreMatchingRoom(baseScore int) {
    89  	/*
    90  		minChips := int64(baseScore) * 10
    91  		switch baseScore {
    92  		case 500:
    93  			minChips = 1000
    94  		}
    95  		if !user.checkRoomMinChips(minChips, false) {
    96  			return
    97  		}
    98  		var tickets int64
    99  		switch baseScore {
   100  		case 500:
   101  			if !user.checkRoomMaxChips(60000, true) {
   102  				return
   103  			}
   104  			tickets = 240
   105  		case 3000:
   106  			tickets = 1800
   107  		case 10000:
   108  			tickets = 3600
   109  		default:
   110  			user.WriteMsg(&msg.S2C_CreateRoom{Error: msg.S2C_CreateRoom_RuleError})
   111  			return
   112  		}
   113  	*/
   114  	data := getMatch(baseScore)
   115  	/*
   116  	   检查入场最低金币数量
   117  	*/
   118  	if !user.checkRoomMinChips(int64(data.BaseScore), false) {
   119  		return
   120  	}
   121  	/*
   122  	   检查最高入场金币数量
   123  	*/
   124  	if !user.checkRoomMaxChips(int64(data.MaxScore), false) {
   125  		return
   126  	}
   127  	//优先进入真实玩家场
   128  	if !user.isRobot() {
   129  		if user.enterBaseScoreMatchingRoom(baseScore, 2, true) {
   130  			return
   131  		}
   132  		if user.enterBaseScoreMatchingRoom(baseScore, 1, true) {
   133  			return
   134  		}
   135  	}
   136  	//后续加入机器人场
   137  	if user.isRobot() {
   138  		if user.enterBaseScoreMatchingRoom(baseScore, 2, false) {
   139  			return
   140  		}
   141  		if user.enterBaseScoreMatchingRoom(baseScore, 1, false) {
   142  			return
   143  		}
   144  	}
   145  	tickets := data.Tickets
   146  	minChips := data.MinScore
   147  	if user.isRobot() { // 只进入房间不创建房间
   148  		user.WriteMsg(&msg.S2C_EnterRoom{Error: msg.S2C_EnterRoom_Unknown})
   149  		return
   150  	}
   151  	rule := &poker.LandlordRule{
   152  		RoomType:   roomBaseScoreMatching,
   153  		MaxPlayers: 3,
   154  		BaseScore:  baseScore,
   155  		MinChips:   int64(minChips),
   156  		Tickets:    int64(tickets),
   157  	}
   158  	user.createLandlordRoom(rule)
   159  }
   160  
   161  func (user *User) createBasePrivateRoom(baseScore int) {
   162  	minChips := int64(baseScore) * 10
   163  	if !user.checkRoomMinChips(minChips, true) {
   164  		return
   165  	}
   166  	var tickets int64
   167  	switch baseScore {
   168  	case 30000:
   169  		tickets = 4500
   170  	case 50000:
   171  		tickets = 6000
   172  	case 100000:
   173  		tickets = 8500
   174  	default:
   175  		user.WriteMsg(&msg.S2C_CreateRoom{Error: msg.S2C_CreateRoom_RuleError})
   176  		return
   177  	}
   178  	rule := &poker.LandlordRule{
   179  		RoomType:   roomBaseScorePrivate,
   180  		MaxPlayers: 3,
   181  		BaseScore:  baseScore,
   182  		Tickets:    tickets,
   183  		MinChips:   minChips,
   184  	}
   185  	user.createLandlordRoom(rule)
   186  }
   187  
   188  func (user *User) createVIPPrivateRoom(maxPlayers int) {
   189  	if common.Index([]int{2, 3}, maxPlayers) == -1 {
   190  		user.WriteMsg(&msg.S2C_CreateRoom{Error: msg.S2C_CreateRoom_RuleError})
   191  		return
   192  	}
   193  	var minChips int64 = 10000
   194  	if !user.checkRoomMinChips(minChips, true) {
   195  		return
   196  	}
   197  	rule := &poker.LandlordRule{
   198  		RoomType:   roomVIPPrivate,
   199  		MaxPlayers: maxPlayers,
   200  		MinChips:   minChips,
   201  	}
   202  	user.createLandlordRoom(rule)
   203  }
   204  
   205  func (user *User) createOrEnterRedPacketMatchingRoom(redPacketType int) {
   206  	if common.Index([]int{1, 10}, redPacketType) == -1 {
   207  		user.WriteMsg(&msg.S2C_CreateRoom{Error: msg.S2C_CreateRoom_RuleError})
   208  		return
   209  	}
   210  	if redPacketType == 1 {
   211  		if time.Now().Hour() < conf.GetOneRedpacketInfo().Start || time.Now().Hour() > conf.GetOneRedpacketInfo().End {
   212  			user.WriteMsg(&msg.S2C_EnterRoom{Error: msg.S2C_EnterRoom_NotRightNow})
   213  			return
   214  		}
   215  	}
   216  	if redPacketType == 10 {
   217  		if time.Now().Hour() < conf.GetTenRedpacketInfo().Start || time.Now().Hour() > conf.GetTenRedpacketInfo().End {
   218  			user.WriteMsg(&msg.S2C_EnterRoom{Error: msg.S2C_EnterRoom_NotRightNow})
   219  			return
   220  		}
   221  	}
   222  
   223  	var minChips int64
   224  	switch redPacketType {
   225  	case 10:
   226  		minChips = conf.GetTenRedpacketInfo().Chips
   227  	default:
   228  		minChips = conf.GetOneRedpacketInfo().Chips
   229  	}
   230  
   231  	if !user.checkRoomMinChips(minChips, false) {
   232  		return
   233  	}
   234  	//真实玩家优先跟真实玩家一起打红包赛
   235  	if !user.isRobot() {
   236  		if user.enterRedPacketMatchingRoom(redPacketType, 2, true) {
   237  			return
   238  		}
   239  		if user.enterRedPacketMatchingRoom(redPacketType, 1, true) {
   240  			return
   241  		}
   242  	}
   243  	if user.isRobot() {
   244  		if user.enterRedPacketMatchingRoom(redPacketType, 2, false) {
   245  			return
   246  		}
   247  		if user.enterRedPacketMatchingRoom(redPacketType, 1, false) {
   248  			return
   249  		}
   250  	}
   251  	if user.isRobot() { // 只进入房间不创建房间
   252  		user.WriteMsg(&msg.S2C_EnterRoom{Error: msg.S2C_EnterRoom_Unknown})
   253  		return
   254  	}
   255  	rule := &poker.LandlordRule{
   256  		RoomType:      roomRedPacketMatching,
   257  		MaxPlayers:    3,
   258  		RedPacketType: redPacketType,
   259  		MinChips:      minChips,
   260  		BaseScore:     int(minChips),
   261  	}
   262  	user.createLandlordRoom(rule)
   263  }
   264  
   265  func (user *User) createRedPacketPrivateRoom(redPacketType int) {
   266  	if time.Now().Hour() < conf.GetHundredRedpacketInfo().Start || time.Now().Hour() > conf.GetHundredRedpacketInfo().End {
   267  		user.WriteMsg(&msg.S2C_EnterRoom{Error: msg.S2C_EnterRoom_NotRightNow})
   268  		return
   269  	}
   270  	var minChips int64
   271  	switch redPacketType {
   272  	case 100:
   273  		minChips = conf.GetHundredRedpacketInfo().Chips
   274  	case 999:
   275  		minChips = 498 * 10000
   276  	default:
   277  		user.WriteMsg(&msg.S2C_CreateRoom{Error: msg.S2C_CreateRoom_RuleError})
   278  		return
   279  	}
   280  	if !user.checkRoomMinChips(minChips, true) {
   281  		return
   282  	}
   283  	rule := &poker.LandlordRule{
   284  		RoomType:      roomRedPacketPrivate,
   285  		MaxPlayers:    3,
   286  		RedPacketType: redPacketType,
   287  		MinChips:      minChips,
   288  	}
   289  	user.createLandlordRoom(rule)
   290  }
   291  
   292  /*
   293  
   294  //如果房间里面有一个真实玩家,可以先加入一个机器人(房间最多只能有一个机器人)
   295  		lable := false
   296  		if room.RealPlayer() == 1 && user.isRobot() {
   297  			lable = true
   298  		}
   299  
   300  		//如果房间里面有两个
   301  		if room.RealPlayer() != 2 && user.isRobot() && !room.RealPlayer() == 1 {
   302  
   303  		}
   304  
   305  */
   306  func (user *User) enterBaseScoreMatchingRoom(baseScore int, playerNumber int, real bool) bool {
   307  	for _, r := range roomNumberRooms {
   308  		room := r.(*LandlordRoom)
   309  		if real {
   310  			if !conf.Server.Model {
   311  				if room.rule.RoomType == roomBaseScoreMatching && room.rule.BaseScore == baseScore && room.RealPlayer() == playerNumber && !room.full() {
   312  					user.enterRoom(r)
   313  					return true
   314  				}
   315  			}
   316  			if !room.loginIPs[user.baseData.userData.LoginIP] && room.rule.RoomType == roomBaseScoreMatching && room.rule.BaseScore == baseScore && len(room.positionUserIDs) == playerNumber {
   317  
   318  				if !room.playTogether(user) {
   319  					user.enterRoom(r)
   320  					return true
   321  				}
   322  			}
   323  		} else {
   324  			if !conf.Server.Model {
   325  				if room.RealPlayer() == 1 && len(room.positionUserIDs) == 1 && user.isRobot() || !user.isRobot() || room.RealPlayer() == 2 {
   326  					if room.rule.RoomType == roomBaseScoreMatching && room.rule.BaseScore == baseScore && len(room.positionUserIDs) == playerNumber && !room.full() {
   327  						user.enterRoom(r)
   328  						return true
   329  					}
   330  				}
   331  			}
   332  			if !room.loginIPs[user.baseData.userData.LoginIP] && room.RealPlayer() == 1 && len(room.positionUserIDs) == 1 && user.isRobot() || !user.isRobot() || room.RealPlayer() == 2 {
   333  				if room.rule.RoomType == roomBaseScoreMatching && room.rule.BaseScore == baseScore && len(room.positionUserIDs) == playerNumber && !room.full() {
   334  					if !room.playTogether(user) {
   335  						user.enterRoom(r)
   336  						return true
   337  					}
   338  				}
   339  			}
   340  		}
   341  	}
   342  	return false
   343  }
   344  
   345  func (user *User) enterRedPacketMatchingRoom(redPacketType int, playerNumber int, real bool) bool {
   346  	for _, r := range roomNumberRooms {
   347  		room := r.(*LandlordRoom)
   348  		//!room.loginIPs[user.baseData.userData.LoginIP] &&
   349  		if real {
   350  			//测试环境
   351  			if !conf.Server.Model {
   352  				if room.rule.RoomType == roomRedPacketMatching && room.rule.RedPacketType == redPacketType && room.RealPlayer() == playerNumber && !room.full() {
   353  					user.enterRoom(r)
   354  					return true
   355  				}
   356  			}
   357  			if !room.loginIPs[user.baseData.userData.LoginIP] && room.rule.RoomType == roomRedPacketMatching && room.rule.RedPacketType == redPacketType && room.RealPlayer() == playerNumber && !room.full() {
   358  				if !room.playTogether(user) {
   359  					user.enterRoom(r)
   360  				}
   361  				return true
   362  			}
   363  		} else {
   364  			if !conf.Server.Model {
   365  				if room.RealPlayer() == 1 && len(room.positionUserIDs) == 1 && user.isRobot() || !user.isRobot() || room.RealPlayer() == 2 {
   366  					if room.rule.RoomType == roomRedPacketMatching && room.rule.RedPacketType == redPacketType && len(room.positionUserIDs) == playerNumber && !room.full() {
   367  						user.enterRoom(r)
   368  						return true
   369  					}
   370  				}
   371  			}
   372  			if room.RealPlayer() == 1 && len(room.positionUserIDs) == 1 && user.isRobot() || !user.isRobot() || room.RealPlayer() == 2 {
   373  				if !room.loginIPs[user.baseData.userData.LoginIP] && room.rule.RoomType == roomRedPacketMatching && room.rule.RedPacketType == redPacketType && len(room.positionUserIDs) == playerNumber && !room.full() {
   374  					if !room.playTogether(user) {
   375  						user.enterRoom(r)
   376  						return true
   377  					}
   378  				}
   379  			}
   380  		}
   381  	}
   382  	return false
   383  }
   384  
   385  func (user *User) enterRoom(r interface{}) {
   386  	room := r.(*LandlordRoom)
   387  	sitDown := room.Enter(user)
   388  	if sitDown {
   389  		userIDRooms[user.baseData.userData.UserID] = r
   390  		user.baseData.ownerUserID = room.ownerUserID
   391  	}
   392  }
   393  
   394  func (user *User) exitRoom(r interface{}, forcible bool) {
   395  	room := r.(*LandlordRoom)
   396  	if room.state == roomGame {
   397  		if forcible {
   398  			user.WriteMsg(&msg.S2C_ExitRoom{Error: msg.S2C_ExitRoom_GamePlaying})
   399  		}
   400  	} else {
   401  		room.Exit(user)
   402  	}
   403  }
   404  
   405  func (user *User) setVIPRoomChips(r interface{}, chips int64) {
   406  	room := r.(*LandlordRoom)
   407  	if room.rule.RoomType != roomVIPPrivate {
   408  		return
   409  	}
   410  	if playerData, ok := room.userIDPlayerDatas[user.baseData.userData.UserID]; ok {
   411  		playerData.vipChips = chips
   412  		broadcast(&msg.S2C_SetVIPRoomChips{
   413  			Error:    msg.S2C_SetVIPChips_OK,
   414  			Position: playerData.position,
   415  			Chips:    playerData.vipChips,
   416  		}, room.positionUserIDs, -1)
   417  	}
   418  }
   419  func getMatch(baseScore int) conf.CfgMatch {
   420  	for _, value := range conf.GetCfgMatchs() {
   421  		if value.BaseScore == baseScore {
   422  			return value
   423  		}
   424  	}
   425  	return conf.CfgMatch{}
   426  }