Ejemplo n.º 1
0
        public static void ParseCommand(string input, IWriter output, BlobDatabase database)
        {
            var tokens = input.Split(' ').ToArray();
            var commandType = tokens[0];
            switch (commandType)
            {
                case "create":
                    CreateCommand(database, tokens);
                    break;
                case "attack":
                    AttackCommand(database, tokens);
                    break;
                case "pass":

                    break;
                case "status":
                    StatusCommand(output, database);
                    break;
                case "report-events":
                    //todo: report events
                    break;
                default:
                    throw new InvalidOperationException("Invalid Command.");
            }

            foreach (var blob in database)
            {
                if (blob.BlobBehavior.HasBeenTriggered)
                {
                    blob.BlobBehavior.EndTurnAction(blob);
                }
            }
        }
Ejemplo n.º 2
0
        static void Main()
        {
            IReader input = new ConsoleReader();
            IWriter output = new ConsoleWriter();
            BlobDatabase database = new BlobDatabase();
            BlobEngine engine = new BlobEngine(database, input, output);

            engine.Run();
        }
Ejemplo n.º 3
0
        public async Task <BlobDBResponsePacket> Clear(BlobDatabase database)
        {
            var clearCommand = new BlobDBCommandPacket()
            {
                Token    = GenerateToken(),
                Database = database,
                Command  = BlobCommand.Clear
            };

            return(await Send(clearCommand));
        }
