index.js 6.8 KB
Newer Older
ItbGcthate's avatar
ItbGcthate 已提交
1
import React, {Component} from 'react';
ItbGcthate's avatar
ItbGcthate 已提交
2 3 4 5 6 7 8 9
import {
  View,
  Text,
  StyleSheet,
  Dimensions,
  Alert,
  PanResponder,
} from 'react-native';
ItbGcthate's avatar
ItbGcthate 已提交
10 11 12 13 14
import MyStatistics from '../MyStatistics';
import MyTitle from '../MyTitle';
import SettingButton from '../SettingButton';
import MyBanner from '../MyBanner';
import RangePKState from '../RangePKState';
ItbGcthate's avatar
ItbGcthate 已提交
15 16
import {parseMinute, parseRangeID, parseTime} from '../MyUtilities';
import '../GlobalACID';
ItbGcthate's avatar
ItbGcthate 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

const {width, height} = Dimensions.get('window');
/**
 * 标语风格切换共使用了组件:
 * 1. GlobalSetting(引入global变量global.bannerStyle,来控制标语风格)
 * 2. SettingButton
 * 3. BannerStyle(在你的Navigator中引入)
 * 4. MyButton(在BannerStyle组件中引入)
 * 5. MyBanner
 *
 * 此外,还使用了:
 * 1. componentDidMount()
 * 2. componentWillUnmount()
 */
class RangePKStudy extends Component {
  constructor() {
    super();
  }

  state = {
    rangeID: undefined,
ItbGcthate's avatar
ItbGcthate 已提交
38 39 40 41 42
    user: {
      name: 'username',
      rank: 'error',
      duration: -1,
    },
ItbGcthate's avatar
ItbGcthate 已提交
43
    ranking: 1,
ItbGcthate's avatar
ItbGcthate 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56
    timeInTheMatch: 0,
    rankingFirst: {
      name: 'username',
      time: -1,
    },
    rankingSecond: {
      name: 'username',
      time: -1,
    },
    rankingThird: {
      name: 'username',
      time: -1,
    },
ItbGcthate's avatar
ItbGcthate 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  };

  componentDidMount() {
    const {
      navigation,
      route: {params},
    } = this.props;
    navigation.setOptions({
      // 引入SettingButton组件至header的右边
      // 并把this.props传给SettingButton组件以实现Navigation的跳转
      headerRight: () => <SettingButton {...this.props} />,
    });
    this.unsubscribe = navigation.addListener('focus', () => {
      // Screen was focused
      // 强制刷新组件,读取最新的global.bannerStyle
      this.forceUpdate();
    });
    // 以下不是标语风格切换相关
ItbGcthate's avatar
ItbGcthate 已提交
75
    if (params.rangeID !== undefined) {
ItbGcthate's avatar
ItbGcthate 已提交
76 77 78 79 80 81 82 83 84 85 86 87
      this.setState({rangeID: params.rangeID});
    }
    this.startTimer();
  }

  componentWillUnmount() {
    this.unsubscribe();
    // 以下不是标语风格切换相关
    clearInterval(this.timer);
  }

  calculateTime = () => {
ItbGcthate's avatar
ItbGcthate 已提交
88
    const {timeInAGame, timeInTheMatch} = this.state;
ItbGcthate's avatar
ItbGcthate 已提交
89 90
    this.setState({
      timeInAGame: timeInAGame + 1,
ItbGcthate's avatar
ItbGcthate 已提交
91
      timeInTheMatch: timeInTheMatch + 1,
ItbGcthate's avatar
ItbGcthate 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104
    });
  };

  startTimer = () => {
    this.timer = setInterval(() => {
      this.calculateTime();
    }, 1000);
  };

  openRankingList = () => {
    this.props.navigation.navigate('区域排行榜');
  };

ItbGcthate's avatar
ItbGcthate 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  panResponder = PanResponder.create({
    onStartShouldSetPanResponder: () => true,

    onMoveShouldSetPanResponder: (event, gestureState) => {
      let {dx} = gestureState;
      return Math.abs(dx) > 10;
    },

    onPanResponderMove: (event, gestureState) => {
      // console.log('onPanResponderMove');
      // console.log(
      //   '滑动参数:dx=' + gestureState.dx + ',dy=' + gestureState.dy,
      // );
      // console.log(
      //   '滑动参数:vx=' + gestureState.vx + ',vy=' + gestureState.vy,
      // );
      if (gestureState.vx < -0.6 && Math.abs(gestureState.dy) < 10) {
        this.openRankingList();
      }
    },
  });

ItbGcthate's avatar
ItbGcthate 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139
  finish = () => {
    Alert.alert('结算确认', '您确定要结束PK并结算成绩吗?', [
      {
        text: '取消',
        onPress: () => console.log('Cancel Pressed'),
        style: 'cancel',
      },
      {
        text: '是的',
        onPress: () => {
          //console.log('OK Pressed');
          clearInterval(this.timer);
          this.props.navigation.navigate('区域PK结算', {
ItbGcthate's avatar
ItbGcthate 已提交
140
            numberTop: parseInt(this.state.timeInTheMatch / 60, 10),
ItbGcthate's avatar
ItbGcthate 已提交
141 142
            numberCentre: 999,
            numberBottom: 9999,
ItbGcthate's avatar
ItbGcthate 已提交
143
            range: parseRangeID(this.state.rangeID),
ItbGcthate's avatar
ItbGcthate 已提交
144 145 146 147 148 149 150
          });
        },
      },
    ]);
  };

