Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Jobs\SendMatchReport;
- use Illuminate\Http\Request;
- use Carbon\Carbon;
- use Datatables;
- use App\League;
- use App\Fee;
- class MatchManagerController extends Controller
- {
- public function __construct()
- {
- $this->middleware('matchmanager');
- }
- public function index() {
- $teams = \App\Team::all();
- $users = \App\User::all();
- $match = \App\Match::all();
- $leagues = \App\League::ongoing()->orderBy('created_at', 'desc')->get();
- $types = \App\GameType::all();
- return view('admin.matchmanager', compact('teams', 'leagues', 'match', 'types'));
- }
- public function editMatchManagerBet(Request $request) {
- $rules = [
- 'betid' => 'required'
- ];
- $validator = \Validator::make($request->all(), $rules);
- if ($validator->passes()) {
- $bet = \App\Bet::find($request->betid);
- $user = \Auth::user();
- if($bet) {
- $deduct_amount = $request->bet_amount - $bet->amount;
- $bet->amount = $request->bet_amount;
- $bet->save();
- // Add to bet history
- \App\BetHistory::create([
- 'type' => 'update',
- 'match_id' => $bet->match_id,
- 'bet_id' => $bet->id,
- 'amount' => $request->bet_amount,
- 'user_id' => $user->id,
- 'curr_credits' => $user->credits
- ]);
- $user->credits -= $deduct_amount;
- $user->save();
- return ['success' => true];
- }
- } else
- return ['success' => false, 'errors' => $validator->errors()];
- }
- public function getAllTeams() {
- return Datatables::of(\App\Team::all())->make(true);
- }
- public function showRawTeamsType(League $league) {
- if ($league->exists) {
- $league_teams = $league->teams;
- $teams = $league_teams->where('type', 'tbd')->count() ? $league_teams :
- $league_teams->merge(\App\Team::type('tbd')->get());
- return $league->exists ? $teams : [];
- }
- }
- public function cancelAdminBet(Request $request) {
- $bet = \App\Bet::find($request->betid);
- if ($bet) {
- $bet->delete();
- return ['success' => true];
- } else
- return ['error' => 'This bet DOES not exist! Incident has been reported to Admins.'];
- }
- public function listGameTypes() {
- return Datatables::of(\App\GameType::all())->make(true);
- }
- public function addEditGameTypes(Request $request) {
- $rules = [
- 'name' => 'required|alpha_num|unique:game_types,name,'.$request->type_id,
- 'description' => 'required'
- ];
- $validator = \Validator::make($request->all(), $rules);
- if ($validator->passes()) {
- $type = \App\GameType::find($request->type_id);
- if($type) {
- $type->fill($request->all());
- $type->save();
- } else
- $type = \App\GameType::create($request->all());
- return ['success' => true, 'type' => $type];
- } else
- return ['success' => false, 'errors' => $validator->errors()];
- }
- public function delGameTypes(Request $request) {
- $type = \App\GameType::find($request->type_id);
- if(\App\Team::where('type', $type->name)->count()) {
- return ['error' => 'Could not delete type. Currently being used by teams'];
- } else
- $type->delete();
- return ['success' => true];
- }
- public function getAllTeamsRaw() {
- return \App\Team::all();
- }
- public function setFavoriteTeam(Request $request) {
- $league = League::find($request->league_id);
- $league->teams()->updateExistingPivot($request->team_id, [
- 'is_favorite' => $request->favopt
- ]);
- }
- public function getOngoingLeagues() {
- return League::ongoing()->orderBy('created_at', 'desc')->get();
- }
- public function setLeagueStatus(Request $request) {
- $league = League::find($request->league_id);
- if ($league) {
- switch ($request->type) {
- case 'inactive':
- $league->status = 0;
- break;
- case 'undo_inactive':
- $league->status = 1;
- break;
- case 'expire':
- if($league->matches()->activeMatches()->count())
- return ['error' => 'Could not process request. There are still active matches for this League.'];
- else
- $league->expired = 1;
- break;
- case 'undo_expire':
- $league->expired = 0;
- break;
- }
- $league->save();
- return ['success' => true];
- } else {
- return ['error' => 'Could not process request. Please try again'];
- }
- }
- public function addEditTeams(Request $request) {
- $rules = [
- 'name' => 'required',
- 'shortname' => 'required|max:20',
- 'type' => 'required',
- 'image' => $request->team_id ? 'image|mimes:jpeg,png,jpg,gif,svg|max:2048' : 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
- ];
- $validator = \Validator::make($request->all(), $rules);
- if ($validator->passes()) {
- $team = $request->team_id ? \App\Team::find($request->team_id) : new \App\Team;
- $team->name = $request->name;
- $team->shortname = $request->shortname;
- $team->type = $request->type;
- if ($request->image) {
- $image_file = time() . '.' . $request->image->getClientOriginalExtension();
- $request->image->move(storage_path('uploads'), $image_file);
- $team->image = 'public_image/' . $image_file;
- }
- $team->save();
- return ['success' => true, 'team' => $team];
- } else
- return ['succss' => false, 'errors' => $validator->errors()];
- }
- public function deleteTeam(Request $request) {
- $team = \App\Team::find($request->team_id);
- if ($team->matches()->count() == 0) {
- $team->delete();
- return ['success' => true];
- } else
- return ['success' => false, 'error' => 'This team already exists on some matches and cannot be deleted.'];
- }
- public function addEditLeagues(Request $request) {
- $validator = \Validator::make($request->all(), [
- 'name' => 'required',
- 'description' => 'required',
- 'type' => 'required',
- 'teams' => 'nullable|array',
- 'image' => $request->league_id ? 'image|mimes:jpeg,png,jpg,gif,svg|max:2048' : 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
- 'bottom_image' => $request->league_id ? 'image|mimes:jpeg,png,jpg,gif,svg|max:2048' : 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
- ]);
- if ($validator->passes()) {
- $input = $request->all();
- if ($request->image) {
- $input['image'] = time() . '.' . $request->image->getClientOriginalExtension();
- $request->image->move(storage_path('uploads'), $input['image']);
- }
- if ($request->bottom_image) {
- $input['bottom_image'] = time() . '.' . $request->bottom_image->getClientOriginalExtension();
- $request->bottom_image->move(storage_path('uploads'), $input['bottom_image']);
- }
- if ($request->league_id) {
- $league = \App\League::find($request->league_id);
- if ($league->betting_status != -1) {
- $league->type = $request->type;
- $league->name = $request->name;
- $league->description = $request->description;
- $league->status = $request->status;
- $league->favorites_minimum = $request->favorites_minimum;
- $league->betting_status = $request->betting_status;
- if($request->expired && $league->matches()->activeMatches()->count())
- return ['error' => ['There are active matches for this league and cannot set to expire!']];
- else
- $league->expired = $request->expired;
- $league->betting_fee = $request->betting_fee / 100;
- if ($request->image)
- $league->image = $input['image'];
- if ($request->bottom_image)
- $league->bottom_image = 'public_image/' . $input['bottom_image'];
- $league->save();
- $league->teams()->sync($request->teams);
- // Remove admin bets if betting is closed
- if ($league->betting_status == 0) {
- foreach ($league->bets as $bet) {
- if (hasMatchManagementAccess($bet->user))
- \App\Bet::find($bet->id)->delete();
- }
- }
- }
- } else {
- $league = \App\League::create([
- 'type' => $request->type,
- 'name' => $request->name,
- 'description' => $request->description,
- 'image' => $input['image'],
- 'league_winner' => 0
- ]);
- if ($request->bottom_image)
- $league->bottom_image = 'public_image/' . $input['bottom_image'];
- $league->status = $request->status;
- $league->betting_status = $request->betting_status;
- $league->favorites_minimum = $request->favorites_minimum;
- $league->save();
- $league->teams()->sync($request->teams);
- }
- return ['success' => 'done'];
- }
- return response()->json(['error' => $validator->errors()->all()]);
- }
- public function deleteLeagues(Request $request) {
- $league = \App\League::find($request->league_id);
- if ($league->bets->count()) {
- return ['error' => 'There are matches/bets linked to this league and cannot be deleted.'];
- } else
- return ['league' => $league->delete()];
- }
- public function listLeagues(Request $request) {
- $dt = Datatables::of(\App\League::all()->load('teams', 'champion'));
- $dt->addColumn('btn_options', function($dt) {
- $buttons = '';
- if ($dt->betting_status == -1) {
- if ($dt->status) {
- if (!$dt->expired) {
- return '<button type="button" class="btn btn-default btn-xs setLeagueStatus" data-sts_type="inactive">Inactive</button> ' .
- '<button type="button" class="btn btn-warning btn-xs setLeagueStatus" data-sts_type="expire">Expire</button>';
- } else {
- if ($dt->updated_at->diffInMinutes(Carbon::now()) < 60)
- return '<button type="button" class="btn btn-default btn-xs setLeagueStatus" data-sts_type="inactive">Inactive</button> ' .
- '<button type="button" class="btn btn-info btn-xs setLeagueStatus" data-sts_type="undo_expire">Undo Expire</button>';
- else
- return '<button type="button" class="btn btn-default btn-xs setLeagueStatus" data-sts_type="inactive">Inactive</button>';
- }
- } else {
- if (!$dt->expired) {
- if ($dt->updated_at->diffInMinutes(Carbon::now()) < 60)
- return '<button type="button" class="btn btn-success btn-xs setLeagueStatus" data-sts_type="undo_inactive">Set Active</button> ' .
- '<button type="button" class="btn btn-warning btn-xs setLeagueStatus" data-sts_type="expire">Expire</button>';
- else
- return '<button type="button" class="btn btn-warning btn-xs setLeagueStatus" data-sts_type="expire">Expire</button>';
- } else {
- if ($dt->updated_at->diffInMinutes(Carbon::now()) < 60)
- return '<button type="button" class="btn btn-success btn-xs setLeagueStatus" data-sts_type="undo_inactive">Set Active</button> ' .
- '<button type="button" class="btn btn-info btn-xs setLeagueStatus" data-sts_type="undo_expire">Undo Expire</button>';
- else
- return '';
- }
- }
- } else {
- return '';
- }
- });
- return $dt->make(true);
- }
- public function listMatches(Request $request) {
- return Datatables::of(\App\Match::mainMatches()->schedNameSort()->get()->load('league', 'teamA', 'teamB', 'teamwinner'))->make(true);
- }
- public function showSubMatches(\App\Match $mainmatch) {
- if($mainmatch) {
- if($mainmatch->type == 'main') {
- $sub_matches = $mainmatch->subMatches;
- $sub_matches = $sub_matches->prepend($mainmatch);
- return $sub_matches;
- } else
- return [];
- } else
- return [];
- }
- public function addEditMatches(Request $request) {
- $validator = \Validator::make($request->all(), [
- 'league_id' => 'required',
- 'schedule' => 'required|date',
- 'best_of' => 'required',
- 'team_a' => 'sometimes|required',
- 'team_b' => 'sometimes|required'
- ]);
- if ($validator->passes()) {
- $input = $request->all();
- $input['schedule'] = date('Y-m-d H:i:s', strtotime($request->schedule));
- $input['fee'] = ($request->fee / 100);
- if ($request->match_id) {
- $match = \App\Match::find($request->match_id);
- if ($match->status == 'open') {
- $match->fill($input);
- $match->save();
- // Update teams on sub-matches
- foreach($match->subMatches as $subMatch) {
- $subMatch->name = str_replace([$subMatch->teamA->name, $subMatch->teamB->name], [$match->teamA->name, $match->teamB->name], $subMatch->name);
- $subMatch->team_a = $match->team_a;
- $subMatch->team_b = $match->team_b;
- $subMatch->save();
- }
- if (in_array($request->status, ['cancelled', 'forfeit'])) {
- foreach ($match->bets as $bet) {
- $user = $bet->user;
- $user->credits += $bet->amount;
- $user->save();
- }
- // Cancel all bets from existing sub-matches
- foreach($match->subMatches as $subMatch) {
- foreach ($subMatch->bets as $bet) {
- $user = $bet->user;
- $user->credits += $bet->amount;
- $user->save();
- }
- $subMatch->status = $request->status;
- $subMatch->save();
- }
- $match->status = $request->status;
- $match->save();
- }
- if ($match->status == 'ongoing') {
- setupOngoingMatch($match);
- }
- return ['success' => 'done1'];
- } else {
- return ['error' => [
- 'status' => ['Cannot edit match. This Match has already started!']
- ]];
- }
- } else {
- $mainmatch = \App\Match::create($input);
- if($request->submatches) {
- foreach($request->submatches as $sub_match) {
- $submatch = \App\SubMatch::create($input);
- $submatch->type = 'sub';
- $submatch->game_grp = $sub_match['ctr'];
- $submatch->sub_type = $sub_match['subtype'];
- $submatch->name = $sub_match['name'];
- $submatch->label = $sub_match['name'];
- if($sub_match['ctr'] > 1)
- $submatch->schedule = Carbon::createFromTimeString($request->schedule)
- ->addHours($sub_match['ctr']-1);
- $submatch->main_match = $mainmatch->id;
- $submatch->save();
- }
- }
- return ['success' => 'done'];
- }
- } else
- return ['error' => $validator->errors(), 'data' => $request->all()];
- }
- //edit match standings (match.blade.php)
- public function editMatch(Request $request) {
- $validator = \Validator::make($request->all(), [
- 'match_id' => 'required',
- 'schedule' => 'sometimes|nullable|date'
- ]);
- if ($validator->passes()) {
- $match = \App\Match::find($request->match_id);
- if ($match->status == 'open') {
- $match->name = $request->name;
- $match->label = $request->label;
- $match->fee = ($request->fee / 100);
- if($request->status)
- $match->status = $request->status;
- if($request->schedule)
- $match->schedule = date('Y-m-d H:i:s', strtotime($request->schedule));
- $match->save();
- if ($match->status == 'ongoing')
- setupOngoingMatch($match);
- return ['success' => 'done1'];
- }
- elseif($match->status =='ongoing'){
- $match->teama_score = $request->teama_score;
- $match->teamb_score = $request->teamb_score;
- $match->save();
- return ['success' => 'done1'];
- }
- elseif($match->status =='settled'){
- $match->teama_score = $request->teama_score;
- $match->teamb_score = $request->teamb_score;
- $match->save();
- return ['success' => 'done1'];
- }
- elseif($match->status =='draw'){
- $match->teama_score = $request->teama_score;
- $match->teamb_score = $request->teamb_score;
- $match->save();
- return ['success' => 'done1'];
- }
- } else
- return ['error' => $validator->errors(), 'data' => $request->all()];
- }
- //Edit stream (matchmanager)
- public function editStream(Request $request) {
- $validator = \Validator::make($request->all(), [
- 'match_id' => 'required',
- 'schedule' => 'sometimes|nullable|date'
- ]);
- if ($validator->passes()) {
- $match = \App\Match::find($request->match_id);
- if ($match->status == 'open') {
- if ($match->status == 'ongoing')
- setupOngoingMatch($match);
- return ['success' => 'done1'];
- }
- elseif($match->status =='ongoing'){
- $match->stream_twitch = $request->stream_twitch;
- $match->stream_yt = $request->stream_yt;
- $match->stream_fb = $request->stream_fb;
- $match->save();
- return ['success' => 'done1'];
- }
- } else
- return ['error' => $validator->errors(), 'data' => $request->all()];
- }
- //set match score (matchmanager)
- public function editMatchStanding(Request $request) {
- $validator = \Validator::make($request->all(), [
- ]);
- if ($validator->passes()) {
- $input = $request->all();
- $input['teama_score'] = ($request->teama_score);
- $input['teamb_score'] = ($request->teamb_score);
- if ($request->match_id) {
- $match = \App\Match::find($request->match_id);
- if ($match->status == 'ongoing') {
- $match->fill($input);
- $match->save();
- return ['success' => 'done1'];
- }
- if ($match->status == 'settled') {
- $match->fill($input);
- $match->save();
- return ['success' => 'done1'];
- }
- }
- } else
- return ['error' => $validator->errors(), 'data' => $request->all()];
- }
- public function setMatchOpen(Request $request) {
- $match = \App\Match::find($request->match_id);
- if ($match && $match->status == 'ongoing') {
- if (Carbon::now()->diffInMinutes($match->updated_at) <= 10) {
- return $this->openOngoingMatch($match);
- } else {
- if($request->passcode && $request->passcode == env('OVERRIDE_CODE'))
- return $this->openOngoingMatch($match);
- else
- return ['success' => false, 'need_auth' => true, 'error' => 'Cannot open match, please contact admin to override!'];
- }
- } else
- return ['success' => false];
- }
- private function openOngoingMatch(\App\Match $match) {
- $match->status = 'open';
- $match->re_opened = true;
- $match->save();
- if ($match->type == 'main') {
- foreach ($match->subMatches as $subMatch) {
- if ($subMatch->status == 'ongoing') {
- $subMatch->status = 'open';
- $subMatch->re_opened = true;
- $subMatch->save();
- }
- }
- } else {
- $subMatches = \App\SubMatch::where('main_match', $match->main_match)
- ->where('game_grp', $match->game_grp)
- ->where('id', '!=', $match->id)->get();
- foreach ($subMatches as $submatch) {
- if ($submatch->status == 'ongoing') {
- $submatch->status = 'open';
- $submatch->re_opened = true;
- $submatch->save();
- }
- }
- }
- return ['success' => true];
- }
- public function extendMatchTime(Request $request) {
- $match = \App\Match::find($request->match_id);
- if ($match) {
- $match->schedule = $match->schedule->addMinutes($request->ext_time);
- $match->save();
- return ['success' => true];
- } else
- return ['success' => false, 'data' => $request->all()];
- }
- private function updateBetRatio($betID, $ratio) {
- $bet = \App\Bet::find($betID);
- $bet->ratio = $ratio;
- $bet->save();
- }
- public function deleteMatches(Request $request) {
- $match = \App\Match::find($request->match_id);
- if ($match->bets->count()) {
- return ['error' => 'There are active bets on this match and cannot be deleted.'];
- } else {
- if ($match->subMatches) {
- foreach ($match->subMatches as $subMatch) {
- if ($subMatch->bets->count()) {
- return ['error' => 'There are active bets on this match and cannot be deleted.'];
- }
- }
- return ['match' => $match->delete()];
- } else
- return ['match' => $match->delete()];
- }
- }
- public function settleMatches(Request $request) {
- $validator = \Validator::make($request->all(), [
- 'match_id' => 'required',
- 'team_winner' => 'required'
- ]);
- if ($validator->passes()) {
- $match = \App\Match::find($request->match_id);
- if ($match->status != 'settled') {
- if (!in_array($request->team_winner, ['draw', 'cancelled']) && $match->type == 'main' && $match->subMatches()->activeMatches()->count())
- return ['error' => 'There are still active sub matches, Please settle them first!'];
- else {
- $total_circulating_credits = calculateCredits('total_circulating_credits');
- $match->team_winner = in_array($request->team_winner, ['draw', 'cancelled']) ? null : $request->team_winner;
- $match->status = in_array($request->team_winner, ['draw', 'cancelled']) ? $request->team_winner : 'settled';
- $match->save();
- foreach ($match->bets as $bet) {
- $user = $bet->user;
- if (in_array($request->team_winner, ['draw', 'cancelled'])) {
- $user->credits += $bet->amount;
- $user->level += 0;
- $user->curr_exp += 0;
- $user->prev_exp += 0;
- $user->exp_needed +0;
- $bet->gains = 0;
- $bet->save();
- $user->save();
- } else {
- if ($bet->team_id == $match->team_winner) {
- $gains = ($bet->amount * $bet->ratio);
- $gainexp = $bet->amount;
- $user->credits += $gains;
- $user->curr_exp += $gainexp;
- $bet->gains = ($gains - $bet->amount);
- $bet->save();
- $user->save();
- } else {
- $gainexp = $bet->amount;
- $user->curr_exp += $gainexp;
- $bet->gains = -($bet->amount);
- $user->save();
- $bet->save();
- }
- while($user->curr_exp >= $user->exp_needed){
- if($user->level <= 19){
- $user->curr_exp -= $user->exp_needed;
- $user->prev_exp = $user->exp_needed;
- $user->exp_needed = $user->prev_exp * 1.4;
- $user->level += 1;
- $user->save();
- }
- elseif($user->level >= 20 && $user->level <= 39){
- $user->curr_exp -= $user->exp_needed;
- $user->prev_exp = $user->exp_needed;
- $user->exp_needed = $user->prev_exp * 1.1;
- $user->level += 1;
- $user->save();
- }
- elseif($user->level >= 40 && $user->level <= 59){
- $user->curr_exp -= $user->exp_needed;
- $user->prev_exp = $user->exp_needed;
- $user->exp_needed = $user->prev_exp * 1.05;
- $user->level += 1;
- $user->save();
- }
- elseif($user->level >= 60 && $user->level <= 79){
- $user->curr_exp -= $user->exp_needed;
- $user->prev_exp = $user->exp_needed;
- $user->exp_needed = $user->prev_exp * 1.04;
- $user->level += 1;
- $user->save();
- }
- elseif($user->level >= 80 && $user->level <= 99){
- $user->curr_exp -= $user->exp_needed;
- $user->prev_exp = $user->exp_needed;
- $user->exp_needed = $user->prev_exp * 1.03;
- $user->level += 1;
- $user->save();
- }
- elseif($user->level == 100){
- $user->curr_exp = $user->curr_exp * 0;
- $user->prev_exp -= $user->exp_needed;
- $user->exp_needed = $user->exp_needed * 0;
- $user->level += 0;
- $user->save();
- }
- return ['success' => 'Level up!'];
- }
- }
- }
- if ($request->team_winner == 'cancelled')
- $this->cancelChildMatches($match);
- $this->saveMatchReport($match, $total_circulating_credits);
- return ['success' => 'done'];
- }
- } else
- return ['error' => 'Match was already settled, please refresh page!'];
- } else
- return ['error' => $validator->errors()];
- }
- public function settleTournament(Request $request) {
- $validator = \Validator::make($request->all(), [
- 'league_id' => 'required',
- 'team_id' => 'required'
- ]);
- if ($validator->passes()) {
- $league = \App\League::find($request->league_id);
- if ($league->betting_status == 1) {
- return ['success' => false, 'error' => 'Tournament betting is still open! Please close it first.'];
- } else {
- $league->league_winner = $request->team_id;
- $bets = \App\Bet::tournament($request->league_id)->get();
- foreach ($bets as $bet) {
- $user = $bet->user;
- $teamRatio = tournamentRatioPerTeam($league->id, $bet->team_id);
- if ($bet->team_id == $league->league_winner) {
- $gains = ($bet->amount * $teamRatio);
- $bet->gains = ($gains - $bet->amount);
- $user->credits += $gains;
- $user->save();
- } else
- $bet->gains = -($bet->amount);
- $bet->ratio = $teamRatio;
- $bet->save();
- }
- $league->betting_status = -1;
- $league->save();
- // Fee
- $fee = new Fee;
- $fee->meta_key = 'tournament';
- $fee->meta_value = $league->id;
- $fee->percent = $league->betting_fee;
- $fee->collected = \App\Bet::tournament($league->id)->sum('amount') * $league->betting_fee;
- $fee->save();
- return ['success' => true];
- }
- } else
- return ['error' => $validator->errors()];
- }
- private function cancelChildMatches(\App\Match $match) {
- if ($match->type == 'main') {
- foreach ($match->subMatches as $submatch) {
- if (in_array($submatch->status, ['open', 'ongoing'])) {
- foreach ($submatch->bets as $bet) {
- $user = $bet->user;
- $user->credits += $bet->amount;
- $bet->gains = 0;
- $user->save();
- $bet->save();
- }
- $submatch->status = 'cancelled';
- $submatch->save();
- }
- }
- } else {
- if ($match->sub_type == 'main') {
- $subMatches = \App\SubMatch::where('main_match', $match->main_match)
- ->where('game_grp', $match->game_grp)
- ->where('id', '!=', $match->id)->get();
- foreach ($subMatches as $submatch) {
- if (in_array($submatch->status, ['open', 'ongoing'])) {
- foreach ($submatch->bets as $bet) {
- $user = $bet->user;
- $user->credits += $bet->amount;
- $bet->gains = 0;
- $user->save();
- $bet->save();
- }
- $submatch->status = 'cancelled';
- $submatch->save();
- }
- }
- }
- }
- }
- /**
- * Save match report
- *
- * @param App\Match $match
- * @return Response $response
- */
- public function saveMatchReport(\App\Match $match,$total_circulating_credits)
- {
- $this->dispatch(new SendMatchReport(['type' => 'settled', 'match_id' => $match->id,'total_circulating_credits' => $total_circulating_credits,'settled_by' => \Auth::user()->id]));
- return response()->json(['success' => true, 'message' => 'Match report successfully save']);
- }
- /**
- * Get match reports
- * @param int match_id
- * @return html table of reports
- */
- public function returnMatchReport(Request $request)
- {
- if (hasMatchManagementAccess(\Auth::user())) {
- $match = \App\Match::find($request->match_id);
- $total_team_a_bettors=0;
- $total_team_a_bets=0;
- $total_team_a_profit=0;
- $total_team_b_bettors=0;
- $total_team_b_bets=0;
- $total_team_b_profit=0;
- $a_users_list = [];
- $b_users_list = [];
- $div = '';
- $div .= '<div class="m-container1" style="width: 98% !important;">';
- $div .= '<div class="main-ct" style="margin-bottom: 0">';
- $div .= '<div class="title">Admin Report</div>';
- $div .= '<div class="clearfix"></div>';
- $div .= '<div class="matchmain">';
- $div .= '<div class="col-md-12" style="max-height: 500px; overflow: auto">';
- $div .= '<div class="col-md-6">';
- $div .= '<table id="team_a_table" class="table table-striped" width="100%">';
- $div .= '<thead>';
- $div .= '<tr>';
- $div .= '<th>ID</th>';
- $div .= '<th>'.$match->teamA->name.' Bettors</th>';
- $div .= '<th>Bet Amount</th>';
- $div .= '<th>Profit/Loss</th>';
- $div .= '</tr>';
- $div .= '</thead>';
- $div .= '<tbody>';
- $color='black';
- foreach ($match->load('bets.user')->bets->sortBy('user_id') as $bet) {
- if($match->team_a == $bet->team_id){
- if($bet->user->type == 'user'){
- if(in_array($bet->user->id,$a_users_list)){
- $style = 'style="background:#ffbaca;"';
- }else{
- $style= '';
- }
- }else{
- $style = 'style="background:#bae6ff;"';
- }
- $total_team_a_bettors ++;
- $profit = $bet->team->potentialMatchWinningPerUser($bet->match->id, $bet->user->id) - $bet->amount;
- $div .= '<tr '.$style.'>';
- $div .='<td>'.$bet->user->id.'</td>';
- $div .='<td>'.$bet->user->name.'</td>';
- $div .= '<td>₱ '.number_format($bet->amount,2).'</td>';
- if($bet->team_id == $match->team_winner && $match->status =='settled'){
- $color = 'green';
- $div .= '<td><span style="color:green">₱ '.number_format($profit,2).'</span></td>';
- $total_team_a_profit+=$profit;
- }elseif ($match->status =='draw'){
- $color='black';
- $div .= '<td><span style="color:black">₱ 0.00</span></td>';
- }elseif ($bet->team_id != $match->team_winner && $match->status =='settled') {
- $color='red';
- $div .= '<td><span style="color:red">₱ '.number_format($bet->amount,2).'</span></td>';
- $total_team_a_profit+=$bet->amount;
- }elseif ($match->status =='open' || $match->status =='ongoing') {
- $color='black';
- if($bet->user->type == 'user')
- $div .= '<td></td>';
- else
- $div .= '<td><button type="button" class="btn btn-danger btn-xs cancelAdminBet" data-betid="'.$bet->id.'">Cancel</button></td>';
- }
- $div .= '</tr>';
- $total_team_a_bets+=$bet->amount;
- array_push($a_users_list,$bet->user->id);
- }
- }
- $div .= ' <tr>';
- $div .= '<td></td>';
- $div .= '<td></td>';
- $div .= '<td style="font-weight: bold">₱ '.number_format($total_team_a_bets,2).'</td>';
- $div .= '<td style="font-weight: bold"><span style="color:'.$color.'">'.($total_team_a_profit != 0 ? '₱ '.number_format($total_team_a_profit,2) : '').'</span></td>';
- $div .= '</tr>';
- $div .= '</tbody>';
- $div .= '</table>';
- $div .= '</div>';
- $div .= '<div class="col-md-6">';
- $div .= '<table id="team_a_table" class="table table-striped" width="100%">';
- $div .= '<thead>';
- $div .= '<tr>';
- $div .= '<th>ID</th>';
- $div .= '<th>'.$match->teamB->name.' Bettors</th>';
- $div .= '<th>Bet Amount</th>';
- $div .= '<th>Profit/Loss</th>';
- $div .= '</tr>';
- $div .= '</thead>';
- $div .= '<tbody>';
- foreach ($match->load('bets.user')->bets->sortBy('user_id') as $bet) {
- if($match->team_b == $bet->team_id){
- if($bet->user->type == 'user'){
- if(in_array($bet->user->id,$b_users_list)){
- $style = 'style="background:#ffbaca;"';
- }else{
- $style= '';
- }
- }else{
- $style = 'style="background:#bae6ff;"';
- }
- $total_team_b_bettors ++;
- $profit = $bet->team->potentialMatchWinningPerUser($bet->match->id, $bet->user->id) - $bet->amount;
- $div .= '<tr '.$style.'>';
- $div .='<td>'.$bet->user->id.'</td>';
- $div .='<td>'.$bet->user->name.'</td>';
- $div .= '<td>₱ '.number_format($bet->amount,2).'</td>';
- if($bet->team_id == $match->team_winner && $match->status =='settled'){
- $color = 'green';
- $div .= '<td><span style="color:green">₱ '.number_format($profit,2).'</span></td>';
- $total_team_b_profit+=$profit;
- }elseif ($match->status =='draw'){
- $color='black';
- $div .= '<td><span style="color:black">₱ 0.00</span></td>';
- }elseif ($bet->team_id != $match->team_winner && $match->status =='settled') {
- $color='red';
- $div .= '<td><span style="color:red">₱ '.number_format($bet->amount,2).'</span></td>';
- $total_team_b_profit+=$bet->amount;
- }elseif ($match->status =='open' || $match->status =='ongoing') {
- $color='black';
- if($bet->user->type == 'user')
- $div .= '<td></td>';
- else
- $div .= '<td><button type="button" class="btn btn-danger btn-xs cancelAdminBet" data-betid="'.$bet->id.'">Cancel</button></td>';
- }
- $div .= '</tr>';
- $total_team_b_bets+=$bet->amount;
- array_push($b_users_list,$bet->user->id);
- }
- }
- $div .= ' <tr>';
- $div .= '<td></td>';
- $div .= '<td></td>';
- $div .= '<td style="font-weight: bold">₱ '.number_format($total_team_b_bets,2).'</td>';
- $div .= '<td style="font-weight: bold"><span style="color:'.$color.'">'.($total_team_b_profit != 0 ? '₱ '.number_format($total_team_b_profit,2) : '').'</span></td>';
- $div .= '</tr>';
- $div .= '</tbody>';
- $div .= '</table>';
- $div .= '</div>';
- $div .= '</div>';
- $div .= '<div class="col-md-8" style="margin-top: 10px;">';
- $total_bets = $total_team_a_bets+$total_team_b_bets;
- switch ($match->status) {
- case 'draw':
- $collected_fees = 0.00;
- $total_payout = number_format(0,2);
- break;
- case 'open':
- $collected_fees = 0.00;
- $total_payout = number_format(0,2);
- break;
- case 'settled':
- $collected_fees = $total_bets*$match->fee;
- $total_payout = number_format($total_bets-$collected_fees,2);
- break;
- case 'ongoing':
- $collected_fees = $total_bets*$match->fee;
- $total_payout = number_format($total_bets-$collected_fees,2);
- break;
- case 'cancelled':
- $collected_fees = 0.00;
- $total_payout = number_format(0,2);
- break;
- default:
- # code...
- break;
- }
- $div .= '<dt class="col-sm-5">Total Bettors on '.$match->teamA->name.'</dt>';
- $div .= '<dd class="col-sm-7">'.$total_team_a_bettors.'</dd>';
- $div .= '<dt class="col-sm-5">Total Bets on '.$match->teamA->name.'</dt>';
- $div .= '<dd class="col-sm-7">₱ '.number_format($total_team_a_bets,2).'</dd>';
- $div .= '<dt class="col-sm-5">Average Bets on '.$match->teamA->name.'</dt>';
- $div .= '<dd class="col-sm-7">₱ '.($total_team_a_bettors != 0 ? number_format($total_team_a_bets/$total_team_a_bettors,2) : '').'</dd>';
- $div .= '<dt class="col-sm-5">Total Bettors on '.$match->teamB->name.'</dt>';
- $div .= '<dd class="col-sm-7">'.$total_team_b_bettors.'</dd>';
- $div .= '<dt class="col-sm-5">Total Bets on '.$match->teamB->name.'</dt>';
- $div .= '<dd class="col-sm-7">₱ '.number_format($total_team_b_bets,2).'</dd>';
- $div .= '<dt class="col-sm-5">Average Bets on '.$match->teamB->name.'</dt>';
- $div .= '<dd class="col-sm-7">₱ '.($total_team_b_bettors != 0 ? number_format($total_team_b_bets/$total_team_b_bettors,2) : '').'</dd>';
- $div .= '<dt class="col-sm-5">Total Bettors on this match</dt>';
- $div .= '<dd class="col-sm-7">'.($total_team_b_bettors+$total_team_a_bettors).'</dd>';
- $div .= '<dt class="col-sm-5">Total Bets on this match</dt>';
- $div .= '<dd class="col-sm-7">₱ '.number_format($total_bets,2).'</dd>';
- $div .= '<dt class="col-sm-5">Average Bets on this match</dt>';
- $div .= '<dd class="col-sm-7">₱ '.($total_team_b_bettors != 0 || $total_team_a_bettors != 0 ? number_format($total_bets/($total_team_b_bettors+$total_team_a_bettors),2) : '').'</dd>';
- $div .= '<dt class="col-sm-5">Match Fee</dt>';
- $div .= '<dd class="col-sm-7">₱ '.($match->fee*100).'%</dd>';
- $div .= '<dt class="col-sm-5">Total Fees collected on this match</dt>';
- $div .= '<dd class="col-sm-7">₱ '.number_format($collected_fees,2).'</dd>';
- $div .= '<dt class="col-sm-5">Total Payout</dt>';
- $div .= '<dd class="col-sm-7">₱ '.$total_payout.'</dd>';
- $div .= '</div>';
- $div .= '</div>';
- $div .= '</div>';
- $div .= '</div>';
- return $div;
- } else {
- return '';
- }
- }
- /**
- * Get League reports
- * @param int league_id
- * @return html table of reports
- */
- public function returnLeagueReport(Request $request)
- {
- if (hasMatchManagementAccess(\Auth::user())) {
- return returnLeagueReport($request->league_id,'div');
- } else {
- return '';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement