Exemple #1
0
        public void GetReplay(Guid clientId, ServerEventHandler <ReplayData, Room> callback)
        {
            Error error = new Error(StatusCode.OK);

            if (!m_clientIdToPlayers.ContainsKey(clientId) && clientId != m_serverIdentity)
            {
                error.Code = StatusCode.NotRegistered;
                callback(error, null, null);
                return;
            }

            ReplayData replay = m_replay.Save();

            if (!m_initialized)
            {
                error.Code    = StatusCode.NotAllowed;
                error.Message = "Match was not initialized";
            }
            callback(error, replay, m_room);
        }
        public void GetReplay(Guid clientId, ServerEventHandler <ReplayData, Room> callback)
        {
            ReplayData replay = m_replay.Save();
            Error      error  = new Error(StatusCode.OK);

            if (!m_initialized)
            {
                error.Code    = StatusCode.NotAllowed;
                error.Message = "Match was not initialized";
            }
            if (m_lag == 0)
            {
                callback(error, replay, m_room);
            }
            else
            {
                m_job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result =>
                {
                    callback(error, replay, m_room);
                });
            }
        }
Exemple #3
0
        public void SetReplay(Guid clientId, Guid id, ServerEventHandler callback)
        {
            Error      error  = new Error(StatusCode.OK);
            ReplayData replay = GetReplayData(id);

            if (replay == null)
            {
                error.Code = StatusCode.NotFound;
            }
            else
            {
                GState.SetValue("LocalGameServer.m_replay", replay);
            }

            if (m_lag == 0)
            {
                callback(error);
            }
            else
            {
                Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error));
            }
        }
        protected override void OnRequest(ILowProtocol sender, LowRequestArgs request)
        {
            RemoteCall rpc;

            try
            {
                rpc = m_serializer.Deserialize <RemoteCall>(request.Data);
            }
            catch (Exception e)
            {
                Log.Error("Invalid RemoteCall format ", e);

                #warning This code is not tested
                sender.Disable();

                throw;
            }

            switch (rpc.Procedure)
            {
            case RemoteCall.Proc.RegisterClient:
            {
                RegisterClient(sender, rpc.ClientId);
                Return(sender, request, new Error(StatusCode.OK));
            }
            break;

            case RemoteCall.Proc.CreateMatch:
            {
                if (rpc.ClientId != ServerIdentity)
                {
                    Return(sender, request, new Error(StatusCode.NotAuthorized));
                }
                else
                {
                    Guid       creatorClientId = rpc.Get <Guid>(0);
                    Room       room            = rpc.Get <Room>(1);
                    Guid[]     clientIds       = rpc.Get <Guid[]>(2);
                    Player[]   players         = rpc.Get <Player[]>(3);
                    ReplayData replay          = rpc.Get <ReplayData>(4);

                    if (m_matchServer == null)
                    {
                        MatchServerImpl matchServer = new MatchServerImpl(this, m_path, creatorClientId, room, clientIds, players, replay);
                        m_matchServer = matchServer;

                        m_matchServer.Tick           += OnTick;
                        m_matchServer.ReadyToPlayAll += OnReadyToPlayAll;
                        m_matchServer.Paused         += OnPaused;
                        m_matchServer.Ping           += OnPing;
                        m_matchServer.ChatMessage    += OnChatMessage;
                    }

                    Return(sender, request, new Error(StatusCode.OK));
                }
            }
            break;

            case RemoteCall.Proc.GetState:
            {
                m_matchServer.GetState(rpc.ClientId, (error, arg, arg2, arg3, arg4, arg5, arg6, arg7, arg8) =>
                    {
                        Room room = arg7;
                        if (room.Mode == GameMode.Replay)
                        {
                            Return(sender, request,
                                   error,
                                   RemoteArg.Create(arg),
                                   RemoteArg.Create(new Guid[0]),
                                   RemoteArg.Create(arg3),
                                   RemoteArg.Create(arg4),
                                   RemoteArg.Create(arg5),
                                   RemoteArg.Create(arg6),
                                   RemoteArg.Create(arg7),
                                   RemoteArg.Create(arg8));
                        }
                        else
                        {
                            Dictionary <Guid, Dictionary <Guid, Player> > clientIdToPlayers = arg2;
                            Dictionary <Guid, Player> players;
                            if (!clientIdToPlayers.TryGetValue(rpc.ClientId, out players))
                            {
                                Return(sender, request, new Error(StatusCode.NotFound));
                            }

                            else
                            {
                                Return(sender, request,
                                       error,
                                       RemoteArg.Create(arg),
                                       RemoteArg.Create(players.Keys.ToArray()),
                                       RemoteArg.Create(arg3),
                                       RemoteArg.Create(arg4),
                                       RemoteArg.Create(arg5),
                                       RemoteArg.Create(arg6),
                                       RemoteArg.Create(arg7),
                                       RemoteArg.Create(arg8));
                            }
                        }
                    });
            }
            break;

            case RemoteCall.Proc.GetReplay:

                m_matchServer.GetReplay(rpc.ClientId, (error, replayData, room) =>
                {
                    Return(sender, request, error, RemoteArg.Create(replayData), RemoteArg.Create(room));
                });
                break;

            case RemoteCall.Proc.DownloadMapData:
                m_matchServer.DownloadMapData(rpc.ClientId, (Error error, byte[] data) =>
                {
                    Return(sender, request, error, RemoteArg.Create(data));
                });
                break;

            case RemoteCall.Proc.ReadyToPlay:
                m_matchServer.ReadyToPlay(rpc.ClientId, error =>
                {
                    Return(sender, request, error);
                });
                break;

            case RemoteCall.Proc.Submit:
                m_matchServer.Submit(rpc.ClientId, rpc.Get <int>(0), rpc.Get <Cmd>(1), (error, returnedCommand) =>
                {
                    Return(sender, request, error, RemoteArg.Create(returnedCommand));
                });
                break;

            case RemoteCall.Proc.SubmitResponse:
                m_matchServer.SubmitResponse(rpc.ClientId, rpc.Get <ClientRequest>(0), (error, response) =>
                {
                    Return(sender, request, error, RemoteArg.Create(response));
                });
                break;

            case RemoteCall.Proc.Pong:
                m_matchServer.Pong(rpc.ClientId, error =>
                {
                    Return(sender, request, error);
                });
                break;

            case RemoteCall.Proc.Pause:
                m_matchServer.Pause(rpc.ClientId, rpc.Get <bool>(0), error =>
                {
                    Return(sender, request, error);
                });
                break;

            case RemoteCall.Proc.IsAliveCheck:
                Return(sender, request, new Error(StatusCode.OK));
                break;

            case RemoteCall.Proc.SendChatMessage:
                m_matchServer.SendMessage(rpc.ClientId, rpc.Get <ChatMessage>(0), (error, messageId) =>
                {
                    Return(sender, request, error, RemoteArg.Create(messageId));
                });
                break;
            }
        }
        public void CreateMatch(Guid creatorClientId, Room room, Guid[] clientIds, Player[] players, ReplayData replay, ServerEventHandler callback)
        {
            if (m_protocol != null)
            {
                throw new InvalidOperationException();
            }

            LowProtocol <ClientSocket> protocol = new LowProtocol <ClientSocket>(m_url + "create", m_time.Time);

            m_protocol              = protocol;
            m_protocol.Enabled     += OnEnabled;
            m_protocol.SocketError += OnError;
            m_protocol.Disabled    += OnDisabled;

            m_response = externalError =>
            {
                callback(externalError);
            };

            m_call = () =>
            {
                RemoteCall rpc = new RemoteCall(RemoteCall.Proc.CreateMatch, ServerContainer.ServerIdentity, RemoteArg.Create(creatorClientId), RemoteArg.Create(room), RemoteArg.Create(clientIds), RemoteArg.Create(players), RemoteArg.Create(replay));

                Call(rpc, (error, remoteResult) =>
                {
                    m_response = externalError =>
                    {
                        if (externalError != null)
                        {
                            callback(externalError);
                        }
                        else
                        {
                            callback(error);
                        }
                    };

                    m_protocol.Disable();
                });
            };

            m_protocol.Enable();
        }
        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();
        }
Exemple #7
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);
            }
        }
Exemple #8
0
 public virtual void Load(ReplayData replay)
 {
     m_ticks         = new Queue <long>(replay.Ticks);
     m_commands      = new Queue <Cmd>(replay.Commands);
     m_playerIndices = new Queue <int>(replay.Players);
 }