forked from julianperrott/WowClassicGrindBot
-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@page "/Chat" | ||
<ChatComponent MaxHeight="0" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,11 @@ | |
<BagChanges /> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td> | ||
<ChatComponent MaxHeight="400" /> | ||
</td> | ||
</tr> | ||
</table> | ||
</div> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters