Skip to content
Browse files

- Add Keybind handlers

- Bind 'W' to whisper
- Implement whisper
- Remove redundant timestamp in console on incoming chat messages
- Handle SMSG_CHAT_PLAYER_NOT_FOUND
  • Loading branch information...
1 parent 7f93b8d commit cb687449ad2b66e2a59d79f51dafbd8a08f7807e @Machiavell1 committed Nov 17, 2011
View
2 CLIClient/CLI.csproj
@@ -53,6 +53,8 @@
<Compile Include="UI\CommandLine\ConsoleInterface.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="UI\CommandLine\KeyBindHandlers\Whisper.cs" />
+ <Compile Include="UI\CommandLine\KeyBindings.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Client\Client.csproj">
View
17 CLIClient/UI/CommandLine/ConsoleInterface.cs
@@ -8,7 +8,7 @@
namespace Client.UI.CommandLine
{
- class CommandLineUI : IGameUI
+ public partial class CommandLineUI : IGameUI
{
#region Private Members
private LogLevel _logLevel;
@@ -21,6 +21,8 @@ public CommandLineUI()
{
_logFile = new StreamWriter(String.Format("{0}.log", DateTime.Now).Replace(':', '_').Replace('/', '-'));
_logFile.AutoFlush = true;
+
+ InitializeKeybinds();
}
#region IGameInterface Members
@@ -35,7 +37,13 @@ public LogLevel LogLevel
public void Update()
{
+ if (Game.World.SelectedCharacter == null)
+ return;
+ ConsoleKeyInfo keyPress = Console.ReadKey();
+ KeyBind handler;
+ if (_keyPressHandlers.TryGetValue(keyPress.Key, out handler))
+ handler();
}
public void Exit()
@@ -86,7 +94,6 @@ public void PresentCharacterList(Character[] characterList)
{
LogLine("\n\tName\tLevel Class Race");
- Character selectedCharacter;
int index = 0;
foreach (Character character in characterList)
LogLine
@@ -113,13 +120,13 @@ public void PresentCharacterList(Character[] characterList)
if (index < characterList.Length)
{
- selectedCharacter = characterList[index];
+ Game.World.SelectedCharacter = characterList[index];
// TODO: enter world
- LogLine(string.Format("Entering pseudo-world with character {0}", selectedCharacter.Name));
+ LogLine(string.Format("Entering pseudo-world with character {0}", Game.World.SelectedCharacter.Name));
OutPacket packet = new OutPacket(WorldCommand.CMSG_PLAYER_LOGIN);
- packet.Write(selectedCharacter.GUID);
+ packet.Write(Game.World.SelectedCharacter.GUID);
Game.SendPacket(packet);
}
else
View
40 CLIClient/UI/CommandLine/KeyBindHandlers/Whisper.cs
@@ -0,0 +1,40 @@
+using System;
+using Client.Chat;
+using Client.Chat.Definitions;
+using Client.World;
+using Client.World.Definitions;
+using Client.World.Network;
+
+namespace Client.UI.CommandLine
+{
+ public partial class CommandLineUI
+ {
+ [KeyBindAttribute(ConsoleKey.W)]
+ public void HandleWhisper()
+ {
+ LogLine("Enter name of player to whisper, or enter 'Q' to go back.");
+ Log("To: ");
+ var target = Console.ReadLine();
+ if (target.Equals('Q'))
+ return;
+
+ Log("Message: ");
+ var message = Console.ReadLine();
+
+ var response = new OutPacket(WorldCommand.CMSG_MESSAGECHAT);
+
+ response.Write((uint)ChatMessageType.Whisper);
+ var race = Game.World.SelectedCharacter.Race;
+ var language = (race == Race.Bloodelf || race == Race.Orc || race == Race.Tauren || race == Race.Troll ||
+ race == Race.Undead)
+ ? Language.Orcish
+ : Language.Common;
+ response.Write((uint)language);
+ response.Write(target.ToCString());
+ response.Write(message.ToCString());
+ Game.SendPacket(response);
+
+ //! Print on WhisperInform message
+ }
+ }
+}
View
43 CLIClient/UI/CommandLine/KeyBindings.cs
@@ -0,0 +1,43 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+
+namespace Client.UI.CommandLine
+{
+ public delegate void KeyBind();
+
+ [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
+ public sealed class KeyBindAttribute : Attribute
+ {
+ public ConsoleKey Key;
+
+ public KeyBindAttribute(ConsoleKey key)
+ {
+ Key = key;
+ }
+ }
+
+ public partial class CommandLineUI
+ {
+ private Dictionary<ConsoleKey, KeyBind> _keyPressHandlers;
+
+ private void InitializeKeybinds()
+ {
+ _keyPressHandlers = new Dictionary<ConsoleKey, KeyBind>();
+
+ const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
+
+ foreach (var method in this.GetType().GetMethods(flags))
+ {
+ var attributes = (KeyBindAttribute[])method.GetCustomAttributes(typeof(KeyBindAttribute), false);
+ if (attributes.Length == 0)
+ continue;
+
+ var handler = (KeyBind)KeyBind.CreateDelegate(typeof(KeyBind), this, method);
+
+ foreach (var attribute in attributes)
+ _keyPressHandlers[attribute.Key] = handler;
+ }
+ }
+ }
+}
View
5 Client/Chat/ChatMessage.cs
@@ -13,14 +13,11 @@ public class ChatMessage
public Language Language;
public ChatTag ChatTag;
public string Message;
- public DateTime Timestamp;
public override string ToString()
{
StringBuilder sb = new StringBuilder();
- sb.Append(Timestamp);
- sb.Append(": ");
- sb.Append(Sender.Type.ToString());
+ sb.Append(Sender.Type == ChatMessageType.WhisperInform ? "To: " : Sender.Type.ToString());
//! Color codes taken from default chat_cache in WTF folder
//! TODO: RTF form?
switch (Sender.Type)
View
1 Client/Game.cs
@@ -85,6 +85,7 @@ public void Start()
while (Running)
{
// main loop here
+ UI.Update();
Thread.Sleep(100);
}
View
4 Client/World.cs
@@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using Client.Chat;
+using Client.World;
namespace Client
{
@@ -16,5 +17,8 @@ public class GameWorld
//! Message queue for when sender's name hasn't been queried trough NAME_QUERY yet
public Dictionary<ulong, Queue<ChatMessage>> QueuedChatMessages = new Dictionary<ulong, Queue<ChatMessage>>();
+
+ //! Character currently logged into world
+ public Character SelectedCharacter;
}
}
View
7 Client/World/Network/PacketHandlers/ChatHandlers.cs
@@ -55,7 +55,6 @@ void HandleMessageChat(InPacket packet)
message.Message = packet.ReadCString();
message.Language = lang;
message.ChatTag = (ChatTag)packet.ReadByte();
- message.Timestamp = DateTime.Now;
message.Sender = channel;
//! If we know the name of the sender GUID, use it
@@ -95,5 +94,11 @@ void HandleMessageChat(InPacket packet)
return;
}
}
+
+ [PacketHandler(WorldCommand.SMSG_CHAT_PLAYER_NOT_FOUND)]
+ void HandleChatPlayerNotFound(InPacket packet)
+ {
+ Game.UI.LogLine(String.Format("Player {0} not found!", packet.ReadCString()));
+ }
}
}

0 comments on commit cb68744

Please sign in to comment.
Something went wrong with that request. Please try again.