Ejemplo n.º 4
0
        private static void ValidateLocalBlobDatabase(string blobDBpath,
                                                      string installDirectory)
        {
            if (!BlobDatabase.ValidateBlobLocalDatabase(blobDBpath, installDirectory))
            {
                Console.WriteLine("Could not get valid blob local database instance");

                return;
            }

            Console.WriteLine("Valid local blob database instance found");
        }
        public void SavesUnsupportedTypes()
        {
            UnsupportedTypes item = null;

            var serializer = new BlobSerializerDelegate(
                obj =>
            {
                if (obj is DateTimeOffset)
                {
                    Assert.AreEqual(item.DateTimeOffset, obj);
                    var offset = (DateTimeOffset)obj;
                    var bytes  = new byte[16];
                    Buffer.BlockCopy(BitConverter.GetBytes(offset.Ticks), 0, bytes, 0, 8);
                    Buffer.BlockCopy(BitConverter.GetBytes(offset.Offset.Ticks), 0, bytes, 8, 8);
                    return(bytes);
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", obj.GetType()));
            },
                (d, t) =>
            {
                if (t == typeof(DateTimeOffset))
                {
                    var ticks  = BitConverter.ToInt64(d, 0);
                    var offset = BitConverter.ToInt64(d, 8);
                    return(new DateTimeOffset(ticks, TimeSpan.FromTicks(offset)));
                }

                if (t == typeof(TimeSpan))
                {
                    return(TimeSpan.FromTicks(BitConverter.ToInt64(d, 0)));
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", t));
            },
                t => true);

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <UnsupportedTypes>();
                item = new UnsupportedTypes()
                {
                    Id             = Guid.NewGuid(),
                    DateTimeOffset = DateTime.Now
                };

                db.Insert(item);
                var dbItem = db.Find <UnsupportedTypes>(item.Id);

                Assert.AreEqual(item.Id, dbItem.Id);
                Assert.AreEqual(item.DateTimeOffset, dbItem.DateTimeOffset);
            }
        }
Ejemplo n.º 6
0
        public async Task <BlobDBResponsePacket> Delete(BlobDatabase database, byte[] key)
        {
            var deleteCommand = new BlobDBCommandPacket()
            {
                Token    = GenerateToken(),
                Database = database,
                Command  = BlobCommand.Delete,
                Key      = key,
            };

            return(await Send(deleteCommand));
        }
Ejemplo n.º 7
0
        public async Task <BlobDBResponsePacket> Insert(BlobDatabase database, byte[] key, byte[] value)
        {
            var insertCommand = new BlobDBCommandPacket()
            {
                Token    = GenerateToken(),
                Database = database,
                Command  = BlobCommand.Insert,
                Key      = key,
                Value    = value
            };

            return(await Send(insertCommand));
        }
Ejemplo n.º 8
0
        public void SavesUnsupportedTypes()
        {
            UnsupportedTypes item = null;

            var serializer = new BlobSerializerDelegate(
                obj =>
            {
                if (obj is DivideByZeroException)
                {
                    var e    = (DivideByZeroException)obj;
                    var json = JsonConvert.SerializeObject(e);            // subst your own serializer
                    return(Encoding.UTF8.GetBytes(json));
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", obj.GetType()));
            },
                (d, t) =>
            {
                if (t == typeof(DivideByZeroException))
                {
                    var json   = Encoding.UTF8.GetString(d);
                    var result = JsonConvert.DeserializeObject <DivideByZeroException>(json);
                    return(result);
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", t));
            },
                t => true);

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <UnsupportedTypes>();
                item = new UnsupportedTypes()
                {
                    Id                    = Guid.NewGuid(),
                    DateTimeOffset        = DateTime.Now,
                    DivideByZeroException = new DivideByZeroException("a message")
                };

                db.Insert(item);
                var dbItem = db.Find <UnsupportedTypes>(item.Id);

                Assert.AreEqual(item.Id, dbItem.Id);
                Assert.AreEqual(item.DateTimeOffset, dbItem.DateTimeOffset);
                //most db stores datetimeoffset as utc, forgetting the offset part. This one is not an exception.
                //TODO: test fails
                Assert.AreEqual(item.DateTimeOffset.Offset, dbItem.DateTimeOffset.Offset);
                //TODO: test fails
                Assert.AreEqual(item.DivideByZeroException.Message, dbItem.DivideByZeroException.Message);
            }
        }
        public void SavesUnsupportedTypes()
        {
            UnsupportedTypes item = null;

            var serializer = new BlobSerializerDelegate(
                obj =>
            {
                if (obj is DivideByZeroException)
                {
                    var e    = (DivideByZeroException)obj;
                    var json = JsonConvert.SerializeObject(e);            // subst your own serializer
                    return(Encoding.UTF8.GetBytes(json));
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", obj.GetType()));
            },
                (d, t) =>
            {
                if (t == typeof(DivideByZeroException))
                {
#if __WINRT__ || WINDOWS_PHONE
                    var json = Encoding.UTF8.GetString(d, 0, d.Length);
#else
                    var json = Encoding.UTF8.GetString(d);
#endif
                    var result = JsonConvert.DeserializeObject <DivideByZeroException>(json);
                    return(result);
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", t));
            },
                t => true);

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <UnsupportedTypes>();
                item = new UnsupportedTypes()
                {
                    Id                    = Guid.NewGuid(),
                    DateTimeOffset        = DateTime.Now,
                    DivideByZeroException = new DivideByZeroException("a message")
                };

                db.Insert(item);
                var dbItem = db.Find <UnsupportedTypes>(item.Id);

                Assert.AreEqual(item.Id, dbItem.Id);
                Assert.AreEqual(item.DateTimeOffset, dbItem.DateTimeOffset);
                Assert.AreEqual(item.DivideByZeroException.Message, dbItem.DivideByZeroException.Message);
            }
        }
Ejemplo n.º 10
0
        private static void CreateCommand(BlobDatabase database, string[] tokens)
        {
            var name = tokens[1];
            var health = int.Parse(tokens[2]);
            var damage = int.Parse(tokens[3]);
            var behaviourType = tokens[4];
            var attackType = tokens[5];

            var createdBlob = new Blob(name, health, damage,
                behaviourType == "Aggressive" ? (IBehavior) new AggressiveBehavior() : new InflatedBehavior(),
                attackType == "PutridFart" ? (IAttack) new PutridFart() : new Blobplode());

            database.Add(createdBlob);
        }
Ejemplo n.º 11
0
        public void VerifyTableCreationSucceedsWithSupportedSerializer()
        {
            if (!this.Serializer.CanDeserialize(typeof(ComplexOrder)))
            {
                Assert.Ignore("Serialize does not support this data type");
            }

            using (var db = new BlobDatabase(this.Serializer))
            {
                db.CreateTable <ComplexOrder>();
                var mapping = db.GetMapping <ComplexOrder>();
                Assert.IsNotNull(mapping);
                Assert.AreEqual(4, mapping.Columns.Length);
            }
        }
Ejemplo n.º 12
0
        public void DoesNotCallOnSupportedTypes()
        {
            var types = new List <Type>();

            var serializer = new BlobSerializerDelegate(obj => null, (d, t) => null, t =>
            {
                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", t));
            });

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <SupportedTypes>();
            }

            Assert.AreEqual(0, types.Count, "Types requested from serializer");
        }
Ejemplo n.º 13
0
        public void CallsOnUnsupportedTypes()
        {
            var types = new List <Type>();

            var serializer = new BlobSerializerDelegate(obj => null, (d, t) => null, t =>
            {
                types.Add(t);
                return(true);
            });

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <UnsupportedTypes>();
            }

            Assert.That(types, Has.Member(typeof(DivideByZeroException)));

            Assert.AreEqual(1, types.Count, "Too many types requested by serializer");
        }
Ejemplo n.º 14
0
        public void CallsOnUnsupportedTypes()
        {
            var types = new List <Type>();

            var serializer = new BlobSerializerDelegate(obj => null, (d, t) => null, t =>
            {
                types.Add(t);
                return(true);
            });

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <UnsupportedTypes>();
            }

            Assert.Contains(typeof(DateTimeOffset), types);

            Assert.AreEqual(1, types.Count, "Too many types requested by serializer");
        }
