mirror of
https://github.com/project-redbud/FunGame-Desktop.git
synced 2025-04-20 12:09:34 +08:00
匹配房间功能 (#19)
* 匹配房间 第一部分 * 修复开始匹配后按钮可用状态异常问题 * 优化匹配结果弹窗 * 优化匹配结束的UI变化 * 匹配成功后进入房间 * 需要再次检查匹配状态 --------- Co-authored-by: yeziuku <yezi@wrss.org>
This commit is contained in:
parent
83276adba7
commit
0882e526e8
1
.gitignore
vendored
1
.gitignore
vendored
@ -363,5 +363,6 @@ MigrationBackup/
|
||||
FodyWeavers.xsd
|
||||
|
||||
# FunGame Implement
|
||||
/FunGame.Implement/*.cs
|
||||
FunGame.Implement/Implement/*.cs
|
||||
FunGame.Implement/Implement/Implement/*.cs
|
@ -15,6 +15,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
private readonly Session Usercfg = RunTime.Session;
|
||||
private readonly DataRequest ChatRequest;
|
||||
private readonly DataRequest CreateRoomRequest;
|
||||
private readonly DataRequest MatchRoomRequest;
|
||||
private readonly DataRequest GetRoomPlayerCountRequest;
|
||||
private readonly DataRequest UpdateRoomRequest;
|
||||
private readonly DataRequest IntoRoomRequest;
|
||||
@ -25,6 +26,7 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
Main = main;
|
||||
ChatRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_Chat);
|
||||
CreateRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_CreateRoom);
|
||||
MatchRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_MatchRoom);
|
||||
GetRoomPlayerCountRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Room_GetRoomPlayerCount);
|
||||
UpdateRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_UpdateRoom);
|
||||
IntoRoomRequest = RunTime.NewLongRunningDataRequest(DataRequestType.Main_IntoRoom);
|
||||
@ -184,6 +186,29 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
return room;
|
||||
}
|
||||
|
||||
public async Task<bool> MatchRoomAsync(string RoomType, bool isCancel = false)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
try
|
||||
{
|
||||
MatchRoomRequest.AddRequestData("roomtype", RoomType);
|
||||
MatchRoomRequest.AddRequestData("matcher", Usercfg.LoginUser);
|
||||
MatchRoomRequest.AddRequestData("iscancel", isCancel);
|
||||
await MatchRoomRequest.SendRequestAsync();
|
||||
if (MatchRoomRequest.Result == RequestResult.Success)
|
||||
{
|
||||
result = MatchRoomRequest.GetResult<bool>("result");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo(), TimeType.None);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> ChatAsync(string msg)
|
||||
{
|
||||
try
|
||||
|
@ -147,6 +147,16 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
Close();
|
||||
}
|
||||
|
||||
protected override void SocketHandler_System(SocketObject ServerMessage)
|
||||
{
|
||||
// 收到系统消息,直接发送弹窗
|
||||
string msg = "";
|
||||
ShowMessageType type = ShowMessageType.General;
|
||||
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);
|
||||
}
|
||||
|
||||
protected override void SocketHandler_HeartBeat(SocketObject ServerMessage)
|
||||
{
|
||||
// 收到心跳包时更新与服务器的连接延迟
|
||||
@ -184,5 +194,23 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
if (ServerMessage.Length > 0) room = ServerMessage.GetParam<Room>(0) ?? General.HallInstance;
|
||||
if (room.Roomid != "-1" && room.Roomid == Usercfg.InRoom.Roomid) Main.UpdateUI(MainInvokeType.UpdateRoomMaster, room);
|
||||
}
|
||||
|
||||
protected override void SocketHandler_MatchRoom(SocketObject ServerMessage)
|
||||
{
|
||||
// 匹配成功,询问是否加入房间
|
||||
Config.FunGame_isMatching = false;
|
||||
Room room = General.HallInstance;
|
||||
if (ServerMessage.Length > 0) room = ServerMessage.GetParam<Room>(0) ?? General.HallInstance;
|
||||
if (room.Roomid == "-1")
|
||||
{
|
||||
Main.ShowMessage(ShowMessageType.General, "暂时无法找到符合条件的房间。", "匹配房间");
|
||||
Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Success, room);
|
||||
Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Cancel);
|
||||
}
|
||||
else if (Main.ShowMessage(ShowMessageType.YesNo, "已找到符合条件的房间,是否加入?", "匹配房间", 10) == MessageResult.Yes)
|
||||
{
|
||||
Main.UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Success, room);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
2
FunGame.Desktop/UI/Main/Main.Designer.cs
generated
2
FunGame.Desktop/UI/Main/Main.Designer.cs
generated
@ -555,6 +555,8 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
StopMatch.UseVisualStyleBackColor = true;
|
||||
StopMatch.Visible = false;
|
||||
StopMatch.Click += StopMatch_Click;
|
||||
StopMatch.MouseHover += StopMatch_MouseHover;
|
||||
StopMatch.MouseLeave += StopMatch_MouseLeave;
|
||||
//
|
||||
// Main
|
||||
//
|
||||
|
@ -26,15 +26,10 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
/**
|
||||
* 变量
|
||||
*/
|
||||
private Task? MatchFunGame = null; // 匹配线程(即将删除)
|
||||
private MainController? MainController = null;
|
||||
private readonly Core.Model.RoomList Rooms = RunTime.RoomList;
|
||||
private readonly Core.Model.Session Usercfg = RunTime.Session;
|
||||
|
||||
/**
|
||||
* 委托【即将删除】
|
||||
*/
|
||||
Action<int, object[]?>? StartMatch_Action = null;
|
||||
private int _MatchSeconds = 0; // 记录匹配房间的秒数
|
||||
|
||||
public Main()
|
||||
{
|
||||
@ -239,6 +234,13 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
}
|
||||
break;
|
||||
|
||||
case MainInvokeType.MatchRoom:
|
||||
if (objs != null && objs.Length > 0)
|
||||
{
|
||||
StartMatch_Method((StartMatchState)objs[0], objs[1..]);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -409,6 +411,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
SetRoomid(Usercfg.InRoom);
|
||||
QuitRoom.Visible = false;
|
||||
StartMatch.Visible = true;
|
||||
StopMatch.Visible = false;
|
||||
RoomSetting.Visible = false;
|
||||
CreateRoom.Visible = true;
|
||||
NowRoomID.Visible = false;
|
||||
@ -422,6 +425,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
{
|
||||
// 显示:退出房间、房间设置、当前房间号、复制房间号
|
||||
// 隐藏:停止匹配、创建房间
|
||||
StartMatch.Visible = false;
|
||||
StopMatch.Visible = false;
|
||||
QuitRoom.Visible = true;
|
||||
CreateRoom.Visible = false;
|
||||
@ -458,13 +462,17 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
CheckTeam.Enabled = isLogon;
|
||||
CheckHasPass.Enabled = isLogon;
|
||||
StartMatch.Enabled = isLogon;
|
||||
CreateRoom.Enabled = isLogon;
|
||||
RoomBox.Enabled = isLogon;
|
||||
AccountSetting.Enabled = isLogon;
|
||||
Stock.Enabled = isLogon;
|
||||
Store.Enabled = isLogon;
|
||||
if (!Config.FunGame_isMatching)
|
||||
{
|
||||
// 匹配中时不修改部分按钮状态
|
||||
RoomBox.Enabled = isLogon;
|
||||
CreateRoom.Enabled = isLogon;
|
||||
RefreshRoomList.Enabled = isLogon;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加入房间
|
||||
@ -539,110 +547,113 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
/// </summary>
|
||||
/// <param name="i">主要参数:触发方法的哪一个分支</param>
|
||||
/// <param name="objs">可传多个参数</param>
|
||||
private void StartMatch_Method(int i, object[]? objs = null)
|
||||
private void StartMatch_Method(StartMatchState status, params object[] objs)
|
||||
{
|
||||
switch (i)
|
||||
switch (status)
|
||||
{
|
||||
case (int)StartMatchState.Matching:
|
||||
case StartMatchState.Matching:
|
||||
// 开始匹配
|
||||
if (MainController != null)
|
||||
{
|
||||
TaskUtility.NewTask(async () =>
|
||||
{
|
||||
Config.FunGame_isMatching = true;
|
||||
int loop = 0;
|
||||
string roomid = Convert.ToString(new Random().Next(1, 10000));
|
||||
// 匹配中 匹配成功返回房间号
|
||||
Task.Factory.StartNew(() =>
|
||||
if (await MainController.MatchRoomAsync(Config.FunGame_GameMode))
|
||||
{
|
||||
// 创建新线程,防止主界面阻塞
|
||||
Thread.Sleep(3000);
|
||||
while (loop < 10000 && Config.FunGame_isMatching)
|
||||
// 开始匹配
|
||||
while (Config.FunGame_isMatching)
|
||||
{
|
||||
loop++;
|
||||
if (loop == Convert.ToInt32(roomid))
|
||||
if (_MatchSeconds < 60)
|
||||
{
|
||||
// 创建委托,操作主界面
|
||||
StartMatch_Action = (int i, object[]? objs) =>
|
||||
{
|
||||
StartMatch_Method(i, objs);
|
||||
};
|
||||
if (InvokeRequired)
|
||||
{
|
||||
Invoke(StartMatch_Action, (int)StartMatchState.Success, new object[] { roomid });
|
||||
await Task.Delay(1000);
|
||||
_MatchSeconds++;
|
||||
SetMatchSecondsText();
|
||||
continue;
|
||||
}
|
||||
else
|
||||
// 达到60秒时
|
||||
if (Config.FunGame_isMatching && await MainController.MatchRoomAsync(Config.FunGame_GameMode, true))
|
||||
{
|
||||
StartMatch_Action((int)StartMatchState.Success, new object[] { roomid });
|
||||
}
|
||||
// 取消匹配
|
||||
UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Success, General.HallInstance);
|
||||
UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Cancel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else Config.FunGame_isMatching = false;
|
||||
});
|
||||
break;
|
||||
case (int)StartMatchState.Success:
|
||||
Config.FunGame_isMatching = false;
|
||||
// 匹配成功返回房间号
|
||||
roomid = "-1";
|
||||
if (objs != null) roomid = (string)objs[0];
|
||||
if (!roomid.Equals(-1))
|
||||
{
|
||||
WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 匹配成功");
|
||||
WritelnGameInfo(">> 房间号: " + roomid);
|
||||
SetRoomid(GetRoom(roomid));
|
||||
}
|
||||
else
|
||||
{
|
||||
WritelnGameInfo("ERROR:匹配失败!");
|
||||
break;
|
||||
}
|
||||
// 设置按钮可见性
|
||||
InRoom();
|
||||
// 创建委托,操作主界面
|
||||
StartMatch_Action = (i, objs) =>
|
||||
break;
|
||||
case StartMatchState.Success:
|
||||
// 匹配成功返回房间号
|
||||
TaskUtility.NewTask(async () =>
|
||||
{
|
||||
StartMatch_Method(i, objs);
|
||||
};
|
||||
if (InvokeRequired)
|
||||
Room room = General.HallInstance;
|
||||
if (objs != null) room = (Room)objs[0];
|
||||
if (room.Roomid != "-1")
|
||||
{
|
||||
Invoke(StartMatch_Action, (int)StartMatchState.Enable, new object[] { true });
|
||||
GetMessage("匹配成功 -> 房间号: " + room.Roomid);
|
||||
if (MainController != null) await MainController.UpdateRoomAsync();
|
||||
if (Rooms.IsExist(room.Roomid))
|
||||
{
|
||||
Room target = Rooms[room.Roomid];
|
||||
await InvokeController_IntoRoom(target);
|
||||
}
|
||||
else
|
||||
{
|
||||
StartMatch_Action((int)StartMatchState.Enable, new object[] { true });
|
||||
GetMessage("加入房间失败!原因:房间号不存在或已被解散。");
|
||||
}
|
||||
MatchFunGame = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
GetMessage("匹配失败!暂时无法找到符合条件的房间。");
|
||||
}
|
||||
// 更新按钮图标和文字
|
||||
UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Enable, true);
|
||||
StopMatch.Visible = false;
|
||||
StartMatch.Visible = true;
|
||||
});
|
||||
break;
|
||||
case (int)StartMatchState.Enable:
|
||||
case StartMatchState.Enable:
|
||||
// 设置匹配过程中的各种按钮是否可用
|
||||
bool isPause = false;
|
||||
if (objs != null) isPause = (bool)objs[0];
|
||||
CheckMix.Enabled = isPause;
|
||||
CheckTeam.Enabled = isPause;
|
||||
CheckHasPass.Enabled = isPause;
|
||||
CreateRoom.Enabled = isPause;
|
||||
RoomBox.Enabled = isPause;
|
||||
Login.Enabled = isPause;
|
||||
bool isEnabel = false;
|
||||
if (objs != null) isEnabel = (bool)objs[0];
|
||||
CheckMix.Enabled = isEnabel;
|
||||
CheckTeam.Enabled = isEnabel;
|
||||
CheckHasPass.Enabled = isEnabel;
|
||||
CreateRoom.Enabled = isEnabel;
|
||||
RoomBox.Enabled = isEnabel;
|
||||
RefreshRoomList.Enabled = isEnabel;
|
||||
Logout.Enabled = isEnabel;
|
||||
break;
|
||||
case (int)StartMatchState.Cancel:
|
||||
case StartMatchState.Cancel:
|
||||
Config.FunGame_isMatching = false;
|
||||
WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 终止匹配");
|
||||
WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已终止匹配。");
|
||||
Config.FunGame_isMatching = false;
|
||||
StartMatch_Action = (i, objs) =>
|
||||
{
|
||||
StartMatch_Method(i, objs);
|
||||
};
|
||||
if (InvokeRequired)
|
||||
{
|
||||
Invoke(StartMatch_Action, (int)StartMatchState.Enable, new object[] { true });
|
||||
}
|
||||
else
|
||||
{
|
||||
StartMatch_Action((int)StartMatchState.Enable, new object[] { true });
|
||||
}
|
||||
MatchFunGame = null;
|
||||
UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Enable, true);
|
||||
StopMatch.Visible = false;
|
||||
StartMatch.Visible = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新当前匹配时间
|
||||
/// </summary>
|
||||
private void SetMatchSecondsText()
|
||||
{
|
||||
if (_MatchSeconds <= 0) StopMatch.Text = "停止匹配";
|
||||
if (_MatchSeconds < 60)
|
||||
{
|
||||
StopMatch.Text = _MatchSeconds + " 秒";
|
||||
}
|
||||
else StopMatch.Text = "1 分 " + (_MatchSeconds - 60) + " 秒";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 登录账号,显示登出按钮
|
||||
/// </summary>
|
||||
@ -685,18 +696,13 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
/// </summary>
|
||||
private void StopMatch_Click()
|
||||
{
|
||||
StartMatch_Action = (i, objs) =>
|
||||
TaskUtility.NewTask(async () =>
|
||||
{
|
||||
StartMatch_Method(i, objs);
|
||||
};
|
||||
if (InvokeRequired)
|
||||
if (MainController != null && await MainController.MatchRoomAsync(Config.FunGame_GameMode, true))
|
||||
{
|
||||
Invoke(StartMatch_Action, (int)StartMatchState.Cancel, new object[] { true });
|
||||
}
|
||||
else
|
||||
{
|
||||
StartMatch_Action((int)StartMatchState.Cancel, new object[] { true });
|
||||
UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Cancel);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -881,6 +887,25 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标离开停止匹配按钮时,恢复按钮文本为匹配时长
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private void StopMatch_MouseLeave(object? sender, EventArgs e) => SetMatchSecondsText();
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标轻触停止匹配按钮时,将文本从匹配时长转为停止匹配
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private void StopMatch_MouseHover(object? sender, EventArgs e)
|
||||
{
|
||||
StopMatch.Text = "停止匹配";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 事件
|
||||
@ -903,6 +928,8 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
private void StartMatch_Click(object sender, EventArgs e)
|
||||
{
|
||||
// 开始匹配
|
||||
_MatchSeconds = 0;
|
||||
SetMatchSecondsText();
|
||||
WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 开始匹配");
|
||||
WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 开始匹配");
|
||||
WriteGameInfo(">> 匹配参数:");
|
||||
@ -911,25 +938,9 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
StartMatch.Visible = false;
|
||||
StopMatch.Visible = true;
|
||||
// 暂停其他按钮
|
||||
StartMatch_Method((int)StartMatchState.Enable, new object[] { false });
|
||||
// 创建委托,开始匹配
|
||||
StartMatch_Action = (i, objs) =>
|
||||
{
|
||||
StartMatch_Method(i, objs);
|
||||
};
|
||||
// 创建新线程匹配
|
||||
MatchFunGame = Task.Factory.StartNew(() =>
|
||||
{
|
||||
|
||||
if (InvokeRequired)
|
||||
{
|
||||
Invoke(StartMatch_Action, (int)StartMatchState.Matching, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
StartMatch_Action((int)StartMatchState.Matching, null);
|
||||
}
|
||||
});
|
||||
UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Enable, false);
|
||||
// 开始匹配
|
||||
UpdateUI(MainInvokeType.MatchRoom, StartMatchState.Matching);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -1407,7 +1418,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
}
|
||||
break;
|
||||
case Constant.FunGame_Disconnect:
|
||||
if (Config.FunGame_isConnected && MainController != null)
|
||||
if (Config.FunGame_isConnected && !Config.FunGame_isMatching && MainController != null)
|
||||
{
|
||||
// 先退出登录再断开连接
|
||||
bool SuccessLogOut = false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user