-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathGameClient.cs
103 lines (89 loc) · 3 KB
/
GameClient.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using YGOSharp.Network;
using YGOSharp.Network.Enums;
using YGOSharp.Network.Utils;
namespace WindBot.Game
{
public class GameClient
{
public YGOClient Connection { get; private set; }
public string Username;
public string Deck;
public string DeckFile;
public string Dialog;
public int Hand;
public bool Debug;
public bool _chat;
private string _serverHost;
private int _serverPort;
private short _proVersion;
private string _roomInfo;
private GameBehavior _behavior;
public GameClient(WindBotInfo Info)
{
Username = Info.Name;
Deck = Info.Deck;
DeckFile = Info.DeckFile;
Dialog = Info.Dialog;
Hand = Info.Hand;
Debug = Info.Debug;
_chat = Info.Chat;
_serverHost = Info.Host;
_serverPort = Info.Port;
_roomInfo = Info.HostInfo;
_proVersion = (short)Info.Version;
}
public void Start()
{
Connection = new YGOClient();
_behavior = new GameBehavior(this);
Connection.Connected += OnConnected;
Connection.PacketReceived += OnPacketReceived;
IPAddress target_address;
try
{
target_address = IPAddress.Parse(_serverHost);
}
catch (System.Exception)
{
IPHostEntry _hostEntry = Dns.GetHostEntry(_serverHost);
target_address = _hostEntry.AddressList.FirstOrDefault(findIPv4 => findIPv4.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
}
Connection.Connect(target_address, _serverPort);
}
private void OnConnected()
{
BinaryWriter packet = GamePacketFactory.Create(CtosMessage.PlayerInfo);
packet.WriteUnicode(Username, 20);
Connection.Send(packet);
byte[] junk = { 0xCC, 0xCC, 0x00, 0x00, 0x00, 0x00 };
packet = GamePacketFactory.Create(CtosMessage.JoinGame);
packet.Write(_proVersion);
packet.Write(junk);
packet.WriteUnicode(_roomInfo, 30);
Connection.Send(packet);
}
public void Tick()
{
Connection.Update();
}
public void Chat(string message)
{
byte[] content = Encoding.Unicode.GetBytes(message + "\0");
BinaryWriter chat = GamePacketFactory.Create(CtosMessage.Chat);
chat.Write(content);
Connection.Send(chat);
}
public void Surrender()
{
Connection.Send(CtosMessage.Surrender);
}
private void OnPacketReceived(BinaryReader reader)
{
_behavior.OnPacket(reader);
}
}
}