194 lines
5.2 KiB
Go
194 lines
5.2 KiB
Go
package model
|
|
|
|
import (
|
|
"errors"
|
|
"rocommon/util"
|
|
)
|
|
|
|
const (
|
|
FRUIT_ROOM_NUM = 0
|
|
LIVE_CARD_ROOM_NUM = 1
|
|
LIVE_SELL_ROOM_NUM = 1
|
|
LIVE_ANSWER_ROOM_NUM = 1
|
|
LIVE_GAME_ROOM_NUM = 1
|
|
LIVE_BID_ROOM_NUM = 1
|
|
LIVEROOM_UPDATE_DELTA_MS = 100
|
|
)
|
|
|
|
type RoomManager struct {
|
|
mapRouterNode string
|
|
socialNode string
|
|
liveRoomUpdateTimer util.ServerTimer
|
|
updateTimer util.ServerTimer
|
|
deletePlayerTimer util.ServerTimer
|
|
roomCount uint32
|
|
fruitGameRooms []*FruitGameRoom
|
|
players map[uint64]*RoomPlayer
|
|
liveRooms []LiveRoomInterface
|
|
}
|
|
|
|
func newRoomManager() *RoomManager {
|
|
fruitManager := &RoomManager{
|
|
liveRoomUpdateTimer: util.NewDurationTimer(util.GetTimeMilliseconds(), LIVEROOM_UPDATE_DELTA_MS),
|
|
updateTimer: util.NewDurationTimer(util.GetTimeMilliseconds(), 1000),
|
|
deletePlayerTimer: util.NewDurationTimer(util.GetTimeMilliseconds(), 60*1000),
|
|
roomCount: 0,
|
|
players: make(map[uint64]*RoomPlayer),
|
|
fruitGameRooms: make([]*FruitGameRoom, 0),
|
|
liveRooms: make([]LiveRoomInterface, 0),
|
|
}
|
|
fruitManager.Init()
|
|
return fruitManager
|
|
}
|
|
|
|
func (self *RoomManager) Init() {
|
|
for i := 0; i < FRUIT_ROOM_NUM; i++ {
|
|
self.AllocFruitRoom()
|
|
}
|
|
for i := 0; i < LIVE_CARD_ROOM_NUM; i++ {
|
|
self.AllocLiveCardRoom()
|
|
}
|
|
for i := 0; i < LIVE_SELL_ROOM_NUM; i++ {
|
|
self.AllocLiveSellRoom()
|
|
}
|
|
for i := 0; i < LIVE_ANSWER_ROOM_NUM; i++ {
|
|
self.AllocLiveAnswerRoom()
|
|
}
|
|
for i := 0; i < LIVE_GAME_ROOM_NUM; i++ {
|
|
self.AllocLiveGameRoom()
|
|
}
|
|
for i := 0; i < LIVE_BID_ROOM_NUM; i++ {
|
|
self.AllocBidGameRoom()
|
|
}
|
|
}
|
|
|
|
func (self *RoomManager) AllocRoomId() uint32 {
|
|
self.roomCount += 1
|
|
return self.roomCount
|
|
}
|
|
|
|
func (self *RoomManager) AllocFruitRoom() uint32 {
|
|
roomId := self.AllocRoomId()
|
|
fruitGameRoom := newGameFruitRoom(roomId)
|
|
self.fruitGameRooms = append(self.fruitGameRooms, fruitGameRoom)
|
|
return roomId
|
|
}
|
|
|
|
func (self *RoomManager) GetFruitGameRoom(uid uint64) *FruitGameRoom {
|
|
return self.fruitGameRooms[0]
|
|
}
|
|
|
|
func (self *RoomManager) AllocLiveCardRoom() uint32 {
|
|
roomId := self.AllocRoomId()
|
|
room := newLiveCardRoom(roomId, LIVEROOM_TICK_INTERVAL)
|
|
self.liveRooms = append(self.liveRooms, room)
|
|
return roomId
|
|
}
|
|
|
|
func (self *RoomManager) AllocLiveSellRoom() uint32 {
|
|
roomId := self.AllocRoomId()
|
|
room := newLiveSellRoom(roomId, LIVEROOM_TICK_INTERVAL)
|
|
self.liveRooms = append(self.liveRooms, room)
|
|
return roomId
|
|
}
|
|
|
|
func (self *RoomManager) AllocLiveAnswerRoom() uint32 {
|
|
roomId := self.AllocRoomId()
|
|
room := newLiveAnswerRoom(roomId, LIVEROOM_TICK_INTERVAL)
|
|
self.liveRooms = append(self.liveRooms, room)
|
|
return roomId
|
|
}
|
|
|
|
func (self *RoomManager) AllocLiveGameRoom() uint32 {
|
|
roomId := self.AllocRoomId()
|
|
room := newLiveGameRoom(roomId, LIVEROOM_TICK_INTERVAL)
|
|
self.liveRooms = append(self.liveRooms, room)
|
|
return roomId
|
|
}
|
|
|
|
func (self *RoomManager) AllocBidGameRoom() uint32 {
|
|
roomId := self.AllocRoomId()
|
|
room := newLiveBidRoom(roomId, LIVEROOM_TICK_INTERVAL)
|
|
self.liveRooms = append(self.liveRooms, room)
|
|
return roomId
|
|
}
|
|
|
|
func (self *RoomManager) FindLiveRoom(uid uint64, liveType uint32) (LiveRoomInterface, error) {
|
|
for _, liveRoom := range self.liveRooms {
|
|
if liveRoom.RoomType() == liveType && liveRoom.HasPlayer(uid) {
|
|
return liveRoom, nil
|
|
}
|
|
}
|
|
for _, liveRoom := range self.liveRooms {
|
|
if liveRoom.RoomType() == liveType && liveRoom.IsFree(uid) {
|
|
return liveRoom, nil
|
|
}
|
|
}
|
|
return nil, errors.New("not find live room")
|
|
}
|
|
|
|
func (self *RoomManager) GetLiveRoomByUid(uid uint64) (LiveRoomInterface, error) {
|
|
for _, liveRoom := range self.liveRooms {
|
|
if liveRoom.HasPlayer(uid) {
|
|
return liveRoom, nil
|
|
}
|
|
}
|
|
return nil, errors.New("not find live room")
|
|
}
|
|
|
|
func (self *RoomManager) GetLiveRoomByRoomId(roomId uint32) (LiveRoomInterface, error) {
|
|
for _, liveRoom := range self.liveRooms {
|
|
if liveRoom.RoomId() == roomId {
|
|
return liveRoom, nil
|
|
}
|
|
}
|
|
return nil, errors.New("not find live room")
|
|
}
|
|
|
|
func (self *RoomManager) AddPlayer(uid uint64, name string) *RoomPlayer {
|
|
_, exist := self.players[uid]
|
|
if exist == false {
|
|
self.players[uid] = newRoomPlayer(uid, name)
|
|
}
|
|
self.players[uid].Online = false
|
|
self.players[uid].WaitLeaveRoom = false
|
|
self.players[uid].LeaveRoom = false
|
|
util.InfoF("AddPlayer: %d", uid)
|
|
return self.players[uid]
|
|
}
|
|
|
|
func (self *RoomManager) RemovePlayer(uid uint64) {
|
|
util.InfoF("RemovePlayer: %d", uid)
|
|
delete(self.players, uid)
|
|
}
|
|
|
|
func (self *RoomManager) GetPlayer(uid uint64) *RoomPlayer {
|
|
return self.players[uid]
|
|
}
|
|
|
|
func (self *RoomManager) Update(ms uint64) {
|
|
if self.updateTimer.IsStart() && self.updateTimer.IsExpired(ms) {
|
|
for _, room := range self.fruitGameRooms {
|
|
room.Update()
|
|
}
|
|
}
|
|
|
|
if self.liveRoomUpdateTimer.IsStart() && self.liveRoomUpdateTimer.IsExpired(ms) {
|
|
for _, room := range self.liveRooms {
|
|
room.Update(LIVEROOM_UPDATE_DELTA_MS)
|
|
}
|
|
}
|
|
|
|
if self.deletePlayerTimer.IsStart() && self.deletePlayerTimer.IsExpired(ms) {
|
|
deletePlayerUidList := make([]uint64, 0)
|
|
for _, player := range self.players {
|
|
if player.LeaveRoom == true && player.Online == false {
|
|
deletePlayerUidList = append(deletePlayerUidList, player.Uid)
|
|
}
|
|
}
|
|
for i := 0; i < len(deletePlayerUidList); i++ {
|
|
self.RemovePlayer(deletePlayerUidList[i])
|
|
}
|
|
}
|
|
}
|