Exemplo n.º 1
0
        public async Task <List <TaskModel> > GetAllTaskForPlayerForGame(int gameId, CyberSparrow user)
        {
            var game           = gh.GetGame(gameId);
            var playerTaskList = await db.PlayerTask.Include(x => x.Task).Where(x => x.Game == game && x.User == user && x.TaskState != "null").ToListAsync();

            List <TaskModel> model = new List <TaskModel>();

            if (playerTaskList.Count() > 0)
            {
                foreach (var t in playerTaskList)
                {
                    if (t.Task != null)
                    {
                        var taskModel = await db.Task.FirstAsync(x => x.TaskId == t.Task.TaskId);

                        TaskModel task = new TaskModel();
                        task = taskModel;
                        var state = t.TaskState;
                        task.State = state;
                        model.Add(task);
                    }
                    else
                    {
                        Console.WriteLine("TASK IS NULL");
                    }
                }
            }
            else
            {
                return(new List <TaskModel>());
            }
            return(model);
        }
Exemplo n.º 2
0
 public bool UpdateProfile(CyberSparrow user, UpdateProfileModel model)
 {
     user.Biography   = model.Biography;
     user.PhoneNumber = model.PhoneNumber;
     db.Users.Update(user);
     return(SaveChanges());
 }
Exemplo n.º 3
0
        public async Task <bool> UpdateProfileBannerPictureAsync(CyberSparrow user, string UpdateProfileBanner)
        {
            var u = await db.Users.FirstAsync(x => x.Id == user.Id);

            u.BannerPictureUrl = UpdateProfileBanner;
            db.Users.Update(u);
            return(SaveChanges());
        }
Exemplo n.º 4
0
        public int GetLevel(CyberSparrow user)
        {
            var level = CalculateLevel(user.TotalPoints, user.Level);

            if (level > user.Level)
            {
                user.Level = level;
                UpdateUser(user);
            }
            return(level);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> RegisterUserAsync([FromBody] RegisterModel userForRegistration)
        {
            if (userForRegistration == null || !ModelState.IsValid || string.IsNullOrWhiteSpace(userForRegistration.RegistrationCodeUsed))
            {
                return(BadRequest("Error: Check if all inputs are filled out!"));
            }

            if (!rch.IsCodeValid(userForRegistration.RegistrationCodeUsed))
            {
                return(BadRequest("Registration token is invalid"));
            }

            //Watch out for duplicate usernames and emails in upperlowercase
            if (await userManager.FindByNameAsync(userForRegistration.UserName) != null)
            {
                return(BadRequest("Usernamne is taken!"));
            }

            if (await userManager.FindByEmailAsync(userForRegistration.Email) != null)
            {
                return(BadRequest("Another account is already registered with this email!"));
            }

            CyberSparrow user = new CyberSparrow()
            {
                Email                = userForRegistration.Email,
                UserName             = userForRegistration.UserName,
                FirstName            = userForRegistration.FirstName,
                LastName             = userForRegistration.LastName,
                Level                = 1,
                TotalPoints          = 0,
                RegistrationCodeUsed = userForRegistration.RegistrationCodeUsed,
                IsUserActive         = true,
            };

            var result = await userManager.CreateAsync(user, userForRegistration.Password);

            if (!result.Succeeded)
            {
                var errors = result.Errors.Select(e => e.Description);

                return(BadRequest("Unable to register user! Contact admin or try different credentials"));
            }
            rch.UseRegistrationCode(userForRegistration.RegistrationCodeUsed);
            var x = await userManager.AddToRoleAsync(user, "Player");

            return(StatusCode(201));
        }
Exemplo n.º 6
0
        public List <Claim> GetClaims(CyberSparrow user)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, user.Email)
            };

            var roles = userManager.GetRolesAsync(user);

            foreach (var role in roles.Result)
            {
                claims.Add(new Claim(ClaimTypes.Role, role));
            }

            return(claims);
        }
Exemplo n.º 7
0
        public async Task <bool> SetTaskFinishedAsync(int taskid, int gameid, CyberSparrow user)
        {
            var game       = gh.GetGame(gameid);
            var task       = th.GetTask(taskid);
            var playerTask = await GetPlayerTaskAsync(game, task, user);

            if (playerTask == null)
            {
                return(false);
            }
            TimeSpan ts = (DateTime.Now - playerTask.Started);

            playerTask.TimeSpent = ts.Minutes;
            playerTask.TaskState = "finished";

            return(UpdatePlayerTask(playerTask));
        }
Exemplo n.º 8
0
        public bool StartTaskForPlayerForGame(GameModel game, TaskModel task, CyberSparrow user)
        {
            var pt = db.PlayerTask.FirstOrDefault(x => x.Game == game && x.Task == task && x.User == user);

            if (pt == null)
            {
                throw new NullReferenceException("DID NOT FIND ANY TASKS FOR GAME OR USER");
                return(false);
            }
            pt.Started   = DateTime.Now;
            pt.TaskState = "started";
            db.Update(pt);
            var x = SaveChanges();

            //TODO CALCULATE TIME AND CLOSE VM AND TASK AFTER TIME!!!!
            return(x);
        }
