Ejemplo n.º 1
0
        public APIMessageView UpdateTimebombStatus(TimeBomb timebomb, int status)
        {
            APIMessageView results = new APIMessageView();

            try
            {
                // Retrieve bomb with id i
                TimeBomb defuseBomb = _context.TimeBombs.FirstOrDefault(x => x.TimeBombId == timebomb.TimeBombId);

                // Examine bomb's status to ensure it is armed
                if (defuseBomb.Status != status)
                {
                    string encryptedMessage = EncryptionMethods.encrypt(EncryptionMethods.crocodile(), timebomb.KillMessage, EncryptionMethods.alphabet());

                    // Hash killmessage and compare it to bomb's message
                    if (encryptedMessage.Equals(defuseBomb.KillMessage))
                    {
                        System.Console.WriteLine("Success! KillMessages match!");

                        // If match, set bomb status to -1
                        defuseBomb.UpdatedAt = DateTime.Now;
                        defuseBomb.Status    = status;
                        _context.SaveChanges();

                        results.TimeBomb = defuseBomb;
                        string stringStatus = getTimebombStatus(status);

                        System.Console.WriteLine($"Success! Timebomb set to: {stringStatus}!");
                        results.Success = true;
                        results.Message = $"Success! Timebomb set to: {stringStatus}!";
                    }
                    else
                    {
                        System.Console.WriteLine("Failure! KillMessages do not match!");
                        results.Success = false;
                        results.Message = "Failure! KillMessages do not match!";
                    }
                }
                else
                {
                    System.Console.WriteLine($"Error! Timebomb cannot set status to {status}! Status already set to {status}");
                    results.Success = false;
                    results.Message = $"Error! Timebomb cannot set status to {status}! Status already set to {status}";
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Something's f****d!");
                System.Console.WriteLine(e);
                results.Success = false;
                results.Message = "An error occured! " + e;
            }

            return(results);
        }
Ejemplo n.º 2
0
        public IActionResult NewTimeBomb([FromBody] SecretView timebomb)
        {
            APIResponseView results = new APIResponseView();

            if (ModelState.IsValid)
            {
                try
                {
                    TimeBomb newTimeBomb = timebomb.NewTimeBomb;

                    newTimeBomb.CreatedAt = DateTime.Now;
                    newTimeBomb.UpdatedAt = DateTime.Now;

                    string encryptedMessage = EncryptionMethods.encrypt(EncryptionMethods.crocodile(), newTimeBomb.KillMessage, EncryptionMethods.alphabet());

                    newTimeBomb.KillMessage = encryptedMessage;

                    _context.Add(newTimeBomb);
                    _context.SaveChanges();

                    System.Console.WriteLine("Success! Adding timebomb with id " + newTimeBomb.TimeBombId);

                    _context.SaveChanges();

                    results.Success = true;
                    results.Message = "Success! Adding timebomb with id " + newTimeBomb.TimeBombId;
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("Something's f****d!");
                    System.Console.WriteLine(e);
                    results.Success = false;
                    results.Message = "An error occured! " + e;
                }
            }
            else
            {
                results.Success = false;
                string message = "Errant input! Model not valid!";
                results.Message = message;
                System.Console.WriteLine(message);
            }

            return(Json(results));
        }