123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- namespace App\Http\Controllers\Syj;
- use App\Models\Syj\GameLocationModel;
- use App\Models\Syj\TacitPaintModel;
- use App\Models\Syj\TacitQuestionAnswerModel;
- use App\Models\Syj\TacitQuestionModel;
- use App\Services\Vendor\Geohash;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- class GameController extends Controller
- {
- /**
- * 玩法一
- *
- *
- */
- /**
- * 画左边
- * @param Request $request
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function drawLeft(Request $request)
- {
- $this->validate($request, [
- 'photo' => 'required',
- 'uuid' => 'required',
- ]);
- $photo = TacitPaintModel::create([
- 'openid' => $request->header('openid', ''),
- 'photo' => $request->photo,
- 'uuid' => $request->uuid,
- ]);
- return response([
- 'code' => 200,
- 'message' => 'OK',
- ]);
- }
- /**
- * 画右边
- * @param $uuid
- * @param Request $request
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function drawRight($uuid, Request $request)
- {
- $this->validate($request, [
- 'photo' => 'required',
- 'uuid' => 'required',
- ]);
- $photo = TacitPaintModel::where('uuid', $uuid)->firstOrFail();
- TacitPaintModel::create([
- 'openid' => $request->header('openid', ''),
- 'photo' => $request->photo,
- 'uuid' => $request->uuid,
- 'parent_id' => $photo->id,
- ]);
- return response([
- 'code' => 200,
- 'message' => 'OK',
- ]);
- }
- /**
- * 查看照片
- * @param $uuid
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function getPhoto($uuid)
- {
- $photo = TacitPaintModel::where('uuid', $uuid)->firstOrFail();
- $photo->user;
- if ($photo->parent_id) {
- $parent = TacitPaintModel::find($photo->parent_id);
- $parent->user;
- $photo->parent = $parent;
- }
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $photo
- ]);
- }
- /*
- * 玩法二
- *
- *
- */
- /**
- * 创建首次问题
- * @param Request $request
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function createAnswers(Request $request)
- {
- $this->validate($request, [
- 'questions' => 'required',
- 'answers' => 'required',
- 'uuid' => 'required',
- ]);
- $question = TacitQuestionAnswerModel::create([
- 'openid' => $request->header('openid'),
- 'questions' => $request->questions,
- 'answers' => $request->answers,
- 'uuid' => $request->uuid,
- ]);
- return response([
- 'code' => 200,
- 'message' => 'OK',
- ]);
- }
- /**
- * 对面测试问题
- * @param int $question_id
- * @param Request $request
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function updateAnswers($uuid, Request $request)
- {
- $this->validate($request, [
- 'answers' => 'required',
- 'uuid' => 'required',
- ]);
- $answer = TacitQuestionAnswerModel::where('uuid', $uuid)->firstOrFail();
- TacitQuestionAnswerModel::create([
- 'openid' => $request->header('openid', ''),
- 'questions' => $answer->questions,
- 'answers' => $request->answers,
- 'parent_id' => $answer->id,
- 'uuid' => $request->uuid,
- ]);
- $count = $this->compare($answer->answers, $request->answers);
- $tacit_degree = is_int($count) ? round(100 * $count / count($answer->questions)) : rand(0, 100);
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => [
- 'tacit_degree' => $tacit_degree
- ]
- ]);
- }
- /**
- * 获取随机一体
- * @param Request $request
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function getRandomQuestion(Request $request)
- {
- $question = TacitQuestionModel::when($request->get('filter'), function ($query) use ($request) {
- $filter = explode(',', $request->get('filter'));
- return $query->whereNotIn('id', $filter);
- })->inRandomOrder()->first();
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $question
- ]);
- }
- /**
- * 获取问题
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function getQuestion(Request $request)
- {
- $questions = TacitQuestionModel::select('id', 'question', 'options')
- ->when($request->filled('uuid'), function ($query) use ($request) {
- $answer = TacitQuestionAnswerModel::where('uuid', $request->uuid)->firstOrFail();
- return $query->whereIn('id', $answer->questions);
- })->inRandomOrder()
- ->limit(10)
- ->get();
- $questions = $questions->sortBy(function ($question) {
- return $question->id;
- })->values()->all();
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $questions
- ]);
- }
- /**
- * 获取问题
- * @param int $question_id
- * @param Request $request
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function getAnswers($uuid, Request $request)
- {
- $answer = TacitQuestionAnswerModel::where('uuid', $uuid)->firstOrFail();
- $answer->user;
- if ($answer->parent_id) {
- $parent = TacitQuestionAnswerModel::find($answer->parent_id);
- $parent->user;
- $answer->parent = $parent;
- $count = $this->compare($answer->answers, $parent->answers);
- $answer->tacit_degree = is_int($count) ? round(100 * $count / count($answer->questions)) : rand(0, 100);
- }
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $answer
- ]);
- }
- /**
- * 对比匹配率
- * @param $array1
- * @param $array2
- * @return bool|int
- */
- private function compare($array1, $array2)
- {
- if (count($array1, 1) != count($array2, 1)) {
- return false;
- } elseif (count($array1) != count($array2)) {
- return false;
- } elseif (count($array1) != count($array1, 1)) {
- return false;
- }
- $count = 0;
- foreach ($array1 as $key => $value) {
- if ($value == $array2[$key]) {
- $count++;
- }
- }
- return $count;
- }
- /**
- * 玩法三
- *
- *
- */
- /**
- * 前者创建定位
- * @param Request $request
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function createLocation(Request $request)
- {
- $this->validate($request, [
- 'uuid' => 'required',
- 'lng' => 'required',
- 'lat' => 'required',
- ]);
- $location = GameLocationModel::create([
- 'openid' => $request->header('openid'),
- 'uuid' => $request->uuid,
- 'lng' => $request->lng,
- 'lat' => $request->lat,
- ]);
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $location
- ]);
- }
- /**
- * 后者定位
- * @param Request $request
- * @param $uuid
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function updateLocation(Request $request, $uuid)
- {
- $this->validate($request, [
- 'uuid' => 'required',
- 'lng' => 'required',
- 'lat' => 'required',
- ]);
- $parent = GameLocationModel::where('uuid', $uuid)->firstOrFail();
- $location = GameLocationModel::create([
- 'openid' => $request->header('openid'),
- 'uuid' => $request->uuid,
- 'lng' => $request->lng,
- 'lat' => $request->lat,
- 'parent_id' => $parent->id
- ]);
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $location
- ]);
- }
- /**
- * 获取位置信息
- * @param $uuid
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function getLocation($uuid)
- {
- $location = GameLocationModel::where('uuid', $uuid)->firstOrFail();
- $location->user;
- if ($location->parent_id) {
- $parent = GameLocationModel::find($location->parent_id);
- $parent->user;
- $location->parent = $parent;
- $geo = new Geohash();
- $location->distance = sprintf(
- '%.1f',
- $geo->getDistance($location->lat, $location->lng, $parent->lat, $parent->lng) / 1000
- );
- }
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $location
- ]);
- }
- }
|