public function sell($sh_id, $qty = 0) { global $session; $database = new Database(); $sh_id = $database->escape_value($sh_id); $qty = (int) $qty; if ($this->isFound()) { $StockCompany = new StockCompany($this->sc_id); /* Check if the user has the stock or not */ $holder_stocks = new HolderStocks($sh_id); $holder = new StockHolder($sh_id); if ($holder_stocks->isFound()) { if ($holder_stocks->hasStock($this->sc_id)) { $index = array_search($this->sc_id, array_column($holder_stocks->holder_stocks, 'sc_id')); $qty_having = $holder_stocks->holder_stocks[$index]['s_qty']; if ($qty <= $qty_having) { $stock_cur_price = Stock::getCurPrice($this->sc_id); //This removes stocks from holder's list $holder_stocks->removeStock($this->sc_id, $qty, $stock_cur_price); //Updates the price of the holder $holder->amtUpdate($holder->cur_amt + $stock_cur_price * $qty); return true; } else { $session->message("You Dont have {$qty} stocks of {$StockCompany->c_name} to sell, You only have {$qty_having} stocks"); return false; } } else { $session->message("You Dont have {$StockCompany->c_name} stocks to sell"); return false; } } else { return false; } } else { return false; } }
public static function updateLeaderBoard() { $records = array(); $db_conn = new Database(); $database = new Database(); $total_ranks = null; //$query_num = "SELECT count(DISTINCT cur_amt) FROM ".self::$table; $query = "SELECT id,cur_amt,s_companies FROM " . self::$table; //$rank_num = $db_conn->query($query_num); $data = $db_conn->query($query); if ($data) { //$total_ranks = array_shift($database->fetch_array($rank_num)); while ($holder = $database->fetch_array($data)) { $total_amount = 0; if ($holder['s_companies'] > 0) { $holder_stk = new HolderStocks($holder['id']); if ($holder_stk->isFound()) { foreach ($holder_stk->holder_stocks as $hs) { $total_amount += $hs['s_qty'] * $hs['cur_price']; } } $total_amount += $holder['cur_amt']; $records[] = array('sh_id' => $holder['id'], 'total_amount' => $total_amount); } } //Sorting Algorithm $rank_record = array(); $column = array_column($records, 'total_amount'); arsort($column); foreach ($column as $key => $value) { $rank_record[] = $records[$key]; } //after sorting everything out //Storing and updating the leaderboard in db $rank = 1; foreach ($rank_record as $record) { $query_update = "UPDATE " . self::$table; $query_update .= " SET leader_pos =" . $rank++; $query_update .= " WHERE id=" . $record['sh_id']; $result = $database->query($query_update); } unset($db_conn); unset($rank); } else { unset($db_conn); return false; } }