private bool RemoveUnitController(bool unitsChanged, IMatchUnitController unitController)
        {
            if (m_idToUnit.ContainsKey(unitController.Id))
            {
                if (VoxelData.IsControllableUnit(unitController.DataController.ControlledData.Type))
                {
                    m_controllableUnitsCount--;
                }

                MatchFactory.DestroyUnitController(unitController);
                m_idToUnit.Remove(unitController.Id);
                unitsChanged = true;

                if (unitController.Assignment != null)
                {
                    AssignmentsController.RemoveAssignment(unitController, null);
                }

                if (unitController.TargetForAssignments != null)
                {
                    AssignmentsController.RemoveTargetFromAssignments(unitController, null);
                }

                if (UnitRemoved != null)
                {
                    UnitRemoved(unitController);
                }
            }

            return(unitsChanged);
        }
        private void OnDestroy()
        {
            m_serializer = null;
            if (m_gState != null)
            {
                m_gState.SetValue("LocalGameServer.m_replay", null);
            }

            if (m_players != null)
            {
                m_players.Remove(m_neutralPlayer.Id);
            }

            if (m_room != null)
            {
                m_room.Players.Remove(m_neutralPlayer.Id);
            }

            if (m_bots != null)
            {
                for (int i = 0; i < m_bots.Length; ++i)
                {
                    IBotController bot = m_bots[i];
                    if (bot != null)
                    {
                        MatchFactory.DestroyBotController(bot);
                    }
                }
            }

            if (m_engine != null)
            {
                MatchFactory.DestroyMatchEngine(m_engine);
            }
        }
Пример #3
0
        public void Destroy()
        {
            for (int i = 0; i < m_players.Length; ++i)
            {
                IMatchPlayerController player = m_players[i];

                ITaskEngine taskEngine = m_taskEngines[i];
                taskEngine.TaskStateChanged -= OnTaskStateChanged;
                taskEngine.ClientRequest    -= OnClientRequest;

                MatchFactory.DestroyTaskEngine(taskEngine);
                MatchFactory.DestroyPathFinder(m_pathFinders[i]);
                MatchFactory.DestroyTaskRunner(m_taskRunners[i]);
            }
        }
Пример #4
0
        public void RegisterPlayer(Guid playerId, int playerIndex, Dictionary <int, VoxelAbilities>[] allAbilities)
        {
            IMatchPlayerController player = MatchFactory.CreatePlayerController(this, playerIndex, allAbilities);

            m_idToPlayers.Add(playerId, player);
            m_players[playerIndex] = player;

            if (m_serverCommands.Commands == null)
            {
                m_serverCommands.Commands = new CommandsArray[1];
            }
            else
            {
                Array.Resize(ref m_serverCommands.Commands, m_serverCommands.Commands.Length + 1);
            }
        }
Пример #5
0
        public void Destroy()
        {
            enabled = false;

            int index = m_room.Players.IndexOf(m_neutralPlayer.Id);

            if (index >= 0)
            {
                m_room.Players.RemoveAt(index);
                m_clientIds.RemoveAt(index);
            }

            if (m_engine != null)
            {
                m_engine.OnSubmitted -= OnEngineCommandSubmitted;
                MatchFactory.DestroyMatchEngine(m_engine);
                m_engine = null;
            }
        }
        private void CreateUnitController(VoxelData voxelData, Coordinate coordinate)
        {
            voxelData.UnitOrAssetIndex = m_identity;
            IMatchUnitController unit = MatchFactory.CreateUnitController(m_engine, coordinate, voxelData.Type, m_playerIndex, m_allAbilities);

            m_idToUnit.Add(m_identity, unit);

            if (VoxelData.IsControllableUnit(unit.DataController.ControlledData.Type))
            {
                m_controllableUnitsCount++;
            }

            unchecked
            {
                m_identity++;
            }

            if (UnitCreated != null)
            {
                UnitCreated(unit);
            }
        }
