public IEnumerator remotePVPSimulTest() { UnityLogger logger = new UnityLogger(); HostManager host = new GameObject(nameof(HostManager)).AddComponent <HostManager>(); host.logger = logger; ClientManager local = new GameObject(nameof(ClientManager)).AddComponent <ClientManager>(); local.logger = logger; //开房,打开Host,自己加入自己,房间应该有Option RoomInfo roomInfo = new RoomInfo(); roomInfo.setOption(new GameOption()); THHGame localGame = null; local.onConnected += () => { //发送玩家信息 RoomPlayerInfo playerInfo = new RoomPlayerInfo() { id = local.id, name = "玩家" + local.id, }; playerInfo.setDeck(new int[] { Reimu.ID }.Concat(Enumerable.Repeat(DrizzleFairy.ID, 30)).ToArray()); return(local.send(playerInfo)); }; local.onReceive += (id, obj) => { if (obj is RoomPlayerInfo newPlayerInfo) { //收到玩家信息 RoomInfo newRoomInfo = new RoomInfo() { playerList = new List <RoomPlayerInfo>(roomInfo.playerList) }; newRoomInfo.setOption(roomInfo.getOption()); newRoomInfo.playerList.Add(newPlayerInfo); //发送房间信息 return(local.send(newRoomInfo)); } else if (obj is RoomInfo newRoomInfo) { roomInfo = newRoomInfo; //收到房间信息 if (newRoomInfo.playerList.Count > 1) { localGame = TestGameflow.initGameWithoutPlayers("本地游戏", newRoomInfo.getOption()); (localGame.answers as AnswerManager).client = local; foreach (var playerInfo in newRoomInfo.playerList) { localGame.createPlayer(playerInfo.id, "玩家" + playerInfo.id, localGame.getCardDefine <MasterCardDefine>(playerInfo.getDeck()[0]), playerInfo.getDeck().Skip(1).Select(did => localGame.getCardDefine(did))); } localGame.run(); } } return(Task.CompletedTask); }; host.start(); local.start(); yield return(local.join(host.ip, host.port).wait()); Assert.AreEqual(1, roomInfo.playerList.Count); ClientManager remote = new GameObject(nameof(ClientManager)).AddComponent <ClientManager>(); remote.logger = logger; THHGame remoteGame = null; remote.onConnected += () => { //发送玩家信息 RoomPlayerInfo playerInfo = new RoomPlayerInfo() { id = remote.id, name = "玩家" + remote.id }; playerInfo.setDeck(new int[] { Reimu.ID }.Concat(Enumerable.Repeat(DrizzleFairy.ID, 30)).ToArray()); return(remote.send(playerInfo)); }; remote.onReceive += (id, obj) => { if (obj is RoomInfo newRoomInfo) { //收到房间信息 if (newRoomInfo.playerList.Count > 1) { remoteGame = TestGameflow.initGameWithoutPlayers("远端游戏", newRoomInfo.getOption()); (remoteGame.answers as AnswerManager).client = remote; foreach (var playerInfo in newRoomInfo.playerList) { remoteGame.createPlayer(playerInfo.id, "玩家" + playerInfo.id, remoteGame.getCardDefine <MasterCardDefine>(playerInfo.getDeck()[0]), playerInfo.getDeck().Skip(1).Select(did => remoteGame.getCardDefine(did))); } remoteGame.run(); } } return(Task.CompletedTask); }; //加入房间 remote.start(); yield return(remote.join(host.ip, host.port).wait()); //连接了,远程玩家把玩家信息发给本地,本地更新房间信息发给远端和开始游戏。 yield return(new WaitUntil(() => localGame != null && remoteGame != null)); Assert.True(localGame.isRunning); Assert.AreEqual(local.id, localGame.players[0].id); Assert.AreEqual(remote.id, localGame.players[1].id); Assert.True(remoteGame.isRunning); Assert.AreEqual(local.id, remoteGame.players[0].id); Assert.AreEqual(remote.id, remoteGame.players[1].id); THHPlayer localPlayer = localGame.getPlayer(local.id); Assert.AreEqual(0, localPlayer.id); yield return(new WaitUntil(() => localGame.answers.getRequests(localPlayer.id).FirstOrDefault() is InitReplaceRequest)); Assert.Greater(localPlayer.init.count, 0); localPlayer.cmdInitReplace(localGame); yield return(new WaitUntil(() => localGame.answers.getResponse(localPlayer.id, localGame.answers.getRequests(localPlayer.id).FirstOrDefault()) is InitReplaceResponse)); THHPlayer remotePlayer = remoteGame.getPlayer(remote.id); Assert.AreEqual(1, remotePlayer.id); yield return(new WaitUntil(() => remoteGame.answers.getRequests(remotePlayer.id).FirstOrDefault() is InitReplaceRequest)); Assert.Greater(remotePlayer.init.count, 0); remotePlayer.cmdInitReplace(remoteGame); yield return(new WaitUntil(() => remoteGame.triggers.getRecordedEvents().Any(e => e is THHGame.StartEventArg))); //拍怪 if (localGame.sortedPlayers[0] == localPlayer) { yield return(new WaitUntil(() => localGame.answers.getRequests(localPlayer.id).FirstOrDefault() is FreeActRequest)); localPlayer.cmdUse(localGame, localPlayer.hand[0], 0); yield return(new WaitUntil(() => localPlayer.field.count > 0)); localPlayer.cmdTurnEnd(localGame); yield return(new WaitUntil(() => localGame.currentPlayer != localPlayer)); } yield return(new WaitUntil(() => remoteGame.answers.getRequests(remotePlayer.id).FirstOrDefault() is FreeActRequest)); remotePlayer.cmdUse(remoteGame, remotePlayer.hand[0], 0); yield return(new WaitUntil(() => remotePlayer.field.count > 0)); remotePlayer.cmdTurnEnd(remoteGame); yield return(new WaitUntil(() => remoteGame.currentPlayer != remotePlayer)); if (localGame.sortedPlayers[0] != localPlayer) { yield return(new WaitUntil(() => localGame.answers.getRequests(localPlayer.id).FirstOrDefault() is FreeActRequest)); localPlayer.cmdUse(localGame, localPlayer.hand[0], 0); yield return(new WaitUntil(() => localPlayer.field.count > 0)); localPlayer.cmdTurnEnd(localGame); yield return(new WaitUntil(() => localGame.currentPlayer != localPlayer)); } do { yield return(new WaitUntil(() => localGame.currentPlayer == localPlayer || remoteGame.currentPlayer == remotePlayer)); if (localGame.currentPlayer == localPlayer) { localPlayer.cmdAttack(localGame, localPlayer.field[0], localGame.getOpponent(localPlayer).master); yield return(new WaitUntil(() => localPlayer.field[0].getAttackTimes(localGame) > 0)); localPlayer.cmdTurnEnd(localGame); yield return(new WaitUntil(() => localGame.currentPlayer != localPlayer)); } else if (remoteGame.currentPlayer == remotePlayer) { remotePlayer.cmdAttack(remoteGame, remotePlayer.field[0], remoteGame.getOpponent(remotePlayer).master); yield return(new WaitUntil(() => remotePlayer.field[0].getAttackTimes(remoteGame) > 0)); remotePlayer.cmdTurnEnd(remoteGame); yield return(new WaitUntil(() => remoteGame.currentPlayer != remotePlayer)); } }while (localGame.isRunning && remoteGame.isRunning); local.disconnect(); remote.disconnect(); yield break; }