Skip to content

Commit

Permalink
Core: Added ChatReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Xian55 committed Sep 22, 2023
1 parent fa3b10b commit 837911c
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Core/Chat/ChatReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.ObjectModel;
using System.Text;

namespace Core;

public enum ChatMessageType
{
Whisper,
Say,
Yell,
Emote,
Party
}

public readonly record struct ChatMessageEntry(DateTime Time, ChatMessageType Type, string Author, string Message);

public sealed class ChatReader : IReader
{
private const int cMsg = 98;
private const int cMeta = 99;

public ObservableCollection<ChatMessageEntry> Messages { get; } = new();

// 12 character name
// 1 space
// 256 maximum message length
private readonly StringBuilder sb = new(12 + 1 + 256);

private int _head;

public void Update(IAddonDataProvider reader)
{
int meta = reader.GetInt(cMeta);
if (meta == 0)
{
_head = 0;
return;
}

ChatMessageType type = (ChatMessageType)(meta / 1000000);
int length = meta % 1000000 / 1000;
int head = meta % 1000;

if (_head != head)
_head = head;
else if (_head == head)
return;

string part = reader.GetString(cMsg).Replace('@', ' ');

sb.Append(part);

if (head + 2 < length)
return;

string text = sb.ToString().ToLowerInvariant();
sb.Clear();

int firstSpaceIdx = text.IndexOf(" ");
string author = text.AsSpan(0, firstSpaceIdx).ToString();
text = text.AsSpan(firstSpaceIdx).ToString();

Messages.Add(new(DateTime.Now, type, author, text));
}
}
2 changes: 2 additions & 0 deletions Core/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static IServiceCollection AddAddonComponents(
s.ForwardSingleton<GossipReader, IReader>();
s.ForwardSingleton<SpellBookReader, IReader>();
s.ForwardSingleton<TalentReader, IReader>();
s.ForwardSingleton<ChatReader, IReader>();

s.ForwardSingleton<ActionBarCostReader, IReader>();
s.ForwardSingleton<ActionBarCooldownReader, IReader>();
Expand Down Expand Up @@ -123,6 +124,7 @@ public static IServiceCollection AddStartupIoC(
s.ForwardSingleton<GossipReader>(sp);
s.ForwardSingleton<SpellBookReader>(sp);
s.ForwardSingleton<TalentReader>(sp);
s.ForwardSingleton<ChatReader>(sp);

s.ForwardSingleton<ActionBarCostReader>(sp);
s.ForwardSingleton<ActionBarCooldownReader>(sp);
Expand Down
2 changes: 2 additions & 0 deletions Frontend/Pages/Chat.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@page "/Chat"
<ChatComponent MaxHeight="0" />
55 changes: 55 additions & 0 deletions Frontend/Pages/ChatComponent.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@using System.Collections.Specialized

@implements IDisposable

@inject ChatReader reader

<style>
.log {
font-family: Consolas, monaco, monospace;
font-size: small;
padding: 0 0 5px 0 !important;
padding-right: 5px !important;
}
</style>

<div class="col-sm" style="@(MaxHeight > 0 ? $"overflow: auto; max-height: {MaxHeight}px;" : string.Empty)">
<table class="table table-borderless table-dark" cellspacing="20">
@foreach (var cme in reader.Messages.Reverse())
{
<tr style="color: @(typeColor[(int)cme.Type])">
<td width="65px" class="log">@cme.Time.ToString("HH:mm:ss")</td>
<td width="80px" class="log">@cme.Author:</td>
<td class="log">@cme.Message</td>
</tr>
}
</table>
</div>

@code {
[Parameter]
public int MaxHeight { get; set; } = 400;

private static readonly string[] typeColor = {
"pink", // ChatMessageType.Whisper
"white", // ChatMessageType.Say
"red", // ChatMessageType.Yell
"orange", // ChatMessageType.Emote
"cyan", // ChatMessageType.Party
};

protected override void OnInitialized()
{
reader.Messages.CollectionChanged += Changed;
}

public void Dispose()
{
reader.Messages.CollectionChanged -= Changed;
}

private void Changed(object? s, NotifyCollectionChangedEventArgs e)
{
base.InvokeAsync(StateHasChanged);
}
}
5 changes: 5 additions & 0 deletions Frontend/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<BagChanges />
</td>
</tr>
<tr>
<td>
<ChatComponent MaxHeight="400" />
</td>
</tr>
</table>
</div>

Expand Down
6 changes: 6 additions & 0 deletions Frontend/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
<span class="@NavMenuTextCssClass">Frame Config</span>
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="Chat">
<span class="oi oi-people" aria-hidden="true"></span>
<span class="@NavMenuTextCssClass">Chat</span>
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="Log">
<span class="oi oi-text" aria-hidden="true"></span>
Expand Down

0 comments on commit 837911c

Please sign in to comment.