Пример #7
0
        public MatchEngine(MapRoot map, int playersCount)
        {
            m_serializer = new ProtobufSerializer();
            m_map        = map;

            m_players = new IMatchPlayerController[playersCount];
            m_map.SetPlayerCount(playersCount);

            m_taskEngines = new ITaskEngine[playersCount];
            m_pathFinders = new IPathFinder[playersCount];
            m_taskRunners = new ITaskRunner[playersCount];
            for (int i = 0; i < playersCount; ++i)
            {
                IPathFinder pathFinder = MatchFactory.CreatePathFinder(m_map);
                ITaskRunner taskRunner = MatchFactory.CreateTaskRunner();
                m_pathFinders[i] = pathFinder;
                m_taskRunners[i] = taskRunner;

                ITaskEngine taskEngine = MatchFactory.CreateTaskEngine(this, taskRunner, pathFinder);
                taskEngine.TaskStateChanged += OnTaskStateChanged;
                taskEngine.ClientRequest    += OnClientRequest;
                m_taskEngines[i]             = taskEngine;
            }
        }
        private void InitEngine(Guid clientId, ServerEventHandler callback)
        {
            if (m_engine != null)
            {
                MatchFactory.DestroyMatchEngine(m_engine);
            }

            m_engine = null;

            DownloadMapData(m_room.MapInfo.Id, (error, mapData) =>
            {
                if (HasError(error))
                {
                    if (callback != null)
                    {
                        callback(error);
                    }
                }
                else
                {
                    m_job.Submit(() =>
                    {
                        ProtobufSerializer serializer = null;
                        MapRoot mapRoot = null;
                        try
                        {
                            var pool = Dependencies.Serializer;
                            if (pool != null)
                            {
                                serializer = pool.Acquire();
                                mapRoot    = serializer.Deserialize <MapRoot>(mapData.Bytes);
                            }
                        }
                        finally
                        {
                            if (serializer != null)
                            {
                                var pool = Dependencies.Serializer;
                                if (pool != null)
                                {
                                    pool.Release(serializer);
                                }
                            }
                        }

                        return(mapRoot);
                    },
                                 result =>
                    {
                        MapRoot mapRoot = (MapRoot)result;

                        IMatchEngine engine = MatchFactory.CreateMatchEngine(mapRoot, m_room.Players.Count);

                        Dictionary <int, VoxelAbilities>[] allAbilities = new Dictionary <int, VoxelAbilities> [m_room.Players.Count];
                        for (int i = 0; i < m_room.Players.Count; ++i)
                        {
                            allAbilities[i] = m_abilities[m_room.Players[i]].ToDictionary(a => a.Type);
                        }

                        if (m_replay == null)
                        {
                            m_replay = MatchFactory.CreateReplayRecorder();
                        }

                        //Zero is neutral
                        for (int i = 0; i < m_room.Players.Count; ++i)
                        {
                            Guid playerGuid = m_room.Players[i];
                            engine.RegisterPlayer(m_room.Players[i], i, allAbilities);
                        }
                        engine.CompletePlayerRegistration();

                        m_prevTickTime = Time.realtimeSinceStartup;
                        m_engine       = engine;

                        if (callback != null)
                        {
                            callback(error);
                        }

                        m_pingTimer.Ping(clientId);

                        if (Ping != null)
                        {
                            Ping(new Error(StatusCode.OK), new RTTInfo());
                        }
                    });
                }
            });
        }
        private void OnPong(Error error)
        {
            //Currently MatchEngine will be launched immediately and it does not care about different RTT for diffierent clients.
            //Some clients will look -50 ms to the past and some clients will look -500 ms or more to the past.
            //Is this a big deal? Don't know... Further investigation and playtest needed

            m_room = m_gState.GetValue <Room>("LocalGameServer.m_room");

            Player[] players;
            VoxelAbilitiesArray[]          abilities;
            SerializedTaskArray[]          serializedTasks;
            SerializedTaskTemplatesArray[] serializedTaskTemplates;
            AssignmentGroupArray[]         assignmentGroups;
            if (m_room != null)
            {
                error.Code = StatusCode.OK;
                players    = new Player[m_room.Players.Count];

                List <IBotController> bots = new List <IBotController>();

                //Will override or
                abilities               = new VoxelAbilitiesArray[m_room.Players.Count];
                serializedTasks         = new SerializedTaskArray[m_room.Players.Count];
                serializedTaskTemplates = new SerializedTaskTemplatesArray[m_room.Players.Count];
                assignmentGroups        = new AssignmentGroupArray[m_room.Players.Count];
                for (int i = 0; i < m_room.Players.Count; ++i)
                {
                    Player player = m_players[m_room.Players[i]];

                    players[i]                 = player;
                    abilities[i]               = m_abilities[m_room.Players[i]];
                    serializedTasks[i]         = m_tasks[m_room.Players[i]];
                    serializedTaskTemplates[i] = m_serializedTaskTemplates[m_room.Players[i]];
                    assignmentGroups[i]        = new AssignmentGroupArray
                    {
                        Groups = new AssignmentGroup[0]
                    };
                    if (player.IsActiveBot)
                    {
                        bots.Add(MatchFactory.CreateBotController(player, m_engine.GetTaskEngine(i), new DefaultStrategy()));
                    }
                }

                m_bots = bots.ToArray();
            }
            else
            {
                error.Code              = StatusCode.NotFound;
                error.Message           = "Room not found";
                players                 = new Player[0];
                abilities               = new VoxelAbilitiesArray[0];
                serializedTasks         = new SerializedTaskArray[0];
                serializedTaskTemplates = new SerializedTaskTemplatesArray[0];
                assignmentGroups        = new AssignmentGroupArray[0];
            }

            RaiseReadyToPlayAll(error, players, abilities, serializedTasks, serializedTaskTemplates, assignmentGroups);
            for (int i = 0; i < players.Length; ++i)
            {
                if (players[i].IsActiveBot)
                {
                    m_engine.GrantBotCtrl(i);
                    break;
                }
            }
        }
Пример #10
0
        public void Activate()
        {
            if (Dependencies.RemoteGameServer != null && Dependencies.RemoteGameServer.IsConnected)
            {
                return;
            }

            if (m_gState == null)
            {
                m_gState = Dependencies.State;
            }
            if (m_job == null)
            {
                m_job = Dependencies.Job;
            }
            //m_persistentDataPath = Application.persistentDataPath;
            m_persistentDataPath = Application.streamingAssetsPath;

            m_loggedInPlayers = m_gState.GetValue <HashSet <Guid> >("LocalGameServer.m_loggedInPlayers");
            if (m_loggedInPlayers == null)
            {
                m_loggedInPlayers = new HashSet <Guid>();
            }

            m_room = m_gState.GetValue <Room>("LocalGameServer.m_room");
            if (m_room == null)
            {
                m_room         = new Room();
                m_room.Players = new List <Guid>();
                m_room.MapInfo = new MapInfo
                {
                    Name           = "Default",
                    MaxPlayers     = GameConstants.MaxPlayers,
                    SupportedModes = GameMode.All
                };
            }

            m_players = m_gState.GetValue <Dictionary <Guid, Player> >("LocalGameServer.m_players");
            if (m_players == null)
            {
                m_players = new Dictionary <Guid, Player>();
            }

            if (!m_players.ContainsKey(m_neutralPlayer.Id))
            {
                m_players.Add(m_neutralPlayer.Id, m_neutralPlayer);
            }

            if (!m_room.Players.Contains(m_neutralPlayer.Id))
            {
                m_room.Players.Insert(0, m_neutralPlayer.Id);
            }

            m_abilities = new Dictionary <Guid, VoxelAbilities[]>();
            m_tasks     = new Dictionary <Guid, SerializedTask[]>();
            m_serializedTaskTemplates = new Dictionary <Guid, SerializedNamedTaskLaunchInfo[]>();
            for (int i = 0; i < m_room.Players.Count; ++i)
            {
                m_abilities.Add(m_room.Players[i], CreateDefaultAbilities());
                m_tasks.Add(m_room.Players[i], CreateDefaultTaskTemplates());
                m_serializedTaskTemplates.Add(m_room.Players[i], CreateDefaultTaskTemplateData());
            }

            Guid[] clientIds = new[] { Guid.Empty };
            m_pingTimer = new PingTimer(clientIds, 3);

            ReplayData replay = m_gState.GetValue <ReplayData>("LocalGameServer.m_replay");

            if (replay != null)
            {
                m_replay = MatchFactory.CreateReplayPlayer();
                m_replay.Load(replay);
            }
            Connect();
        }
