/** * Store a newly created resource in storage. * * @return Response */ public function store($userId, $exId) { $user = loadUser($userId)[0]; if (loadChallengeByUsersExercise(\Auth::id(), $user->id, $exId) == []) { storeChallenge(\Auth::id(), $user->id, $exId); $challengeId = loadChallengeByUsersExercise(\Auth::id(), $user->id, $exId)[0]->id; setWinner($challengeId, \Auth::id()); $newScore = loadUser(\Auth::id())[0]->score + 1; setUserScore(\Auth::id(), $newScore); flash()->success("{$user->username} was challenged succefully"); storeNotification($user->id, "challenged", \Auth::id(), $challengeId); } else { flash()->error("This challenge already exists"); } $sId = \Session::get('currentSerie'); $nextEx = nextExerciseofSerie($exId, $sId); if ($nextEx == []) { return redirect('series/'); } return redirect('exercises/' . $nextEx[0]->id); }
/** * __setScore * 扣除(或增加)用户相应积分 * @param $uid 用户ID,$credit 积分数,为正时表示增加积分,为负时表示扣除积分 * @return void; */ private function __setScore($uid, $credit) { $test['credit'] = $credit; $test['action'] = 'send_gift'; $test['actioncn'] = '发送礼物'; $res = setUserScore($uid, $test); return $res; }
public function storeAnswer($id, CreateAnswerRequest $request) { $input = $request->all(); // Get time between exercise load and store answer. $endTime = microtime(true); $diffTime = $endTime - $input['start_time']; $exercise = loadExercise($id)[0]; //must check for empty answers & stuff like that... //must also find a way to avoid duplicate answers since 'text' types can't be used as key $ans = new Answer(); $ans->given_code = $input['given_code']; $ans->time = $diffTime; $result = preg_replace('/[^A-Za-z0-9\\-\\ ,\\.;:\\[\\]\\?\\!@#$%&\\*\\(\\)\\-=\\+\\.^\\P{C}\\n]/', '', $input['result']); // dd($result); // dd(preg_match("/^[hH]ello, [wW]orld$/", substr_replace($result, "", -1))); // dd(preg_match("/^Hello, world$/", $result)); if ($exercise->expected_result == '*') { $ans->success = true; } else { $rule = "/" . $exercise->expected_result . "/"; // dd($rule); if (preg_match($rule, $result)) { $ans->success = true; } elseif (compare(bin2hex($result), bin2hex($exercise->expected_result . chr(0xd) . chr(0xa)))) { $ans->success = true; } else { $ans->success = false; } } $ans->uId = Auth::id(); $ans->eId = $id; storeAnswer($ans); if ($exercise->expected_result != '*') { if ($ans->success) { flash()->success("You solved the exercise in " . $diffTime . " seconds."); \Session::flash('correctAnswer', 'blabla'); } else { flash()->error("Too bad, the answer was wrong."); } } // $result = $input['result']; $answer = $input['given_code']; $sId = \Session::get('currentSerie'); $challenges = loadChallengesByUserExercise(\Auth::id(), $id); // Only update challenge if the given answer is correct. if ($ans->success) { foreach ($challenges as $c) { if ($c->winner != \Auth::id()) { if ($c->userA == \Auth::id()) { if (!empty(loadCorrectAnswers($c->userB, $id)) && $diffTime < loadCorrectAnswers($c->userB, $id)[0]->time) { $newScore = loadUser(\Auth::id())[0]->score; $newScore += 1; setUserScore(\Auth::id(), $newScore); setWinner($c->id, \Auth::id()); storeNotification($c->userB, "challenge beaten", \Auth::id(), $c->id); } } else { if (!empty(loadCorrectAnswers($c->userA, $id)) && $diffTime < loadCorrectAnswers($c->userA, $id)[0]->time) { $newScore = loadUser(\Auth::id())[0]->score; $newScore += 1; setUserScore(\Auth::id(), $newScore); setWinner($c->id, \Auth::id()); storeNotification($c->userA, "challenge beaten", \Auth::id(), $c->id); } } } } } return redirect('exercises/' . $id)->with(['result' => $result, 'answer' => $answer]); }
public function doSetCredit() { //查询条件 set_time_limit(0); if (!empty($_REQUEST['name'])) { $map['name'] = array('like', '%' . $_REQUEST['name'] . '%'); } if (!empty($_REQUEST['uid'])) { $map['id'] = array('in', explode(',', $_REQUEST['uid'])); } if (!empty($_REQUEST['email'])) { $map['email'] = $_REQUEST['email']; } if (!empty($_REQUEST['commend'])) { $map['commend'] = intval($_REQUEST['commend']); } if ($_REQUEST['status'] != '9') { $map['active'] = $_REQUEST['status']; } if ($_REQUEST['groupid'] != '9') { $map['admin_level'] = $_REQUEST['groupid']; } if (!empty($_REQUEST['bDate'])) { //开始和结束时间 $bdate = explode('-', $_REQUEST['bDate']); $begindate = mktime(0, 0, 0, $bdate[1], $bdate[2], $bdate[0]); if (empty($_REQUEST['eDate'])) { $enddate = time(); } else { $edate = explode('-', $_REQUEST['eDate']); $enddate = mktime(0, 0, 0, $edate[1], $edate[2] + 1, $edate[0]); } $map['cTime'] = array('in', array($begindate, $enddate)); } $user = D('User')->where($map)->field('id')->findAll(); if ($user == false) { $this->error('查询失败,没有这样条件的人'); } $credit['credit'] = $_POST['credit']; $credit['action'] = 'sys_add'; $credit['actioncn'] = '系统操作积分'; $credit['info'] = trim($_POST['info']); foreach ($user as $value) { $result[] = setUserScore($value['id'], $credit); } if (count(array_filter($result)) == count($user)) { $this->success('调整积分成功'); } else { $this->error('操作失败'); } }