<?php
// Function autoload
function __autoload($class)
{
include("../admin_login/classess/" . $class . ".php");
}
$sqlfun = new sqlfunction();
$admin = new admin_login();
date_default_timezone_set('Asia/Kolkata');
$cur_date = date("Y-m-d");
$open_time = date("H:i:s");
$json = file_get_contents("php://input");
$obj = json_decode($_POST['data']);
$game_ids = $obj[0]->game_id;
// Fetch game details once
$check_game = $sqlfun->query("SELECT open_timestamp, close_timestamp FROM game WHERE g_id = '$game_ids'");
$row_game = $check_game->fetch(PDO::FETCH_OBJ);
$current_datetime = date("Y-m-d H:i:s");
$current_datetimestamp = strtotime($current_datetime);
// Check if game is within time window
if ($row_game->open_timestamp <= $current_datetimestamp && $row_game->close_timestamp >= $current_datetimestamp) {
$p_mobile = $obj[0]->mobile;
// Fetch user details and wallet in one query
$check_mob = $sqlfun->query("SELECT p_id, wallet, winwallet FROM player WHERE p_mobile = '$p_mobile'");
$row_mob = $check_mob->fetch(PDO::FETCH_OBJ);
$user_id = $row_mob->p_id;
$wallet_money = $row_mob->wallet;
$winwallet_money = $row_mob->winwallet;
// Initialize total amount
$t_amnt = 0;
// Begin transaction to ensure atomic operations
$sqlfun->beginTransaction();
// Prepare data for batch insertion in manage_game_id
$game_data_array = [];
foreach ($obj as $data) {
$number = $data->number;
$t_amnt += $data->amount;
$game_data_array[] = "('" . $data->date . "', '" . $data->game_id . "', '" . $data->refer_id . "', '$user_id', '" . $data->user_name . "', '$number', '" . $data->amount . "', '', '" . $data->ah . "', '" . $data->bh . "', '" . $data->subtype . "', '" . $data->win_amount . "', 'Pending', '" . date('Y-m-d H:i:s') . "')";
}
// Insert all data into manage_game_id table at once
if (!empty($game_data_array)) {
$insert_query = "INSERT INTO manage_game_id (play_date, game_id, refer_id, user_id, name, number, amount, result_status, andar_haruf, bahr_haruf, subtype, winning_amnt, status, play_datetime) VALUES " . implode(",", $game_data_array);
$sqlfun->query($insert_query);
}
// Calculate the wallet and winwallet deduction
if (intval($wallet_money) > intval($winwallet_money)) {
$recharge = $wallet_money - $winwallet_money;
if (intval($t_amnt) > intval($recharge)) {
$tobededuct_amnt = ($t_amnt - $recharge);
$final_wallet = ($wallet_money - $t_amnt);
$final_winwallet = ($winwallet_money - $tobededuct_amnt);
} else {
$final_wallet = ($wallet_money - $t_amnt);
$final_winwallet = $winwallet_money;
}
} else {
$final_wallet = ($wallet_money - $t_amnt);
$final_winwallet = ($winwallet_money - $t_amnt);
}
// Update wallet and winwallet in one query
$update_wallet_query = "UPDATE player SET wallet = :final_wallet, winwallet = :final_winwallet WHERE p_id = :user_id";
$update_wallet_stmt = $sqlfun->prepare($update_wallet_query);
$update_wallet_stmt->execute([':final_wallet' => $final_wallet, ':final_winwallet' => $final_winwallet, ':user_id' => $user_id]);
// Insert into wallet_history
$history_data = array("date" => date('Y-m-d'), "row_id" => "", "type" => "Play Game", "user_id" => $user_id, "game_id" => $game_ids, "amount" => $t_amnt, "history_datetime" => date("Y-m-d H:i:s"), "left_balance" => $final_wallet);
if ($sqlfun->ins_data("wallet_history", $history_data)) {
// Commit the transaction
$sqlfun->commit();
echo json_encode(array("result" => "success", "msg" => "successfully added"));
} else {
// Rollback in case of failure
$sqlfun->rollback();
echo json_encode(array("result" => "failure", "msg" => "history not created"));
}
} else {
echo json_encode(array("result" => "failure", "msg" => "Timeout"));
}
?>