chess/application/models/Api_model.php

381 lines
9.9 KiB
PHP
Raw Normal View History

2024-03-08 13:03:59 +00:00
<?php
class Api_model extends CI_Model
{
// function fetch_all()
// {
// $this->db->order_by('tid', 'ASC');
// return $this->db->get('tournament');
// }
function fetch_all()
{
$this->db->select('tournament.*, COUNT(tournament_registrations.id) as user_count');
$this->db->from('tournament');
$this->db->join('tournament_registrations', 'tournament_registrations.tid = tournament.tid', 'left');
$this->db->group_by('tournament.tid');
$this->db->order_by('tournament.tid', 'ASC');
return $this->db->get();
}
public function insertData($tbl,$arr)
{
$this->db->insert($tbl,$arr);
return $this->db->insert_id();
}
public function getData($tbl,$where)
{
$this->db->select('*');
$this->db->from($tbl);
$this->db->where($where);
$query = $this->db->get();
return $query->row_array();
}
public function getAllData($tbl,$where)
{
$this->db->select('*');
$this->db->from($tbl);
$this->db->where($where);
$query = $this->db->get();
return $query->result_array();
}
// public function get_tournament_participants($tid) {
// $this->db->select('tr.user_id, tr.score','tr.round');
// $this->db->from('tournament_registrations tr');
// $this->db->join('tbl_user u', 'tr.user_id = u.user_id', 'left');
// $this->db->where('tr.tid', $tid);
// $query = $this->db->get();
// return $query->result_array();
// }
public function get_tournament_participants($tid) {
$this->db->select('tr.user_id, tr.score, tr.round');
$this->db->from('tournament_registrations tr');
$this->db->join('tbl_user u', 'tr.user_id = u.user_id', 'left');
$this->db->where('tr.tid', $tid);
$query = $this->db->get();
return $query->result_array();
}
public function get_tournament_leaderboard($tid) {
$this->db->select('tr.user_id, tr.score, tr.round, u.username, tr.winnings');
$this->db->from('tournament_registrations tr');
$this->db->join('tbl_user u', 'tr.user_id = u.user_id', 'left');
$this->db->where('tr.tid', $tid);
$query = $this->db->get();
return $query->result_array();
}
public function get_tournament_win_amount($tid) {
$this->db->select('win_amount');
$this->db->from('tournament');
$this->db->where('tid', $tid);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->win_amount;
}
return 0;
}
public function update_winnings_in_database($winningsDistribution, $tid)
{
foreach ($winningsDistribution as $user_id => $winnings) {
if ($winnings > 0) {
$data = array('winnings' => $winnings);
$this->db->where('tid', $tid);
$this->db->where('user_id', $user_id);
$this->db->update('tournament_registrations', $data);
$existingRecord = $this->db
->where('tournament_id', $tid)
->where('user_id', $user_id)
->get('wallet_transaction_details')
->row();
if ($existingRecord) {
exit();
} else {
$wallet_data = array(
'tournament_id' => $tid,
'user_id' => $user_id,
'amount' => $winnings,
'wallet_amount' => $this->get_current_wallet_amount($user_id) + $winnings,
'message' => 'Winnings from tournament',
'added_by' => 1,
't_status' => 1,
'added_date' => date('Y-m-d H:i:s'),
);
$this->db->insert('wallet_transaction_details', $wallet_data);
}
}
}
}
// Function to get the current wallet amount for a user
private function get_current_wallet_amount($user_id)
{
$this->db->select('wallet_amount');
$this->db->from('wallet_transaction_details');
$this->db->where('user_id', $user_id);
$this->db->order_by('added_date', 'desc');
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->wallet_amount;
}
return 0;
}
public function get_tournament_winners($tid)
{
$winners = $this->db
->select('tr.user_id, tr.winnings, u.username')
->from('tournament_registrations tr')
->join('tbl_user u', 'tr.user_id = u.user_id', 'left')
->where('tr.tid', $tid)
->where('tr.winnings >', 0)
->order_by('tr.winnings', 'desc')
->limit(2)
->get()
->result_array();
return $winners;
}
// public function get_tournament_leaderboard($tid)
// {
// $leaderboard = $this->db
// ->select('tr.user_id, u.username, tr.winnings, tr.score')
// ->from('tournament_registrations tr')
// ->join('tbl_user u', 'tr.user_id = u.user_id', 'left')
// ->where('tr.tid', $tid)
// ->order_by('tr.winnings', 'desc')
// ->get()
// ->result_array();
// return $leaderboard;
// }
public function check_tournament_completion_status($tid) {
$this->db->select('is_tournament_completed');
$this->db->from('tournament');
$this->db->where('tid', $tid);
$query = $this->db->get();
$row = $query->row();
return ($row && $row->is_tournament_completed == 1);
}
public function update_user_score($user_id, $score, $tid,$round) {
$data = array('score' => $score,
'round' => $round);
$this->db->where('user_id', $user_id);
$this->db->where('tid', $tid);
$this->db->update('tournament_registrations', $data);
}
public function update_tournament_registration($user_id, $data, $tid) {
$this->db->where('user_id', $user_id);
$this->db->where('tid', $tid);
$this->db->update('tournament_registrations', $data);
}
public function check_tournament_completion($tid)
{
$this->db->select('is_match_completed');
$this->db->from('tournament_registrations');
$this->db->where('tid', $tid);
$query = $this->db->get();
$result = $query->result_array();
$is_tournament_completed = 1;
foreach ($result as $row) {
if ($row['is_match_completed'] != 1) {
$is_tournament_completed = 0;
break;
}
}
$this->update_tournament_completion_status($tid, $is_tournament_completed);
$response = array(
'is_tournament_completed' => $is_tournament_completed,
'message' => $is_tournament_completed ? 'Tournament completed' : 'Tournament is ongoing'
);
return $response;
}
private function update_tournament_completion_status($tid, $is_tournament_completed) {
$data = array('is_tournament_completed' => $is_tournament_completed);
$this->db->where('tid', $tid);
$this->db->update('tournament', $data);
}
function insert_api($data)
{
// Assuming $data['round'] contains a JSON string
$insert_data = array(
'tname' => $data['tname'],
'entry_fee' => $data['entry_fee'],
'start_date_time' => $data['start_date_time'],
// 'end_date' => $data['end_date'],
'win_amount' => $data['win_amount'],
't_type'=> $data['t_type'],
'time_slot' => $data['time_slot'],
'round' => $data['round']
);
$this->db->insert('tournament', $insert_data);
$db_error = $this->db->error();
if (!empty($db_error['code'])) {
// Log or print the database error for debugging
log_message('error', 'Database Error: ' . $db_error['code'] . ' ' . $db_error['message']);
}
return $this->db->insert_id(); // Assuming you want to return the inserted ID
/* $this->db->insert('tournament', $data);
if($this->db->affected_rows() > 0)
{
return true;
}
else
{
return false;
}*/
}
public function insert_and_get_last_id($table, $data)
{
$this->db->insert($table, $data);
if ($this->db->affected_rows() > 0) {
$insert_id = $this->db->insert_id();
return $insert_id;
} else {
// Display or log the database error
echo $this->db->error()['message'];
return false;
}
}
function fetch_single_user($user_id)
{
$this->db->where("tid", $user_id);
$query = $this->db->get('tournament');
return $query->result_array();
}
function update_api($user_id, $data)
{
$this->db->where("tid", $user_id);
$this->db->update("tournament", $data);
if($this->db->affected_rows() > 0)
{
return true;
}
else
{
return false;
}
}
function delete_single_user($user_id)
{
$this->db->where("tid", $user_id);
$this->db->delete("tournament");
if($this->db->affected_rows() > 0)
{
return true;
}
else
{
return false;
}
}
public function insertRoundData($tournamentId, $roundData)
{
foreach ($roundData as $round) {
$data = array(
'tid' => $tournamentId,
'round' => $round
// Add any other columns if needed
);
$this->db->insert('tournament_round', $data);
if ($this->db->error()) {
// Handle the error, e.g., log or print the error message
log_message('error', 'Database Error: ' . $this->db->error()['message']);
}
}
}
public function editData2($table, $data, $where)
{
$this->db->where($where);
$this->db->update($table, $data);
return $this->db->affected_rows() > 0;
}
}