<?php
include "include.php";
include "../inc/database.class.php";
include "../inc/bitskins.class.php";
$db = new Database();
if(isset($_POST['state']) && isset($_POST['request_id']))
{
    $requestId = $_POST['request_id'];
    $status = intval($_POST['state']);
    if(substr($requestId, 0, 2) === "rt")
    {
        $raffleTimeId = substr($requestId, 2);
        $raffleTimeState = $db->getRaffleTimeTradeState($raffleTimeId);
        if($raffleTimeId > 0 && $raffleTimeState != null)
        {
            switch($status)
            {
                case 1: //Success
                    if($raffleTimeState == 2)
                    {
                        $db->setRaffleTimeTradeOver($raffleTimeId, 3);
                        if($db->getRaffleTimeCategory($raffleTimeId) == 1)
                        {
                            $bitskins = new Bitskins(0);
                            $array_types = ["Pistol", "Rifle", "SMG"];
                            $type = $array_types[array_rand($array_types)];
                            $bitskins->buyRandomItem($minPrice, $maxPrice, $type);
                        }
                    }
                    break;
                case 2: //Refund
                    if($raffleTimeState < 3)
                    {
                        $db->setRaffleTimeTradeOver($raffleTimeId, 4);
                    }
                    break;
                case 3: // No refund (don't have the item)
                    if($raffleTimeState < 3)
                    {
                        $db->setRaffleTimeTradeOver($raffleTimeId, 5);
                    }
            }
        }
    }
    elseif(substr($requestId, 0, 1) === "r")
    {
        $raffleId = substr($requestId, 1);
        $raffleState = $db->getRaffleTradeState($raffleId);
        if($raffleId > 0 && $raffleState != null)
        {
            switch($status)
            {
                case 1: //Success
                    if($raffleState == 2)
                    {
                        $db->setRaffleTradeOver($raffleId, 3);
                        if($db->getRaffleCategory($raffleId) == 1)
                        {
                            $bitskins = new Bitskins(0);
                            $array_types = ["Pistol", "Rifle", "SMG"];
                            $type = $array_types[array_rand($array_types)];
                            $bitskins->buyRandomItem($minPrice, $maxPrice, $type);
                        }
                    }
                    break;
                case 2: //Refund
                    if($raffleState < 3)
                    {
                        $db->setRaffleTradeOver($raffleId, 4);
                    }
                    break;
                case 3: // No refund (don't have the item)
                    if($raffleState < 3)
                    {
                        $db->setRaffleTradeOver($raffleId, 5);
                    }
            }
        }
    }
    elseif(substr($requestId, 0, 2) == "xp")
    {
        $xpWinningId = substr($requestId, 2);
        $xpWinningState = $db->getXpWinningState($xpWinningId);
        if($xpWinningId > 0 && $xpWinningState != null)
        {
            switch($status)
            {
                case 1: //Success
                    if($xpWinningState == 2)
                    {
                        $db->setXpWinningOver($xpWinningId, 3);
                        $bitskins = new Bitskins(0);
                        $array_types = ["Pistol", "Rifle", "SMG"];
                        $type = $array_types[array_rand($array_types)];
                        $bitskins->buyRandomItem($minPrice, $maxPrice, $type);
                    }
                    break;
                case 2: //Refund
                    if($xpWinningState < 3)
                    {
                        $db->setXpWinningOver($xpWinningId, 4);
                    }
                    break;
                case 3: // No refund (don't have the item)
                    if($xpWinningState < 3)
                    {
                        $db->setXpWinningOver($xpWinningId, 5);
                    }
            }
        }
    }
}
?>