示例#1
0
        public ActionResult Delete(int id, BlockDetail detail)
        {
            var model = _bSvc.Value.GetBlockByID(id);

            if (!_gmSvc.Value.DetachOrDeleteGameEvent(detail.ID, detail.DeleteAttachedEvent))
            {
                ModelState.AddModelError("", "Something went wrong.");
                return(View(model));
            }
            _bSvc.Value.DeleteBlock(id);
            TempData["SaveResult"] = "Block was succesfully deleted.";
            return(RedirectToAction("Details", "Map", new { id = model.BlockDetail.MapID }));
        }
示例#2
0
        public BlockDetail GetBlockDetail(string hash)
        {
            var block = BlockDac.Default.SelectByHash(hash);

            if (block == null)
            {
                return(null);
            }

            BlockDetail detail = new BlockDetail();

            detail.TradeCount  = block.Header.TotalTransaction;
            detail.TotalOutput = block.Transactions.SelectMany(x => x.Outputs).Where(x => x.Amount > 0).Sum(x => x.Amount);
            var reward = POC.GetNewBlockReward(block.Header.Height);

            detail.Height            = block.Header.Height;
            detail.Timestamp         = block.Header.Timestamp;
            detail.Difficulty        = POC.CalculateDifficulty(block.Header.Bits);
            detail.Bits              = block.Header.Bits;
            detail.Version           = block.Header.Version;
            detail.Nonce             = block.Header.Nonce;
            detail.BlockReward       = reward;
            detail.Hash              = block.Header.Hash;
            detail.PreviousBlockHash = block.Header.PreviousBlockHash;
            var nextHash = BlockDac.Default.GetBlockHashByHeight(detail.Height + 1);

            detail.NextBlockHash = nextHash;


            detail.TranList = new List <TransOM>();

            block.Transactions.ForEach(x => {
                detail.TranList.Add(x.ConvertToDetail());
            });

            var tx       = detail.TranList.Skip(0);
            var totalFee = tx.SelectMany(x => x.OutputList).Sum(x => x.Amount) - tx.SelectMany(x => x.InputList).Sum(x => x.Amount);

            detail.TransactionFees = totalFee;
            return(detail);
        }
示例#3
0
        public BlockDetailsWithEvent GetBlockByID(int id)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity      = ctx.Blocks.Single(e => e.ID == id);
                var userEntity  = ctx.Users.FirstOrDefault(e => e.Id == entity.OwnerID.ToString());
                var blockDetail = new BlockDetail
                {
                    ID          = entity.ID,
                    MapID       = entity.MapID,
                    Creator     = userEntity.UserName,
                    TypeOfBlock = entity.TypeOfBlock.ToString(),
                    Name        = entity.Name,
                    Description = entity.Description,
                    PosX        = entity.PosX,
                    PosY        = entity.PosY,
                    HasEvent    = ctx.GameEvents.Any(ge => ge.BlockID == entity.ID)
                };

                if (entity.TypeOfBlock.ToString() == "Exit")
                {
                    var exitEntity = ctx.ExitBlocks.Single(e => e.ID == id);
                    blockDetail.ExitDirection = exitEntity.ExitDirection.ToString();
                    blockDetail.ExitToID      = exitEntity.ExitToID;
                }

                var eventDetail = new GameEventDetail();
                if (blockDetail.HasEvent)
                {
                    eventDetail = GetGameEvent(entity.ID);
                }

                return(new BlockDetailsWithEvent {
                    BlockDetail = blockDetail, GameEventDetail = eventDetail
                });
            }
        }
        public async Task <BlockViewModel> CreateTopic(CreateTopic newTopic)
        {
            try
            {
                if (newTopic == null || newTopic.BlockNumber == -1)
                {
                    throw new ApplicationException("Topic is not null");
                }
                if (
                    string.IsNullOrEmpty(newTopic.CountryName) ||
                    string.IsNullOrEmpty(newTopic.Description) ||
                    string.IsNullOrEmpty(newTopic.CountryName))
                {
                    throw new ApplicationException("Topic detail is not completed to save");
                }

                //Make sure only 1 topic per block
                var alreadyHasTopic = _blockRepository.Get(x => x.BlockNumber == newTopic.BlockNumber).Any();

                if (alreadyHasTopic)
                {
                    throw new ApplicationException("This block number already has topic");
                }

                // Find the block this new topic will belong too.
                var owningCountry = _countryRepository
                                    .GetQueryable()
                                    .Single(x => x.Blocks.Any(y => y.BlockNumber == newTopic.BlockNumber));
                var countryBlock = owningCountry.Blocks.Single(y => y.BlockNumber == newTopic.BlockNumber);

                var objectId = countryBlock.Id;
                var UId      = countryBlock.UId;

                // TODO: Consider refactor for BlockDetail and BlockViewModel for interacting with BlockService.
                var blockObj = new BlockDetail
                {
                    UId            = UId,
                    Id             = objectId,
                    Name           = newTopic.Name,
                    Description    = newTopic.Description,
                    BlockAxis      = newTopic.BlockAxis,
                    BlockYxis      = newTopic.BlockYxis,
                    BlockNumber    = newTopic.BlockNumber,
                    CreatedOn      = DateTime.UtcNow,
                    CreatedBy      = newTopic.UserUId,
                    CountryId      = newTopic.CountryId,
                    OwnerId        = newTopic.OwnerId,
                    TotalResidents = 1,
                    IsDeleted      = false,
                };

                await _blockRepository.Add(blockObj);

                var blockViewModel = new BlockViewModel
                {
                    Id             = blockObj.Id,
                    Name           = blockObj.Name,
                    Description    = blockObj.Description,
                    BlockAxis      = blockObj.BlockAxis,
                    BlockYxis      = blockObj.BlockYxis,
                    CreatedOn      = blockObj.CreatedOn,
                    CreatedBy      = blockObj.CreatedBy,
                    Owner          = null,
                    TotalResidents = blockObj.TotalResidents,
                };

                return(blockViewModel);
            }
            catch (System.Exception e)
            {
                throw new ApplicationException("Create topic error " + e.Message);
            }
        }