package main

import (
	"mongo.games.com/game/proto"
	"mongo.games.com/game/protocol/gamehall"
	"mongo.games.com/game/protocol/server"
)

type PlatformGameHall struct {
	HallId     int32               //游戏厅id
	Scenes     map[int]*Scene      //游戏房间列表
	Players    map[int32]*Player   //大厅中的玩家
	p          *Platform           //所属平台
	dbGameFree *server.DB_GameFree //厅配置数据(这里都是模板值,非后台实例数据)
	dbGameRule *server.DB_GameRule //游戏配置数据(这里都是模板值,非后台实例数据)
}

func (pgh *PlatformGameHall) PlayerLeave(p *Player) {
	delete(pgh.Players, p.SnId)
	if p.hallId == pgh.HallId {
		p.hallId = 0
	}
}

func (p *Player) CreateRoomPlayerInfoProtocol() *gamehall.RoomPlayerInfo {
	pack := &gamehall.RoomPlayerInfo{
		SnId:        proto.Int32(p.SnId),
		Head:        proto.Int32(p.Head),
		Sex:         proto.Int32(p.Sex),
		Name:        proto.String(p.Name),
		Pos:         proto.Int(p.pos),
		Flag:        proto.Int32(p.flag),
		HeadOutLine: proto.Int32(p.HeadOutLine),
		VIP:         proto.Int32(p.VIP),
	}
	return pack
}

func (pgh *PlatformGameHall) OnPlayerEnterScene(scene *Scene, player *Player) {
	delete(pgh.Players, player.SnId)
	pack := &gamehall.SCRoomPlayerEnter{
		RoomId: proto.Int(scene.sceneId),
		Player: player.CreateRoomPlayerInfoProtocol(),
	}
	proto.SetDefaults(pack)
	pgh.Broadcast(int(gamehall.GameHallPacketID_PACKET_SC_ROOMPLAYERENTER), pack, player.SnId)
}

func (pgh *PlatformGameHall) OnPlayerLeaveScene(scene *Scene, player *Player) {
	pack := &gamehall.SCRoomPlayerLeave{
		RoomId: proto.Int(scene.sceneId),
		Pos:    proto.Int(player.pos),
	}
	proto.SetDefaults(pack)
	pgh.Broadcast(int(gamehall.GameHallPacketID_PACKET_SC_ROOMPLAYERLEAVE), pack, player.SnId)
}

func (pgh *PlatformGameHall) OnDestroyScene(scene *Scene) {
	delete(pgh.Scenes, scene.sceneId)
	pack := &gamehall.SCDestroyRoom{
		RoomId:    proto.Int(scene.sceneId),
		OpRetCode: gamehall.OpResultCode_Game_OPRC_Sucess_Game,
		IsForce:   proto.Int(1),
	}
	proto.SetDefaults(pack)
	pgh.Broadcast(int(gamehall.GameHallPacketID_PACKET_SC_DESTROYROOM), pack, 0)
}

func (pgh *PlatformGameHall) Broadcast(packetid int, packet interface{}, exclude int32) {
	PlatformMgrSingleton.Broadcast(packetid, packet, pgh.Players, exclude)
}