  render() {
ItbGcthate's avatar
ItbGcthate 已提交
151 152 153 154 155 156 157 158
    const {
      user,
      ranking,
      timeInTheMatch,
      rankingFirst,
      rankingSecond,
      rankingThird,
    } = this.state;
ItbGcthate's avatar
ItbGcthate 已提交
159 160

    return (
ItbGcthate's avatar
ItbGcthate 已提交
161
      <View style={styles.container} {...this.panResponder.panHandlers}>
ItbGcthate's avatar
ItbGcthate 已提交
162 163 164 165 166 167 168 169 170
        <MyTitle
          title={'当前的排名为 第' + ranking + ''}
          containerStyle={styles.containerTitle}
        />
        <View style={styles.containerMain}>
          <MyStatistics
            text={{
              first: {
                top: '用户',
ItbGcthate's avatar
ItbGcthate 已提交
171
                bottom: user.name,
ItbGcthate's avatar
ItbGcthate 已提交
172 173 174
              },
              second: {
                top: '段位',
ItbGcthate's avatar
ItbGcthate 已提交
175
                bottom: user.rank,
ItbGcthate's avatar
ItbGcthate 已提交
176 177 178
              },
              third: {
                top: '本次时长',
ItbGcthate's avatar
ItbGcthate 已提交
179
                bottom: parseMinute(timeInTheMatch) + ' min',
ItbGcthate's avatar
ItbGcthate 已提交
180 181 182
              },
              forth: {
                top: '生涯时长',
ItbGcthate's avatar
ItbGcthate 已提交
183
                bottom: parseMinute(timeInTheMatch) + user.duration + ' min',
ItbGcthate's avatar
ItbGcthate 已提交
184 185 186
              },
            }}
          />
ItbGcthate's avatar
ItbGcthate 已提交
187
          <RangePKState range={parseRangeID(this.state.rangeID)} />
ItbGcthate's avatar
ItbGcthate 已提交
188 189 190 191 192 193 194
          <MyStatistics
            containerStyle={styles.containerOnRight}
            insideStyle={styles.containerInRightColumn}
            textStyle={styles.textOnRight}
            onPress={this.openRankingList}
            text={{
              first: {
ItbGcthate's avatar
ItbGcthate 已提交
195
                top: parseRangeID(this.state.rangeID) + '区域',
ItbGcthate's avatar
ItbGcthate 已提交
196
                bottom: 'Top 3',
ItbGcthate's avatar
ItbGcthate 已提交
197 198
              },
              second: {
ItbGcthate's avatar
ItbGcthate 已提交
199 200
                top: rankingFirst.name,
                bottom: rankingFirst.time + ' min',
ItbGcthate's avatar
ItbGcthate 已提交
201 202
              },
              third: {
ItbGcthate's avatar
ItbGcthate 已提交
203 204
                top: rankingSecond.name,
                bottom: rankingSecond.time + ' min',
ItbGcthate's avatar
ItbGcthate 已提交
205 206
              },
              forth: {
ItbGcthate's avatar
ItbGcthate 已提交
207 208
                top: rankingThird.name,
                bottom: rankingThird.time + ' min',
ItbGcthate's avatar
ItbGcthate 已提交
209 210 211 212 213
              },
            }}
          />
        </View>
        <View style={styles.containerTimer}>
ItbGcthate's avatar
ItbGcthate 已提交
214
          <Text style={styles.text}>{'<--'}左滑打开排行榜</Text>
ItbGcthate's avatar
ItbGcthate 已提交
215
          <Text style={styles.textTimer}>{parseTime(timeInTheMatch)}</Text>
ItbGcthate's avatar
ItbGcthate 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        </View>
        <MyBanner
          containerStyle={styles.myBanner.container}
          title={'结算'}
          onPress={this.finish}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#F2E2CE',
    //opacity: 0.5,
    flex: 1,
  },
  containerTitle: {
    flex: 1,
  },
  containerMain: {
    flex: 3,
    flexDirection: 'row',
  },
  containerTimer: {
    justifyContent: 'center',
    flex: 1,
  },
  containerOnRight: {
    borderColor: '#f4cb8c',
    backgroundColor: '#BF7636',
  },
  containerInRightColumn: {
    borderColor: '#f4cb8c',
    backgroundColor: '#BF7636',
  },
  textOnRight: {
    color: '#f2e2ce',
  },
ItbGcthate's avatar
ItbGcthate 已提交
255 256 257 258 259 260 261
  text: {
    textAlignVertical: 'center',
    color: '#592202',
    fontSize: parseInt(height, 10) / 50,
    fontWeight: '300',
    textAlign: 'center',
  },
ItbGcthate's avatar
ItbGcthate 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  textTimer: {
    textAlignVertical: 'center',
    flex: 1,
    color: '#592202',
    fontSize: parseInt(height, 10) / 12,
    fontWeight: 'bold',
    textAlign: 'center',
  },
  myBanner: {
    container: {
      flex: 2,
    },
  },
});

export default RangePKStudy;