Exemplo n.º 9
0
        public async Task <List <object> > GetAllVmInfoAsync()
        {
            var bearer = await aapi.GetBearerTokenAsync();

            var allVms = await aapi.GetAllVmAsync(bearer);

            List <object> result = new List <object>();

            foreach (var vm in allVms)
            {
                var origin = vmh.FindOrigin(vm);
                var user   = new CyberSparrow();
                if (origin != "unknown")
                {
                    var originObject = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(origin);
                    var userId       = originObject[0]["PlayerId"];
                    if (userId == null || string.IsNullOrWhiteSpace(userId.ToString()))
                    {
                        var obj    = originObject[0];
                        var usrobj = originObject[0]["User"];
                        //user = obj["Id"];
                        user   = usrobj.ToObject <CyberSparrow>();
                        userId = null;
                    }
                    if (userId != null)
                    {
                        var userIdString = userId.ToString();
                        //userIdString = userId.Split(new char[] { '{', '}' }, StringSplitOptions.RemoveEmptyEntries);
                        user = userHandler.GetUserById(userIdString);
                    }
                }
                //var originUser = userHandler.GetUserById(originObject);
                var x = new {
                    vmName = vm,
                    origin = vmh.FindOrigin(vm),
                    player = user
                };
                result.Add(x);
            }
            return(result);
        }
Exemplo n.º 10
0
        public async Task <bool> DeliverFlagAsync(GameModel game, TaskModel task, CyberSparrow user, string flag)
        {
            var playerTask = db.PlayerTask.FirstOrDefault(x => x.Game == game && x.Task == task && x.User == user);

            if (playerTask == null)
            {
                throw new NullReferenceException("PLAYER TASK NOT FOUND");
                return(false);
            }
            //Edgecase if function is called twice
            if (playerTask.TaskState == "finished")
            {
                return(true);
            }
            if (playerTask.Task.Flag.ToLower() == flag.ToLower() || playerTask.Task.Flag.ToLower() == "kcsc{" + flag.ToLower() + "}")
            {
                playerTask.TaskState = "finished";
                TimeSpan ts = (DateTime.Now - playerTask.Started);
                playerTask.TimeSpent = ts.Minutes;
                //CalculatePoints
                var score = CalculateTaskScore(task.Duration, playerTask.TimeSpent, task.MaxPoints, playerTask.NumberOfFailedflags);
                playerTask.PointsRecieved = score;
                userhandler.AddPoints(user, score);
                //
                db.PlayerTask.Update(playerTask);
                SaveChanges();
                return(true);
            }
            else
            {
                playerTask.NumberOfFailedflags = playerTask.NumberOfFailedflags + 1;
                db.PlayerTask.Update(playerTask);
                SaveChanges();
                if (playerTask.NumberOfFailedflags >= 5)
                {
                    await SetTaskFinishedAsync(playerTask.Task.TaskId, playerTask.Game.GameId, user);
                }
            }
            return(false);
        }
Exemplo n.º 11
0
 public bool UpdateUser(CyberSparrow user)
 {
     db.Users.Update(user);
     return(SaveChanges());
 }
Exemplo n.º 12
0
 //Returns totalPoints
 public int AddPoints(CyberSparrow user, int points)
 {
     user.TotalPoints += points;
     UpdateUser(user);
     return(user.TotalPoints);
 }
        public async Task <VirtualMachineModel> CreateVirtualMachineEntityAsync(string id, CyberSparrow user)
        {
            VirtualMachineModel model = new VirtualMachineModel();

            model.state   = "deploying";
            model.Id      = id;
            model.Started = DateTime.Now;
            model.user    = user;
            var result = await db.AddAsync(model);

            SaveChanges();
            return(result.Entity);
        }
Exemplo n.º 14
0
        public async Task <List <PlayerTaskModel> > GetAllPlayerTaskForPlayerForGame(int gameId, CyberSparrow user)
        {
            var game           = gh.GetGame(gameId);
            var playerTaskList = await db.PlayerTask.Include(x => x.Task).Where(x => x.Game.GameId == gameId && x.User.Id == user.Id).ToListAsync();

            return(playerTaskList);
        }
Exemplo n.º 15
0
        public async Task <PlayerTaskModel> CreatePlayerTaskAsync(int taskId, int gameId, CyberSparrow user)
        {
            var task = th.GetTask(taskId);
            var game = gh.GetGame(gameId);

            if (task == null || game == null)
            {
                throw new NullReferenceException("GAME OR TASK IS NULL");
            }
            PlayerTaskModel model = new PlayerTaskModel();

            model.Game = game;
            model.Task = task;
            model.User = user;
            model.NumberOfFailedflags = 0;
            model.TaskState           = "ready";
            var m = await db.PlayerTask.AddAsync(model);

            db.SaveChanges();
            return(m.Entity);
        }
Exemplo n.º 16
0
 public async Task <PlayerTaskModel> GetPlayerTaskAsync(GameModel game, TaskModel task, CyberSparrow user)
 {
     return(await db.PlayerTask.FirstOrDefaultAsync(x => x.Game.GameId == game.GameId && x.Task.TaskId == task.TaskId && x.User.Id == user.Id));
 }