Commit 3b1accc3 authored by mercury233's avatar mercury233

test windbot server

parent 9c6d6fd5
......@@ -55,12 +55,7 @@ namespace WindBot.Game.AI
{
_game = game;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DialogsData));
string dialogfilename = "zh-CN";
string envdialogfilename = Environment.GetEnvironmentVariable("YGOPRO_DIALOG");
if (envdialogfilename != null)
{
dialogfilename = envdialogfilename;
}
string dialogfilename = game.Dialog;
using (FileStream fs = File.OpenRead("Dialogs/" + dialogfilename + ".json"))
{
DialogsData data = (DialogsData)serializer.ReadObject(fs);
......
......@@ -74,6 +74,7 @@ namespace WindBot.Game
_packets.Add(StocMessage.DuelEnd, OnDuelEnd);
_packets.Add(StocMessage.Chat, OnChat);
_messages.Add(GameMessage.Retry, OnRetry);
_messages.Add(GameMessage.Start, OnStart);
_messages.Add(GameMessage.Win, OnWin);
_messages.Add(GameMessage.Draw, OnDraw);
......@@ -227,6 +228,11 @@ namespace WindBot.Game
packet.ReadUnicode(256); // message
}
private void OnRetry(BinaryReader packet)
{
throw new Exception("Got MSG_RETRY.");
}
private void OnStart(BinaryReader packet)
{
int type = packet.ReadByte();
......
......@@ -12,17 +12,20 @@ namespace WindBot.Game
public YGOClient Connection { get; private set; }
public string Username;
public string Deck;
public string Dialog;
private string _serverHost;
private int _serverPort;
private string _roomInfos;
private GameBehavior _behavior;
public GameClient(string username, string deck, string serverHost, int serverPort, string roomInfos = "")
public GameClient(string username = "Windbot", string deck = "Blue-Eyes", string serverHost = "127.0.0.1", int serverPort = 7911, string dialog = "default", string roomInfos = "")
{
Username = username;
Deck = deck;
Dialog = dialog;
_serverHost = serverHost;
_serverPort = serverPort;
_roomInfos = roomInfos;
......
......@@ -8,5 +8,9 @@ namespace WindBot
{
Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + message);
}
public static void WriteErrorLine(string message)
{
Console.Error.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + message);
}
}
}
\ No newline at end of file
......@@ -4,29 +4,82 @@ using System.IO;
using System.Threading;
using WindBot.Game;
using WindBot.Game.AI;
using System.Net;
using System.Web;
namespace WindBot
{
public class WindBotInfo
{
public string Name { get; set; }
public string Deck { get; set; }
public string Host { get; set; }
public string Dialog { get; set; }
public int Port { get; set; }
public int Version { get; set; }
}
public class Program
{
public static short ProVersion = 0x133A;
public static int PlayerNameSize = 20;
internal static Random Rand;
internal static void Main()
{
#if !DEBUG
try
{
Run();
}
catch (Exception ex)
Init("cards.cdb");
using (HttpListener MainServer = new HttpListener())
{
Console.Error.WriteLine("Error: " + ex);
MainServer.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
MainServer.Prefixes.Add("http://127.0.0.1:2399/");
MainServer.Start();
Console.WriteLine("Windbot Server Start Successed.");
while (true)
{
try
{
HttpListenerContext ctx = MainServer.GetContext();
WindBotInfo Info = new WindBotInfo();
string RawUrl = Path.GetFileName(ctx.Request.RawUrl);
Info.Name = HttpUtility.ParseQueryString(RawUrl).Get("name");
Info.Deck = HttpUtility.ParseQueryString(RawUrl).Get("deck");
Info.Host = HttpUtility.ParseQueryString(RawUrl).Get("host");
Info.Dialog = HttpUtility.ParseQueryString(RawUrl).Get("dialog");
string port = HttpUtility.ParseQueryString(RawUrl).Get("port");
if (port != null)
Info.Port = Int32.Parse(port);
string version = HttpUtility.ParseQueryString(RawUrl).Get("version");
if (version != null)
Info.Version = Int16.Parse(version);
if (Info.Name == null || Info.Deck == null || Info.Host == null || Info.Dialog == null || port == null || version == null)
{
ctx.Response.StatusCode = 400;
ctx.Response.Close();
}
else
{
try
{
Thread workThread = new Thread(new ParameterizedThreadStart(Run));
workThread.Start(Info);
}
catch (Exception ex)
{
Logger.WriteErrorLine("Start Thread Error: " + ex);
}
ctx.Response.StatusCode = 200;
ctx.Response.Close();
}
}
catch (Exception ex)
{
Logger.WriteErrorLine("Parse Http Request Error: " + ex);
}
}
}
#else
Run();
#endif
}
public static void Init(string databasePath)
......@@ -36,33 +89,52 @@ namespace WindBot
InitCardsManager(databasePath);
}
private static void Run()
private static void InitCardsManager(string databasePath)
{
Init("cards.cdb");
GameClient client;
string EnvName = Environment.GetEnvironmentVariable("YGOPRO_NAME");
if (EnvName != null)
string currentPath = Path.GetFullPath(".");
string absolutePath = Path.Combine(currentPath, databasePath);
NamedCardsManager.Init(absolutePath);
}
private static void Run(object o)
{
#if !DEBUG
try
{
ProVersion = Int16.Parse(Environment.GetEnvironmentVariable("YGOPRO_VERSION"));
client = new GameClient(EnvName, Environment.GetEnvironmentVariable("YGOPRO_DECK"), Environment.GetEnvironmentVariable("YGOPRO_HOST"), Int32.Parse(Environment.GetEnvironmentVariable("YGOPRO_PORT")));
WindBotInfo Info = (WindBotInfo)o;
GameClient client = new GameClient(Info.Name, Info.Deck, Info.Host, Info.Port, Info.Dialog);
client.Start();
Logger.WriteLine(client.Username + " started.");
while (client.Connection.IsConnected)
{
try
{
client.Tick();
Thread.Sleep(30);
}
catch (Exception ex)
{
Logger.WriteErrorLine("Tick Error: " + ex);
}
}
Logger.WriteLine(client.Username + " end.");
}
else
catch (Exception ex)
{
client = new GameClient("谜之剑士LV4", "Blue-Eyes", "127.0.0.1", 7911);
Logger.WriteErrorLine("Run Error: " + ex);
}
#else
WindBotInfo Info = (WindBotInfo)o;
GameClient client = new GameClient(Info.Name, Info.Deck, Info.Host, Info.Port, Info.Dialog);
client.Start();
Logger.WriteLine(client.Username + " started.");
while (client.Connection.IsConnected)
{
client.Tick();
Thread.Sleep(30);
}
}
private static void InitCardsManager(string databasePath)
{
string currentPath = Path.GetFullPath(".");
string absolutePath = Path.Combine(currentPath, databasePath);
NamedCardsManager.Init(absolutePath);
Logger.WriteLine(client.Username + " end.");
#endif
}
}
}
......@@ -42,6 +42,7 @@
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
<Reference Include="YGOSharp.Network">
<HintPath>.\YGOSharp.Network.dll</HintPath>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment