Files
MahjongServer/container/Hall.go
T
2020-03-27 12:46:19 +08:00

72 lines
1.2 KiB
Go

package container
import (
"github.com/c-my/MahjongServer/rule"
)
type Hall struct {
rooms map[int]*Room
players map[int]int
maxRoomSize int
}
var hall *Hall = nil
func GetHall() *Hall {
if hall != nil {
return hall
} else {
hall = NewHall(100)
return hall
}
}
func NewHall(maxRoomSize int) *Hall {
return &Hall{maxRoomSize: maxRoomSize, rooms: map[int]*Room{}, players: map[int]int{}}
}
func (h *Hall) CreateRoom(userID int, roomName string, passwd string) int {
for i := 0; i < h.maxRoomSize; i++ {
if !h.hasRoom(i) {
room := NewRoom(rule.NewJinzhouRule())
room.playerCount = 1
h.rooms[i] = room
h.players[userID] = i
return i
}
}
return -1
}
func (h *Hall) JoinRoom(userID int, roomID int) bool {
if !h.hasRoom(roomID) {
return false
}
room := h.rooms[roomID]
if room.playerCount >= 4 {
return false
}
h.players[userID] = roomID
room.playerCount = room.playerCount + 1
return true
}
func (h *Hall) GetRoomID(userID int) int {
roomID, ok := h.players[userID]
if !ok {
return -1
} else {
return roomID
}
}
func (h *Hall) hasRoom(roomID int) bool {
_, ok := h.rooms[roomID]
return ok
}
func (h *Hall) destroyRoom(roomID int) {
delete(h.rooms, roomID)
}