コード例 #1
0
            //数据同步类型
            public static void AsyncInfo(NetAcyncType AcyncType)
            {
                if (Info.AgainstInfo.isPVP && (Info.AgainstInfo.isMyTurn || AcyncType == NetAcyncType.FocusCard || AcyncType == NetAcyncType.ExchangeCard || AcyncType == NetAcyncType.RoundStartExchangeOver))
                {
                    switch (AcyncType)
                    {
                    case NetAcyncType.FocusCard:
                    {
                        Location TargetCardLocation = Info.AgainstInfo.PlayerFocusCard != null ? Info.AgainstInfo.PlayerFocusCard.location : new Location(-1, -1);
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID, (int)TargetCardLocation.x, (int)TargetCardLocation.y).ToJson());
                        break;
                    }

                    case NetAcyncType.PlayCard:
                    {
                        Debug.Log("发出同步");
                        Location TargetCardLocation = Info.AgainstInfo.PlayerPlayCard.location;
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID, (int)TargetCardLocation.x, (int)TargetCardLocation.y).ToJson());

                        break;
                    }

                    case NetAcyncType.SelectRegion:
                    {
                        int RowRank = Info.AgainstInfo.SelectRegion.RowRank;
                        Debug.Log("同步焦点区域为" + RowRank);
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID, (int)RowRank).ToJson());

                        break;
                    }

                    case NetAcyncType.SelectUnites:
                    {
                        List <Location> Locations = Info.AgainstInfo.SelectUnits.Select(unite => unite.location).ToList();
                        Debug.LogError("发出的指令为:" + new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID, Locations).ToJson());
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID, Locations.ToJson()).ToJson());
                        Debug.LogError("选择单位完成");
                        break;
                    }

                    case NetAcyncType.SelectLocation:
                    {
                        int RowRank      = Info.AgainstInfo.SelectRegion.RowRank;
                        int LocationRank = Info.AgainstInfo.SelectLocation;
                        Debug.Log("同步焦点坐标给对面:" + RowRank);
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID, RowRank, LocationRank).ToJson());
                        break;
                    }

                    case NetAcyncType.ExchangeCard:
                    {
                        Debug.Log("触发交换卡牌信息");
                        Location Locat      = Info.AgainstInfo.TargetCard.location;
                        int      RandomRank = Info.AgainstInfo.RandomRank;
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID, Locat.ToJson(), RandomRank).ToJson());
                        break;
                    }

                    case NetAcyncType.RoundStartExchangeOver:
                        Debug.Log("触发回合开始换牌完成信息");
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID).ToJson());
                        break;

                    case NetAcyncType.Pass:
                    {
                        Debug.Log("pass");
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID).ToJson());
                        break;
                    }

                    case NetAcyncType.Surrender:
                    {
                        Debug.Log("投降");
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID).ToJson());
                        break;
                    }

                    case NetAcyncType.SelectProperty:
                    {
                        Debug.Log("选择场地属性");
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID, Info.AgainstInfo.SelectProperty).ToJson());
                        break;
                    }

                    case NetAcyncType.Init:
                        break;

                    case NetAcyncType.SelectBoardCard:
                        Debug.Log("同步面板卡牌数据选择");
                        AsyncConnect.Send(new GeneralCommand(AcyncType, Info.AgainstInfo.RoomID, Info.AgainstInfo.selectBoardCardRanks, Info.AgainstInfo.IsFinishSelectBoardCard).ToJson());



                        break;

                    default:
                    {
                        Debug.Log("异常同步指令");
                    }
                    break;
                    }
                }
            }
