Exemplo n.º 1
0
        public async Task <IActionResult> StartMinerSessionAsync([FromHeader(Name = "Authorization")] string token)
        {
            var miner = await DbContext.Miners.FirstOrDefaultAsync(x => x.Token == token);

            if (miner == null)
            {
                return(Unauthorized());
            }

            var minerAddress = GetRequestIP();

            if (minerAddress == null)
            {
                return(NotFound());
            }
            if (!minerAddress.Equals(miner.LastAddress))
            {
                await FirewallService.SwapMinerIP(miner.LastAddress, minerAddress);

                miner.LastAddress = minerAddress;
            }

            await DbContext.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> BuyPlotTranferAsync([FromHeader(Name = "Authorization")] string token, [FromRoute] int deadlineHours = 12)
        {
            var miner = await DbContext.Miners.FirstOrDefaultAsync(x => x.Token == token);

            if (miner == null)
            {
                return(Unauthorized());
            }
            if (miner.PlotMinutes < 0)
            {
                return(Forbid());
            }

            long?plotterId = PlotterService.GetSuitablePlotterId();

            if (plotterId == null)
            {
                return(NotFound());
            }

            var plotter = await DbContext.Plotters.FirstOrDefaultAsync(x => x.Id == plotterId);

            var plotTransfer = await PlotterService.TryRequestPlotTransferAsync(miner.Id, plotter.Id, deadlineHours);

            plotter.PlotMinutes += plotTransfer.Cost;
            miner.PlotMinutes   -= plotTransfer.Cost;
            DbContext.PlotTranfers.Add(plotTransfer);
            await DbContext.SaveChangesAsync();

            return(Ok(plotTransfer));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> RegisterUserAsync([FromForm] string name, [FromForm] string password)
        {
            if (!CustomizationOptions.AllowUserRegistration)
            {
                return(NotFound());
            }
            if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(password))
            {
                return(UnprocessableEntity());
            }
            if (await DbContext.Users.AnyAsync(x => x.Name.ToUpper() == name.ToUpper()))
            {
                return(Conflict("Username already taken!"));
            }

            string passwordHash = HashingService.HashString(password);
            var    user         = new User(name, passwordHash);

            DbContext.Users.Add(user);
            await DbContext.SaveChangesAsync();

            var userInfo = UserService.GetUserInfo(user);

            return(Ok(userInfo));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> CreatePlotterAsync([FromForm] string name)
        {
            if (!CustomizationOptions.AllowPlotterCreation)
            {
                return(NotFound());
            }

            long userId  = long.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));
            var  plotter = new Plotter(name, userId);

            DbContext.Plotters.Add(plotter);
            await DbContext.SaveChangesAsync();

            var plotterInfo = PlotterService.GetPlotterInfo(plotter);
            var result      = new CreatePlotterResult(plotter.Token, plotterInfo);

            return(Ok(result));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> CreateMinerAsync([FromForm] string name)
        {
            if (!CustomizationOptions.AllowMinerCreation)
            {
                return(NotFound());
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                return(UnprocessableEntity());
            }

            long userId = long.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));
            var  miner  = new Miner(name, userId);

            DbContext.Miners.Add(miner);
            await DbContext.SaveChangesAsync();

            var minerInfo = MinerService.GetMinerInfo(miner);
            var result    = new CreateMinerResult(miner.Token, minerInfo);

            return(Ok(result));
        }