private function findFreelancers() { $freelancers = Freelancer::where('category', Auth::user()->projects()->first()->category)->orderBy('updated_at', 'desc')->get(); foreach ($freelancers as $freelancer) { if (Decision::where('project_id', Auth::user()->projects()->first()->id)->where('freelancer_id', $freelancer->id)->where('decision', 0)->exists() || Decision::where('project_id', Auth::user()->projects()->first()->id)->where('freelancer_id', $freelancer->id)->where('user_id', Auth::id())->exists() || Match::where('project_id', Auth::user()->projects()->first()->id)->where('freelancer_id', $freelancer->id)->exists()) { continue; } return $freelancer; } return null; }
public function head2head($player1, $player2) { //Match detail $matches = \DB::table('matches')->join('tournaments', 'tournaments.tournament_id', '=', 'matches.tournament_id')->join('players as winner', 'winner.player_id', '=', 'matches.player1_id')->join('players as loser', 'loser.player_id', '=', 'matches.player2_id')->where(function ($q) use($player1) { $q->where('player1_id', '=', $player1)->orWhere('player2_id', '=', $player1); })->where(function ($q) use($player2) { $q->where('player1_id', '=', $player2)->orWhere('player2_id', '=', $player2); })->orderBy('match_division')->orderBy('round')->select('*', 'winner.first_name as winner_first_name', 'winner.last_name as winner_last_name', 'loser.first_name as loser_first_name', 'loser.last_name as loser_last_name', 'loser.player_id as loser_id', 'tournaments.name as tournament')->distinct()->get(); //Count wins by player $player1_wins = Match::where('winner_id', '=', $player1)->where('player2_id', '=', $player2)->get()->count(); $player2_wins = Match::where('winner_id', '=', $player2)->where('player2_id', '=', $player1)->get()->count(); $versus = ['player1' => ['wins' => $player1_wins], 'player2' => ['wins' => $player2_wins], 'matches' => $matches]; return $versus; }
/** * [getPlayerStreak description] * @param [type] $player_id [description] * @return [type] [description] */ public function getPlayerStreak() { $league_id = Input::get('league_id'); $player_id = Input::get('player_id'); $matches = Match::where('player1_id', '=', $player_id)->orWhere('player2_id', '=', $player_id)->orderby('match_date', 'desc')->get(); if (!is_null($matches)) { $last_match = $matches->take(1)->first(); $count = 0; if ($last_match->winner_id == $player_id) { $streak = "W"; //count win streak foreach ($matches as $m) { if ($m->winner_id == $player_id) { $count++; } else { break; } } } else { $streak = "L"; //count loss streak foreach ($matches as $m) { if ($m->winner_id != $player_id) { $count++; } else { break; } } } } $result = $streak . $count; return $result; }
public function getScore(Request $request) { $user = Auth::user(); // Get the last match I closed $closed_match = Match::where('concept_id', $request->input('concept_score'))->where('opponent_user_id', $user->id)->orderBy('updated_at', 'desc')->get()->first(); // Get the last match I opened $opened_match = Match::where('concept_id', $request->input('concept_score'))->where('user_id', $user->id)->orderBy('updated_at', 'desc')->get()->first(); if ($closed_match == null || $opened_match != null && $opened_match->updated_at > $closed_match->updated_at) { // Opened a match if ($opened_match->opponent_user_id == null) { // no one closed my opened match $user_associations = $this->getAssociationsForUser($opened_match->concept_id, $user); return Response::json(['match' => $opened_match, 'user_associations' => $user_associations]); } $match = $opened_match; $opponent = $match->opponent; } else { // closed a match $match = $closed_match; $opponent = $match->user; } // calculate matchmaking $matching = $user->calculateMatching($opponent, $request->input('domain_id')); $concept = $match->concept; // Load the user's associations $user_associations = $this->getAssociationsForUser($concept->id, $user); // load the opponent answers and asoociations $opponent_associations = $this->getAssociationsForUser($concept->id, $opponent); $total_score = 0; // calclating Score: foreach ($user_associations->associations as $user_association) { $user_association->opponent_association = null; $user_association->opponent_degree = 85.0; foreach ($opponent_associations->associations as $opponent_association) { // calculate levenshtein distance similar_text(strtoupper($user_association->associated_concept->name), strtoupper($opponent_association->associated_concept->name), $similarity); // keep the higher one if ($similarity > $user_association->opponent_degree) { $user_association->opponent_degree = $similarity; $user_association->opponent_association = $opponent_association; } } // if there is a similarity if ($user_association->opponent_association != null) { $score = 100; $score -= abs($user_association->weight - $user_association->opponent_association->weight) * 5; // take into account similarity $score *= $user_association->opponent_degree / 100; // take into account Appreciation $user_appreciation = null; $opponent_appreciation = null; if ($user_association->associated_concept->appreciations->count() > 0) { $user_appreciation = $user_association->associated_concept->appreciations->first()->appreciation; } if ($user_association->opponent_association->associated_concept->appreciations->count() > 0) { $opponent_appreciation = $user_association->opponent_association->associated_concept->appreciations->first()->appreciation; } if ($user_appreciation != null && $opponent_appreciation != null) { $score -= abs($user_appreciation - $opponent_appreciation) * 10; } else { $score -= 3 * 10; } // take into account Matchmaking $score += 5 * (1 - $matching) * 10; // add it to total score: $user_association->score = floor($score); $total_score += $score; } } $total_score = floor($total_score); // 1pts for participation $total_score += 1; if ($match->score == null) { $match->score = $total_score; $match->save(); } $match->score = $total_score; return Response::json(['matching' => $matching, 'match' => $match, 'opponent' => $opponent, 'user_associations' => $user_associations, 'opponent_associations' => $opponent_associations]); }
/** * Download Player Matches * * @return Response */ public function matches(Request $request) { $match_player_id = $request->input('player_id'); $ss = new Scraper(); $ss->get_matches($match_player_id); $match = Match::where('player2_id', '=', $match_player_id)->get(); return redirect('admin/scraper'); }
/** * Get matches of the team. * @return int */ public function matches() { return Match::where('team1_id', $this->id)->orWhere('team2_id', $this->id); }
public function saveAssociations(Request $request) { $user = Auth::user(); $domain_id = $request->input('domain_id'); $inputs = $request->input('associations'); $main_concept = Concept::find($inputs[0]['concept_id']); // Remove all existing associations for the concept and user Association::where('user_id', $user->id)->where('concept_id', $main_concept->id)->delete(); $associations = array(); foreach ($inputs as $association) { // Adding the entred concepts $concept = Concept::create($association['associated_concept']); // Adding the associations $associations[] = Association::create(['user_id' => $user->id, 'concept_id' => $association['concept_id'], 'weight' => $association['weight'], 'associated_concept_id' => $concept->id]); // Adding the appreciations if exists if (isset($association['associated_concept']['appreciations']) && isset($association['associated_concept']['appreciations'][0])) { Appreciation::create(['user_id' => $user->id, 'concept_id' => $concept->id, 'appreciation' => $association['associated_concept']['appreciations'][0]['appreciation']]); } } // Look if there is an open match $match = Match::where('concept_id', $main_concept->id)->where('user_id', '<>', $user->id)->whereNull('opponent_user_id')->get()->first(); if ($match) { // if there is an open match then close it $match->opponent_user_id = $user->id; $match->save(); } else { // if there is no open match then sorry :( // create a match if (!Match::where('concept_id', $main_concept->id)->where('user_id', $user->id)->whereNull('opponent_user_id')->get()->first()) { // if there is no open match by the same user for the same concept Match::create(['user_id' => $user->id, 'concept_id' => $main_concept->id]); } return Response::json(['flash' => 'No opponent found. your answers will be saved, and you will be notifed when an other player challenges them ;) .', 'nomatch' => true]); } // there is an open match! return Response::json(['match_id' => $match->id]); }