Ejemplo n.º 1
0
        public void AddMessage(string authorEntity, string recipientEntity, string message)
        {
            if (authorEntity == null)
            {
                return;
            }
            if (_entityBackend.GetEntity(authorEntity) == null)
            {
                return;
            }
            if (recipientEntity != null)
            {
                if (_entityBackend.GetEntity(recipientEntity) == null)
                {
                    return;
                }
            }
            _messages.Insert(new Peacenet.Backend.ChatMessage
            {
                Id                = Guid.NewGuid().ToString(),
                AuthorEntityId    = authorEntity,
                RecipientEntityId = recipientEntity,
                Message           = message,
                Timestamp         = DateTime.UtcNow
            });

            string authorName    = _entityBackend.GetEntity(authorEntity).DisplayName;
            string recipientName = (recipientEntity == null) ? "group" : _entityBackend.GetEntity(recipientEntity).DisplayName;

            Plex.Objects.Logger.Log($"[chat] <{authorName} -> {recipientName}> {message}");
            using (var ms = new MemoryStream())
            {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8))
                {
                    writer.Write(authorName);
                    writer.Write(recipientName);
                    writer.Write(message);
                    writer.Flush();
                    if (recipientEntity == null)
                    {
                        _backend.Broadcast(ServerBroadcastType.Chat_MessageReceived, ms.ToArray());
                    }
                    else
                    {
                        if (_entityBackend.GetPlayerId(recipientEntity) != null)
                        {
                            _backend.BroadcastToPlayer(ServerBroadcastType.Chat_MessageReceived, ms.ToArray(), _entityBackend.GetPlayerId(recipientEntity));
                        }
                        if (_entityBackend.GetPlayerId(authorEntity) != null)
                        {
                            _backend.BroadcastToPlayer(ServerBroadcastType.Chat_MessageReceived, ms.ToArray(), _entityBackend.GetPlayerId(authorEntity));
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void MakeConnection(uint from, uint to, ushort port)
        {
            if (from == to)
            {
                throw new InvalidOperationException("You cannot connect to yourself.");
            }
            var existing = _connections.FirstOrDefault(x => x.To == to && x.From == from && x.Port == port);

            if (existing != null)
            {
                throw new InvalidOperationException("These systems are already connected on that port.");
            }
            _connections.Add(new PeacenetIPConnection
            {
                To   = to,
                From = from,
                Port = port
            });
            string toEntity = GrabEntity(to);

            if (toEntity != null)
            {
                var playerId = _entityBackend.GetPlayerId(toEntity);
                using (var memstr = new MemoryStream())
                {
                    using (var writer = new BinaryWriter(memstr, Encoding.UTF8))
                    {
                        writer.Write(60000);
                        writer.Write(JsonConvert.SerializeObject(DateTime.UtcNow));
                        writer.Flush();
                        _backend.BroadcastToPlayer(Plex.Objects.ServerBroadcastType.SYSTEM_CONNECTED, memstr.ToArray(), playerId);
                    }
                }
            }
        }
 private void dispatchAlertLevel(string id, double alert, bool dissipating)
 {
     using (var ms = new MemoryStream())
     {
         using (var writer = new BinaryWriter(ms, Encoding.UTF8))
         {
             writer.Write(id);
             writer.Write(alert);
             writer.Write(dissipating);
             writer.Flush();
             _backend.BroadcastToPlayer(Plex.Objects.ServerBroadcastType.GovernmentAlert, ms.ToArray(), id);
         }
     }
 }