示例#1
0
        public bool UpdateData(string xmlAction)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xmlAction);
                XmlNode xmlNode = xmlDoc.SelectSingleNode("/MatchInfo");
                if (xmlNode == null)
                {
                    LastErrorString = "查找<MatchInfo 失败";
                    return(false);
                }
                XmlNodeList xmlList = xmlNode.ChildNodes;
                if (xmlList.Count == 0)
                {
                    LastErrorString = "Acition list xml \"MatchInfo\"子节点为空";
                    return(false);
                }
                if (xmlList[0].Name != "Score")
                {
                    LastErrorString = "节点名不为Score,违反协议";
                    return(false);
                }

                string curMatch = xmlNode.Attributes["CurSubMatch_No"].Value.ToString();
                string curGame  = xmlNode.Attributes["CurGame_No"].Value.ToString();


                List <ScoreItem> scoreItems = new List <ScoreItem>();
                List <string>    gameXmlAll = new List <string>();

                //保存当前局的所有Score和xml
                foreach (XmlNode sNode in xmlList)
                {
                    if (sNode.Attributes["Match_No"].Value.ToString() == curMatch &&
                        sNode.Attributes["Game_No"].Value.ToString() == curGame)
                    {
                        ScoreItem item = new ScoreItem();
                        item.ActionOrder = Convert.ToInt32(sNode.Attributes["Order"].Value);
                        item.GameScoreA  = Convert.ToInt32(sNode.Attributes["GameScoreA"].Value);
                        item.GameScoreB  = Convert.ToInt32(sNode.Attributes["GameScoreB"].Value);
                        scoreItems.Add(item);
                        gameXmlAll.Add(sNode.OuterXml);
                    }
                }
                if (scoreItems.Count == 0)
                {
                    LastErrorString = "当前局下没有action,请检查xml文件";
                    return(false);
                }

                if (scoreItems[0].ActionOrder != 0)
                {
                    LastErrorString = "actionList_不是从1开始,请检查文件";
                    return(false);
                }

                //判断当前局是否变化,如果当前局变化则设置修改编号为0
                if (curMatch != curMatchOrder_.ToString() || curGame != curGameOrder_.ToString())
                {
                    AdvisedOrder = 0;
                }
                else
                {
                    //当前局未变化,则首先取出历史记录和新获取的记录数中最少的一个
                    int count = actionList_.Count <= scoreItems.Count ? actionList_.Count : scoreItems.Count;

                    //首先判断前面的数据是否有改变,如果有改变,则设置改变的那一条为修改编号
                    bool bChanged = false;
                    for (int i = 0; i < count; i++)
                    {
                        if (actionList_[i].ActionOrder != scoreItems[i].ActionOrder ||
                            actionList_[i].GameScoreA != scoreItems[i].GameScoreA ||
                            actionList_[i].GameScoreB != scoreItems[i].GameScoreB)
                        {
                            AdvisedOrder = actionList_[i].ActionOrder;
                            bChanged     = true;
                            break;
                        }
                    }

                    //前面的数据对比无误
                    if (!bChanged)
                    {
                        //如果数量也相等,说明完全相同,则只取最后一条用于更新比分
                        if (actionList_.Count == scoreItems.Count)
                        {
                            AdvisedOrder = -1;
                            NewXml       = gameXmlAll[gameXmlAll.Count - 1];
                            NewXml       = "<MatchInfo>" + NewXml + "</MatchInfo>";
                            return(true);
                        }
                        else
                        {
                            //数量不等,如果原记录小于新记录,则设置原记录为修改编号,如果原记录大于新记录,则从新记录设置为修改标志
                            AdvisedOrder = actionList_.Count < scoreItems.Count ? actionList_.Count: scoreItems.Count;
                        }
                    }
                }

                actionList_.Clear();
                foreach (ScoreItem sItem in scoreItems)
                {
                    actionList_.Add(sItem);
                }

                string strNewXml = "";
                //提取修改部分的xml,如果修改编号大于xml的数量,则说明是删除操作,strNewXml为空
                if (AdvisedOrder <= gameXmlAll.Count - 1)
                {
                    for (int i = AdvisedOrder; i < gameXmlAll.Count; i++)
                    {
                        strNewXml += gameXmlAll[i];
                    }
                }
                else
                {
                    strNewXml = "";
                }
                NewXml = strNewXml;
                if (NewXml.Trim() != "")
                {
                    NewXml = "<MatchInfo>" + NewXml + "</MatchInfo>";
                }

                curMatchOrder_ = Convert.ToInt32(curMatch);
                curGameOrder_  = Convert.ToInt32(curGame);
                return(true);
            }
            catch (System.Exception e)
            {
                LastErrorString = e.ToString();
                BDCommon.Writelog("ParsedActionXml异常", e.ToString());
                return(false);
            }
        }