-
Notifications
You must be signed in to change notification settings - Fork 2
/
Receiver.cs
89 lines (79 loc) · 3.4 KB
/
Receiver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Text;
using WhirlpoolCore.Communication.Messages;
using WhirlpoolCore.Communication.Messages.Client;
using WhirlpoolCore.Reactors;
namespace WhirlpoolCore
{
static class Receiver
{
private static bool DEBUG_MODE = true;
private static Dictionary<int, IReactor> Reactors = new Dictionary<int, IReactor>()
{
{ PacketId.JoinRoom, new JoinRoomReactor() },
{ PacketId.Movement, new MovementReactor() },
{ PacketId.RoomChat, new RoomChatReactor() },
{ PacketId.ChangeClothes, new ChangeClothesReactor() }
};
public static void Process(int MessageId, String SenderId, byte[] MessageBytes)
{
ClientPacket Message;
switch (MessageId)
{
case PacketId.JoinRoom: Message = new ClientJoinRoomPacket(MessageBytes, SenderId); break;
case PacketId.Movement: Message = new ClientMovementPacket(MessageBytes, SenderId); break;
case PacketId.RoomChat: Message = new ClientRoomChatPacket(MessageBytes, SenderId); break;
case PacketId.ChangeClothes: Message = new ClientChangeClothesPacket(MessageBytes, SenderId); break;
default: Message = new ClientPacket(MessageBytes, SenderId); break;
}
if (Reactors.ContainsKey(MessageId))
{
if (!DEBUG_MODE)
{
try
{
Reactors[MessageId].React(Message);
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Critical reactor meltdown!");
Console.WriteLine($"{e.ToString()}");
Console.ForegroundColor = ConsoleColor.White;
}
}
else
{
Reactors[MessageId].React(Message);
}
}
else
{
Console.WriteLine($"The appropriate reactor has not been registered for Message ID: {MessageId}");
}
}
// Special handler for a handshake. The reactor won't be able to handle it with a ref.
public static void Authenticate(byte[] MessageBytes, ref String AuthenticatedId)
{
ClientAuthenticatePacket AuthenticatePacket = new ClientAuthenticatePacket(MessageBytes);
String RegisteredTicket = DataManager.GetUserGameTicket(AuthenticatePacket.SenderId);
// String RegisteredTicket = DataManager.GetUserGameTicket(AuthenticatePacket.SenderId.Substring(0, 4));
// Temporary.
// RegisteredTicket = "WHIRLPOOL-2019";
if (RegisteredTicket != String.Empty && RegisteredTicket != null)
{
if (AuthenticatePacket.SessionTicket == RegisteredTicket)
{
AuthenticatedId = AuthenticatePacket.SenderId;
WorldManager.AddUserToWorld(AuthenticatedId);
}
}
}
public static void Disconnect(String PlayerId)
{
ConnectionManager.DestroyConnection(ConnectionManager.FindPlayerConnection(PlayerId));
WorldManager.RemoveUserFromWorld(PlayerId);
}
}
}