game_test_page.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'dart:math';
  5. import 'dart:ui';
  6. import 'package:dio/dio.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter/services.dart';
  9. import 'package:get_it/get_it.dart';
  10. import 'package:path_provider/path_provider.dart';
  11. import 'package:provider/provider.dart';
  12. import 'package:sport/application.dart';
  13. import 'package:sport/bean/game.dart';
  14. import 'package:sport/game/sdk_parse.dart';
  15. import 'package:sport/pages/web/statistics.dart';
  16. import 'package:sport/provider/bluetooth.dart';
  17. import 'package:sport/provider/user_model.dart';
  18. import 'package:sport/services/Converter.dart';
  19. import 'package:sport/services/api/inject_api.dart';
  20. import 'package:sport/services/app_subscription_state.dart';
  21. import 'package:sport/utils/toast.dart';
  22. import 'package:wakelock/wakelock.dart';
  23. import 'package:web_socket_channel/io.dart';
  24. import 'package:webview_flutter/webview_flutter.dart';
  25. class GameTestPage extends StatefulWidget {
  26. const GameTestPage({Key? key}) : super(key: key);
  27. @override
  28. State<StatefulWidget> createState() => _PageState();
  29. }
  30. class _PageState extends State<GameTestPage> with SubscriptionState, InjectApi, TickerProviderStateMixin {
  31. late Bluetooth bluetooth;
  32. Timer? _timer;
  33. Timer? _timerGameMode;
  34. bool _onPageFinished = false;
  35. int _onPageProgress = 0;
  36. int _cmd = 0;
  37. int _timeSecond = 0;
  38. bool _gamePlaying = false;
  39. bool _openGameData = false;
  40. int _stepFreq = 0, _stepCount = 0;
  41. Statistics? _statistics;
  42. late String playGroup;
  43. int sample = 30;
  44. List<List<int>> c = [];
  45. @override
  46. void initState() {
  47. super.initState();
  48. playGroup = "${DateTime.now().millisecondsSinceEpoch}";
  49. bluetooth = GetIt.I();
  50. bluetooth.gameInit(100);
  51. bluetooth.setupGameMode4h5(true);
  52. AnimationController _animationController = new AnimationController(duration: Duration(milliseconds: 200), vsync: this);
  53. _timer = Timer.periodic(Duration(milliseconds: 8), (timer) {
  54. _timeSecond++;
  55. // if (_onPageFinished != true) return;
  56. // if(_xy < -1400){
  57. // _type = Random().nextInt(3);
  58. // _xy = 0;
  59. // _xx = Random().nextInt(300) + 100;
  60. // }
  61. // _xy -= 3;
  62. // _jump = max(0, --_jump);
  63. // _down = max(0, --_down);
  64. // if(_down > 0){
  65. // int dd = 30 - _down;
  66. // _speed = _speed + 4;
  67. // }else {
  68. // _speed = max(0, _speed - 4);
  69. // }
  70. // print("xy $_xx $_xy");
  71. // double width = MediaQuery.of(context).size.longestSide;
  72. // double target= _dx -100;
  73. // target = max(-width/2, min(target, width/2));
  74. // _animationController.stop();
  75. // print("111111111111111111111111 222222222222222222222222222222222 ${target}" );
  76. // Tween<double>(begin: _dx, end: target)
  77. // .animate(_animationController).addListener(() {
  78. // setState(() {
  79. // print("111111111111111111111111 ${_animationController.value}" );
  80. // _dx += _animationController.value;
  81. // });
  82. // });
  83. // _animationController.forward(from: 0);
  84. double px = 5.0;
  85. if(_xx > _dx){
  86. _dx += px;
  87. }else if(_xx < _dx){
  88. _dx -= px;
  89. }
  90. double range = screen/2;
  91. _dx = max(-range, min(_dx, range));
  92. if(_xx < -range){
  93. _xx = -range.toInt();
  94. }
  95. if(_xx > range){
  96. _xx = range.toInt();
  97. }
  98. setState(() {
  99. });
  100. });
  101. _timerGameMode = Timer.periodic(Duration(seconds: 60), (timer) {
  102. if (_onPageFinished == true) bluetooth.setupGameMode4h5(true);
  103. _onPageFinished = true;
  104. });
  105. addSubscription(bluetooth.sdkMotionStream.listen((event) {
  106. if (_onPageFinished != true) return;
  107. if (_gamePlaying == true) {
  108. // String gameData = sdk.getGameDataStr();
  109. // if(gameData.length < 10)return;
  110. // List<int> data = gameData.split(",").map((e) => int.parse(e)).toList();
  111. // if (c.isNotEmpty == true) {
  112. // int length = c.length;
  113. // List<int> mag = [0, 0, 0, 0];
  114. // int down = 0;
  115. // List<int> magTotal = List.filled(mag.length, 0);
  116. // List<int> magLast = List.filled(mag.length, 0);
  117. // for (var i = 0; i < 2; i++) {
  118. // magTotal[i * 2] = c[0][i * 12 + 9];
  119. // magTotal[i * 2 + 1] = c[0][i * 12 + 9 + 1];
  120. // for (var j = 1; j < length; j++) {
  121. // int d1 = c[j][i * 12 + 9] - c[j-1][i* 12 + 9];
  122. // int d2 = c[j][i * 12 + 9 + 1] - c[j-1][i * 12 + 9 + 1];
  123. // mag[i * 2] += d1.abs() > 100 ? 1 : -1;
  124. // mag[i * 2 + 1] += d2.abs() > 100 ? 1 : -1;
  125. //
  126. // magLast[i * 2] = c[j][i * 12 + 9];
  127. // magLast[i * 2 + 1] = c[j][i * 12 + 9 + 1];
  128. // }
  129. // }
  130. // List<bool> magflag = List.filled(mag.length, false);
  131. // List<int> ffff = [15500,11800, 18000, 13000];
  132. // List<int> fffff = [15500,19000, 21000, 22000];
  133. // for (var i = 0; i < mag.length; i++) {
  134. // // int p = mag[i];
  135. // // line[i] = magTotal[i] / length;
  136. // // if(p >= 15){
  137. // // magflag[i] = true;
  138. // // }else if(magflag1[i]){
  139. // // if(magLast[i] > 14000){
  140. // // magflag[i] = true;
  141. // // }
  142. // // }
  143. // if(magLast[i] > ffff[i]){
  144. // magflag[i] = true;
  145. // }
  146. // // if(magLast[i] > fffff[i]){
  147. // // down ++;
  148. // // }
  149. // }
  150. // print("test data $mag $magflag $line $magLast");
  151. //
  152. // List<int> result = sdk.getMotion();
  153. // print("sdk -- motion $result");
  154. // if (result.any((element) => element == 3)) {
  155. // down = 5;
  156. // }
  157. //
  158. // setState(() {
  159. // _rf = magflag[0];
  160. // _rb = magflag[1];
  161. // _lf = magflag[2];
  162. // _lb = magflag[3];
  163. //
  164. // double olddx = _dx;
  165. // // if(_lb && _rb){
  166. // // _dx -= 2;
  167. // // }
  168. // // if(_lf && _rf){
  169. // // _dx += 2;
  170. // // }
  171. //
  172. //
  173. // // if(data[11] == 0 && data[11 + 12] == 0){
  174. // // _jump = 30;
  175. // // }
  176. // int d = magflag.map((e) => e ? 1 : 0).reduce((value, element) => value + element);
  177. // print("test d _down $down");
  178. // if(down > 1){
  179. // _down = 30;
  180. // // _speed += 150;
  181. // }else{
  182. // if(_lb){
  183. // _dx -= 2;
  184. // }
  185. // if(_lf){
  186. // _dx -= 2;
  187. // }
  188. // if(_rb){
  189. // _dx += 2;
  190. // }
  191. // if(_rf){
  192. // _dx += 2;
  193. // }
  194. //
  195. // if(_dx < -100){
  196. // _dx = -100;
  197. // }
  198. // if(_dx > 100){
  199. // _dx = 100;
  200. // }
  201. //
  202. // _ori = olddx - _dx;
  203. // }
  204. // if(_speed > 500){
  205. // _speed = 500;
  206. // }
  207. // // if(_lf && _lb && _rb){
  208. // // _down = 30;
  209. // // }
  210. // // if(_lf && _lb && _rf){
  211. // // _speed += 4;
  212. // // }
  213. // // int d = magflag.map((e) => e ? 1 : 0).reduce((value, element) => value + element);
  214. // // print("test d _down $d");
  215. // // if(magflag.map((e) => e ? 1 : 0).reduce((value, element) => value + element) >=3){
  216. // // _down = 30;
  217. // // }
  218. //
  219. // });
  220. // }
  221. // c.add(data);
  222. // if (c.length > sample) {
  223. // c.removeAt(0);
  224. // }
  225. List<int> result = event;
  226. // print("11111111111111111 $result");
  227. // setState(() {
  228. // double width = MediaQuery.of(context).size.longestSide;
  229. // if(data[23] == 1){
  230. // _xttttb = data[12].toDouble();
  231. // _xttttyb = left;
  232. // }else{
  233. // left = _xttttyb -(_xttttb - data[12].toDouble());
  234. // _dx = max(-width/2, min((left + right) / 2, width/2));
  235. // print("left = $_xtttt ${data[0].toDouble()} $_dx ${data[11]}");
  236. // }
  237. // if(data[11] == 1){
  238. // _xtttt = data[0].toDouble();
  239. // _xtttty = right;
  240. // }else{
  241. // right = _xtttty -(_xtttt - data[0].toDouble());
  242. // _dx = max(-width/2, min((left + right) / 2, width/2));
  243. // print("right = $_xtttt ${data[0].toDouble()} $_dx ${data[11]}");
  244. // }
  245. double offset = 100;
  246. // result[0] = 200;
  247. if(result[0] > 0){
  248. _xx -= result[0] ;
  249. }
  250. if(result[1] > 0){
  251. _xx +=result[1] ;
  252. }
  253. // if(result[2] > 0){
  254. // _dy -=offset;
  255. // }if(result[3] > 0){
  256. // _dy +=offset;
  257. // }
  258. // });
  259. }
  260. }));
  261. WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
  262. _onGameStart();
  263. _onPageFinished = true;
  264. screen = MediaQuery.of(context).size.longestSide;
  265. print("1111111111111111111111 screen $screen");
  266. });
  267. SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
  268. }
  269. double screen =0;
  270. double right = 0, left = 0;
  271. @override
  272. void dispose() {
  273. SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  274. _timer?.cancel();
  275. _timerGameMode?.cancel();
  276. bluetooth.setupGameMode4h5(false);
  277. super.dispose();
  278. }
  279. _initGame() {
  280. bluetooth.setupGameMode4h5(true);
  281. bluetooth.gameInit(100);
  282. }
  283. _onGameStart() {
  284. _gamePlaying = true;
  285. _initGame();
  286. }
  287. _onGameEnd({int level = 0, double score = 0, int record = 0, int mode = 0, int opponentId = 0, int isCancel = 0}) {
  288. _gamePlaying = false;
  289. }
  290. bool _lf = false, _lb = false;
  291. bool _rf = false, _rb = false;
  292. int _down = 0;
  293. int _jump = 0;
  294. double _dx = 0;
  295. double _dy = 0;
  296. int _xx = 0;
  297. int _xy = 0;
  298. int _type = 0;
  299. double _ori = 0;
  300. int _speed = 0;
  301. List<double> line = [0, 0, 0, 0];
  302. double _xtttt = 0;
  303. double _xtttty = 0;
  304. double _xttttb = 0;
  305. double _xttttyb = 0;
  306. @override
  307. Widget build(BuildContext context) {
  308. return Scaffold(
  309. body: CustomPaint(
  310. painter: _GamePainter(lf:_lf, lb:_lb, rf:_rf, rb:_rb, dx: _dx, xx:_xx, xy: _dy.toInt(), down: _down,jump: _jump, type: _type, ori: _ori,speed: _speed),
  311. child: Container(
  312. width: double.infinity,
  313. height: double.infinity,
  314. ),
  315. ),
  316. );
  317. }
  318. }
  319. class _GamePainter extends CustomPainter {
  320. bool lf = false, lb = false;
  321. bool rf = false, rb = false;
  322. double dx = 0;
  323. int xx = 0;
  324. int xy = 0;
  325. int down = 0;
  326. int jump = 0;
  327. int type = 0;
  328. double a = 0;
  329. int speed = 0;
  330. double ori = 0;
  331. _GamePainter({this.lf = false, this.lb = false, this.rf = false, this.rb = false, this.dx = 0,this.xx = 0, this.xy = 0, this.down = 0, this.jump = 0, this.type = 0, this.ori = 0, this.speed = 0});
  332. final Paint _paintSkate = Paint()
  333. ..color = Colors.blue
  334. ..strokeWidth = 0.5
  335. ..isAntiAlias = true;
  336. final Paint _paintSkateDown = Paint()
  337. ..color = Colors.yellow
  338. ..strokeWidth = 0.5
  339. ..isAntiAlias = true;
  340. final Paint _paintSkateJump = Paint()
  341. ..color = Colors.purple
  342. ..strokeWidth = 0.5
  343. ..isAntiAlias = true;
  344. final Paint _paintPoint = Paint()
  345. ..color = Colors.black
  346. ..isAntiAlias = true;
  347. final Paint _paintPointPressed = Paint()
  348. ..color = Colors.red
  349. ..isAntiAlias = true;
  350. final Paint _paintLine = Paint()
  351. ..color = Colors.black
  352. ..isAntiAlias = true;
  353. final Paint _paintLineOver = Paint()
  354. ..color = Colors.yellow
  355. ..isAntiAlias = true;
  356. @override
  357. void paint(Canvas canvas, Size size) {
  358. final bg = Rect.fromLTWH(0, 0, size.width, size.height);
  359. final center = bg.center;
  360. print("bg $bg, center: $center");
  361. canvas.save();
  362. var c = center.translate(dx, xy.toDouble() + size.height / 2);
  363. canvas.translate(c.dx, c.dy);
  364. //
  365. // if(ori > 0){
  366. // canvas.rotate(-0.1);
  367. // }else if(ori < 0){
  368. // canvas.rotate(0.1);
  369. // }
  370. // var skate = Rect.fromCenter(center: Offset(0,0 - this.speed.toDouble()), width: 100, height: 250);
  371. //
  372. // canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkate);
  373. // if(jump > 0){
  374. // canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkateJump);
  375. // }else if(down > 0){
  376. // canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkateDown);
  377. // }
  378. // canvas.drawCircle(skate.center.translate(-20, -20), 15, lb ? _paintPointPressed : _paintPoint);
  379. // canvas.drawCircle(skate.center.translate(20, -40), 15, lf ? _paintPointPressed : _paintPoint);
  380. //
  381. // canvas.drawCircle(skate.center.translate(-20, 60), 15, rb ? _paintPointPressed : _paintPoint);
  382. // canvas.drawCircle(skate.center.translate(20, 40), 15, rf ? _paintPointPressed : _paintPoint);
  383. //
  384. // canvas.translate(-c.dx, -c.dy);
  385. var x1 = Offset(0,0 - this.speed.toDouble());
  386. // var skate = Rect.fromCenter(center: Offset(0,0 - this.speed.toDouble()), width: 50, height: 150);
  387. for(var i = 0; i < 2; i++){
  388. var skate = Rect.fromCenter(center: Offset(x1.dx + (i == 0?-30.0:30.0),x1.dy), width: 40, height: 200);
  389. canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkate);
  390. if(jump > 0){
  391. canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkateJump);
  392. }else if(down > 0){
  393. canvas.drawRRect(RRect.fromRectAndRadius(skate, Radius.circular(50)), _paintSkateDown);
  394. }
  395. canvas.drawCircle(skate.center.translate(0, 30), 15, (i == 0 ? lb : rb) ? _paintPointPressed : _paintPoint);
  396. canvas.drawCircle(skate.center.translate(0, -30), 15, (i == 0 ? lf : rf) ? _paintPointPressed : _paintPoint);
  397. }
  398. // canvas.drawCircle(skate.center.translate(-20, -20), 15, lb ? _paintPointPressed : _paintPoint);
  399. // canvas.drawCircle(skate.center.translate(20, -40), 15, lf ? _paintPointPressed : _paintPoint);
  400. //
  401. // canvas.drawCircle(skate.center.translate(-20, 60), 15, rb ? _paintPointPressed : _paintPoint);
  402. // canvas.drawCircle(skate.center.translate(20, 40), 15, rf ? _paintPointPressed : _paintPoint);
  403. canvas.translate(-c.dx, -c.dy);
  404. // canvas.translate(size.width / 2, size.height / 2);
  405. canvas.restore();
  406. // if(type == 0) {
  407. // var line = Rect.fromLTWH(xx.toDouble(), -xy.toDouble() - 400, 10, 500);
  408. // canvas.drawRect(line, _paintLine);
  409. // }else if(type == 1) {
  410. // var line = Rect.fromLTWH(0, -xy.toDouble() - 100, size.width, 200);
  411. // canvas.drawRect(line, _paintSkateJump);
  412. // }else if(type == 2) {
  413. // var line = Rect.fromLTWH(0, -xy.toDouble() - 200, size.width, 400);
  414. // canvas.drawRect(line, _paintSkateDown);
  415. // }
  416. //
  417. // var line = Rect.fromLTWH(xx.toDouble(), -xy.toDouble() - 400, 10, 500);
  418. // canvas.drawRect(line, _paintLine);
  419. final text = TextPainter(
  420. text: TextSpan(
  421. style: TextStyle(fontSize: 12.0, color: Colors.red),
  422. text: "${dx}",
  423. ),
  424. textAlign: TextAlign.left,
  425. textDirection: TextDirection.ltr,
  426. )
  427. ..layout(minWidth: 100);
  428. text.paint(canvas, Offset(50, 50));
  429. }
  430. @override
  431. bool shouldRepaint(covariant CustomPainter oldDelegate) {
  432. return true;
  433. }
  434. }