Beispiel #1
0
        public string ShowOwnedCards()
        {
            string token = requestContext.AuthorizationToken;

            if (token == null)
            {
                return(CreateResponse("400 Bad Request", "text/plain", "Token Null Unauthorized command!"));
            }

            if (GetUsernameFromToken(token) == null)
            {
                return(CreateResponse("400 Bad Request", "text/plain", "UserToken null Unauthorized command!"));
            }

            using var conn = new NpgsqlConnection(ConnectionString);
            conn.Open();

            string          username = GetUsernameFromToken(token);
            List <JsonCard> cards    = new List <JsonCard>();

            var cmd = new NpgsqlCommand("Select cards.id, name, damage from cards inner join player_cards on cards.id = player_cards.card_id where player_cards.username = @username", conn);

            cmd.Parameters.AddWithValue("username", username);
            cmd.Prepare();

            try
            {
                var reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        var temp = new JsonCard();
                        temp.Id     = (string)reader[0];
                        temp.Name   = (string)reader[1];
                        temp.Damage = (double)((decimal)reader[2]);
                        cards.Add(temp);
                    }
                    if (cards != null)
                    {
                        return(CreateResponse("200 OK", "application/json", JsonConvert.SerializeObject(cards)));
                    }
                }
                return(CreateResponse("400 Bad Request", "text/plain", "No cards found!"));
            }
            catch (PostgresException)
            {
                return(CreateResponse("400 Bad Request", "text/plain", "No cards found!"));
            }
        }