Пример #11
0
        GetReadyToPlayAllArgs(Error error)
        {
            Player[] players;
            VoxelAbilitiesArray[]          abilities;
            SerializedTaskArray[]          taskInfo;
            SerializedTaskTemplatesArray[] taskTemplateInfo;
            AssignmentGroupArray[]         assignmentGroupArray;
            if (m_room != null)
            {
                enabled        = true;
                m_prevTickTime = m_time.Time;
                m_initialized  = true;

                error.Code = StatusCode.OK;
                players    = new Player[m_players.Count];

                List <IBotController> bots = new List <IBotController>();

                //Will override or
                abilities            = new VoxelAbilitiesArray[m_players.Count];
                taskInfo             = new SerializedTaskArray[m_players.Count];
                taskTemplateInfo     = new SerializedTaskTemplatesArray[m_players.Count];
                assignmentGroupArray = new AssignmentGroupArray[players.Length];
                for (int i = 0; i < m_players.Count; ++i)
                {
                    Player player = m_players[m_room.Players[i]];

                    players[i]              = player;
                    abilities[i]            = m_abilities[m_room.Players[i]];
                    taskInfo[i]             = m_taskTemplates[m_room.Players[i]];
                    taskTemplateInfo[i]     = m_taskTemplatesInfo[m_room.Players[i]];
                    assignmentGroupArray[i] = new AssignmentGroupArray
                    {
                        Groups = m_engine.GetPlayerView(i).AssignmentsController.GetGroups()
                    };
                    if (player.IsBot && player.BotType != BotType.Replay)
                    {
                        bots.Add(MatchFactory.CreateBotController(player, m_engine.GetTaskEngine(i)));
                    }
                }

                m_bots = bots.ToArray();
            }
            else
            {
                error.Code           = StatusCode.NotFound;
                error.Message        = "Room not found";
                players              = new Player[0];
                abilities            = new VoxelAbilitiesArray[0];
                taskInfo             = new SerializedTaskArray[0];
                taskTemplateInfo     = new SerializedTaskTemplatesArray[0];
                assignmentGroupArray = new AssignmentGroupArray[0];
            }


            var readyToPlayAllArgs = new ServerEventArgs <Player[], Dictionary <Guid, Dictionary <Guid, Player> >, VoxelAbilitiesArray[], SerializedTaskArray[], SerializedTaskTemplatesArray[], AssignmentGroupArray[], Room>();

            readyToPlayAllArgs.Arg    = players;
            readyToPlayAllArgs.Arg2   = m_clientIdToPlayers;
            readyToPlayAllArgs.Arg3   = abilities;
            readyToPlayAllArgs.Arg4   = taskInfo;
            readyToPlayAllArgs.Arg5   = taskTemplateInfo;
            readyToPlayAllArgs.Arg6   = assignmentGroupArray;
            readyToPlayAllArgs.Arg7   = m_room;
            readyToPlayAllArgs.Except = Guid.Empty;
            return(readyToPlayAllArgs);
        }
