private static Dictionary <String, String> AttachIdCarte(int pos, BgoPlayerAction action, Dictionary <String, String> otherData = null)
        {
            if (otherData == null)
            {
                otherData = new Dictionary <string, string>();
            }

            if (action.Data.Count > pos)
            {
                otherData.Add("idCarte", action.Data[pos].ToString());
            }

            return(otherData);
        }
Exemple #2
0
        public IEnumerator TakeInternalAction(TtaGame game, PlayerAction action, Action <List <PlayerAction> > callback)
        {
            BgoGame         bgoGame   = game as BgoGame;
            BgoPlayerAction bgoAction = action as BgoPlayerAction;
            Action <List <PlayerAction> > callbackDelegate = (actions) =>
            {
                if (callback != null)
                {
                    callback(actions);
                }
            };

            if (bgoAction == null)
            {
                LogRecorder.Log("Null Action!");
                return(null);
            }

            return(BgoPostProvider.PostInternalAction(sessionObject, bgoGame, bgoAction, callbackDelegate));
        }
        public static IEnumerator PostInternalAction(BgoSessionObject sessionObject, BgoGame game, BgoPlayerAction action,
                                                     Action <List <PlayerAction> > callback)
        {
            switch (action.ActionType)
            {
            case PlayerActionType.PlayActionCard:
                return(Perform2StepAction(sessionObject, game, action.Data[1].ToString(), callback));

            case PlayerActionType.BuildWonder:
                return(Perform2StepAction(sessionObject, game, action.Data[3].ToString(), callback));

            default:
                return(null);
            }
        }
        private static IEnumerator Perform2StepAction(BgoSessionObject sessionObject, BgoGame game, String actionValue,
                                                      Action <List <PlayerAction> > callback)
        {
            var postUrl = RemoveCharacterEntities(game.ActionFormSubmitUrl);

            WWWForm myPostData = new WWWForm();

            var actionSp = actionValue.Split(";".ToCharArray());
            var idCarte  = "0";

            foreach (var pair in game.ActionForm)
            {
                if (pair.Key == "action")
                {
                    myPostData.AddField("action", actionValue);
                }
                else if (pair.Key == "idCarteAction")
                {
                    if (actionSp.Length > 1 && actionSp[0] == "12")
                    {
                        idCarte = actionSp[1];
                        myPostData.AddField("idCarteAction", actionSp[1]);
                    }
                    else
                    {
                        myPostData.AddField("idCarteAction", pair.Value);
                    }
                }
                else
                {
                    myPostData.AddField(pair.Key, pair.Value);
                }
            }

            var cookieHeaders = myPostData.headers;

            cookieHeaders.Add("Cookie", "PHPSESSID=" + sessionObject._phpSession + "; identifiant=" + sessionObject._identifiant + "; mot_de_passe=" + sessionObject._motDePasse);

            //var data = Encoding.UTF8.GetString(myPostData.data);

            var www = new WWW(BgoBaseUrl + postUrl, myPostData.data, cookieHeaders);

            yield return(www);

            if (www.error != null)
            {
                Assets.CSharpCode.UI.Util.LogRecorder.Log(www.error);

                yield break;
            }

            var responseText = www.text;

            //Step1 Complete

            var labelMatches = BgoRegexpCollections.ExtractActionChoice.Matches(responseText);

            if (labelMatches.Count <= 0)
            {
                callback(new List <PlayerAction>());
                yield break;
            }
            var actions = new List <PlayerAction>();

            foreach (Match m in labelMatches)
            {
                var msg      = m.Groups[2].Value;
                var optValue = m.Groups[1].Value;

                BgoPlayerAction pa = new BgoPlayerAction {
                    ActionType = PlayerActionType.Unknown
                };
                pa.Data[0] = msg;
                pa.Data[1] = optValue;
                pa.Data[2] = idCarte;
                actions.Add(pa);
            }
            BgoActionFormater.FormatInternalAction(actions, responseText);

            if (callback != null)
            {
                callback(actions);
            }
        }
        public static IEnumerator PostAction(BgoSessionObject sessionObject, BgoGame game, BgoPlayerAction action,
                                             Action callback)
        {
            switch (action.ActionType)
            {
            //Special function
            case PlayerActionType.TakeCardFromCardRow:
            case PlayerActionType.PutBackCard:
                return(SendTakePutBackCardPostMessage(sessionObject, game, action.Data[3].ToString(),
                                                      action.Data[2].ToString(), callback));

            //----optvalue is [1]
            case PlayerActionType.ResetActionPhase:
            case PlayerActionType.PassPoliticalPhase:
            case PlayerActionType.EndActionPhase:
            case PlayerActionType.PlayActionCard:
            case PlayerActionType.ElectLeader:
            case PlayerActionType.SetupTactic:
            case PlayerActionType.AdoptTactic:
            case PlayerActionType.PlayColony:
            case PlayerActionType.PlayEvent:
                return(PerformAction(sessionObject, game, action.Data[1].ToString(), callback));

            //----optvalue is [2]
            case PlayerActionType.IncreasePopulation:
            case PlayerActionType.BuildBuilding:
            case PlayerActionType.Revolution:
            case PlayerActionType.ResolveEventOption:
            case PlayerActionType.DevelopTechCard:
                return(PerformAction(sessionObject, game, action.Data[2].ToString(), AttachIdCarte(3, action),
                                     callback));

            //----optvalue is [3]
            case PlayerActionType.UpgradeBuilding:
            case PlayerActionType.BuildWonder:
                return(PerformAction(sessionObject, game, action.Data[3].ToString(), AttachIdCarte(4, action), callback));

            //----has additional form
            case PlayerActionType.Destory:
            case PlayerActionType.Disband:
                return(PerformAction(sessionObject, game, action.Data[1].ToString(),
                                     new Dictionary <String, String>
                {
                    { "unite", action.Data[2].ToString() }
                },
                                     callback));

            case PlayerActionType.Aggression:
                return(PerformAction(sessionObject, game, action.Data[3].ToString(),
                                     new Dictionary <String, String>
                {
                    { action.Data[4].ToString(), action.Data[5].ToString() }
                },
                                     callback));

            //----Unknown
            case PlayerActionType.Unknown:
                return(PerformAction(sessionObject, game, action.Data[1].ToString(), callback));

            default:
                return(null);
            }
        }
        /// <summary>
        /// 填充面板的帮助方法
        /// </summary>
        /// <param name="html"></param>
        /// <param name="game"></param>
        // ReSharper disable once FunctionComplexityOverflow
        internal static void FillGameBoard(String html, BgoGame game)
        {
            //分析用户面板

            game.PossibleActions = new List <PlayerAction>();

            //解出卡牌列
            var matches = BgoRegexpCollections.ExtractCardRow.Matches(html);

            game.CardRow = new List <CardRowCardInfo>();

            foreach (Match mc in matches)
            {
                var card = civilopedia.GetCardInfoByName((Age)Enum.Parse(typeof(Age), mc.Groups[5].Value),
                                                         mc.Groups[6].Value);

                BgoCardRowCardInfo cardRowCardInfo = new BgoCardRowCardInfo
                {
                    Card            = card,
                    CanPutBack      = mc.Groups[4].Value.Contains("carteEnMain"),
                    CanTake         = mc.Groups[2].Value != "" && (!mc.Groups[4].Value.Contains("carteEnMain")),
                    CivilActionCost =
                        BgoRegexpCollections.ExtractGovenrmentAndActionPointsMissing.Matches(mc.Groups[8].Value).Count
                };

                //LogRecorder.Log(card.CardName + mc.Groups[2].Value);

                BgoPlayerAction pa = new BgoPlayerAction
                {
                    ActionType = PlayerActionType.TakeCardFromCardRow
                };
                pa.Data[0] = cardRowCardInfo;
                pa.Data[1] = game.CardRow.Count; //Card Row Pos
                pa.Data[2] = mc.Groups[3].Value; //idNote
                pa.Data[3] = mc.Groups[2].Value; //PostUrl

                //能拿能放回去,才能有Action
                if (cardRowCardInfo.CanPutBack || cardRowCardInfo.CanTake)
                {
                    game.PossibleActions.Add(pa);
                }

                game.CardRow.Add(cardRowCardInfo);
            }

            //当前事件
            var matchCurrentEvent = BgoRegexpCollections.ExtractCurrentEvent.Match(html);

            if (matchCurrentEvent.Groups[3].Value == "")
            {
                game.CurrentEventAge  = (Age)Enum.Parse(typeof(Age), matchCurrentEvent.Groups[5].Value);
                game.CurrentEventCard =
                    civilopedia.GetCardInfoByName(game.CurrentEventAge, matchCurrentEvent.Groups[6].Value);
                game.CurrentEventCount = matchCurrentEvent.Groups[7].Value;
            }
            else
            {
                game.CurrentEventAge   = (Age)Enum.Parse(typeof(Age), matchCurrentEvent.Groups[3].Value);
                game.CurrentEventCard  = null;
                game.CurrentEventCount = matchCurrentEvent.Groups[7].Value;
            }

            //未来事件
            var matchFutureEvent = BgoRegexpCollections.ExtractFutureEvent.Match(html);

            if (matchFutureEvent.Groups[1].Value.Length > 4)
            {
                game.FutureEventAge   = Age.A;
                game.FutureEventCount = "0";
            }
            else
            {
                game.FutureEventAge   = (Age)Enum.Parse(typeof(Age), matchFutureEvent.Groups[1].Value);
                game.FutureEventCount = matchFutureEvent.Groups[2].Value;
            }

            //卡牌剩余
            var matchCivilRemain = BgoRegexpCollections.ExtractCivilCardRemains.Match(html);

            game.CivilCardsRemain = Convert.ToInt32(matchCivilRemain.Groups[2].Value);
            var matchMilitaryRemain = BgoRegexpCollections.ExtractMilitryCardRemains.Match(html);

            game.MilitaryCardsRemain = Convert.ToInt32(matchMilitaryRemain.Groups[2].Value);

            //当前阶段
            var matchPhase = BgoRegexpCollections.ExtractGamePhase.Match(html);
            var phase      = matchPhase.Groups[1].Value;

            switch (phase.Trim())
            {
            case "Political Phase":
                game.CurrentPhase = TtaPhase.PoliticalPhase;
                break;

            case "Action Phase":
                game.CurrentPhase = TtaPhase.ActionPhase;
                break;

            case "Event Resolution":
                game.CurrentPhase = TtaPhase.EventResolution;
                break;

            case "Discard Phase":
                game.CurrentPhase = TtaPhase.DiscardPhase;
                break;

            default:
                game.CurrentPhase = TtaPhase.OtherPhase;
                break;
            }

            //时代和回合
            var matchAgeAndRound = BgoRegexpCollections.ExtractAgeAndRound.Match(html);

            game.CurrentAge   = (Age)Enum.Parse(typeof(Age), matchAgeAndRound.Groups[1].Value);
            game.CurrentRound = Convert.ToInt32(matchAgeAndRound.Groups[2].Value);

            //可抄袭阵型
            var sharedTacticsMatch = BgoRegexpCollections.ExtractSharedTactics.Match(html);

            game.SharedTactics = new List <CardInfo>();
            foreach (Match m in BgoRegexpCollections.ExtractSharedTacticsItem.Matches(sharedTacticsMatch.Groups[1].Value))
            {
                if (m.Groups[1].Value == "&nbsp;")
                {
                    continue;
                }
                game.SharedTactics.Add(civilopedia.GetCardInfoByName((Age)Enum.Parse(typeof(Age), m.Groups[1].Value),
                                                                     m.Groups[2].Value));
            }

            //拆开玩家面板

            matches     = BgoRegexpCollections.ExtractPlayerPlate.Matches(html);
            game.Boards = new List <TtaBoard>();

            int plateStart = -1;

            foreach (Match mc in matches)
            {
                if (plateStart != -1)
                {
                    String plate = html.Substring(plateStart, mc.Index - plateStart);

                    TtaBoard board = new TtaBoard();
                    game.Boards.Add(board);

                    FillPlayerBoard(board, plate);
                }

                plateStart = mc.Index;


                if (mc.Groups[2].Value != "")
                {
                    break;
                }
            }

            #region 校准名字和资源

            ExtractPlayerNameAndResource(html, game);

            #endregion


            //可用行动
            var subDropdown = BgoRegexpCollections.ExtractSubDropDown("action").Match(html);
            if (subDropdown.Success)
            {
                matches = BgoRegexpCollections.ExtractActions.Matches(subDropdown.Groups[1].Value);
                foreach (Match mc in matches)
                {
                    BgoPlayerAction pa = new BgoPlayerAction {
                        ActionType = PlayerActionType.Unknown
                    };
                    pa.Data[0] = mc.Groups[2].Value;
                    pa.Data[1] = mc.Groups[1].Value;

                    game.PossibleActions.Add(pa);
                }

                Match mSubmitForm = BgoRegexpCollections.ExtractSubmitForm.Match(html);
                game.ActionForm = new Dictionary <string, string>();

                if (mSubmitForm.Success)
                {
                    game.ActionFormSubmitUrl = mSubmitForm.Groups[1].Value;
                    matches = BgoRegexpCollections.ExtractSubmitFormDetail.Matches(mSubmitForm.Groups[2].Value);
                    foreach (Match mc in matches)
                    {
                        if (mc.Groups[2].Value != "")
                        {
                            game.ActionForm[mc.Groups[2].Value] = "";
                        }
                        else
                        {
                            game.ActionForm[mc.Groups[4].Value] = mc.Groups[5].Value;
                        }
                    }
                }
            }

            BgoActionFormater.Format((BgoGame)game, html);
        }