实现自动连接服务器和自动登录

This commit is contained in:
Mili 2022-09-17 14:36:12 +08:00
parent 938bc19e12
commit 5a4e87595a
8 changed files with 223 additions and 76 deletions

View File

@ -46,6 +46,13 @@ namespace FunGame.Core.Api.Model.Enum
Gaming Gaming
} }
public enum ClientState
{
Online,
WaitConnect,
WaitLogin
}
#endregion #endregion
#region Type #region Type

View File

@ -62,9 +62,9 @@ namespace FunGame.Core.Api.Util
} }
/// <summary> /// <summary>
/// 初始化ini文件 /// 初始化服务器ini模板文件
/// </summary> /// </summary>
public void Init() public void InitServerConfigs()
{ {
/** /**
* Server * Server
@ -90,5 +90,23 @@ namespace FunGame.Core.Api.Util
WriteINI("MySQL", "DBUser", "root"); WriteINI("MySQL", "DBUser", "root");
WriteINI("MySQL", "DBPassword", "pass"); WriteINI("MySQL", "DBPassword", "pass");
} }
/// <summary>
/// 初始化客户端ini模板文件
/// </summary>
public void InitClientConfigs()
{
/**
* Config
*/
WriteINI("Config", "AutoConnect", "true");
WriteINI("Config", "AutoLogin", "false");
/**
* Account
*/
WriteINI("Account", "UserName", "");
WriteINI("Account", "Password", "");
WriteINI("Account", "AutoKey", "");
}
} }
} }

View File

@ -124,12 +124,13 @@ namespace FunGame.Desktop.Models.Component
Task.Run(() => Task.Run(() =>
{ {
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(action); Invoke(action);
else else
action(); action();
}); });
Show();
} }
ShowDialog(); else ShowDialog();
} }
/// <summary> /// <summary>

View File

