Ejemplo n.º 1
0
        /// <summary>
        /// ゲームを初期化する
        /// </summary>
#else
        /// <summary>
        /// Initializes the game.
        /// </summary>
#endif
        void Init()
        {
            gameData            = new GameData(gameSetting);
            gameServer.GameData = gameData;
            agentNameMap.Clear();

            var agentList = gameServer.ConnectedAgentList.Shuffle();

            if (agentList.Count() != gameSetting.PlayerNum)
            {
                throw new Exception($"Player num is {gameSetting.PlayerNum} but connected agent is {agentList.Count()}.");
            }

            // 希望役職を優先して配役する
            var vacantMap           = gameSetting.RoleNumMap.Where(p => p.Value > 0).ToDictionary(p => p.Key, p => p.Value);
            var noRequestAgentQueue = new Queue <Agent>();

            foreach (var agent in agentList)
            {
                var requestedRole = gameServer.RequestRequestRole(agent);
                if (vacantMap.ContainsKey(requestedRole))
                {
                    if (vacantMap[requestedRole] > 0)
                    {
                        gameData.AddAgent(agent, Status.ALIVE, requestedRole);
                        vacantMap[requestedRole]--;
                    }
                    else
                    {
                        noRequestAgentQueue.Enqueue(agent);
                    }
                }
                else
                {
                    noRequestAgentQueue.Enqueue(agent);
                }
            }

            foreach (var pair in vacantMap.Where(p => p.Value > 0))
            {
                for (var i = 0; i < pair.Value; i++)
                {
                    gameData.AddAgent(noRequestAgentQueue.Dequeue(), Status.ALIVE, pair.Key);
                }
            }

            gameServer.GameSetting = gameSetting;
            foreach (var agent in agentList)
            {
                gameServer.Init(agent);
                agentNameMap[agent] = gameServer.RequestName(agent);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialize Game
        /// </summary>
        public void Init()
        {
            gameDataMap  = new Dictionary <int, GameData>();
            gameData     = new GameData(gameSetting);
            agentNameMap = new Dictionary <Agent, string>();
            gameServer.SetGameData(gameData);

            List <Agent> agentList = gameServer.ConnectedAgentList;

            if (agentList.Count != gameSetting.PlayerNum)
            {
                throw new IllegalPlayerNumException("Player num is " + gameSetting.PlayerNum + " but connected agent is " + agentList.Count);
            }

            agentList = agentList.Shuffle().ToList();

            Dictionary <Role, List <Agent> > requestRoleMap = new Dictionary <Role, List <Agent> >();

            foreach (Role role in Enum.GetValues(typeof(Role)))
            {
                requestRoleMap[role] = new List <Agent>();
            }
            List <Agent> noRequestAgentList = new List <Agent>();

            foreach (Agent agent in agentList)
            {
                Role?requestedRole = gameServer.RequestRequestRole(agent);
                if (requestedRole != null)
                {
                    if (requestRoleMap[(Role)requestedRole].Count < gameSetting.RoleNumMap[(Role)requestedRole])
                    {
                        requestRoleMap[(Role)requestedRole].Add(agent);
                    }
                    else
                    {
                        noRequestAgentList.Add(agent);
                    }
                }
                else
                {
                    noRequestAgentList.Add(agent);
                    //Console.WriteLine(agent + " request no role");
                }
            }

            foreach (Role role in Enum.GetValues(typeof(Role)))
            {
                List <Agent> requestedAgentList = requestRoleMap[role];
                for (int i = 0; i < gameSetting.RoleNumMap[role]; i++)
                {
                    if (requestedAgentList.Count == 0)
                    {
                        gameData.AddAgent(noRequestAgentList[0], Status.ALIVE, role);
                        noRequestAgentList.RemoveAt(0);
                    }
                    else
                    {
                        gameData.AddAgent(requestedAgentList[0], Status.ALIVE, role);
                        requestedAgentList.RemoveAt(0);
                    }
                }
            }

            gameDataMap[gameData.Day] = gameData;

            gameServer.SetGameData(gameData);
            gameServer.SetGameSetting(gameSetting);
            foreach (Agent agent in agentList)
            {
                gameServer.Init(agent);
                agentNameMap[agent] = gameServer.RequestName(agent);
            }
        }