コード例 #2
0
            //初始化接收响应
            private static void InitAsyncConnection()
            {
                AsyncConnect = new WebSocket($"ws://{ip}/AsyncInfo");
                AsyncConnect.Connect();
                AsyncConnect.OnMessage += (sender, e) =>
                {
                    try
                    {
                        Debug.Log("收到信息" + e.Data);
                        object[]     receiveInfo = e.Data.ToObject <GeneralCommand>().Datas;
                        NetAcyncType Type        = (NetAcyncType)int.Parse(receiveInfo[0].ToString());
                        switch (Type)
                        {
                        case NetAcyncType.FocusCard:
                        {
                            int X = int.Parse(receiveInfo[2].ToString());
                            int Y = int.Parse(receiveInfo[3].ToString());
                            Info.AgainstInfo.OpponentFocusCard = Info.RowsInfo.GetCard(X, Y);
                            break;
                        }

                        case NetAcyncType.PlayCard:
                        {
                            //Debug.Log("触发卡牌同步");
                            int X = int.Parse(receiveInfo[2].ToString());
                            int Y = int.Parse(receiveInfo[3].ToString());
                            //Command.CardCommand.PlayCard(Info.RowsInfo.GetCard(X, Y), false).Wait();
                            Card targetCard = Info.RowsInfo.GetCard(X, Y);
                            Task.Run(async() =>
                                {
                                    await GameSystem.TransSystem.PlayCard(new TriggerInfo(targetCard, targetCard), false);
                                    Info.AgainstInfo.IsCardEffectCompleted = true;
                                });
                            break;
                        }

                        case NetAcyncType.SelectRegion:
                        {
                            //Debug.Log("触发区域同步");
                            int X = int.Parse(receiveInfo[2].ToString());
                            Info.AgainstInfo.SelectRegion = Info.RowsInfo.GetSingleRowInfoById(X);
                            break;
                        }

                        case NetAcyncType.SelectUnites:
                        {
                            //Debug.Log("收到同步单位信息为" + rawData);
                            List <Location> Locations = receiveInfo[2].ToString().ToObject <List <Location> >();
                            Info.AgainstInfo.SelectUnits.AddRange(Locations.Select(location => Info.RowsInfo.GetCard(location.x, location.y)));
                            break;
                        }

                        case NetAcyncType.SelectLocation:
                        {
                            Debug.Log("触发坐标同步");
                            int X = int.Parse(receiveInfo[2].ToString());
                            int Y = int.Parse(receiveInfo[3].ToString());
                            //Info.RowsInfo.SingleRowInfos.First(infos => infos.ThisRowCard == Info.RowsInfo.GlobalCardList[X]);
                            Info.AgainstInfo.SelectRegion   = Info.RowsInfo.GetSingleRowInfoById(X);
                            Info.AgainstInfo.SelectLocation = Y;
                            Debug.Log($"坐标为:{X}:{Y}");
                            Debug.Log($"信息为:{"gezi"}:{Info.AgainstInfo.SelectLocation}");
                            break;
                        }

                        case NetAcyncType.Pass:
                        {
                            GameUI.UiCommand.SetCurrentPass();
                            break;
                        }

                        case NetAcyncType.Surrender:
                        {
                            Task.Run(async() =>
                                {
                                    Debug.Log("收到结束指令");
                                    await StateCommand.BattleEnd(true, true);
                                });
                            break;
                        }

                        case NetAcyncType.ExchangeCard:
                        {
                            Debug.Log("交换卡牌信息");
                            // Debug.Log("收到信息" + rawData);
                            Location location   = receiveInfo[2].ToString().ToObject <Location>();
                            int      randomRank = int.Parse(receiveInfo[3].ToString());
                            _ = CardCommand.ExchangeCard(Info.RowsInfo.GetCard(location), false, RandomRank: randomRank);
                            break;
                        }

                        case NetAcyncType.RoundStartExchangeOver:
                            if (Info.AgainstInfo.isPlayer1)
                            {
                                Info.AgainstInfo.isPlayer2RoundStartExchangeOver = true;
                            }
                            else
                            {
                                Info.AgainstInfo.isPlayer1RoundStartExchangeOver = true;
                            }
                            break;

                        case NetAcyncType.SelectProperty:
                        {
                            Info.AgainstInfo.SelectProperty = (Region)int.Parse(receiveInfo[2].ToString());
                            Debug.Log("通过网络同步当前属性为" + Info.AgainstInfo.SelectProperty);
                            break;
                        }

                        case NetAcyncType.SelectBoardCard:
                            Info.AgainstInfo.selectBoardCardRanks    = receiveInfo[2].ToString().ToObject <List <int> >();;
                            Info.AgainstInfo.IsFinishSelectBoardCard = (bool)receiveInfo[3];
                            break;

                        case NetAcyncType.Init:
                            break;

                        default:
                            break;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        Debug.Log(ex);
                        throw;
                    }
                };
                AsyncConnect.OnError += (sender, e) =>
                {
                    Debug.Log("连接失败" + e.Message);
                    Debug.Log("连接失败" + e.Exception);
                };
                Debug.LogError("初始化数据" + new GeneralCommand(NetAcyncType.Init, Info.AgainstInfo.RoomID, Info.AgainstInfo.isPlayer1).ToJson());
                AsyncConnect.Send(new GeneralCommand(NetAcyncType.Init, Info.AgainstInfo.RoomID, Info.AgainstInfo.isPlayer1).ToJson());
            }