修改部分用法 (#29)

* 修改游戏相关

* 添加禁止房主使用准备和取消准备

* 因底层改动,使用了NewTask的地方都要Invoke。(所以当初一直await不好吗。。)
This commit is contained in:
milimoe 2024-10-07 22:02:57 +08:00 committed by GitHub
parent 1e5ee1fc2e
commit f442d71c41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 129 additions and 92 deletions

View File

@ -216,18 +216,19 @@ namespace Milimoe.FunGame.Desktop.Controller
}
}
public async Task<Room> CreateRoomAsync(RoomType RoomType, string GameModule, string GameMap, bool IsRank, string Password = "")
public async Task<Room> CreateRoomAsync(RoomType roomType, string gameModuleServer, string gameMap, bool isRank, int maxUsers, string password = "")
{
Room room = General.HallInstance;
try
{
CreateRoomRequest.AddRequestData("roomtype", RoomType);
CreateRoomRequest.AddRequestData("gamemodule", GameModule);
CreateRoomRequest.AddRequestData("gamemap", GameMap);
CreateRoomRequest.AddRequestData("roomtype", roomType);
CreateRoomRequest.AddRequestData("gamemoduleserver", gameModuleServer);
CreateRoomRequest.AddRequestData("gamemap", gameMap);
CreateRoomRequest.AddRequestData("master", Usercfg.LoginUser);
CreateRoomRequest.AddRequestData("password", Password);
CreateRoomRequest.AddRequestData("isrank", IsRank);
CreateRoomRequest.AddRequestData("password", password);
CreateRoomRequest.AddRequestData("isrank", isRank);
CreateRoomRequest.AddRequestData("maxusers", maxUsers);
await CreateRoomRequest.SendRequestAsync();
if (CreateRoomRequest.Result == RequestResult.Success)
{
@ -242,13 +243,13 @@ namespace Milimoe.FunGame.Desktop.Controller
return room;
}
public async Task<bool> MatchRoomAsync(RoomType RoomType, bool isCancel = false)
public async Task<bool> MatchRoomAsync(RoomType roomType, bool isCancel = false)
{
bool result = false;
try
{
MatchRoomRequest.AddRequestData("roomtype", RoomType);
MatchRoomRequest.AddRequestData("roomtype", roomType);
MatchRoomRequest.AddRequestData("matcher", Usercfg.LoginUser);
MatchRoomRequest.AddRequestData("iscancel", isCancel);
await MatchRoomRequest.SendRequestAsync();

View File

@ -276,9 +276,9 @@ namespace Milimoe.FunGame.Desktop.Controller
protected override void SocketHandler_Gaming(SocketObject ServerMessage)
{
GamingType gamingtype = GamingType.None;
Hashtable data = [];
Dictionary<string, object> data = [];
if (ServerMessage.Length > 0) gamingtype = ServerMessage.GetParam<GamingType>(0);
if (ServerMessage.Length > 1) data = ServerMessage.GetParam<Hashtable>(1) ?? data;
if (ServerMessage.Length > 1) data = ServerMessage.GetParam<Dictionary<string, object>>(1) ?? data;
RunTime.Gaming?.GamingHandler(gamingtype, data);
}
}

View File

@ -105,6 +105,16 @@ namespace Milimoe.FunGame.Desktop.Library.Component
return input;
}
/// <summary>
/// 委托更新UI
/// </summary>
/// <param name="action"></param>
public virtual void InvokeUpdateUI(Action action)
{
if (InvokeRequired) Invoke(action);
else action();
}
/// <summary>
/// 绑定事件,子类需要重写
/// </summary>
@ -197,16 +207,5 @@ namespace Milimoe.FunGame.Desktop.Library.Component
{
BindEvent();
}
/// <summary>
/// 委托更新UI
/// </summary>
/// <param name="action"></param>
protected virtual void InvokeUpdateUI(Action action)
{
if (InvokeRequired) Invoke(action);
else action();
}
}
}

View File

@ -244,7 +244,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component
ChangeSecond(msg, autoclose);
}
MessageResult = AutoResult;
Close();
InvokeUpdateUI(Dispose);
}
}
@ -273,7 +273,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component
/// <param name="s"></param>
private void ChangeSecond(string msg, int s)
{
MsgText.Text = msg + "\n[ " + s + " 秒后自动关闭 ]";
InvokeUpdateUI(() => MsgText.Text = msg + "\n[ " + s + " 秒后自动关闭 ]");
}
private void LeftButton_Click(object sender, EventArgs e)

View File

@ -122,7 +122,7 @@ namespace Milimoe.FunGame.Desktop.Library
}
public static object[] SupportedGameMap(GameModule module)
{
IEnumerable<object> list = module.GameModuleDepend.Maps.Where(map => module.GameModuleDepend.Maps.Contains(map)).Distinct();
IEnumerable<object> list = module.GameModuleDepend.Maps.Where(module.GameModuleDepend.Maps.Contains).Select(m => m.Name).Distinct();
if (list.Any()) return AllComboItem.Union(list).ToArray();
return ["- 缺少地图 -"];
}

View File

@ -22,12 +22,17 @@ namespace Milimoe.FunGame.Desktop.UI
{
if (RunTime.Socket != null)
{
string username = UsernameText.Text.Trim();
string email = EmailText.Text.Trim();
string username = "";
string email = "";
InvokeUpdateUI(() =>
{
username = UsernameText.Text.Trim();
email = EmailText.Text.Trim();
});
if (username == "" || email == "")
{
ShowMessage(ShowMessageType.Error, "账号或邮箱不能为空!");
UsernameText.Focus();
InvokeUpdateUI(() => UsernameText.Focus());
return;
}
@ -111,7 +116,7 @@ namespace Milimoe.FunGame.Desktop.UI
}
if (success)
{
Dispose();
InvokeUpdateUI(Dispose);
}
}
}

View File

@ -73,7 +73,7 @@ namespace Milimoe.FunGame.Desktop.UI
result = await Login_Handler();
}).OnCompleted(() =>
{
if (result) Dispose();
if (result) InvokeUpdateUI(Dispose);
else GoToLogin.Enabled = true;
});
}

View File