Пример #12
0
        public MatchServerImpl(ITimeService timeService, string persistentDataPath, Guid creatorClientId, Room room, Guid[] clientIds, Player[] players, ReplayData replay)
        {
            m_serializer = new ProtobufSerializer();

            m_time = timeService;
            m_persistentDataPath = persistentDataPath;
            m_room = room;
            m_room.CreatorClientId = creatorClientId;

            m_registeredClients            = new HashSet <Guid>();
            m_readyToPlayClients           = new HashSet <Guid>();
            m_readyToBeUnregisteredClients = new List <DisconnectedClient>();
            m_clientIdToPlayers            = new Dictionary <Guid, Dictionary <Guid, Player> >();
            m_playerToClientId             = new Dictionary <Guid, Guid>();
            m_clientIds = clientIds.ToList();
            for (int i = 0; i < m_clientIds.Count; ++i)
            {
                Guid clientId = m_clientIds[i];
                if (clientId != Guid.Empty)
                {
                    Dictionary <Guid, Player> idToPlayer;
                    if (!m_clientIdToPlayers.TryGetValue(clientId, out idToPlayer))
                    {
                        idToPlayer = new Dictionary <Guid, Player>();
                        m_clientIdToPlayers.Add(clientId, idToPlayer);
                    }
                    Player player = players[i];
                    idToPlayer.Add(player.Id, player);
                    m_playerToClientId.Add(player.Id, clientId);
                }
            }

            m_players = players.ToDictionary(p => p.Id);

            //Adding neutral player to room
            m_neutralPlayer         = new Player();
            m_neutralPlayer.BotType = BotType.Neutral;
            m_neutralPlayer.Name    = "Neutral";
            m_neutralPlayer.Id      = Guid.NewGuid();

            //Dictionary<Guid, Player> idToPlayer = new Dictionary<Guid, Player>();
            //idToPlayer.Add(m_neutralPlayer.Id, m_neutralPlayer);
            m_players.Add(m_neutralPlayer.Id, m_neutralPlayer);

            if (!m_room.Players.Contains(m_neutralPlayer.Id))
            {
                m_room.Players.Insert(0, m_neutralPlayer.Id);
                m_clientIds.Insert(0, Guid.Empty);
            }

            m_abilities         = new Dictionary <Guid, VoxelAbilities[]>();
            m_taskTemplates     = new Dictionary <Guid, SerializedTask[]>();
            m_taskTemplatesInfo = new Dictionary <Guid, SerializedNamedTaskLaunchInfo[]>();
            for (int i = 0; i < m_room.Players.Count; ++i)
            {
                m_abilities.Add(m_room.Players[i], CreateDefaultAbilities());
                m_taskTemplates.Add(m_room.Players[i], CreateDefaultTaskTemplates());
                m_taskTemplatesInfo.Add(m_room.Players[i], CreateDefaultTaskTemplateInfo());
            }

            m_pingTimer = new PingTimer(m_clientIdToPlayers.Keys.ToArray(), 3);

            if (replay != null)
            {
                m_replay = MatchFactory.CreateReplayPlayer();
                m_replay.Load(replay);
            }
        }
Пример #13
0
        private void InitEngine(ServerEventHandler callback)
        {
            m_initializationStarted = true;
            DownloadMapData((Error error, MapData mapData) =>
            {
                m_initializationStarted = false;
                if (HasError(error))
                {
                    if (callback != null)
                    {
                        callback(error);
                    }
                }
                else
                {
                    MapRoot mapRoot     = m_serializer.Deserialize <MapRoot>(mapData.Bytes);
                    IMatchEngine engine = MatchFactory.CreateMatchEngine(mapRoot, m_room.Players.Count);

                    Dictionary <int, VoxelAbilities>[] allAbilities = new Dictionary <int, VoxelAbilities> [m_room.Players.Count];
                    for (int i = 0; i < m_room.Players.Count; ++i)
                    {
                        allAbilities[i] = m_abilities[m_room.Players[i]].ToDictionary(a => a.Type);
                    }

                    if (m_replay == null)
                    {
                        m_replay = MatchFactory.CreateReplayRecorder();
                    }

                    //Zero is neutral
                    for (int i = 0; i < m_room.Players.Count; ++i)
                    {
                        Guid playerGuid = m_room.Players[i];
                        engine.RegisterPlayer(m_room.Players[i], i, allAbilities);
                    }
                    engine.CompletePlayerRegistration();

                    if (m_engine != null)
                    {
                        m_engine.OnSubmitted -= OnEngineCommandSubmitted;
                    }

                    m_engine              = engine;
                    m_engine.OnSubmitted += OnEngineCommandSubmitted;

                    if (callback != null)
                    {
                        callback(error);
                    }

                    if (m_room.Mode != GameMode.Replay)
                    {
                        m_pingTimer.PingAll();

                        if (Ping != null)
                        {
                            m_pingArgs.Arg    = new RTTInfo();
                            m_pingArgs.Except = Guid.Empty;
                            Ping(new Error(StatusCode.OK), m_pingArgs);
                        }
                    }
                    else
                    {
                        OnPingPongCompleted(new Error(StatusCode.OK), m_room.CreatorClientId);
                    }
                }
            });
        }