@ -16,8 +16,20 @@ namespace FunGame.Desktop.Models.Config
*/ */
public static FunGameEnums.FunGame FunGameType = FunGameEnums.FunGame.FunGame_Desktop; public static FunGameEnums.FunGame FunGameType = FunGameEnums.FunGame.FunGame_Desktop;
public static INIHelper DefaultINIHelper = new(); public static INIHelper INIHelper = new();
public static AssemblyHelper DefaultAssemblyHelper = new(); public static AssemblyHelper AssemblyHelper = new();
/**
* FunGame Desktop Configs
*/
public static bool FunGame_isAutoConnect = true; // 是否自动连接服务器
public static bool FunGame_isAutoLogin = false; // 是否自动登录
public static bool FunGame_isMatching = false; // 是否在匹配中
public static bool FunGame_isConnected = false; // 是否连接上服务器
public static bool FunGame_isRetrying = false; // 是否正在重连
public static bool FunGame_isAutoRetry = true; // 是否自动重连
public static string FunGame_Roomid = "-1"; // 房间号
public static string FunGame_Notice = ""; // 公告
/** /**
* WebHelper Configs * WebHelper Configs
@ -26,10 +38,13 @@ namespace FunGame.Desktop.Models.Config
public const string WebHelper_SetGreenAndPing = "-WebHelper .set greenandping"; public const string WebHelper_SetGreenAndPing = "-WebHelper .set greenandping";
public const string WebHelper_SetRed = "-WebHelper .set red"; public const string WebHelper_SetRed = "-WebHelper .set red";
public const string WebHelper_SetYellow = "-WebHelper .set yellow"; public const string WebHelper_SetYellow = "-WebHelper .set yellow";
public const string WebHelper_WaitConnectAndSetYellow = "-WebHelper .waitconnect .set yellow";
public const string WebHelper_WaitLoginAndSetYellow = "-WebHelper .waitlogin .set yellow";
public const string WebHelper_Disconnected = "-WebHelper .disconnected"; public const string WebHelper_Disconnected = "-WebHelper .disconnected";
public const string WebHelper_LogOut = "-WebHelper .logout"; public const string WebHelper_LogOut = "-WebHelper .logout";
public const string WebHelper_GetUser = "-WebHelper .get user"; public const string WebHelper_GetUser = "-WebHelper .get user";
public const string WebHelper_SetUser = "-WebHelper .set user"; public const string WebHelper_SetUser = "-WebHelper .set user";
public const string WebHelper_SetNotice = "-WebHelper .set notice";
public static int WebHelper_HeartBeatFaileds = 0; public static int WebHelper_HeartBeatFaileds = 0;
/** /**
@ -55,6 +70,8 @@ namespace FunGame.Desktop.Models.Config
public const string FunGame_CreateMix = "创建游戏 混战"; public const string FunGame_CreateMix = "创建游戏 混战";
public const string FunGame_CreateTeam = "创建游戏 团队"; public const string FunGame_CreateTeam = "创建游戏 团队";
public const string FunGame_StartGame = "开始游戏"; public const string FunGame_StartGame = "开始游戏";
public const string FunGame_Connect = "连接服务器";
public const string FunGame_Disconnect = "断开连接";
public const string FunGame_Retry = "重新连接"; public const string FunGame_Retry = "重新连接";
public const string FunGame_AutoRetryOn = "开启自动重连"; public const string FunGame_AutoRetryOn = "开启自动重连";
public const string FunGame_AutoRetryOff = "关闭自动重连"; public const string FunGame_AutoRetryOff = "关闭自动重连";
@ -68,13 +85,21 @@ namespace FunGame.Desktop.Models.Config
FunGame_CreateMix, FunGame_CreateMix,
FunGame_CreateTeam, FunGame_CreateTeam,
FunGame_StartGame, FunGame_StartGame,
FunGame_Retry, FunGame_Disconnect,
FunGame_AutoRetryOn, FunGame_AutoRetryOn,
FunGame_AutoRetryOff FunGame_AutoRetryOff
}; };
public static readonly object[] PresetNoLoginItems = public static readonly object[] PresetNoLoginItems =
{ {
FunGame_PresetMessage, FunGame_PresetMessage,
FunGame_Disconnect,
FunGame_AutoRetryOn,
FunGame_AutoRetryOff
};
public static readonly object[] PresetNoConnectItems =
{
FunGame_PresetMessage,
FunGame_Connect,
FunGame_Retry, FunGame_Retry,
FunGame_AutoRetryOn, FunGame_AutoRetryOn,
FunGame_AutoRetryOff FunGame_AutoRetryOff

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace FunGame.Desktop.Models.Config namespace FunGame.Desktop.Models.Config
{ {
public static class Usercfg public class Usercfg
{ {
/** /**
* *
@ -17,10 +17,5 @@ namespace FunGame.Desktop.Models.Config
public static bool Match_Mix = false; // 混战模式选项 public static bool Match_Mix = false; // 混战模式选项
public static bool Match_Team = false; // 团队模式选项 public static bool Match_Team = false; // 团队模式选项
public static bool Match_HasPass = false; // 密码房间选项 public static bool Match_HasPass = false; // 密码房间选项
public static bool FunGame_isMatching = false; // 是否在匹配中
public static bool FunGame_isConnected = false; // 是否连接上服务器
public static bool FunGame_isRetrying = false; // 是否正在重连
public static bool FunGame_isAutoRetry = true; // 是否自动重连
public static string FunGame_roomid = "-1"; // 房间号
} }
} }

View File

@ -136,13 +136,13 @@ namespace FunGame.Desktop.UI
this.Connection.Name = "Connection"; this.Connection.Name = "Connection";
this.Connection.Size = new System.Drawing.Size(130, 23); this.Connection.Size = new System.Drawing.Size(130, 23);
this.Connection.TabIndex = 92; this.Connection.TabIndex = 92;
this.Connection.Text = "服务器连接失败"; this.Connection.Text = "等待连接服务器";
this.Connection.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.Connection.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// //
// Light // Light
// //
this.Light.BackColor = System.Drawing.Color.Transparent; this.Light.BackColor = System.Drawing.Color.Transparent;
this.Light.Image = global::FunGame.Desktop.Properties.Resources.red; this.Light.Image = global::FunGame.Desktop.Properties.Resources.yellow;
this.Light.Location = new System.Drawing.Point(777, 426); this.Light.Location = new System.Drawing.Point(777, 426);
this.Light.Name = "Light"; this.Light.Name = "Light";
this.Light.Size = new System.Drawing.Size(18, 18); this.Light.Size = new System.Drawing.Size(18, 18);

View File

@ -50,10 +50,11 @@ namespace FunGame.Desktop.UI
/// </summary> /// </summary>
public void Init() public void Init()
{ {
SetButtonEnableIfLogon(false); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
SetRoomid("-1"); // 房间号初始化 SetRoomid("-1"); // 房间号初始化
ShowFunGameInfo(); // 显示FunGame信息 ShowFunGameInfo(); // 显示FunGame信息
GetServerConnection(); // 开始连接服务器 GetFunGameConfig(); // 获取FunGame配置
if (Config.FunGame_isAutoConnect) GetServerConnection(); // 开始连接服务器
} }
#endregion #endregion
@ -76,77 +77,110 @@ namespace FunGame.Desktop.UI
switch (msg) switch (msg)
{ {
case Config.WebHelper_SetGreen: case Config.WebHelper_SetGreen:
Usercfg.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Green); SetServerStatusLight((int)LightType.Green);
SetButtonEnableIfLogon(true); SetButtonEnableIfLogon(true, ClientState.Online);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
else else
WebHelper_Action(this); WebHelper_Action(this);
Usercfg.FunGame_isConnected = true; Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0; NOW_CONNECTEDRETRY = 0;
break; break;
case Config.WebHelper_SetGreenAndPing: case Config.WebHelper_SetGreenAndPing:
Usercfg.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Green, GetServerPing(Config.SERVER_IPADRESS)); SetServerStatusLight((int)LightType.Green, ping: GetServerPing(Config.SERVER_IPADRESS));
SetButtonEnableIfLogon(true); SetButtonEnableIfLogon(true, ClientState.Online);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
else else
WebHelper_Action(this); WebHelper_Action(this);
Usercfg.FunGame_isConnected = true; Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0; NOW_CONNECTEDRETRY = 0;
break; break;
case Config.WebHelper_SetYellow: case Config.WebHelper_SetYellow:
Usercfg.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Yellow); SetServerStatusLight((int)LightType.Yellow);
SetButtonEnableIfLogon(false); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
else else
WebHelper_Action(this); WebHelper_Action(this);
Usercfg.FunGame_isConnected = true; Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0;
break;
case Config.WebHelper_WaitConnectAndSetYellow:
Config.FunGame_isRetrying = false;
WebHelper_Action = (main) =>
{
SetServerStatusLight((int)LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
};
if (InvokeRequired)
BeginInvoke(WebHelper_Action, this);
else
WebHelper_Action(this);
Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0;
if (WebHelper != null && Config.FunGame_isAutoConnect)
{
// 自动连接服务器
GetServerConnection();
}
break;
case Config.WebHelper_WaitLoginAndSetYellow:
Config.FunGame_isRetrying = false;
WebHelper_Action = (main) =>
{
SetServerStatusLight((int)LightType.Yellow, true);
SetButtonEnableIfLogon(false, ClientState.WaitLogin);
};
if (InvokeRequired)
BeginInvoke(WebHelper_Action, this);
else
WebHelper_Action(this);
Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0; NOW_CONNECTEDRETRY = 0;
break; break;
case Config.WebHelper_SetRed: case Config.WebHelper_SetRed:
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Red); SetServerStatusLight((int)LightType.Red);
SetButtonEnableIfLogon(false); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
else else
WebHelper_Action(this); WebHelper_Action(this);
Usercfg.FunGame_isConnected = false; Config.FunGame_isConnected = false;
break; break;
case Config.WebHelper_Disconnected: case Config.WebHelper_Disconnected:
Usercfg.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
Usercfg.FunGame_isConnected = false; Config.FunGame_isConnected = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Red); SetServerStatusLight((int)LightType.Red);
SetButtonEnableIfLogon(false); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
else else
WebHelper_Action(this); WebHelper_Action(this);
if (Usercfg.FunGame_isAutoRetry && NOW_CONNECTEDRETRY <= MAX_CONNECTEDRETRY) if (Config.FunGame_isAutoRetry && NOW_CONNECTEDRETRY <= MAX_CONNECTEDRETRY)
{ {
Task.Run(() => Task.Run(() =>
{ {
Thread.Sleep(5000); Thread.Sleep(5000);
if (Usercfg.FunGame_isAutoRetry) Connect(); // 再次判断是否开启自动重连 if (Config.FunGame_isAutoRetry) Connect(); // 再次判断是否开启自动重连
}); });
if (needTime) if (needTime)
throw new Exception(GetNowShortTime() + "\nERROR连接服务器失败5秒后自动尝试重连。"); throw new Exception(GetNowShortTime() + "\nERROR连接服务器失败5秒后自动尝试重连。");
@ -159,19 +193,20 @@ namespace FunGame.Desktop.UI
else else
throw new Exception("ERROR无法连接至服务器请检查你的网络连接。"); throw new Exception("ERROR无法连接至服务器请检查你的网络连接。");
case Config.WebHelper_LogOut: case Config.WebHelper_LogOut:
Usercfg.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
Usercfg.FunGame_isConnected = false; Config.FunGame_isConnected = false;
Config.FunGame_isAutoLogin = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Red); SetServerStatusLight((int)LightType.Red);
SetButtonEnableIfLogon(false); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
LogoutAccount(); LogoutAccount();
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
else else
WebHelper_Action(this); WebHelper_Action(this);
if (Usercfg.FunGame_isAutoRetry) if (Config.FunGame_isAutoRetry)
{ {
NOW_CONNECTEDRETRY = -1; NOW_CONNECTEDRETRY = -1;
Task.Run(() => Task.Run(() =>
@ -194,6 +229,21 @@ namespace FunGame.Desktop.UI
SetLoginUser(objs); SetLoginUser(objs);
} }
return null; return null;
case Config.WebHelper_SetNotice:
Action action = () =>
{
NoticeText.Text = Config.FunGame_Notice;
if (WebHelper != null && Config.FunGame_isAutoLogin)
{
// 自动登录
WebHelper.WebHelpMethod((int)WebHelperMethod.Login);
}
};
if (InvokeRequired)
BeginInvoke(action);
else
action();
return null;
default: default:
if (needTime) if (needTime)
WritelnGameInfo(webHelper, GetNowShortTime() + msg); WritelnGameInfo(webHelper, GetNowShortTime() + msg);
@ -223,7 +273,7 @@ namespace FunGame.Desktop.UI
{ {
try try
{ {
string? ipaddress = (string?)Config.DefaultAssemblyHelper.GetFunGameCoreValue((int)InterfaceType.ClientConnectInterface, (int)InterfaceMethod.RemoteServerIP); // 获取服务器IP string? ipaddress = (string?)Config.AssemblyHelper.GetFunGameCoreValue((int)InterfaceType.ClientConnectInterface, (int)InterfaceMethod.RemoteServerIP); // 获取服务器IP
if (ipaddress != null) if (ipaddress != null)
{ {
string[] s = ipaddress.Split(':'); string[] s = ipaddress.Split(':');
@ -261,7 +311,7 @@ namespace FunGame.Desktop.UI
} }
Main_Action = (main) => Main_Action = (main) =>
{ {
if (!Usercfg.FunGame_isConnected) if (!Config.FunGame_isConnected)
{ {
NOW_CONNECTEDRETRY++; NOW_CONNECTEDRETRY++;
if (NOW_CONNECTEDRETRY == 0) if (NOW_CONNECTEDRETRY == 0)
@ -284,7 +334,7 @@ namespace FunGame.Desktop.UI
WebHelper.WebHelpMethod((int)WebHelperMethod.CloseSocket); WebHelper.WebHelpMethod((int)WebHelperMethod.CloseSocket);
WebHelper = null; WebHelper = null;
} }
Usercfg.FunGame_isRetrying = true; Config.FunGame_isRetrying = true;
Application.DoEvents(); Application.DoEvents();
WebHelper = new WebHelper(main); WebHelper = new WebHelper(main);
WebHelper.WebHelpMethod((int)WebHelperMethod.CreateSocket); // Invoke -> CreateSocket WebHelper.WebHelpMethod((int)WebHelperMethod.CreateSocket); // Invoke -> CreateSocket
@ -319,18 +369,49 @@ namespace FunGame.Desktop.UI
}); });
} }
/// <summary>
/// 获取FunGame配置文件设定
/// </summary>
private void GetFunGameConfig()
{
try
{
if (Config.INIHelper.ExistINIFile())
{
string isAutoConncet = Config.INIHelper.ReadINI("Config", "AutoConnect");
string isAutoLogin = Config.INIHelper.ReadINI("Config", "AutoLogin");
if (isAutoConncet != null && !isAutoConncet.Equals("") && (isAutoConncet.Equals("false") || isAutoConncet.Equals("true")))
Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConncet);
else throw new Exception("ERROR: 读取配置文件出错,参数格式不正确");
if (isAutoLogin != null && !isAutoLogin.Equals("") && (isAutoLogin.Equals("false") || isAutoLogin.Equals("true")))
Config.FunGame_isAutoLogin = Convert.ToBoolean(isAutoLogin);
else throw new Exception("ERROR: 读取配置文件出错,参数格式不正确");
}
else
{
Config.INIHelper.InitClientConfigs();
WritelnGameInfo(">> 首次启动,已自动为你创建配置文件。");
GetFunGameConfig();
}
}
catch (Exception e)
{
WritelnGameInfo(GetNowTime() + e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace);
}
}
/// <summary> /// <summary>
/// 设置房间号和显示信息 /// 设置房间号和显示信息
/// </summary> /// </summary>
/// <param name="roomid"></param> /// <param name="roomid"></param>
private void SetRoomid(string roomid) private void SetRoomid(string roomid)
{ {
Usercfg.FunGame_roomid = roomid; Config.FunGame_Roomid = roomid;
if (!roomid.Equals("-1")) if (!roomid.Equals("-1"))
{ {
WritelnGameInfo(GetNowShortTime() + " 加入房间"); WritelnGameInfo(GetNowShortTime() + " 加入房间");
WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已加入房间 -> [ " + Usercfg.FunGame_roomid + " ]"); WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已加入房间 -> [ " + Config.FunGame_Roomid + " ]");
Room.Text = "[ 当前房间 ]\n" + Convert.ToString(Usercfg.FunGame_roomid); Room.Text = "[ 当前房间 ]\n" + Convert.ToString(Config.FunGame_Roomid);
} }
else else
Room.Text = "暂未进入房间"; Room.Text = "暂未进入房间";
@ -343,7 +424,7 @@ namespace FunGame.Desktop.UI
private void SetLoginUser(object[]? objs = null) private void SetLoginUser(object[]? objs = null)
{ {
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(LoginAccount, objs); Invoke(LoginAccount, objs);
else else
LoginAccount(objs); LoginAccount(objs);
} }
@ -416,7 +497,7 @@ namespace FunGame.Desktop.UI
// 显示:匹配、创建房间 // 显示:匹配、创建房间
// 隐藏:退出房间、房间设定 // 隐藏:退出房间、房间设定
WritelnGameInfo(GetNowShortTime() + " 离开房间"); WritelnGameInfo(GetNowShortTime() + " 离开房间");
WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已离开房间 -> [ " + Usercfg.FunGame_roomid + " ]"); WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已离开房间 -> [ " + Config.FunGame_Roomid + " ]");
SetRoomid("-1"); SetRoomid("-1");
QuitRoom.Visible = false; QuitRoom.Visible = false;
StartMatch.Visible = true; StartMatch.Visible = true;
@ -440,17 +521,22 @@ namespace FunGame.Desktop.UI
/// <summary> /// <summary>
/// 未登录和离线时,停用按钮 /// 未登录和离线时,停用按钮
/// </summary> /// </summary>
private void SetButtonEnableIfLogon(bool isLogon) private void SetButtonEnableIfLogon(bool isLogon, ClientState status)
{ {
if (isLogon) switch (status)
{ {
case ClientState.Online:
PresetText.Items.Clear(); PresetText.Items.Clear();
PresetText.Items.AddRange(Config.PresetOnineItems); PresetText.Items.AddRange(Config.PresetOnineItems);
} break;
else case ClientState.WaitConnect:
{ PresetText.Items.Clear();
PresetText.Items.AddRange(Config.PresetNoConnectItems);
break;
case ClientState.WaitLogin:
PresetText.Items.Clear(); PresetText.Items.Clear();
PresetText.Items.AddRange(Config.PresetNoLoginItems); PresetText.Items.AddRange(Config.PresetNoLoginItems);
break;
} }
this.PresetText.SelectedIndex = 0; this.PresetText.SelectedIndex = 0;
CheckMix.Enabled = isLogon; CheckMix.Enabled = isLogon;
@ -476,7 +562,7 @@ namespace FunGame.Desktop.UI
{ {
if (CheckRoomIDExist(roomid)) if (CheckRoomIDExist(roomid))
{ {
if (Usercfg.FunGame_roomid.Equals("-1")) if (Config.FunGame_Roomid.Equals("-1"))
{ {
if (ShowMessage.YesNoMessage("已找到房间 -> [ " + roomid + " ]\n是否加入", "已找到房间") == MessageResult.Yes) if (ShowMessage.YesNoMessage("已找到房间 -> [ " + roomid + " ]\n是否加入", "已找到房间") == MessageResult.Yes)
{ {
@ -504,7 +590,7 @@ namespace FunGame.Desktop.UI
{ {
if (CheckRoomIDExist(roomid)) if (CheckRoomIDExist(roomid))
{ {
if (Usercfg.FunGame_roomid.Equals("-1")) if (Config.FunGame_Roomid.Equals("-1"))
{ {
if (ShowMessage.YesNoMessage("已找到房间 -> [ " + roomid + " ]\n是否加入", "已找到房间") == MessageResult.Yes) if (ShowMessage.YesNoMessage("已找到房间 -> [ " + roomid + " ]\n是否加入", "已找到房间") == MessageResult.Yes)
{ {
@ -535,7 +621,7 @@ namespace FunGame.Desktop.UI
{ {
case (int)StartMatch_State.Matching: case (int)StartMatch_State.Matching:
// 开始匹配 // 开始匹配
Usercfg.FunGame_isMatching = true; Config.FunGame_isMatching = true;
int loop = 0; int loop = 0;
string roomid = Convert.ToString(new Random().Next(1, 10000)); string roomid = Convert.ToString(new Random().Next(1, 10000));
// 匹配中 匹配成功返回房间号 // 匹配中 匹配成功返回房间号
@ -543,7 +629,7 @@ namespace FunGame.Desktop.UI
{ {
// 创建新线程,防止主界面阻塞 // 创建新线程,防止主界面阻塞
Thread.Sleep(3000); Thread.Sleep(3000);
while (loop < 10000 && Usercfg.FunGame_isMatching) while (loop < 10000 && Config.FunGame_isMatching)
{ {
loop++; loop++;
if (loop == Convert.ToInt32(roomid)) if (loop == Convert.ToInt32(roomid))
@ -567,7 +653,7 @@ namespace FunGame.Desktop.UI
}); });
break; break;
case (int)StartMatch_State.Success: case (int)StartMatch_State.Success:
Usercfg.FunGame_isMatching = false; Config.FunGame_isMatching = false;
// 匹配成功返回房间号 // 匹配成功返回房间号
roomid = "-1"; roomid = "-1";
if (objs != null) roomid = (string)objs[0]; if (objs != null) roomid = (string)objs[0];
@ -613,7 +699,7 @@ namespace FunGame.Desktop.UI
case (int)StartMatch_State.Cancel: case (int)StartMatch_State.Cancel:
WritelnGameInfo(GetNowShortTime() + " 终止匹配"); WritelnGameInfo(GetNowShortTime() + " 终止匹配");
WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已终止匹配。"); WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已终止匹配。");
Usercfg.FunGame_isMatching = false; Config.FunGame_isMatching = false;
StartMatch_Action = (i, objs) => StartMatch_Action = (i, objs) =>
{ {
StartMatch_Method(i, objs); StartMatch_Method(i, objs);
@ -647,10 +733,7 @@ namespace FunGame.Desktop.UI
Login.Visible = false; Login.Visible = false;
Logout.Visible = true; Logout.Visible = true;
SetServerStatusLight((int)LightType.Green); SetServerStatusLight((int)LightType.Green);
Task.Run(() =>
{ // DEBUG
ShowMessage.TipMessage("欢迎回来, " + Usercfg.LoginUserName + "", "登录成功", 5); ShowMessage.TipMessage("欢迎回来, " + Usercfg.LoginUserName + "", "登录成功", 5);
});
} }
/// <summary> /// <summary>
@ -734,7 +817,7 @@ namespace FunGame.Desktop.UI
/// <param name="objs">可传多个参数</param> /// <param name="objs">可传多个参数</param>
private void CreateRoom_Method(int i, object[]? objs = null) private void CreateRoom_Method(int i, object[]? objs = null)
{ {
if (!Usercfg.FunGame_roomid.Equals("-1")) if (!Config.FunGame_Roomid.Equals("-1"))
{ {
ShowMessage.WarningMessage("已在房间中,无法创建房间。"); ShowMessage.WarningMessage("已在房间中,无法创建房间。");
return; return;
@ -777,7 +860,7 @@ namespace FunGame.Desktop.UI
/// </summary> /// </summary>
/// <param name="light"></param> /// <param name="light"></param>
/// <param name="ping"></param> /// <param name="ping"></param>
private void SetServerStatusLight(int light, int ping = 0) private void SetServerStatusLight(int light, bool waitlogin = false, int ping = 0)
{ {
switch(light) switch(light)
{ {
@ -786,7 +869,7 @@ namespace FunGame.Desktop.UI
this.Light.Image = Properties.Resources.green; this.Light.Image = Properties.Resources.green;
break; break;
case (int)LightType.Yellow: case (int)LightType.Yellow:
Connection.Text = "等待登录账号"; Connection.Text = waitlogin ? "等待登录账号" : "等待连接服务器";
this.Light.Image = Properties.Resources.yellow; this.Light.Image = Properties.Resources.yellow;
break; break;
case (int)LightType.Red: case (int)LightType.Red:
@ -1010,7 +1093,7 @@ namespace FunGame.Desktop.UI
/// <param name="e"></param> /// <param name="e"></param>
private void Login_Click(object sender, EventArgs e) private void Login_Click(object sender, EventArgs e)
{ {
if (WebHelper != null) if (WebHelper != null && Config.FunGame_isConnected)
WebHelper.WebHelpMethod((int)WebHelperMethod.Login); WebHelper.WebHelpMethod((int)WebHelperMethod.Login);
else else
ShowMessage.WarningMessage("请先连接服务器!"); ShowMessage.WarningMessage("请先连接服务器!");
@ -1033,7 +1116,7 @@ namespace FunGame.Desktop.UI
/// <param name="e"></param> /// <param name="e"></param>
private void RoomList_MouseDoubleClick(object sender, MouseEventArgs e) private void RoomList_MouseDoubleClick(object sender, MouseEventArgs e)
{ {
#pragma warning disable CS8600, CS8604 #pragma warning disable CS8600, CS8604
if (RoomList.SelectedItem != null) if (RoomList.SelectedItem != null)
JoinRoom(true, RoomList.SelectedItem.ToString()); JoinRoom(true, RoomList.SelectedItem.ToString());
} }
@ -1270,14 +1353,14 @@ namespace FunGame.Desktop.UI
break; break;
case Config.FunGame_AutoRetryOn: case Config.FunGame_AutoRetryOn:
WritelnGameInfo(">> 自动重连开启"); WritelnGameInfo(">> 自动重连开启");
Usercfg.FunGame_isAutoRetry = true; Config.FunGame_isAutoRetry = true;
break; break;
case Config.FunGame_AutoRetryOff: case Config.FunGame_AutoRetryOff:
WritelnGameInfo(">> 自动重连关闭"); WritelnGameInfo(">> 自动重连关闭");
Usercfg.FunGame_isAutoRetry = false; Config.FunGame_isAutoRetry = false;
break; break;
case Config.FunGame_Retry: case Config.FunGame_Retry:
if (!Usercfg.FunGame_isRetrying) if (!Config.FunGame_isRetrying)
{ {
NOW_CONNECTEDRETRY = -1; NOW_CONNECTEDRETRY = -1;
Connect(); Connect();
@ -1285,6 +1368,19 @@ namespace FunGame.Desktop.UI
else else
WritelnGameInfo(">> 你不能在连接服务器的同时重试连接!"); WritelnGameInfo(">> 你不能在连接服务器的同时重试连接!");
break; break;
case Config.FunGame_Connect:
if (!Config.FunGame_isConnected)
{
NOW_CONNECTEDRETRY = -1;
GetServerConnection();
}
break;
case Config.FunGame_Disconnect:
if (Config.FunGame_isConnected)
{
WritelnGameInfo(">> 实验性功能。");
}
break;
} }
} }

View File

@ -81,7 +81,7 @@ namespace FunGame.Desktop.Utils
client.Connect(server); client.Connect(server);
if (IsConnected()) if (IsConnected())
{ {
Main.GetMessage(this, Config.WebHelper_SetYellow); Main.GetMessage(this, Config.WebHelper_WaitLoginAndSetYellow);
break; break;
} }
} }
@ -95,7 +95,10 @@ namespace FunGame.Desktop.Utils
else else
objs = new object[] { main, socket }; objs = new object[] { main, socket };
if (Send((int)SocketMessageType.GetNotice, objs)) // 接触服务器并获取公告 if (Send((int)SocketMessageType.GetNotice, objs)) // 接触服务器并获取公告
{
main.GetMessage(this, " >> 连接服务器成功请登录账号以体验FunGame。", true); main.GetMessage(this, " >> 连接服务器成功请登录账号以体验FunGame。", true);
main.GetMessage(this, Config.WebHelper_SetNotice);
}
}; };
Task t = Task.Factory.StartNew(() => Task t = Task.Factory.StartNew(() =>
{ {
@ -158,7 +161,9 @@ namespace FunGame.Desktop.Utils
switch (type) switch (type)
{ {
case (int)SocketMessageType.GetNotice: case (int)SocketMessageType.GetNotice:
main.GetMessage(this, read, true); string[] reads = read.Split(';');
Config.FunGame_Notice = reads[1];
main.GetMessage(this, " >> 已连接至服务器: " + reads[0] + "。\n\n********** 服务器公告 **********\n\n" + Config.FunGame_Notice + "\n\n", true);
return true; return true;
case (int)SocketMessageType.Login: case (int)SocketMessageType.Login:
break; break;
@ -242,7 +247,7 @@ namespace FunGame.Desktop.Utils
} }
else else
{ {
Usercfg.FunGame_isAutoRetry = false; Config.FunGame_isAutoRetry = false;
throw new Exception("ERROR: 请登录账号。"); throw new Exception("ERROR: 请登录账号。");
} }
break; break;