Ejemplo n.º 1
0
 public void Add(PlayerMessage message)
 {
     var item = new Item(message, _game.GameTime.TotalRealTime);
     var log = item.IsChatMessage ? ChatItems : CombatLogItems;
     log.Add(item);
     if (log.Count >= 2 * MESSAGE_KEEP_COUNT) log.RemoveRange(0, log.Count - MESSAGE_KEEP_COUNT);
     if (item.IsChatMessage && NewChatMessage != null) NewChatMessage(message);
     if (!item.IsChatMessage && NewCombatLogMessage != null) NewCombatLogMessage(message);
 }
Ejemplo n.º 2
0
 protected override void Deserialize(NetworkBinaryReader reader)
 {
     base.Deserialize(reader);
     PlayerID = reader.ReadInt16();
     int byteCount = reader.ReadUInt16();
     StreamedData = reader.ReadBytes(byteCount);
     Message = new PlayerMessage();
     Read(Message, SerializationModeFlags.AllFromServer, 0);
 }
Ejemplo n.º 3
0
 private void SendPlayerMessageToRemoteSpectator(PlayerMessage message, Player player)
 {
     if (!player.IsRemote) return;
     try
     {
         var messageMessage = new PlayerMessageMessage { PlayerID = player.ID, Message = message };
         NetworkEngine.GetGameClientConnection(player.ConnectionID).Send(messageMessage);
     }
     catch (InvalidOperationException)
     {
         // The connection of the player doesn't exist any more. Just don't send the message then.
     }
 }
Ejemplo n.º 4
0
 public void SendMessageToAllPlayers(string text, Player from)
 {
     var messageContent = text.Trim();
     if (messageContent == "") return;
     var preText = from.Name + ">";
     var textColor = from.Color;
     var message = new PlayerMessage(preText, messageContent, textColor);
     switch (NetworkMode)
     {
         case NetworkMode.Server:
             foreach (var plr in DataEngine.Players) plr.Messages.Add(message);
             break;
         case NetworkMode.Client:
             foreach (var plr in DataEngine.Players.Where(plr => plr.IsLocal)) plr.Messages.Add(message);
             NetworkEngine.GameServerConnection.Send(new PlayerMessageMessage
             {
                 PlayerID = -1,
                 Message = message,
             });
             break;
         default: throw new InvalidOperationException("Text messages not supported in mode " + NetworkMode);
     }
 }
Ejemplo n.º 5
0
 private IEnumerable<Line> GetMessageLines(PlayerMessage message, float textWidth)
 {
     var fullText = message.PreText.Length > 0 ? message.PreText + " " + message.Text : message.Text;
     var lines = new WrappedText(fullText, GetMessageLineWidth).WrapToWidth(textWidth);
     if (lines.Length == 0) yield break;
     var lineContainsPretext = message.PreText.Length > 0;
     foreach (var line in lines)
     {
         yield return new Line(line, message.TextColor, lineContainsPretext);
         lineContainsPretext = false;
     }
 }
Ejemplo n.º 6
0
 private void AddMessage(PlayerMessage message, float textWidth)
 {
     var lines = _items[textWidth];
     lines.AddRange(GetMessageLines(message, textWidth));
     if (lines.Count >= LINE_KEEP_COUNT * 2) lines.RemoveRange(0, lines.Count - LINE_KEEP_COUNT);
 }
Ejemplo n.º 7
0
 private void AddMessage(PlayerMessage message)
 {
     foreach (var textWidth in _items.Keys) AddMessage(message, textWidth);
 }
Ejemplo n.º 8
0
 public Item(PlayerMessage message, TimeSpan entryRealTime)
 {
     Message = message;
     EntryRealTime = entryRealTime;
 }
Ejemplo n.º 9
0
 private int StatsMessageHandler(ArraySegment<byte> messageHeaderAndBody, IPEndPoint remoteEndPoint)
 {
     // "\r\n" marks the end of a JSON serialized object.
     var bytesRead = 0;
     var startIndex = messageHeaderAndBody.Offset;
     for (int i = messageHeaderAndBody.Offset; i < messageHeaderAndBody.Offset + messageHeaderAndBody.Count - 1; i++)
     {
         if (messageHeaderAndBody.Array[i] != '\r' || messageHeaderAndBody.Array[i + 1] != '\n') continue;
         var nextStartIndex = i + 2;
         try
         {
             var str = Encoding.UTF8.GetString(messageHeaderAndBody.Array, startIndex, i - startIndex);
             if (StatsNotAllowed(messageHeaderAndBody))
             {
                 Log.Write("Battlefront statistics won't be collected from this server.");
                 _statsDataSocket.Dispose();
                 _statsNotAllowed = true;
                 return messageHeaderAndBody.Count;
             }
             var obj = JObject.Parse(str);
             var loginToken = obj.GetString("token");
             var announcement = obj.GetString("announcement");
             if (loginToken != "")
             {
                 var spectator = GetSpectatorOrNull(loginToken);
                 if (spectator != null) spectator.GetStats().Update(obj);
             }
             else if (announcement != "")
             {
                 var message = new PlayerMessage(obj.GetString("announcement"), PlayerMessage.DEFAULT_COLOR);
                 if (obj.GetString("all") == "true")
                     foreach (var plr in Game.DataEngine.Players) plr.Messages.Add(message);
                 else
                 {
                     var plr = GetSpectatorOrNull(obj.GetString("to")) as Player;
                     if (plr != null) plr.Messages.Add(message);
                 }
             }
         }
         catch (ArgumentException) { } // Encoding.GetString failed
         catch (JsonReaderException) { } // JsonConvert.DeserializeObject failed
         bytesRead += nextStartIndex - startIndex;
         startIndex = nextStartIndex;
     }
     return bytesRead;
 }