| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:flutter_redux/flutter_redux.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import 'package:url_launcher/url_launcher.dart';
- import 'package:battery/battery.dart';
- import 'package:wanna_battle/model/PlayerInfo.dart';
- import 'package:wanna_battle/pages/RoomInfoDetail.dart';
- import 'package:wanna_battle/redux/AppState.dart';
- import 'package:wanna_battle/styles/totast.dart';
- import '../model/HouseInfo.dart';
- import '../Constants.dart';
- import '../styles/colors.dart';
- import '../net/HttpManager.dart';
- import '../net/Result.dart';
- import '../plugins/ScreenStramPlugin.dart';
- import './StartWindow.dart';
- import '../widget/Dialog.dart';
- import 'CompetitionNotice.dart';
- import 'SecondRoomInfo.dart';
- class RoomInfo extends StatefulWidget {
- final HouseInfo houseInfo;
- final PlayerInfo playerInfo;
- final bool interrupted;
- RoomInfo(this.houseInfo, {Key key, this.playerInfo, this.interrupted = false}) : super(key: key);
- @override
- State<StatefulWidget> createState() {
- return _RoomInfoState();
- }
- }
- class _RoomInfoState extends State<RoomInfo> with SingleTickerProviderStateMixin {
- HouseInfo mHouseInfo;
- PlayerInfo mPlayerInfo;
- TabController tabController;
- RoomInfoDetail roomInfoDetail;
- int finishedPlayerNum = 0;
- Timer timer;
- final Key key1 = GlobalKey<RoomInfoDetailState>();
- final Key key2 = GlobalKey<SecondPageState>();
- @override
- void initState() {
- super.initState();
- mHouseInfo = widget.houseInfo;
- mPlayerInfo = widget.playerInfo;
- tabController = TabController(
- length: 2,
- vsync: this,
- );
- Future.delayed(Duration.zero, () async {
- final prefs = await SharedPreferences.getInstance();
- bool showed = prefs.getBool('house_' + mHouseInfo.id.toString()) ?? false;
- if (!showed) {
- prefs.setBool('house_' + mHouseInfo.id.toString(), true);
- showNotice(context);
- }
- refreshData();
- });
- if (mHouseInfo.statusFlag == HouseStatus.WAIT || mHouseInfo.statusFlag == HouseStatus.START) {
- timer = Timer.periodic(Duration(seconds: 1), (timer) async {
- refreshData();
- });
- Future.delayed(Duration.zero, () async {
- if (mHouseInfo.statusFlag == HouseStatus.START) {
- showFinishDialog();
- } else {
- final battery = Battery();
- final batteryLevel = await battery.batteryLevel;
- showCustomDialog(context, (batteryLevel < 50 ? '系统检测到你的电量低于50%' : '') + ' 为了正常上传比赛结果,建议插上电源线保持电量充足', submitText: '知道了');
- }
- });
- }
- }
- @override
- void dispose() {
- super.dispose();
- tabController.dispose();
- if (timer != null) {
- timer.cancel();
- }
- }
- @override
- Widget build(BuildContext context) {
- return WillPopScope(
- child: Scaffold(
- appBar: AppBar(
- title: Container(
- child: TabBar(
- controller: tabController,
- labelColor: PRIMARY_COLOR,
- unselectedLabelColor: Color(0xCCFFFFFF),
- labelStyle: TextStyle(fontSize: 16.0),
- indicatorColor: PRIMARY_COLOR,
- indicatorWeight: 3,
- indicatorSize: TabBarIndicatorSize.label,
- tabs: <Widget>[
- Tab(text: '房间信息'),
- Tab(text: '参赛成员'),
- ],
- ),
- ),
- centerTitle: true,
- elevation: 0,
- actions: <Widget>[
- Container(
- padding: EdgeInsets.only(right: 30, left: 20),
- )
- ],
- ),
- body: Container(
- child: TabBarView(
- controller: tabController,
- children: [
- Builder(
- builder: (context) {
- roomInfoDetail = RoomInfoDetail(
- mHouseInfo,
- mPlayerInfo,
- onStart: () {
- showStart();
- },
- key: key1,
- finishedPlayerNum: finishedPlayerNum,
- );
- return roomInfoDetail;
- },
- ),
- SecondPage(mHouseInfo, key: key2, playerInfo: mPlayerInfo),
- ],
- ),
- ),
- ),
- onWillPop: () {
- if ((mHouseInfo?.statusFlag ?? 0) == 0) {
- showCustomDialog(context, '比赛即将开始,确定离开房间?', isCancel: true, onsubmit: () async {
- Toast.show(context, '退出房间', -1, 'loading');
- await HttpManager.get('houseInfo/quitRoom', data: {'houseId': mHouseInfo.id});
- Toast.hide();
- Navigator.of(context).pop();
- });
- return Future.value(false);
- }
- return Future.value(true);
- },
- );
- }
- Future<void> refreshData() async {
- setState(() {});
- HttpManager.get('houseInfo/getOne', data: {'id': mHouseInfo.id}).then((res) {
- if (res.success) {
- if (mHouseInfo.statusFlag == 0 && res.data['statusFlag'] == 2) {
- showStart();
- }
- setState(() {
- mHouseInfo = HouseInfo.fromJson(res.data);
- });
- }
- });
- HttpManager.get('playerInfo/getOne', data: {
- 'userId': StoreProvider.of<AppState>(context).state.userInfo.id,
- 'houseId': mHouseInfo.id,
- }).then((res) {
- if (res.success) {
- setState(() {
- mPlayerInfo = PlayerInfo.fromJson(res.data);
- });
- }
- });
- HttpManager.get('playerInfo/endNum', data: {'houseId': mHouseInfo.id}).then((res) {
- if (res.success) {
- setState(() {
- finishedPlayerNum = res.data;
- });
- }
- });
- }
- Future<void> showStart() async {
- if (mPlayerInfo == null) {
- return;
- }
- final result = await Navigator.of(context).push(
- PageRouteBuilder(
- opaque: false,
- transitionDuration: Duration(milliseconds: 300),
- transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
- return FadeTransition(
- opacity: CurvedAnimation(parent: animation, curve: Curves.linear),
- child: child,
- );
- },
- pageBuilder: (BuildContext context, _, __) {
- return StartWindow();
- },
- ),
- );
- final Map<String, dynamic> data = {'id': mPlayerInfo.id};
- bool success = true;
- if (result) {
- success = await ScreenStreamPlugin.start(mPlayerInfo.id.toString());
- if (success) {
- data['statusFlag'] = 2;
- data['played'] = true;
- data['beginTime'] = DateTime.now().millisecondsSinceEpoch;
- } else {
- data['statusFlag'] = 6;
- data['played'] = false;
- }
- } else {
- data['statusFlag'] = 6;
- data['played'] = false;
- }
- final Result res = await HttpManager.post('playerInfo/update', data: data);
- if (res.success) {
- if (data['statusFlag'] == 2) {
- const url = 'pubgmhd1106467070://';
- if (await canLaunch(url)) {
- Timer(Duration(seconds: 1), () async {
- await launch(url);
- showFinishDialog();
- });
- } else {
- showCustomDialog(context, '自动启动游戏失败,请手动切换到游戏app开始竞赛', submitText: '知道了');
- Timer(Duration(seconds: 10), () {
- showFinishDialog();
- });
- }
- } else {
- showStartFailDialog(success ? 1 : 0);
- }
- }
- }
- void showFinishDialog() {
- if (widget.interrupted) {
- showCustomDialog(
- context,
- '您已经完成比赛了吗,确认完成,那就点击下方完成竞赛按钮,上传本次成绩,祝您赢取大奖',
- title: '完成比赛',
- submitText: '我已完成比赛',
- onsubmit: () async {
- await HttpManager.post('playerInfo/update', data: {
- 'id': mPlayerInfo.id,
- 'endTime': DateTime.now().millisecondsSinceEpoch,
- 'statusFlag': 9,
- });
- setState(() {
- mPlayerInfo.statusFlag = 3;
- });
- refreshData();
- },
- );
- } else {
- showCustomDialog(
- context,
- '您已经完成比赛了吗,确认完成,那就点击下方完成竞赛按钮,上传本次成绩,祝您赢取大奖',
- title: '完成比赛',
- submitText: '我已完成比赛',
- onsubmit: () async {
- await HttpManager.post('playerInfo/update', data: {'id': mPlayerInfo.id, 'endTime': DateTime.now().millisecondsSinceEpoch});
- await ScreenStreamPlugin.stop();
- setState(() {
- mPlayerInfo.statusFlag = 3;
- });
- refreshData();
- },
- );
- }
- }
- void showStartFailDialog(type) {
- showCustomDialog(context, (type == 1 ? '由于您未在十秒内点击开始按钮' : '由于您未授权录屏') + ',系统已经判定您放弃比赛,谢谢您的参与。', title: '很遗憾');
- }
- }
|