@ -403,9 +403,7 @@ namespace Milimoe.FunGame.Desktop.UI
/// </summary>
private void WritelnGameInfo()
{
GameInfo.AppendText("\n");
GameInfo.SelectionStart = GameInfo.Text.Length - 1;
GameInfo.ScrollToCaret();
WritelnGameInfo("\r\n");
}
/// <summary>
@ -416,9 +414,7 @@ namespace Milimoe.FunGame.Desktop.UI
{
if (msg.Trim() != "")
{
GameInfo.AppendText(msg + "\n");
GameInfo.SelectionStart = GameInfo.Text.Length - 1;
GameInfo.ScrollToCaret();
WriteGameInfo(msg + "\r\n");
}
}
@ -429,10 +425,13 @@ namespace Milimoe.FunGame.Desktop.UI
private void WriteGameInfo(string msg)
{
if (msg.Trim() != "")
{
InvokeUpdateUI(() =>
{
GameInfo.AppendText(msg);
GameInfo.SelectionStart = GameInfo.Text.Length - 1;
GameInfo.ScrollToCaret();
});
}
}
@ -490,16 +489,16 @@ namespace Milimoe.FunGame.Desktop.UI
TaskUtility.NewTask(async () =>
{
int PlayerCount = users.Count;
for (int i = 10; i > 0; i--)
for (int i = 5; i > 0; i--)
{
WritelnGameInfo("房间 [ " + room.Roomid + " (" + PlayerCount + "人" + RoomSet.GetTypeString(room.RoomType) + ") ] 的游戏将在" + i + "秒后开始...");
await Task.Delay(1000);
}
WritelnGameInfo("房间 [ " + room.Roomid + " (" + PlayerCount + "人" + RoomSet.GetTypeString(room.RoomType) + ") ] 的游戏正式开始!");
if (RunTime.GameModuleLoader?.Modules.ContainsKey(room.GameModule) ?? false)
if (RunTime.GameModuleLoader?.Modules.TryGetValue(room.GameModule, out GameModule? module) ?? false && module != null)
{
RunTime.Gaming = Core.Model.Gaming.StartGame(RunTime.GameModuleLoader[room.GameModule], room, RunTime.Session.LoginUser, users, RunTime.GameModuleLoader);
Visible = false; // 隐藏主界面
RunTime.Gaming = Core.Model.Gaming.StartGame(module, room, RunTime.Session.LoginUser, users, RunTime.GameModuleLoader);
Visible = !module.HideMain; // 隐藏主界面
}
else
{
@ -526,8 +525,8 @@ namespace Milimoe.FunGame.Desktop.UI
{
// 恢复界面
Visible = true;
_InGame = false;
SetButtonEnabled(true, ClientState.InRoom);
_InGame = false;
OnSucceedEndGameEvent(this, e);
RunTime.PluginLoader?.OnSucceedEndGameEvent(this, e);
}
@ -630,12 +629,12 @@ namespace Milimoe.FunGame.Desktop.UI
/// <summary>
/// 通过双击房间列表的房间号加入房间
/// </summary>
/// <param name="selectedindex"></param>
private async Task<bool> JoinRoom(int selectedindex)
/// <param name="selectedIndex"></param>
private async Task<bool> JoinRoom(int selectedIndex)
{
if (selectedindex != -1 && RunTime.RoomList.Count > selectedindex)
if (selectedIndex != -1 && RunTime.RoomList.Count > selectedIndex)
{
string roomid = RunTime.RoomList.ListRoom[selectedindex]?.Roomid ?? "";
string roomid = RunTime.RoomList.ListRoom[selectedIndex]?.Roomid ?? "";
return await JoinRoom_Handler(roomid);
}
return false;
@ -655,7 +654,8 @@ namespace Milimoe.FunGame.Desktop.UI
{
if (Usercfg.InRoom.Roomid == "-1")
{
if (await MainController.GetRoomPlayerCountAsync(roomid) < 8)
GameModule? gameModule = RunTime.GameModuleLoader?[RunTime.RoomList[roomid].GameModule];
if (await MainController.GetRoomPlayerCountAsync(roomid) < gameModule?.MaxUsers)
{
if (ShowMessage(ShowMessageType.YesNo, "已找到房间 -> [ " + roomid + " ]\n是否加入", "已找到房间") == MessageResult.Yes)
{
@ -890,23 +890,27 @@ namespace Milimoe.FunGame.Desktop.UI
/// <summary>
/// 创建房间的处理方法
/// </summary>
/// <param name="RoomType"></param>
/// <param name="Password"></param>
/// <param name="roomType"></param>
/// <param name="gameModule"></param>
/// <param name="gameMap"></param>
/// <param name="isRank"></param>
/// <param name="password"></param>
/// <returns></returns>
private async Task CreateRoom_Handler(RoomType RoomType, string GameModule, string GameMap, bool IsRank, string Password = "")
private async Task CreateRoom_Handler(RoomType roomType, string gameModule, string gameMap, bool isRank, string password = "")
{
if (Usercfg.InRoom.Roomid != "-1")
{
ShowMessage(ShowMessageType.Warning, "已在房间中,无法创建房间。");
return;
}
GameModule? mode = RunTime.GameModuleLoader?.Modules.Values.FirstOrDefault() ?? default;
if (mode is null)
GameModule? module = RunTime.GameModuleLoader?[gameModule];
if (module is null)
{
ShowMessage(ShowMessageType.Error, ">> 缺少" + Config.FunGame_RoomType + "所需的模组,无法创建房间。");
return;
}
Room room = await InvokeController_CreateRoom(RoomType, GameModule, GameMap, IsRank, Password);
string gameModuleServer = module.AssociatedServerModuleName;
Room room = await InvokeController_CreateRoom(roomType, gameModuleServer, gameMap, isRank, module.MaxUsers, password);
if (MainController is not null && room.Roomid != "-1")
{
await MainController.UpdateRoomAsync();
@ -1071,12 +1075,12 @@ namespace Milimoe.FunGame.Desktop.UI
string modmap = ComboGameMap.SelectedItem?.ToString() ?? all;
if (RunTime.GameModuleLoader is null || (modname != all && !RunTime.GameModuleLoader.Modules.ContainsKey(modname)))
{
ShowMessage(ShowMessageType.Error, ">> 模组未正确加载,无法创建房间。");
ShowMessage(ShowMessageType.Error, "模组未正确加载,无法创建房间。");
return;
}
if (RunTime.GameModuleLoader is null || (modmap != all && !RunTime.GameModuleLoader.Maps.ContainsKey(modmap)))
{
ShowMessage(ShowMessageType.Error, ">> 地图未正确加载,无法创建房间。");
ShowMessage(ShowMessageType.Error, "地图未正确加载,无法创建房间。");
return;
}
// 开始匹配
@ -1123,12 +1127,12 @@ namespace Milimoe.FunGame.Desktop.UI
}
if (RunTime.GameModuleLoader is null || (modname != all && !RunTime.GameModuleLoader.Modules.ContainsKey(modname)))
{
ShowMessage(ShowMessageType.Error, ">> 模组未正确加载,无法创建房间。");
ShowMessage(ShowMessageType.Error, "模组未正确加载,无法创建房间。");
return;
}
if (RunTime.GameModuleLoader is null || (modmap != all && !RunTime.GameModuleLoader.Maps.ContainsKey(modmap)))
{
ShowMessage(ShowMessageType.Error, ">> 地图未正确加载,无法创建房间。");
ShowMessage(ShowMessageType.Error, "地图未正确加载,无法创建房间。");
return;
}
if (CheckHasPass.Checked)
@ -1159,7 +1163,7 @@ namespace Milimoe.FunGame.Desktop.UI
{
WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " 离开房间");
WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 已离开房间 -> [ " + roomid + " ]");
InMain();
InvokeUpdateUI(InMain);
_ = MainController?.UpdateRoomAsync();
result = true;
}
@ -1276,7 +1280,7 @@ namespace Milimoe.FunGame.Desktop.UI
SetRoomTypeString();
ComboGameMap.Items.Clear();
ComboGameMap.Items.AddRange(Constant.SupportedGameMap(mod));
ComboGameMap.SelectedIndex = 0;
ComboGameMap.SelectedItem = mod.DefaultMap;
}
}
else
@ -1295,7 +1299,8 @@ namespace Milimoe.FunGame.Desktop.UI
{
if (RoomList.SelectedItem != null)
{
TaskUtility.NewTask(async () => await JoinRoom(RoomList.SelectedIndex));
int selected = RoomList.SelectedIndex;
TaskUtility.NewTask(async () => await JoinRoom(selected));
}
}
@ -1532,9 +1537,12 @@ namespace Milimoe.FunGame.Desktop.UI
/// <param name="sender"></param>
/// <param name="e"></param>
private void SucceedIntoRoomEvent(object sender, RoomEventArgs e)
{
InvokeUpdateUI(() =>
{
SetRoomid(e.Room);
InRoom();
});
}
/// <summary>
@ -1624,6 +1632,12 @@ namespace Milimoe.FunGame.Desktop.UI
if (MainController != null)
{
if (Usercfg.InRoom.Roomid != "-1")
{
if (Usercfg.LoginUser.Id == Usercfg.InRoom.RoomMaster.Id)
{
WritelnGameInfo(">> 房主无法使用此命令。");
}
else
{
TaskUtility.NewTask(async () =>
{
@ -1633,6 +1647,7 @@ namespace Milimoe.FunGame.Desktop.UI
}
});
}
}
else WritelnGameInfo(">> 不在房间中无法使用此命令。");
}
break;
@ -1643,6 +1658,12 @@ namespace Milimoe.FunGame.Desktop.UI
if (MainController != null)
{
if (Usercfg.InRoom.Roomid != "-1")
{
if (Usercfg.LoginUser.Id == Usercfg.InRoom.RoomMaster.Id)
{
WritelnGameInfo(">> 房主无法使用此命令。");
}
else
{
TaskUtility.NewTask(async () =>
{
@ -1652,6 +1673,7 @@ namespace Milimoe.FunGame.Desktop.UI
}
});
}
}
else WritelnGameInfo(">> 不在房间中无法使用此命令。");
}
break;
@ -1967,9 +1989,9 @@ namespace Milimoe.FunGame.Desktop.UI
/// </summary>
/// <param name="room"></param>
/// <returns></returns>
public async Task<Room> InvokeController_CreateRoom(RoomType RoomType, string GameModule, string GameMap, bool IsRank, string Password = "")
public async Task<Room> InvokeController_CreateRoom(RoomType roomType, string gameModuleServer, string gameMap, bool isRank, int maxUsers,string password = "")
{
RoomEventArgs EventArgs = new(RoomType, Password);
RoomEventArgs EventArgs = new(roomType, password);
Room room = General.HallInstance;
try
@ -1978,7 +2000,7 @@ namespace Milimoe.FunGame.Desktop.UI
RunTime.PluginLoader?.OnBeforeCreateRoomEvent(this, EventArgs);
if (EventArgs.Cancel) return room;
room = MainController is null ? room : await MainController.CreateRoomAsync(RoomType, GameModule, GameMap, IsRank, Password);
room = MainController is null ? room : await MainController.CreateRoomAsync(roomType, gameModuleServer, gameMap, isRank, maxUsers, password);
if (room.Roomid != "-1")
{
@ -2029,7 +2051,7 @@ namespace Milimoe.FunGame.Desktop.UI
OnSucceedQuitRoomEvent(this, EventArgs);
RunTime.PluginLoader?.OnSucceedQuitRoomEvent(this, EventArgs);
// 禁用和激活按钮,并切换预设快捷消息
SetButtonEnabled(true, ClientState.Online);
InvokeUpdateUI(() => SetButtonEnabled(true, ClientState.Online));
}
else
{
@ -2047,7 +2069,7 @@ namespace Milimoe.FunGame.Desktop.UI
OnAfterQuitRoomEvent(this, EventArgs);
RunTime.PluginLoader?.OnAfterQuitRoomEvent(this, EventArgs);
// 禁用和激活按钮,并切换预设快捷消息
SetButtonEnabled(true, ClientState.Online);
InvokeUpdateUI(() => SetButtonEnabled(true, ClientState.Online));
}
return result;

View File

@ -32,10 +32,17 @@ namespace Milimoe.FunGame.Desktop.UI
{
try
{
string username = UsernameText.Text.Trim();
string password = PasswordText.Text.Trim();
string checkpassword = CheckPasswordText.Text.Trim();
string email = EmailText.Text.Trim();
string username = "";
string password = "";
string checkpassword = "";
string email = "";
InvokeUpdateUI(() =>
{
username = UsernameText.Text.Trim();
password = PasswordText.Text.Trim();
checkpassword = CheckPasswordText.Text.Trim();
email = EmailText.Text.Trim();
});
if (username != "")
{
if (NetworkUtility.IsUserName(username))
@ -46,21 +53,21 @@ namespace Milimoe.FunGame.Desktop.UI
if (password != checkpassword)
{
ShowMessage(ShowMessageType.Error, "两个密码不相同,请重新输入!");
CheckPasswordText.Focus();
InvokeUpdateUI(() => CheckPasswordText.Focus());
return false;
}
}
else
{
ShowMessage(ShowMessageType.Error, "账号名长度不符合要求3~12个字符数一个中文2个字符");
UsernameText.Focus();
InvokeUpdateUI(() => UsernameText.Focus());
return false;
}
}
else
{
ShowMessage(ShowMessageType.Error, "账号名不符合要求:不能包含特殊字符");
UsernameText.Focus();
InvokeUpdateUI(() => UsernameText.Focus());
return false;
}
}
@ -70,26 +77,26 @@ namespace Milimoe.FunGame.Desktop.UI
if (length < 6 || length > 15) // 字节范围 6~15
{
ShowMessage(ShowMessageType.Error, "密码长度不符合要求6~15个字符数");
PasswordText.Focus();
InvokeUpdateUI(() => PasswordText.Focus());
return false;
}
}
if (username == "" || password == "" || checkpassword == "")
{
ShowMessage(ShowMessageType.Error, "请将账号和密码填写完整!");
UsernameText.Focus();
InvokeUpdateUI(() => UsernameText.Focus());
return false;
}
if (email == "")
{
ShowMessage(ShowMessageType.Error, "邮箱不能为空!");
EmailText.Focus();
InvokeUpdateUI(() => EmailText.Focus());
return false;
}
if (!NetworkUtility.IsEmail(email))
{
ShowMessage(ShowMessageType.Error, "这不是一个邮箱地址!");
EmailText.Focus();
InvokeUpdateUI(() => EmailText.Focus());
return false;
}
return await RegController.RegAsync(username, password, email);
@ -116,7 +123,10 @@ namespace Milimoe.FunGame.Desktop.UI
string username = ((RegisterEventArgs)e).Username;
string password = ((RegisterEventArgs)e).Password;
TaskUtility.NewTask(async () => await LoginController.LoginAccountAsync(username, password, encrypt: false));
RunTime.Login?.Close();
if (RunTime.Login != null)
{
RunTime.Login.InvokeUpdateUI(RunTime.Login.Close);
}
}
private void RegButton_Click(object sender, EventArgs e)
@ -124,8 +134,8 @@ namespace Milimoe.FunGame.Desktop.UI
RegButton.Enabled = false;
TaskUtility.NewTask(async () =>
{
if (!await Reg_Handler()) RegButton.Enabled = true;
else Close();
if (!await Reg_Handler()) InvokeUpdateUI(() => RegButton.Enabled = true);
else InvokeUpdateUI(Close);
});
}