public function upload() { if ($this->validate()) { $newsourcecode = new Sourcecodes(); $user = User::findByUsername(Yii::$app->user->identity->username); $newsourcecode->team = $user->teamname; $newsourcecode->uploaded_by = $user->username; date_default_timezone_set('PRC'); $newsourcecode->uploaded_at = date("Y/m/d H:i:s"); $newsourcecode->save(); //先在数据库中insert一条数据,然后再命名文件 $this->sourcecode->saveAs('uploads/' . $newsourcecode->id . '.cpp'); return $newsourcecode->id; } else { return false; } }
public function actionIndex() { //没登录就回到主页 if (Yii::$app->user->isGuest) { return $this->render('/site/index'); } $myteamname = User::findByUsername(Yii::$app->user->identity->username)->teamname; //没加入队伍就到/team/error页面 if ($myteamname == "") { return $this->render('/team/error', ['message' => '<h2>你还没有加入任何一个战队呢!</h2>']); } //$myteam = Team::findByTeamname($myteamname); $myteam = Team::findOne(['teamname' => $myteamname]); //我已上传成功的代码 $mycodes = Sourcecodes::find()->where(['team' => $myteamname])->orderBy('uploaded_at DESC')->all(); $mycodes2 = $mycodes; foreach ($mycodes2 as $mycode) { $mycode->uploaded_at = $mycode->id . '号,上传于' . $mycode->uploaded_at; } //已上传代码的队伍,除掉自己所在的队伍 $teamnames = Sourcecodes::find()->select('team')->where("team<>'{$myteamname}'")->distinct()->orderBy('uploaded_at DESC'); $teams = Team::find()->where(['teamname' => $teamnames])->all(); //别的队伍的代码 $otherscodes = Sourcecodes::find()->where("team<>'{$myteamname}'")->select('uploaded_at')->orderBy('uploaded_at DESC')->column(); //对战结果 $results1 = Battleresult::find(); //对战结果分页器 $pagination = new Pagination(['defaultPageSize' => 20, 'totalCount' => $results1->count()]); $results = $results1->orderBy('battle_at DESC')->offset($pagination->offset)->limit($pagination->limit)->all(); //表单 $model = new BattleForm(); //上传文件$model, if ($model->load(Yii::$app->request->post())) { if ($myteam->battled_time < 200) { //如果对战次数还没到10次 $myteam->battled_time++; $myteam->save(false); $model->myteam = $myteam->id; //我方代码编号 $model->mycode = $_POST['BattleForm']['mycode']; //敌方队伍名称 $model->enemyteam = $_POST['BattleForm']['enemyteam']; //敌方代码编号 $model->enemycode = $_POST['BattleForm']['enemycode']; $result = new Battleresult(); $result->team1 = $myteamname; $result->ai1 = $model->mycode; $result->team2 = Team::findOne(['id' => $model->enemyteam])->teamname; $result->ai2 = $model->enemycode; date_default_timezone_set('PRC'); $result->battle_at = date("Y-m-d H:i:s"); $result->result = '对战中'; $result->save(); $model->id = $result->id; $re = $model->battle(); return $this->redirect(['index']); } else { print "哥们你对战太多次了<br>明天再来吧~"; } die; } return $this->render('index', ['model' => $model, 'myteam' => $myteam, 'mycodes' => $mycodes2, 'teams' => $teams, 'otherscodes' => $otherscodes, 'results' => $results, 'pagination' => $pagination]); }
public function getTeamcodes($teamname) { $teamcodes = Sourcecodes::find()->where(['team' => $teamname])->orderBy('uploaded_at DESC')->all(); return $teamcodes; }