# #! /usr/bin/env python
# # _*_ coding: utf-8 _*_
# """
# author:     Chen XuHui
# date:       2023/12/26
# file:       multiroom_manger.py
# version:
# doc:        重要, 多房间管理.
#
# YLserve.exe可以开启多个房间room, 每个房间都有: 房间name,ip和port参数
# """
# import os
#
# import numpy as np
#
# from engine.zl_engine.connection import server
# from engine.zl_engine.connection.yl_pb2 import RoomList
#
#
# # server.delete_room('Room_5')
# class MultiroomManager:
#     """
#     多房间管理
#     """
#     ip_address: str = os.getenv('ip_address', '127.0.0.1')  # 相当于 YLserve.exe 的 ip
#     port: int = 20000  # 相当于 YLserve.exe 的 port
#     trainroom_pre: str = 'train_'  # 训练用房间的名字前缀
#
#     def __init__(self, need_num=3):
#         """
#
#         :param need_num: int, 需要的训练房间数
#         """
#         self.need_num: int = need_num
#
#         # 初始化
#         self.room_ids = set()  # 所有房间的id
#         self.room_names = set()  # 所有房间的name
#         self.room_infos = []  # 所有房间的(id, name)
#         self.train_rooms = []  # list[room], 训练房间的列表
#
#         # 检查训练房间并创建
#         self.check_train_room(need_num=need_num)  # 更新上述类属性
#         pass
#
#     @property
#     def room_list(self):
#         """查找所有房间"""
#         # try:
#         room_list: RoomList = server.RoomManger.getroomlist(self.ip_address, self.port)
#         if room_list.message != 'OK':
#             raise ValueError(f'{self.ip_address}:{self.port}下查找房间失败, 请检查')
#         rooms = room_list.rooms  # 可以认为是list[objecet]
#         return rooms
#
#     @property
#     def train_room(self):
#         """查找目前可供训练的房间"""
#         rooms = self.room_list
#
#         # 重置
#         train_room_num: int = 0  # 用于训练的房间数量
#         self.room_ids = set()
#         self.room_names = set()
#         self.room_infos = []
#         self.train_rooms = []
#         for room in rooms:
#             room_id: str = room.id
#             room_name: str = room.name
#             self.room_ids.add(room_id)
#             self.room_names.add(room_name)
#             self.room_infos.append([room_id, room_name])
#             if self.trainroom_pre in room_name:
#                 train_room_num += 1
#                 self.train_rooms.append(room)
#         return train_room_num
#
#     def check_train_room(self, need_num=None):
#         """
#         检查训练房间
#         :param need_num: 需要的训练房间数
#         :return:
#         """
#         if need_num is None:
#             need_num = self.need_num
#
#         train_room_num: int = self.train_room
#         unenough_num: int = 0 if need_num <= train_room_num else need_num - train_room_num  # 缺少的训练房间数量
#         for _ in range(unenough_num):  # 生成新的训练房间
#             index: int = 1
#             while f'{self.trainroom_pre}{index}' in self.room_names:
#                 index += 1
#             new_room_name: str = f'{self.trainroom_pre}{index}'
#             new_room_id = server.RoomManger.creat_room_by_name(new_room_name)
#             self.room_ids.add(new_room_id)
#             self.room_names.add(new_room_name)
#
#     @property
#     def server_infos(self):
#         server_infos = {}
#         for i, room in enumerate(sorted(self.train_rooms)):  # room: object
#             room_id = room.id
#             room_name = room.name
#             roomserver = server.RoomManger.getroom(room_id)
#             server_infos[i + 1]: dict = {
#                 "room_id": room_id,
#                 "room_name": room_name,
#                 "code_execute": "0",
#                 "server_path": "D:rojectSCEerver",
#                 "server_ip": roomserver.appip,  # str
#                 "server_port": str(roomserver.appport),  # int -> str
#                 "simulate_ip": roomserver.appip,
#                 "simulate_port": "20001",
#                 "dds_ip": roomserver.ddsip,  # str
#                 "dds_port": str(roomserver.ddsport),  # int -> str
#                 "continuous": "1",
#                 "service_ip": roomserver.appip,
#                 "plan52_ip": roomserver.appip,  # plan52服务的ip
#                 "plan52_port": 6000 + int(i)  # plan52服务的port
#             }  # dict[worker_index, server_info]
#         return server_infos
#
#
# if __name__ == '__main__':
#     np.set_printoptions(suppress=True, threshold=np.inf)
#     room_manger = MultiroomManager(3)
#     pass
