示例#1
0
文件: Blocks.cs 项目: cbries/ecos
 public Block(RailwayEssentialContext ctx)
 {
     Ctx         = ctx;
     _sidePlus   = new Blocks(ctx);
     _sideMinus  = new Blocks(ctx);
     WaitMode    = new BlockWaitMode();
     Exclude     = new List <BlockLockRef>();
     Locked      = false;
     Occupation  = null;
     Commuting   = false;
     PlusEvents  = new BlockEvents(ctx);
     MinusEvents = new BlockEvents(ctx);
 }
示例#2
0
文件: Blocks.cs 项目: cbries/ecos
        public bool Parse(JToken tkn)
        {
            var o = tkn as JObject;

            if (o == null)
            {
                return(false);
            }

            try
            {
                if (o["name"] != null)
                {
                    Name = o["name"].ToString();
                }
                if (o["description"] != null)
                {
                    Description = o["description"].ToString();
                }
                if (o["waitMode"] != null)
                {
                    var m = new BlockWaitMode();
                    if (m.Parse(o["waitMode"]))
                    {
                        WaitMode = m;
                    }
                    else
                    {
                        WaitMode = new BlockWaitMode
                        {
                            WaitMin = 1,
                            WaitMax = 60,
                            Mode    = "random"
                        }
                    };
                }

                if (o["exclude"] != null)
                {
                    if (Exclude == null)
                    {
                        Exclude = new List <BlockLockRef>();
                    }

                    var ar = o["exclude"] as JArray;
                    if (ar != null)
                    {
                        for (int i = 0; i < ar.Count; ++i)
                        {
                            var item    = ar[i];
                            var itemObj = new BlockLockRef();
                            if (itemObj.Parse(item))
                            {
                                Exclude.Add(itemObj);
                            }
                        }
                    }
                }

                if (o["locked"] != null)
                {
                    var lockTkn = o["locked"];
                    if (lockTkn != null)
                    {
                        if (lockTkn.Type == JTokenType.String)
                        {
                            var lockV = lockTkn.ToString();
                            if (!string.IsNullOrEmpty(lockV))
                            {
                                Locked = lockV.Equals("true", StringComparison.OrdinalIgnoreCase);
                            }
                            else
                            {
                                Locked = false;
                            }
                        }
                        else if (lockTkn.Type == JTokenType.Boolean)
                        {
                            Locked = (bool)lockTkn;
                        }
                        else if (lockTkn.Type == JTokenType.Integer)
                        {
                            Locked = (int)lockTkn == 1;
                        }
                    }
                }

                if (o["occupation"] != null)
                {
                    var locRef = new BlockLockRef();
                    if (locRef.Parse(o["occupation"]))
                    {
                        Occupation = locRef;
                    }
                }

                if (o["sidePlus"] != null)
                {
                    var arPlus = o["sidePlus"] as JArray;
                    if (arPlus != null)
                    {
                        foreach (var e in arPlus)
                        {
                            if (e == null)
                            {
                                continue;
                            }
                            if (_sidePlusNames.Contains(e.ToString()))
                            {
                                continue;
                            }
                            _sidePlusNames.Add(e.ToString());
                        }
                    }
                }

                if (o["sideMinus"] != null)
                {
                    var arMinus = o["sideMinus"] as JArray;
                    if (arMinus != null)
                    {
                        foreach (var e in arMinus)
                        {
                            if (e == null)
                            {
                                continue;
                            }
                            if (_sideMinusNames.Contains(e.ToString()))
                            {
                                continue;
                            }
                            _sideMinusNames.Add(e.ToString());
                        }
                    }
                }

                if (o["plusEvents"] != null)
                {
                    var evList = new BlockEvents(Ctx);
                    if (evList.Parse(o["plusEvents"]))
                    {
                        PlusEvents = evList;
                    }
                    else
                    {
                        PlusEvents = null;
                    }
                }

                if (o["minusEvents"] != null)
                {
                    var evList = new BlockEvents(Ctx);
                    if (evList.Parse(o["minusEvents"]))
                    {
                        MinusEvents = evList;
                    }
                    else
                    {
                        MinusEvents = null;
                    }
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }