Exemple #1
0
 public BoardGame(IPlayerQueue playerQueue)
 {
     this.playerQueue = playerQueue;
     board            = new Board();
     CreateStartingBoard();
     SnapshotContainer = new SnapshotContainer <Turn>();
 }
Exemple #2
0
 public BoardGame(IPlayerQueue playerQueue)
 {
     this.playerQueue = playerQueue;
     board = new Board();
     CreateStartingBoard();
     SnapshotContainer = new SnapshotContainer<Turn>();
 }
        public void SetUp()
        {
            _model = new Model();

            var eventDeserializer = new EventDeserializer(SimpleJson.DeserializeObject);
            var eventSerializer   = new EventSerializer(SimpleJson.SerializeObject);
            var eventMapping      = new EventMapping(new Dictionary <string, Type>
            {
                { typeof(SnapshotStub).AssemblyQualifiedName, typeof(SnapshotStub) },
                { typeof(EventStub).AssemblyQualifiedName, typeof(EventStub) },
            });

            var snapshotContainer = new SnapshotContainer
            {
                Info =
                {
                    Position = 4,
                    Type     = typeof(SnapshotStub).AssemblyQualifiedName
                },
                Data = eventSerializer.SerializeObject(new SnapshotStub(1))
            };

            _sut = new RepositoryScenarioBuilder(eventMapping, eventDeserializer)
                   .ScheduleAppendToStream(_model.KnownIdentifier, new EventStub(0))
                   .ScheduleAppendToStream(_model.KnownIdentifier, new EventStub(1))
                   .ScheduleAppendToStream(_model.KnownIdentifier, new EventStub(2))
                   .ScheduleAppendToStream(_model.KnownIdentifier, new EventStub(3))
                   .ScheduleAppendToStream(_model.KnownIdentifier, new EventStub(4))
                   .ScheduleAppendToStream($"{_model.KnownIdentifier}-snapshots", snapshotContainer)
                   .ScheduleAppendToStream(_model.KnownIdentifier, new EventStub(5))
                   .ScheduleAppendToStream(_model.KnownIdentifier, new EventStub(6))
                   .BuildForRepository <AggregateRootEntitySnapshotableStub>();
        }
Exemple #4
0
 private static void ValidateSnapshot(SnapshotContainer container)
 {
     if (container.version == null || !container.version.Equals(ContainerVersion))
     {
         throw new IOException("Invalid state file. Unsupported version number.");
     }
     else if (!container.type.Equals(SnapshotTypeStateAndRemainingGCode))
     {
         throw new IOException("Invalid state file. Unknown type.");
     }
 }
Exemple #5
0
 public static PrintingStateSnapshot LoadSnapshotFile(Stream stream)
 {
     System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(SnapshotContainer));
     try
     {
         SnapshotContainer container = (SnapshotContainer)x.Deserialize(stream);
         ValidateSnapshot(container);
         return(container.snapshot);
     }
     catch (InvalidOperationException e)
     {
         throw new IOException("Invalid state file.", e);
     }
 }
Exemple #6
0
        public static void SaveSnapshotFile(PrintingStateSnapshot state, Stream fileStream)
        {
            SnapshotContainer container = new SnapshotContainer();

            container.type     = SnapshotTypeStateAndRemainingGCode;
            container.version  = ContainerVersion;
            container.snapshot = state;

            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(container.GetType());
            try
            {
                x.Serialize(fileStream, container);
            }
            catch (InvalidOperationException ex)
            {
                throw new IOException("Failed to write state file.", ex);
            }
        }
        private static async Task CreateSnapshot(
            ISnapshotable snapshotSupport,
            SnapshotStrategyContext context,
            IStreamStore streamStore,
            ConcurrentUnitOfWork uow,
            EventMapping eventMapping,
            EventSerializer eventSerializer,
            CancellationToken ct)
        {
            if (!snapshotSupport.Strategy.ShouldCreateSnapshot(context))
            {
                return;
            }

            var snapshot = snapshotSupport.TakeSnapshot();

            if (snapshot == null)
            {
                throw new InvalidOperationException("Snapshot missing.");
            }

            var snapshotContainer = new SnapshotContainer
            {
                Data = eventSerializer.SerializeObject(snapshot),
                Info =
                {
                    Type     = eventMapping.GetEventName(snapshot.GetType()),
                    Position = context.SnapshotPosition
                }
            };

            await streamStore.AppendToStream(
                uow.GetSnapshotIdentifier(context.Aggregate.Identifier),
                ExpectedVersion.Any,
                new NewStreamMessage(
                    Deterministic.Create(Deterministic.Namespaces.Events, $"snapshot-{context.SnapshotPosition}"),
                    $"SnapshotContainer<{snapshotContainer.Info.Type}>",
                    eventSerializer.SerializeObject(snapshotContainer)),
                ct);
        }
Exemple #8
0
        public void Play(Player p, int x, int y)
        {
            if (!IsValidPlay(p, x, y))
            {
                return;
            }

            var list = MakeListOfConvertedTokens(p, x, y);

            if (list.Any())
            {
                PutTokens(p, list);
                HandlePlayerChange();
                if (SnapshotContainer != null)
                {
                    SnapshotContainer.TakeSnapShot(new OnePartTurn()
                    {
                        Coord = new Coord(x, y), PlayerThatPlayed = p, PlayerToPlay = CurrentPlayer, Board = board.Clone()
                    });
                }
            }
        }
 private static void ValidateSnapshot(SnapshotContainer container)
 {
     if (container.version == null || !container.version.Equals(ContainerVersion))
     {
         throw new IOException("Invalid state file. Unsupported version number.");
     }
     else if (!container.type.Equals(SnapshotTypeStateAndRemainingGCode))
     {
         throw new IOException("Invalid state file. Unknown type.");
     }
 }
        private static void Serialize(SnapshotContainer container, Stream fileStream)
        {
            //System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(container.GetType());
            XElement[] extrudersTempNode = new XElement[container.snapshot.extrudersTemp.Count()];
            for (int i = 0; i < container.snapshot.extrudersTemp.Count(); i++)
            {
                extrudersTempNode[i] = new XElement("float", container.snapshot.extrudersTemp[i]);
            }

            XElement containerNode = new XElement("SnapshotContainer",
                new XElement("version", container.version),
                new XElement("type", container.type),
                new XElement("snapshot",
                    new XElement("x", container.snapshot.x),
                    new XElement("y", container.snapshot.y),
                    new XElement("z", container.snapshot.z),
                    new XElement("speed", container.snapshot.speed),
                    new XElement("fanVoltage", container.snapshot.fanVoltage),
                    new XElement("fanOn", container.snapshot.fanOn),
                    new XElement("relative", container.snapshot.relative),
                    new XElement("extrudersTemp", extrudersTempNode),
                    new XElement("bedTemp", container.snapshot.bedTemp),
                    new XElement("layer", container.snapshot.layer),
                    new XElement("activeExtruderId", container.snapshot.activeExtruderId),
                    new XElement("activeExtruderValue", container.snapshot.activeExtruderValue),
                    new XElement("remainingCode", container.snapshot.remainingCode)
                )
            );

            try
            {
                containerNode.Save(new StreamWriter(fileStream));
            }
            catch (InvalidOperationException ex)
            {
                throw new IOException("Failed to write state file.", ex);
            }
        }
        public static void SaveSnapshotFile(PrintingStateSnapshot state, Stream fileStream)
        {
            SnapshotContainer container = new SnapshotContainer();

            container.type = SnapshotTypeStateAndRemainingGCode;
            container.version = ContainerVersion;
            container.snapshot = state;

            Serialize(container, fileStream);
        }
        public static PrintingStateSnapshot LoadSnapshotFile(Stream stream)
        {
            XElement containerNode = XElement.Load(new StreamReader(stream));
            SnapshotContainer container = new SnapshotContainer();

            foreach (XElement elem in containerNode.Elements())
            {
                if ("version".Equals(elem.Name.LocalName))
                {
                    container.version = elem.Value;
                }
                else if ("type".Equals(elem.Name.LocalName))
                {
                    container.type = elem.Value;
                }
                else if ("snapshot".Equals(elem.Name.LocalName))
                {
                    container.snapshot = new PrintingStateSnapshot();
                    foreach (XElement elemCh in elem.Elements())
                    {
                        if ("x".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.x = float.Parse(elemCh.Value, CultureInfo.InvariantCulture);
                        }
                        else if ("y".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.y = float.Parse(elemCh.Value, CultureInfo.InvariantCulture);
                        }
                        else if ("z".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.z = float.Parse(elemCh.Value, CultureInfo.InvariantCulture);
                        }
                        else if ("speed".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.speed = float.Parse(elemCh.Value, CultureInfo.InvariantCulture);
                        }
                        else if ("fanVoltage".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.fanVoltage = float.Parse(elemCh.Value, CultureInfo.InvariantCulture);
                        }
                        else if ("fanOn".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.fanOn = bool.Parse(elemCh.Value);
                        }
                        else if ("relative".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.relative = bool.Parse(elemCh.Value);
                        }
                        else if ("extrudersTemp".Equals(elemCh.Name.LocalName))
                        {
                            LinkedList<float> extrudersTempList = new LinkedList<float>();
                            foreach (XElement elemChCh in elemCh.Elements())
                            {
                                if ("float".Equals(elemChCh.Name.LocalName))
                                {
                                    extrudersTempList.AddLast(float.Parse(elemCh.Value, CultureInfo.InvariantCulture));
                                }
                            }
                            container.snapshot.extrudersTemp = extrudersTempList.ToArray();
                        }

                        else if ("bedTemp".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.bedTemp = float.Parse(elemCh.Value, CultureInfo.InvariantCulture);
                        }
                        else if ("layer".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.layer = int.Parse(elemCh.Value, CultureInfo.InvariantCulture);
                        }
                        else if ("activeExtruderId".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.activeExtruderId = int.Parse(elemCh.Value, CultureInfo.InvariantCulture);
                        }
                        else if ("activeExtruderValue".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.activeExtruderValue = float.Parse(elemCh.Value, CultureInfo.InvariantCulture);
                        }
                        else if ("remainingCode".Equals(elemCh.Name.LocalName))
                        {
                            container.snapshot.remainingCode = elemCh.Value;
                        }
                    }
                }
            }
            try
            {
                ValidateSnapshot(container);
                return container.snapshot;
            }
            catch (InvalidOperationException e)
            {
                throw new IOException("Invalid state file.", e);
            }
        }
        public static void SaveSnapshotFile(PrintingStateSnapshot state, Stream fileStream)
        {
            SnapshotContainer container = new SnapshotContainer();

            container.type = SnapshotTypeStateAndRemainingGCode;
            container.version = ContainerVersion;
            container.snapshot = state;

            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(container.GetType());
            try
            {
                x.Serialize(fileStream, container);
            }
            catch (InvalidOperationException ex)
            {
                throw new IOException("Failed to write state file.", ex);
            }
        }