房间系统优化;添加准备和取消准备功能 (#23)

* 房间列表添加显示房间名称

* 添加了准备和取消准备

* 释放DataRequest

* 修复准备提示文本问题

* 添加了房间相关命令的使用限制

* 添加了玩家是否在房间中的标记

* set button to correct enable

* fix ready&cancelready bug

* 开始游戏/提醒玩家准备/提醒房主开始游戏
This commit is contained in:
milimoe 2023-11-21 10:52:13 +08:00 committed by GitHub
parent 2fbb511ea7
commit d3ac188fa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 298 additions and 65 deletions

View File

@ -20,6 +20,7 @@ namespace Milimoe.FunGame.Desktop.Controller
private readonly DataRequest UpdateRoomRequest;
private readonly DataRequest IntoRoomRequest;
private readonly DataRequest QuitRoomRequest;
private readonly DataRequest StartGameRequest;
public MainController(Main main)
{
@ -31,6 +32,7 @@ namespace Milimoe.FunGame.Desktop.Controller
UpdateRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_UpdateRoom);
IntoRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_IntoRoom);
QuitRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_QuitRoom);
StartGameRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_StartGame);
}
#region
@ -43,6 +45,7 @@ namespace Milimoe.FunGame.Desktop.Controller
UpdateRoomRequest.Dispose();
IntoRoomRequest.Dispose();
QuitRoomRequest.Dispose();
StartGameRequest.Dispose();
}
public async Task<bool> LogOutAsync()
@ -136,6 +139,70 @@ namespace Milimoe.FunGame.Desktop.Controller
}
}
public async Task<bool> SetReadyAsync(string roomid)
{
try
{
bool result = true;
DataRequest request = RunTime.NewDataRequest(DataRequestType.Main_Ready);
request.AddRequestData("roomid", roomid);
await request.SendRequestAsync();
if (request.Result == RequestResult.Success)
{
result = request.GetResult<bool>("result");
if (result)
{
Config.FunGame_isInRoom = true;
Main.GetMessage("[ " + Usercfg.LoginUser.Username + " ] 准备完毕。");
}
List<User> ReadyPlayerList = request.GetResult<List<User>>("ready") ?? new();
if (ReadyPlayerList.Count > 0) Main.GetMessage("已准备的玩家:" + string.Join(", ", ReadyPlayerList.Select(u => u.Username)));
List<User> NotReadyPlayerList = request.GetResult<List<User>>("notready") ?? new();
if (NotReadyPlayerList.Count > 0) Main.GetMessage("仍未准备的玩家:" + string.Join(", ", NotReadyPlayerList.Select(u => u.Username)));
}
request.Dispose();
return result;
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
return false;
}
}
public async Task<bool> CancelReadyAsync(string roomid)
{
try
{
bool result = true;
DataRequest request = RunTime.NewDataRequest(DataRequestType.Main_CancelReady);
request.AddRequestData("roomid", roomid);
await request.SendRequestAsync();
if (request.Result == RequestResult.Success)
{
result = request.GetResult<bool>("result");
if (result)
{
Config.FunGame_isInRoom = false;
Main.GetMessage("[ " + Usercfg.LoginUser.Username + " ] 已取消准备。");
}
List<User> ReadyPlayerList = request.GetResult<List<User>>("ready") ?? new();
if (ReadyPlayerList.Count > 0) Main.GetMessage("已准备的玩家:" + string.Join(", ", ReadyPlayerList.Select(u => u.Username)));
List<User> NotReadyPlayerList = request.GetResult<List<User>>("notready") ?? new();
if (NotReadyPlayerList.Count > 0) Main.GetMessage("仍未准备的玩家:" + string.Join(", ", NotReadyPlayerList.Select(u => u.Username)));
}
request.Dispose();
return result;
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
return false;
}
}
public async Task<bool> QuitRoomAsync(string roomid, bool isMaster)
{
bool result = false;
@ -228,6 +295,26 @@ namespace Milimoe.FunGame.Desktop.Controller
}
}
public async Task<bool> StartGameAsync(string roomid, bool isMaster)
{
try
{
StartGameRequest.AddRequestData("roomid", roomid);
StartGameRequest.AddRequestData("isMaster", isMaster);
await StartGameRequest.SendRequestAsync();
if (StartGameRequest.Result == RequestResult.Success)
{
return true;
}
}
catch (Exception e)
{
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
}
return false;
}
#endregion
}
}

View File

@ -149,12 +149,15 @@ namespace Milimoe.FunGame.Desktop.Controller
protected override void SocketHandler_System(SocketObject ServerMessage)
{
// 收到系统消息,直接发送弹窗
// 收到系统消息,可输出消息或弹窗
string msg = "";
ShowMessageType type = ShowMessageType.General;
ShowMessageType type = ShowMessageType.None;
int autoclose = 60;
if (ServerMessage.Parameters.Length > 0) msg = ServerMessage.GetParam<string>(0) ?? "";
if (ServerMessage.Parameters.Length > 1) type = ServerMessage.GetParam<ShowMessageType>(1);
Main.ShowMessage(type, msg, "系统消息", 60);
if (ServerMessage.Parameters.Length > 2) autoclose = ServerMessage.GetParam<int>(2);
if (type == ShowMessageType.None) Main.GetMessage(msg);
else Main.ShowMessage(type, msg, "系统消息", autoclose);
}
protected override void SocketHandler_HeartBeat(SocketObject ServerMessage)

View File

@ -58,6 +58,15 @@ namespace Milimoe.FunGame.Desktop.Library
set => RunTime.Config.FunGame_isAutoRetry = value;
}
/// <summary>
/// 是否在房间中
/// </summary>
public static bool FunGame_isInRoom
{
get => RunTime.Config.FunGame_isInRoom;
set => RunTime.Config.FunGame_isInRoom = value;
}
/// <summary>
/// 当前游戏模式
/// </summary>

View File

