GameController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace App\Http\Controllers\Syj;
  3. use App\Models\Syj\GameLocationModel;
  4. use App\Models\Syj\TacitPaintModel;
  5. use App\Models\Syj\TacitQuestionAnswerModel;
  6. use App\Models\Syj\TacitQuestionModel;
  7. use App\Services\Vendor\Geohash;
  8. use Illuminate\Http\Request;
  9. use App\Http\Controllers\Controller;
  10. class GameController extends Controller
  11. {
  12. /**
  13. * 玩法一
  14. *
  15. *
  16. */
  17. /**
  18. * 画左边
  19. * @param Request $request
  20. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  21. */
  22. public function drawLeft(Request $request)
  23. {
  24. $this->validate($request, [
  25. 'photo' => 'required',
  26. 'uuid' => 'required',
  27. ]);
  28. $photo = TacitPaintModel::create([
  29. 'openid' => $request->header('openid', ''),
  30. 'photo' => $request->photo,
  31. 'uuid' => $request->uuid,
  32. ]);
  33. return response([
  34. 'code' => 200,
  35. 'message' => 'OK',
  36. ]);
  37. }
  38. /**
  39. * 画右边
  40. * @param $uuid
  41. * @param Request $request
  42. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  43. */
  44. public function drawRight($uuid, Request $request)
  45. {
  46. $this->validate($request, [
  47. 'photo' => 'required',
  48. 'uuid' => 'required',
  49. ]);
  50. $photo = TacitPaintModel::where('uuid', $uuid)->firstOrFail();
  51. TacitPaintModel::create([
  52. 'openid' => $request->header('openid', ''),
  53. 'photo' => $request->photo,
  54. 'uuid' => $request->uuid,
  55. 'parent_id' => $photo->id,
  56. ]);
  57. return response([
  58. 'code' => 200,
  59. 'message' => 'OK',
  60. ]);
  61. }
  62. /**
  63. * 查看照片
  64. * @param $uuid
  65. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  66. */
  67. public function getPhoto($uuid)
  68. {
  69. $photo = TacitPaintModel::where('uuid', $uuid)->firstOrFail();
  70. $photo->user;
  71. if ($photo->parent_id) {
  72. $parent = TacitPaintModel::find($photo->parent_id);
  73. $parent->user;
  74. $photo->parent = $parent;
  75. }
  76. return response([
  77. 'code' => 200,
  78. 'message' => 'OK',
  79. 'data' => $photo
  80. ]);
  81. }
  82. /*
  83. * 玩法二
  84. *
  85. *
  86. */
  87. /**
  88. * 创建首次问题
  89. * @param Request $request
  90. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  91. */
  92. public function createAnswers(Request $request)
  93. {
  94. $this->validate($request, [
  95. 'questions' => 'required',
  96. 'answers' => 'required',
  97. 'uuid' => 'required',
  98. ]);
  99. $question = TacitQuestionAnswerModel::create([
  100. 'openid' => $request->header('openid'),
  101. 'questions' => $request->questions,
  102. 'answers' => $request->answers,
  103. 'uuid' => $request->uuid,
  104. ]);
  105. return response([
  106. 'code' => 200,
  107. 'message' => 'OK',
  108. ]);
  109. }
  110. /**
  111. * 对面测试问题
  112. * @param int $question_id
  113. * @param Request $request
  114. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  115. */
  116. public function updateAnswers($uuid, Request $request)
  117. {
  118. $this->validate($request, [
  119. 'answers' => 'required',
  120. 'uuid' => 'required',
  121. ]);
  122. $answer = TacitQuestionAnswerModel::where('uuid', $uuid)->firstOrFail();
  123. TacitQuestionAnswerModel::create([
  124. 'openid' => $request->header('openid', ''),
  125. 'questions' => $answer->questions,
  126. 'answers' => $request->answers,
  127. 'parent_id' => $answer->id,
  128. 'uuid' => $request->uuid,
  129. ]);
  130. $count = $this->compare($answer->answers, $request->answers);
  131. $tacit_degree = is_int($count) ? round(100 * $count / count($answer->questions)) : rand(0, 100);
  132. return response([
  133. 'code' => 200,
  134. 'message' => 'OK',
  135. 'data' => [
  136. 'tacit_degree' => $tacit_degree
  137. ]
  138. ]);
  139. }
  140. /**
  141. * 获取随机一体
  142. * @param Request $request
  143. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  144. */
  145. public function getRandomQuestion(Request $request)
  146. {
  147. $question = TacitQuestionModel::when($request->get('filter'), function ($query) use ($request) {
  148. $filter = explode(',', $request->get('filter'));
  149. return $query->whereNotIn('id', $filter);
  150. })->inRandomOrder()->first();
  151. return response([
  152. 'code' => 200,
  153. 'message' => 'OK',
  154. 'data' => $question
  155. ]);
  156. }
  157. /**
  158. * 获取问题
  159. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  160. */
  161. public function getQuestion(Request $request)
  162. {
  163. $questions = TacitQuestionModel::select('id', 'question', 'options')
  164. ->when($request->filled('uuid'), function ($query) use ($request) {
  165. $answer = TacitQuestionAnswerModel::where('uuid', $request->uuid)->firstOrFail();
  166. return $query->whereIn('id', $answer->questions);
  167. })->inRandomOrder()
  168. ->limit(10)
  169. ->get();
  170. $questions = $questions->sortBy(function ($question) {
  171. return $question->id;
  172. })->values()->all();
  173. return response([
  174. 'code' => 200,
  175. 'message' => 'OK',
  176. 'data' => $questions
  177. ]);
  178. }
  179. /**
  180. * 获取问题
  181. * @param int $question_id
  182. * @param Request $request
  183. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  184. */
  185. public function getAnswers($uuid, Request $request)
  186. {
  187. $answer = TacitQuestionAnswerModel::where('uuid', $uuid)->firstOrFail();
  188. $answer->user;
  189. if ($answer->parent_id) {
  190. $parent = TacitQuestionAnswerModel::find($answer->parent_id);
  191. $parent->user;
  192. $answer->parent = $parent;
  193. $count = $this->compare($answer->answers, $parent->answers);
  194. $answer->tacit_degree = is_int($count) ? round(100 * $count / count($answer->questions)) : rand(0, 100);
  195. }
  196. return response([
  197. 'code' => 200,
  198. 'message' => 'OK',
  199. 'data' => $answer
  200. ]);
  201. }
  202. /**
  203. * 对比匹配率
  204. * @param $array1
  205. * @param $array2
  206. * @return bool|int
  207. */
  208. private function compare($array1, $array2)
  209. {
  210. if (count($array1, 1) != count($array2, 1)) {
  211. return false;
  212. } elseif (count($array1) != count($array2)) {
  213. return false;
  214. } elseif (count($array1) != count($array1, 1)) {
  215. return false;
  216. }
  217. $count = 0;
  218. foreach ($array1 as $key => $value) {
  219. if ($value == $array2[$key]) {
  220. $count++;
  221. }
  222. }
  223. return $count;
  224. }
  225. /**
  226. * 玩法三
  227. *
  228. *
  229. */
  230. /**
  231. * 前者创建定位
  232. * @param Request $request
  233. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  234. */
  235. public function createLocation(Request $request)
  236. {
  237. $this->validate($request, [
  238. 'uuid' => 'required',
  239. 'lng' => 'required',
  240. 'lat' => 'required',
  241. ]);
  242. $location = GameLocationModel::create([
  243. 'openid' => $request->header('openid'),
  244. 'uuid' => $request->uuid,
  245. 'lng' => $request->lng,
  246. 'lat' => $request->lat,
  247. ]);
  248. return response([
  249. 'code' => 200,
  250. 'message' => 'OK',
  251. 'data' => $location
  252. ]);
  253. }
  254. /**
  255. * 后者定位
  256. * @param Request $request
  257. * @param $uuid
  258. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  259. */
  260. public function updateLocation(Request $request, $uuid)
  261. {
  262. $this->validate($request, [
  263. 'uuid' => 'required',
  264. 'lng' => 'required',
  265. 'lat' => 'required',
  266. ]);
  267. $parent = GameLocationModel::where('uuid', $uuid)->firstOrFail();
  268. $location = GameLocationModel::create([
  269. 'openid' => $request->header('openid'),
  270. 'uuid' => $request->uuid,
  271. 'lng' => $request->lng,
  272. 'lat' => $request->lat,
  273. 'parent_id' => $parent->id
  274. ]);
  275. return response([
  276. 'code' => 200,
  277. 'message' => 'OK',
  278. 'data' => $location
  279. ]);
  280. }
  281. /**
  282. * 获取位置信息
  283. * @param $uuid
  284. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  285. */
  286. public function getLocation($uuid)
  287. {
  288. $location = GameLocationModel::where('uuid', $uuid)->firstOrFail();
  289. $location->user;
  290. if ($location->parent_id) {
  291. $parent = GameLocationModel::find($location->parent_id);
  292. $parent->user;
  293. $location->parent = $parent;
  294. $geo = new Geohash();
  295. $location->distance = sprintf(
  296. '%.1f',
  297. $geo->getDistance($location->lat, $location->lng, $parent->lat, $parent->lng) / 1000
  298. );
  299. }
  300. return response([
  301. 'code' => 200,
  302. 'message' => 'OK',
  303. 'data' => $location
  304. ]);
  305. }
  306. }