Beispiel #2
0
        public string Battle()
        {
            string token = requestContext.AuthorizationToken;

            if (token == null)
            {
                return(CreateResponse("400 Bad Request", "text/plain", "Token Null Unauthorized command!"));
            }

            string username = GetUsernameFromToken(token);

            if (username == null)
            {
                return(CreateResponse("400 Bad Request", "text/plain", "UserToken null Unauthorized command!"));
            }

            using var conn = new NpgsqlConnection(ConnectionString);
            conn.Open();

            //Check if user has deck
            var cmd = new NpgsqlCommand("Select * from player_deck where username = @username", conn);

            cmd.Parameters.AddWithValue("username", username);
            cmd.Prepare();

            //Check if Battle entry without user exists
            var cmd2 = new NpgsqlCommand("Select id from battle where player1 != @username and player2 is null", conn);

            cmd2.Parameters.AddWithValue("username", username);
            cmd2.Prepare();

            //Create Battle entry if necessary
            var cmd3 = new NpgsqlCommand("INSERT INTO battle (player1) VALUES (@username)", conn);

            cmd3.Parameters.AddWithValue("username", username);
            cmd3.Prepare();

            var cmdUpdate = new NpgsqlCommand("UPDATE battle SET player2 = @username where id = @id", conn);

            //Get created BattleID
            var cmdBattle = new NpgsqlCommand("Select id from battle where player1 = @username", conn);

            cmdBattle.Parameters.AddWithValue("username", username);
            cmdBattle.Prepare();

            //If Battle entry exists already execute battle
            var cmd4 = new NpgsqlCommand("Select player1 from battle where id = @id", conn);
            var cmd5 = new NpgsqlCommand("Select id, name, damage from cards inner join player_deck on cards.id = player_deck.card_id where username = @username", conn);
            var cmd6 = new NpgsqlCommand("Select id, name, damage from cards inner join player_deck on cards.id = player_deck.card_id where username = @username", conn);

            //reduce Elo
            var cmdLoser  = new NpgsqlCommand("UPDATE player SET elo = (elo-5) where username = @username", conn);
            var cmdWinner = new NpgsqlCommand("UPDATE player SET elo = (elo+3) where username = @username", conn);

            //Save Battle in BattleLog
            var cmd9 = new NpgsqlCommand("INSERT INTO battle_log (id, battle_text, winner) VALUES (@id, @battle_text, @winner)", conn);

            //Loop until BattleLog exists
            var cmd10 = new NpgsqlCommand("Select battle_text from battle_log where id = @id", conn);

            try
            {
                //Check if user has deck
                var reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    return(CreateResponse("400 Bad Request", "text/plain", "No deck found!"));
                }

                reader.Close();

                //Check if Battle entry without user exists
                var reader2 = cmd2.ExecuteReader();
                //If Battle entry exists already -> execute battle
                int battleId;
                if (reader2.HasRows)
                {
                    reader2.Read();
                    battleId = (int)reader2[0];
                    reader2.Close();

                    cmd4.Parameters.AddWithValue("id", battleId);
                    cmd4.Prepare();

                    var reader4 = cmd4.ExecuteReader();
                    if (!reader4.HasRows)
                    {
                        return(CreateResponse("400 Bad Request", "text/plain", "Unknown Error"));
                    }
                    reader4.Read();
                    var player1 = (string)reader4[0];
                    var player2 = username;

                    reader4.Close();

                    cmdUpdate.Parameters.AddWithValue("username", username);
                    cmdUpdate.Parameters.AddWithValue("id", battleId);
                    cmdUpdate.Prepare();

                    cmdUpdate.ExecuteNonQuery();

                    cmd5.Parameters.AddWithValue("username", player1);
                    cmd5.Prepare();

                    cmd6.Parameters.AddWithValue("username", player2);
                    cmd6.Prepare();

                    var cardList1 = new List <BaseCard>();
                    var cardList2 = new List <BaseCard>();

                    var reader5 = cmd5.ExecuteReader();
                    if (!reader5.HasRows)
                    {
                        return(CreateResponse("400 Bad Request", "text/plain", "Unknown Error"));
                    }
                    while (reader5.Read())
                    {
                        var card = new JsonCard();
                        card.Id     = (string)reader5[0];
                        card.Name   = (string)reader5[1];
                        card.Damage = (double)((decimal)reader5[2]);
                        cardList1.Add(card.ConvertToCard());
                    }

                    reader5.Close();

                    var reader6 = cmd6.ExecuteReader();
                    if (!reader6.HasRows)
                    {
                        return(CreateResponse("400 Bad Request", "text/plain", "Unknown Error"));
                    }
                    while (reader6.Read())
                    {
                        var card = new JsonCard();
                        card.Id     = (string)reader6[0];
                        card.Name   = (string)reader6[1];
                        card.Damage = (double)((decimal)reader6[2]);
                        cardList2.Add(card.ConvertToCard());
                    }

                    reader6.Close();

                    //DB inputs
                    var deck1 = new List <BaseCard>();
                    var deck2 = new List <BaseCard>();
                    var user1 = new Player();
                    var user2 = new Player();

                    deck1             = cardList1;
                    deck2             = cardList2;
                    user1.CurrentDeck = deck1;
                    user2.CurrentDeck = deck2;
                    user1.Username    = player1;
                    user2.Username    = player2;

                    Arena arena = new Arena();
                    var   log   = arena.Fight(user1, user2, arena);

                    //reduce elo
                    if (log.Winner != null)
                    {
                        cmdWinner.Parameters.AddWithValue("username", log.Winner.Username);
                        cmdWinner.Prepare();

                        cmdLoser.Parameters.AddWithValue("username",
                                                         player1.Equals(log.Winner.Username) ? player2 : player1);
                        cmdLoser.Prepare();

                        cmdLoser.ExecuteNonQuery();
                        cmdWinner.ExecuteNonQuery();
                    }

                    //Save Battle in BattleLog
                    cmd9.Parameters.AddWithValue("id", battleId);
                    cmd9.Parameters.AddWithValue("battle_text", log.ArenaText);
                    if (log.Winner != null)
                    {
                        cmd9.Parameters.AddWithValue("winner", log.Winner.Username);
                    }
                    else
                    {
                        cmd9.Parameters.AddWithValue("winner", "");
                    }
                    cmd9.Prepare();
                    cmd9.ExecuteNonQuery();
                }

                //Create Battle entry if necessary
                else
                {
                    reader2.Close();

                    var reader3 = cmd3.ExecuteNonQuery();
                    if (reader3 == -1)
                    {
                        return(CreateResponse("400 Bad Request", "text/plain", "Unknown Error"));
                    }

                    //Get created battleID
                    battleId = (int)cmdBattle.ExecuteScalar();
                }


                //Loop until ArenaLog exists

                cmd10.Parameters.AddWithValue("id", battleId);
                cmd10.Prepare();

                while (true)
                {
                    var reader10 = cmd10.ExecuteReader();
                    if (reader10.HasRows)
                    {
                        reader10.Read();
                        return(CreateResponse("200 OK", "text/plain", (string)reader10[0]));
                    }
                    reader10.Close();

                    System.Threading.Thread.Sleep(1000);
                }
            }
            catch (PostgresException)
            {
                return(CreateResponse("400 Bad Request", "text/plain", "PostgresError"));
            }
        }