-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarterBot.cs
More file actions
99 lines (81 loc) · 3.57 KB
/
Copy pathStarterBot.cs
File metadata and controls
99 lines (81 loc) · 3.57 KB
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
90
91
92
93
94
95
96
97
98
99
using System;
using System.Configuration;
using System.Net;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
namespace Mia.Bot.Starter
{
public class StarterBot
{
public UdpState udpStateServer;
public StarterBot()
{
string serverAddress = ConfigurationManager.AppSettings["ServerAddress"];
string serverPort = ConfigurationManager.AppSettings["ServerPort"];
var serverEndPoint = new IPEndPoint(IPAddress.Parse(serverAddress), int.Parse(serverPort));
var hostEntry = Dns.GetHostEntry(Dns.GetHostName());
var localIpAddress = (from address in hostEntry.AddressList where address.AddressFamily == AddressFamily.InterNetwork select address.ToString()).FirstOrDefault();
var localEndPoint = new IPEndPoint(IPAddress.Parse(localIpAddress), int.Parse(ConfigurationManager.AppSettings["LocalPort"]));
Thread.Sleep(2000);
var udpClient = new UdpClient(localEndPoint);
udpClient.Connect(serverEndPoint);
string playerName = ConfigurationManager.AppSettings["PlayerName"];
string commandText = "REGISTER;" + playerName;
SendCommand(commandText, udpClient);
udpStateServer = new UdpState
{
EndPoint = serverEndPoint,
UdpClient = udpClient
};
udpStateServer.UdpClient.BeginReceive(new AsyncCallback(ReceiveCallBack), udpStateServer);
}
public void SendCommand(string message, UdpClient udpClient)
{
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
udpClient.Send(messageBytes, messageBytes.Length);
WriteLog(message);
}
public void ReceiveCallBack(IAsyncResult result)
{
UdpClient client = ((UdpState)(result.AsyncState)).UdpClient;
IPEndPoint endPoint = ((UdpState)(result.AsyncState)).EndPoint;
byte[] receivedBytes = client.EndReceive(result, ref endPoint);
client.BeginReceive(new AsyncCallback(ReceiveCallBack), udpStateServer);
string receivedValue = Encoding.UTF8.GetString(receivedBytes);
WriteLog(receivedValue);
string[] messageParts = receivedValue.Split(';');
string commandText = string.Empty;
string token = string.Empty;
string dice = string.Empty;
switch (messageParts[0])
{
case "REGISTERED":
break;
case "ROUND_STARTING":
token = messageParts[1];
commandText = "JOIN;" + token;
SendCommand(commandText, client);
break;
case "YOUR_TURN":
token = messageParts[1];
commandText = "ROLL;" + token;
SendCommand(commandText, client);
break;
case "ROLLED":
token = messageParts[1];
dice = messageParts[2];
commandText = "ANNOUNCE;" + token + ";" + dice;
SendCommand(commandText, client);
break;
}
}
public void WriteLog(string message)
{
string filteredMessage = Regex.Replace(message, @"(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}", "", RegexOptions.IgnoreCase);
Console.WriteLine(message);
}
}
}