Ejemplo n.º 15
0
        public void CanDeserializeIsRequested()
        {
            var types = new List <Type>();

            var serializer = new BlobSerializerDelegate(obj => null, (d, t) => null, t =>
            {
                types.Add(t);
                return(true);
            });

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <ComplexOrder>();
            }

            Assert.That(types, Has.Member(typeof(List <ComplexHistory>)));
            Assert.That(types, Has.Member(typeof(List <ComplexLine>)));

            Assert.AreEqual(2, types.Count, "Too many types requested by serializer");
        }
Ejemplo n.º 16
0
        public void VerifyTableCreationFailsWithNoSerializer()
        {
            NotSupportedException ex = null;

            using (var db = new BlobDatabase(null))
            {
                try
                {
                    var count = db.CreateTable <ComplexOrder>();
                    Assert.IsTrue(count == 0);
                    Assert.IsNull(db.GetMapping <ComplexOrder>());
                    return;
                }
                catch (NotSupportedException e)
                {
                    ex = e;
                }
            }

            Assert.IsNotNull(ex);
        }
Ejemplo n.º 17
0
        private static void AttackCommand(BlobDatabase database, string[] tokens)
        {
            var sourceBlob = database.First(a => a.Name == tokens[1]);
            var targetBlob = database.First(a => a.Name == tokens[2]);

            sourceBlob.Attack(targetBlob);

            if (sourceBlob.Health <= sourceBlob.InitialHealth / 2)
            {
                sourceBlob.BlobBehavior.Trigger(targetBlob);
            }

            if (targetBlob.Health <= targetBlob.InitialHealth/2)
            {
                targetBlob.BlobBehavior.Trigger(targetBlob);
            }

            if (targetBlob.Health <= 0)
            {
                targetBlob.IsAlive = false;
            }
        }
Ejemplo n.º 18
0
        //[Test]
        public void TestListOfObjects()
        {
            if (!this.Serializer.CanDeserialize(typeof(ComplexOrder)))
            {
                Assert.Ignore("Serialize does not support this data type");
            }

            using (var db = new BlobDatabase(this.Serializer))
            {
                db.CreateTable <ComplexOrder>();
                var order = new ComplexOrder();

                var count = db.Insert(order);
                Assert.AreEqual(count, 1);
                var orderCopy = db.Find <ComplexOrder>(order.Id);
                Assert.AreEqual(order, orderCopy);

                for (var n = 0; n < 10;)
                {
                    order.Lines.Add(new ComplexLine()
                    {
                        ProductId = 1, Quantity = ++n, Status = OrderLineStatus.Placed, UnitPrice = (n / 10m)
                    });
                    db.Update(order);
                    orderCopy = db.Find <ComplexOrder>(order.Id);
                    Assert.AreEqual(order, orderCopy);
                    order.History.Add(new ComplexHistory()
                    {
                        Time = DateTime.UtcNow, Comment = string.Format("New history {0}", n)
                    });
                    db.Update(order);
                    orderCopy = db.Find <ComplexOrder>(order.Id);
                    Assert.AreEqual(order, orderCopy);
                }
            }
        }
Ejemplo n.º 19
0
 public BlobEngine(BlobDatabase blobDatabase, IReader input, IWriter output)
 {
     BlobDatabase = blobDatabase;
     Input = input;
     Output = output;
 }
Ejemplo n.º 20
0
 private static void StatusCommand(IWriter output, BlobDatabase database)
 {
     StringBuilder sb = new StringBuilder();
     foreach (var blob in database)
     {
         sb.AppendLine(blob.ToString());
     }
     output.Write(sb.ToString());
 }