@ -28,7 +28,9 @@ namespace Milimoe.FunGame.Desktop.Library
public const string FunGame_Retry = "重新连接";
public const string FunGame_AutoRetryOn = "开启自动重连";
public const string FunGame_AutoRetryOff = "关闭自动重连";
public static readonly object[] PresetOnineItems =
public const string FunGame_Ready = "准备就绪";
public const string FunGame_CancelReady = "取消准备";
public static readonly object[] PresetOnlineItems =
{
FunGame_PresetMessage,
FunGame_SignIn,
@ -38,11 +40,23 @@ namespace Milimoe.FunGame.Desktop.Library
FunGame_ClearGameInfo,
FunGame_CreateMix,
FunGame_CreateTeam,
FunGame_StartGame,
FunGame_Disconnect,
FunGame_AutoRetryOn,
FunGame_AutoRetryOff
};
public static readonly object[] PresetInRoomItems =
{
FunGame_PresetMessage,
FunGame_SignIn,
FunGame_ShowCredits,
FunGame_ShowStock,
FunGame_ShowStore,
FunGame_ClearGameInfo,
FunGame_Ready,
FunGame_CancelReady,
FunGame_StartGame,
FunGame_Disconnect
};
public static readonly object[] PresetNoLoginItems =
{
FunGame_PresetMessage,
@ -63,6 +77,7 @@ namespace Milimoe.FunGame.Desktop.Library
public enum ShowMessageType
{
None,
General,
Tip,
Warning,

View File

@ -101,23 +101,25 @@ namespace Milimoe.FunGame.Desktop.UI
{
case MainInvokeType.SetGreen:
Config.FunGame_isRetrying = false;
SetServerStatusLight((int)LightType.Green);
SetButtonEnableIfLogon(true, ClientState.Online);
SetServerStatusLight(LightType.Green);
if (Usercfg.InRoom.Roomid != "-1") SetButtonEnableIfLogon(true, ClientState.InRoom);
else SetButtonEnableIfLogon(true, ClientState.Online);
Config.FunGame_isConnected = true;
CurrentRetryTimes = 0;
break;
case MainInvokeType.SetGreenAndPing:
Config.FunGame_isRetrying = false;
SetServerStatusLight((int)LightType.Green, ping: NetworkUtility.GetServerPing(RunTime.Session.Server_IP));
SetButtonEnableIfLogon(true, ClientState.Online);
SetServerStatusLight(LightType.Green, ping: NetworkUtility.GetServerPing(RunTime.Session.Server_IP));
if (Usercfg.InRoom.Roomid != "-1") SetButtonEnableIfLogon(true, ClientState.InRoom);
else SetButtonEnableIfLogon(true, ClientState.Online);
Config.FunGame_isConnected = true;
CurrentRetryTimes = 0;
break;
case MainInvokeType.SetYellow:
Config.FunGame_isRetrying = false;
SetServerStatusLight((int)LightType.Yellow);
SetServerStatusLight(LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
Config.FunGame_isConnected = true;
CurrentRetryTimes = 0;
@ -125,7 +127,7 @@ namespace Milimoe.FunGame.Desktop.UI
case MainInvokeType.WaitConnectAndSetYellow:
Config.FunGame_isRetrying = false;
SetServerStatusLight((int)LightType.Yellow);
SetServerStatusLight(LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
Config.FunGame_isConnected = true;
CurrentRetryTimes = 0;
@ -138,14 +140,14 @@ namespace Milimoe.FunGame.Desktop.UI
case MainInvokeType.WaitLoginAndSetYellow:
Config.FunGame_isRetrying = false;
SetServerStatusLight((int)LightType.Yellow, true);
SetServerStatusLight(LightType.Yellow, true);
SetButtonEnableIfLogon(false, ClientState.WaitLogin);
Config.FunGame_isConnected = true;
CurrentRetryTimes = 0;
break;
case MainInvokeType.SetRed:
SetServerStatusLight((int)LightType.Red);
SetServerStatusLight(LightType.Red);
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
Config.FunGame_isConnected = false;
break;
@ -155,7 +157,7 @@ namespace Milimoe.FunGame.Desktop.UI
RoomList.Items.Clear();
Config.FunGame_isRetrying = false;
Config.FunGame_isConnected = false;
SetServerStatusLight((int)LightType.Red);
SetServerStatusLight(LightType.Red);
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
LogoutAccount();
MainController?.MainController_Disposed();
@ -170,7 +172,7 @@ namespace Milimoe.FunGame.Desktop.UI
Config.FunGame_isAutoConnect = false;
Config.FunGame_isAutoLogin = false;
Config.FunGame_isConnected = false;
SetServerStatusLight((int)LightType.Yellow);
SetServerStatusLight(LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect);
LogoutAccount();
MainController?.MainController_Disposed();
@ -182,7 +184,7 @@ namespace Milimoe.FunGame.Desktop.UI
case MainInvokeType.LogOut:
Config.FunGame_isRetrying = false;
Config.FunGame_isAutoLogin = false;
SetServerStatusLight((int)LightType.Yellow, true);
SetServerStatusLight(LightType.Yellow, true);
SetButtonEnableIfLogon(false, ClientState.WaitLogin);
LogoutAccount();
if (objs != null && objs.Length > 0)
@ -211,10 +213,19 @@ namespace Milimoe.FunGame.Desktop.UI
{
RoomList.Items.Clear();
Rooms.Clear();
Rooms.AddRooms((List<Room>)objs[0]);
foreach (string roomid in Rooms.ListRoomID)
List<Room> list = (List<Room>)objs[0];
Rooms.AddRooms(list);
foreach (Room r in list)
{
if (roomid != "-1") RoomList.Items.Add(roomid);
if (r.Roomid != "-1")
{
string item = r.Roomid;
if (r.Name.Trim() != "")
{
item += " [ " + r.Name + " ]";
}
RoomList.Items.Add(item);
}
}
}
break;
@ -432,6 +443,8 @@ namespace Milimoe.FunGame.Desktop.UI
RoomSetting.Visible = true;
NowRoomID.Visible = true;
CopyRoomID.Visible = true;
// 禁用和激活按钮,并切换预设快捷消息
SetButtonEnableIfLogon(true, ClientState.InRoom);
}
/// <summary>
@ -444,10 +457,6 @@ namespace Milimoe.FunGame.Desktop.UI
{
switch (status)
{
case ClientState.Online:
PresetText.Items.Clear();
PresetText.Items.AddRange(Constant.PresetOnineItems);
break;
case ClientState.WaitConnect:
PresetText.Items.Clear();
PresetText.Items.AddRange(Constant.PresetNoConnectItems);
@ -456,11 +465,16 @@ namespace Milimoe.FunGame.Desktop.UI
PresetText.Items.Clear();
PresetText.Items.AddRange(Constant.PresetNoLoginItems);
break;
case ClientState.Online:
PresetText.Items.Clear();
PresetText.Items.AddRange(Constant.PresetOnlineItems);
break;
case ClientState.InRoom:
PresetText.Items.Clear();
PresetText.Items.AddRange(Constant.PresetInRoomItems);
break;
}
this.PresetText.SelectedIndex = 0;
CheckMix.Enabled = isLogon;
CheckTeam.Enabled = isLogon;
CheckHasPass.Enabled = isLogon;
StartMatch.Enabled = isLogon;
AccountSetting.Enabled = isLogon;
Stock.Enabled = isLogon;
@ -471,6 +485,9 @@ namespace Milimoe.FunGame.Desktop.UI
RoomBox.Enabled = isLogon;
CreateRoom.Enabled = isLogon;
RefreshRoomList.Enabled = isLogon;
CheckMix.Enabled = isLogon;
CheckTeam.Enabled = isLogon;
CheckHasPass.Enabled = isLogon;
}
}
@ -810,19 +827,19 @@ namespace Milimoe.FunGame.Desktop.UI
/// </summary>
/// <param name="light"></param>
/// <param name="ping"></param>
private void SetServerStatusLight(int light, bool waitlogin = false, int ping = 0)
private void SetServerStatusLight(LightType light, bool waitlogin = false, int ping = 0)
{
switch (light)
{
case (int)LightType.Green:
case LightType.Green:
Connection.Text = "服务器连接成功";
this.Light.Image = Properties.Resources.green;
break;
case (int)LightType.Yellow:
case LightType.Yellow:
Connection.Text = waitlogin ? "等待登录账号" : "等待连接服务器";
this.Light.Image = Properties.Resources.yellow;
break;
case (int)LightType.Red:
case LightType.Red:
default:
Connection.Text = "服务器连接失败";
this.Light.Image = Properties.Resources.red;
@ -1335,8 +1352,6 @@ namespace Milimoe.FunGame.Desktop.UI
/// <param name="e"></param>
private void SucceedCreateRoomEvent(object sender, RoomEventArgs e)
{
SetRoomid(e.Room);
InRoom();
WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 创建" + e.RoomTypeString + "房间");
WritelnGameInfo(">> 创建" + e.RoomTypeString + "房间成功!房间号: " + e.RoomID);
ShowMessage(ShowMessageType.General, "创建" + e.RoomTypeString + "房间成功!\n房间号是 -> [ " + e.RoomID + " ]", "创建成功");
@ -1384,12 +1399,65 @@ namespace Milimoe.FunGame.Desktop.UI
GameInfo.Clear();
break;
case Constant.FunGame_CreateMix:
if (Usercfg.InRoom.Roomid == "-1")
{
TaskUtility.NewTask(() => CreateRoom_Handler(GameMode.Mix));
}
else WritelnGameInfo(">> 先退出当前房间才可以创建房间。");
break;
case Constant.FunGame_CreateTeam:
if (Usercfg.InRoom.Roomid == "-1")
{
TaskUtility.NewTask(() => CreateRoom_Handler(GameMode.Team));
}
else WritelnGameInfo(">> 先退出当前房间才可以创建房间。");
break;
case Constant.FunGame_Ready:
case ".r":
case ".ready":
if (MainController != null)
{
if (Usercfg.InRoom.Roomid != "-1")
{
TaskUtility.NewTask(async () =>
{
if (await MainController.SetReadyAsync(Usercfg.InRoom.Roomid))
{
await InvokeController_SendTalk(" [ " + Usercfg.LoginUser.Username + " ] 已准备。");
}
});
}
else WritelnGameInfo(">> 不在房间中无法使用此命令。");
}
break;
case Constant.FunGame_CancelReady:
case ".cr":
case ".ready -c":
case ".cancelready":
if (MainController != null)
{
if (Usercfg.InRoom.Roomid != "-1")
{
TaskUtility.NewTask(async () =>
{
if (await MainController.CancelReadyAsync(Usercfg.InRoom.Roomid))
{
await InvokeController_SendTalk(" [ " + Usercfg.LoginUser.Username + " ] 已取消准备。");
}
});
}
else WritelnGameInfo(">> 不在房间中无法使用此命令。");
}
break;
case Constant.FunGame_StartGame:
if (MainController != null)
{
if (Usercfg.InRoom.Roomid != "-1")
{
TaskUtility.NewTask(async () => await InvokeController_StartGame(Usercfg.InRoom.Roomid, Usercfg.InRoom.RoomMaster?.Id == Usercfg.LoginUser.Id));
}
else WritelnGameInfo(">> 不在房间中无法使用此命令。");
}
break;
case Constant.FunGame_AutoRetryOn:
WritelnGameInfo(">> 自动重连开启");
@ -1438,6 +1506,8 @@ namespace Milimoe.FunGame.Desktop.UI
}
break;
case Constant.FunGame_ConnectTo:
if (!Config.FunGame_isConnected)
{
string msg = ShowInputMessage("请输入服务器IP地址和端口号如: 127.0.0.1:22222。", "连接指定服务器");
if (msg.Equals("")) return true;
string[] addr = msg.Split(':');
@ -1470,6 +1540,7 @@ namespace Milimoe.FunGame.Desktop.UI
else if (ErrorType == ErrorIPAddressType.IsNotIP) ShowMessage(ShowMessageType.Error, "这不是一个IP地址");
else if (ErrorType == ErrorIPAddressType.IsNotPort) ShowMessage(ShowMessageType.Error, "这不是一个端口号!\n正确范围1~65535");
else ShowMessage(ShowMessageType.Error, "格式错误!\n这不是一个服务器地址。");
}
break;
default:
break;
@ -1725,6 +1796,8 @@ namespace Milimoe.FunGame.Desktop.UI
{
OnSucceedQuitRoomEvent(this, EventArgs);
RunTime.PluginLoader?.OnSucceedQuitRoomEvent(this, EventArgs);
// 禁用和激活按钮,并切换预设快捷消息
SetButtonEnableIfLogon(true, ClientState.Online);
}
else
{
@ -1741,6 +1814,8 @@ namespace Milimoe.FunGame.Desktop.UI
RunTime.PluginLoader?.OnFailedQuitRoomEvent(this, EventArgs);
OnAfterQuitRoomEvent(this, EventArgs);
RunTime.PluginLoader?.OnAfterQuitRoomEvent(this, EventArgs);
// 禁用和激活按钮,并切换预设快捷消息
SetButtonEnableIfLogon(true, ClientState.Online);
}
return result;
@ -1796,6 +1871,50 @@ namespace Milimoe.FunGame.Desktop.UI
return result;
}
/// <summary>
/// 开始游戏/提醒玩家准备/提醒房主开始游戏
/// </summary>
/// <param name="roomid"></param>
/// <param name="isMaster"></param>
/// <returns></returns>
public async Task<bool> InvokeController_StartGame(string roomid, bool isMaster)
{
GeneralEventArgs EventArgs = new();
bool result = false;
try
{
OnBeforeStartGameEvent(this, EventArgs);
RunTime.PluginLoader?.OnBeforeStartGameEvent(this, EventArgs);
if (EventArgs.Cancel) return result;
result = MainController is not null && await MainController.StartGameAsync(roomid, isMaster);
if (result)
{
OnSucceedStartGameEvent(this, EventArgs);
RunTime.PluginLoader?.OnSucceedStartGameEvent(this, EventArgs);
}
else
{
OnFailedStartGameEvent(this, EventArgs);
RunTime.PluginLoader?.OnFailedStartGameEvent(this, EventArgs);
}
OnAfterStartGameEvent(this, EventArgs);
RunTime.PluginLoader?.OnAfterStartGameEvent(this, EventArgs);
}
catch (Exception e)
{
GetMessage(e.GetErrorInfo(), TimeType.None);
OnFailedStartGameEvent(this, EventArgs);
RunTime.PluginLoader?.OnFailedStartGameEvent(this, EventArgs);
OnAfterStartGameEvent(this, EventArgs);
RunTime.PluginLoader?.OnAfterStartGameEvent(this, EventArgs);
}
return result;
}
#endregion
}
}