|
@@ -1,34 +1,52 @@
|
|
|
package com.jyc.threegames.activity;
|
|
|
|
|
|
import android.app.Activity;
|
|
|
+import android.app.Dialog;
|
|
|
+import android.app.ProgressDialog;
|
|
|
import android.content.Intent;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Handler;
|
|
|
import android.os.Looper;
|
|
|
import android.os.Message;
|
|
|
import android.text.Html;
|
|
|
+import android.view.Gravity;
|
|
|
import android.view.View;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
|
|
|
|
+import com.jyc.threegames.App;
|
|
|
import com.jyc.threegames.R;
|
|
|
import com.jyc.threegames.activity.base.BaseActivity;
|
|
|
import com.jyc.threegames.bean.GameInfo;
|
|
|
+import com.jyc.threegames.bean.result.ResGameInfo;
|
|
|
+import com.jyc.threegames.controller.GameController;
|
|
|
+import com.jyc.threegames.controller.LoginController;
|
|
|
+import com.jyc.threegames.net.SimpleRequest;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
+import butterknife.OnClick;
|
|
|
+import it.sephiroth.android.library.numberpicker.NumberPicker;
|
|
|
|
|
|
public class GameActivity extends BaseActivity {
|
|
|
private static final String PARAM_GAME_INFO = "gameInfo";
|
|
|
+ private static final String PARAM_RES_GAME_INFO = "resGameInfo";
|
|
|
|
|
|
- private static final int PRE_GAME_COUNT_DOWN = 5;
|
|
|
private int mCurrentPreGameCountDown = 0;
|
|
|
private Handler mPreGameHandler;
|
|
|
|
|
|
+ private Integer[] mPractiseNumbers;
|
|
|
+ private Handler mPractiseHandler;
|
|
|
+ private int mCurrentPractiseIndex;
|
|
|
+
|
|
|
private GameInfo mGameInfo;
|
|
|
+ private ResGameInfo mResGameInfo;
|
|
|
|
|
|
private int mSpecialNumberOne;
|
|
|
private int mSpecialNumberTwo;
|
|
@@ -45,27 +63,54 @@ public class GameActivity extends BaseActivity {
|
|
|
@BindView(R.id.click_area)
|
|
|
View mVClickArea;
|
|
|
|
|
|
+ @BindView(R.id.form)
|
|
|
+ ConstraintLayout mCLForm;
|
|
|
+
|
|
|
+ @BindView(R.id.right)
|
|
|
+ NumberPicker mNPRight;
|
|
|
+ @BindView(R.id.grasp)
|
|
|
+ NumberPicker mNPGrasp;
|
|
|
+
|
|
|
+ private boolean mClickable = false;
|
|
|
+ private long mClickTime = 0;
|
|
|
+ private long mCurrentTime = 0;
|
|
|
+
|
|
|
+ private List<Integer> mRecordList;
|
|
|
+ private List<Long> mResponseList;
|
|
|
+ private List<Integer> mPressRecordList;
|
|
|
+
|
|
|
+ private long mStartTime = System.currentTimeMillis();
|
|
|
+ private long mEndTime = 0;
|
|
|
+
|
|
|
@Override
|
|
|
protected void init(Bundle instance) {
|
|
|
super.init(instance);
|
|
|
- if (instance == null)
|
|
|
+ if (instance == null) {
|
|
|
mGameInfo = getIntent().getParcelableExtra(PARAM_GAME_INFO);
|
|
|
- else
|
|
|
+ mResGameInfo = getIntent().getParcelableExtra(PARAM_RES_GAME_INFO);
|
|
|
+ }
|
|
|
+ else {
|
|
|
mGameInfo = instance.getParcelable(PARAM_GAME_INFO);
|
|
|
+ mResGameInfo = getIntent().getParcelableExtra(PARAM_RES_GAME_INFO);
|
|
|
+ }
|
|
|
|
|
|
startPrePractise();
|
|
|
+
|
|
|
+ App.CAN_PLAY_GAME = false;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
|
|
super.onSaveInstanceState(outState);
|
|
|
outState.putParcelable(PARAM_GAME_INFO, mGameInfo);
|
|
|
+ outState.putParcelable(PARAM_RES_GAME_INFO, mResGameInfo);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
|
mGameInfo = savedInstanceState.getParcelable(PARAM_GAME_INFO);
|
|
|
+ mResGameInfo = savedInstanceState.getParcelable(PARAM_RES_GAME_INFO);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -74,8 +119,53 @@ public class GameActivity extends BaseActivity {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void onBackPressed() {
|
|
|
- super.onBackPressed();
|
|
|
+ protected void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ App.CAN_PLAY_GAME = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onBackPressed() {}
|
|
|
+
|
|
|
+ @OnClick(R.id.click_area)
|
|
|
+ public void clickNumber(){
|
|
|
+ if (mClickable && mClickTime == 0){
|
|
|
+ mClickTime = System.currentTimeMillis();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClick(R.id.sure)
|
|
|
+ public void clickSure(){
|
|
|
+ if (LoginController.getInstance().isCurrentUserAdmin()){
|
|
|
+ goFinish();
|
|
|
+ }else{
|
|
|
+ Dialog loading = new ProgressDialog(this);
|
|
|
+ loading.setTitle("正在提交數據");
|
|
|
+ loading.show();
|
|
|
+ new SimpleRequest<>()
|
|
|
+ .request(this, GameController.getInstance().addAnswer(mResGameInfo.playGameId, mPractiseNumbers, mRecordList, mResponseList, mPressRecordList, mSpecialNumberOne, mSpecialNumberTwo, mGameInfo.gameVersion, mStartTime, mEndTime, mNPRight.getProgress(), mNPGrasp.getProgress()), "提交數據失敗!請檢查網絡狀態", loading, new SimpleRequest.Executor<Object>() {
|
|
|
+ @Override
|
|
|
+ public void execute(Object obj) {
|
|
|
+ goFinish();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void goFinish(){
|
|
|
+ hideAllView();
|
|
|
+
|
|
|
+ mTVPreGameHint.setGravity(Gravity.CENTER);
|
|
|
+ mTVPreGameHint.setText("數據提交成功\n\n謝謝參與");
|
|
|
+ mTVPreGameHint.setVisibility(View.VISIBLE);
|
|
|
+
|
|
|
+ new Handler(Looper.getMainLooper(), new Handler.Callback() {
|
|
|
+ @Override
|
|
|
+ public boolean handleMessage(@NonNull Message message) {
|
|
|
+ finish();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }).sendEmptyMessageDelayed(0, 3000);
|
|
|
}
|
|
|
|
|
|
private void startPrePractise(){
|
|
@@ -84,25 +174,32 @@ public class GameActivity extends BaseActivity {
|
|
|
mSpecialNumberOne = getRandomNumber();
|
|
|
mSpecialNumberTwo = getRandomNumber(mSpecialNumberOne);
|
|
|
|
|
|
- mCurrentPreGameCountDown = PRE_GAME_COUNT_DOWN;
|
|
|
+ mCurrentPreGameCountDown = mGameInfo.getPractiseDurationSecond();
|
|
|
mPreGameHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
|
|
|
@Override
|
|
|
public boolean handleMessage(@NonNull Message message) {
|
|
|
if (mCurrentPreGameCountDown > 0){
|
|
|
mTVPreGameHint.setVisibility(View.VISIBLE);
|
|
|
+ mTVPreGameHint.setGravity(Gravity.START);
|
|
|
mTVPreGameHint.setText(Html.fromHtml(getPrePractiseText(mSpecialNumberOne, mSpecialNumberTwo, mCurrentPreGameCountDown), Html.FROM_HTML_MODE_LEGACY));
|
|
|
mCurrentPreGameCountDown --;
|
|
|
mPreGameHandler.sendEmptyMessageDelayed(0, 1000);
|
|
|
}else{
|
|
|
mTVPreGameHint.setVisibility(View.GONE);
|
|
|
- startPractise();
|
|
|
+ startGame(true);
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
mPreGameHandler.sendEmptyMessage(0);
|
|
|
}
|
|
|
|
|
|
+ private String getPreGameText(int countDown){
|
|
|
+ return String.format(Locale.TRADITIONAL_CHINESE,"練習結束,<br/><br/><font color='#cc0000'>%1$d</font>秒后進入正式測試",
|
|
|
+ countDown);
|
|
|
+ }
|
|
|
+
|
|
|
private String getPrePractiseText(int numberOne, int numberTwo, int countDown){
|
|
|
switch (mGameInfo.gameVersion){
|
|
|
case GameInfo.VERSION_GAME_ONE:
|
|
@@ -118,15 +215,178 @@ public class GameActivity extends BaseActivity {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void startPractise(){
|
|
|
+ private void startGame(boolean isPractise){
|
|
|
hideAllView();
|
|
|
|
|
|
- mVClickArea.setOnClickListener(new View.OnClickListener() {
|
|
|
+ mPractiseNumbers = getPractiseNumbers(isPractise ? mGameInfo.practiseOccurrenceNumber : mGameInfo.errorNumberOccurrenceNumber);
|
|
|
+
|
|
|
+ mRecordList = new ArrayList<>();
|
|
|
+ mResponseList = new ArrayList<>();
|
|
|
+ mPressRecordList = new ArrayList<>();
|
|
|
+
|
|
|
+ mCurrentPractiseIndex = 0;
|
|
|
+ restoreClickInfo(false);
|
|
|
+ mPractiseHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
|
|
|
@Override
|
|
|
- public void onClick(View view) {
|
|
|
+ public boolean handleMessage(@NonNull Message message) {
|
|
|
+ switch (message.what){
|
|
|
+ case 0:
|
|
|
+ if (mCurrentPractiseIndex == mPractiseNumbers.length){
|
|
|
+ restoreClickInfo(false);
|
|
|
+ if (isPractise)
|
|
|
+ startPregame();
|
|
|
+ else
|
|
|
+ startFeedBack();
|
|
|
+ }else{
|
|
|
+ restoreClickInfo(true);
|
|
|
+ mCurrentTime = System.currentTimeMillis();
|
|
|
+ mTVNumber.setVisibility(View.VISIBLE);
|
|
|
+ mTVGameHint.setVisibility(View.GONE);
|
|
|
+ mTVNumber.setText(String.valueOf(mPractiseNumbers[mCurrentPractiseIndex]));
|
|
|
+ mPractiseHandler.sendEmptyMessageDelayed(1, mGameInfo.displayDuration);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ mTVNumber.setVisibility(View.GONE);
|
|
|
+ if (isPractise) {
|
|
|
+ mTVGameHint.setText(Html.fromHtml(getGameHint(), Html.FROM_HTML_MODE_LEGACY));
|
|
|
+ mTVGameHint.setVisibility(View.VISIBLE);
|
|
|
+ }else {
|
|
|
+ int result = getClickResult();
|
|
|
+ mRecordList.add(result);
|
|
|
+ mPressRecordList.add(mClickTime == 0 ? 0 : 1);
|
|
|
+ if (mClickTime != 0 && result == GameInfo.CLICK_RIGHT)
|
|
|
+ mResponseList.add(mClickTime - mCurrentTime);
|
|
|
+ else
|
|
|
+ mResponseList.add(-1L);
|
|
|
+ }
|
|
|
+ restoreClickInfo(false);
|
|
|
+ mCurrentPractiseIndex ++;
|
|
|
+ mPractiseHandler.sendEmptyMessageDelayed(0, mGameInfo.intervalDuration);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mPractiseHandler.sendEmptyMessage(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startPregame(){
|
|
|
+ hideAllView();
|
|
|
|
|
|
+ mCurrentPreGameCountDown = mGameInfo.getPractiseDurationSecond();
|
|
|
+ mPreGameHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
|
|
|
+ @Override
|
|
|
+ public boolean handleMessage(@NonNull Message message) {
|
|
|
+ if (mCurrentPreGameCountDown > 0){
|
|
|
+ mTVPreGameHint.setVisibility(View.VISIBLE);
|
|
|
+ mTVPreGameHint.setGravity(Gravity.CENTER);
|
|
|
+ mTVPreGameHint.setText(Html.fromHtml(getPreGameText(mCurrentPreGameCountDown), Html.FROM_HTML_MODE_LEGACY));
|
|
|
+ mCurrentPreGameCountDown --;
|
|
|
+ mPreGameHandler.sendEmptyMessageDelayed(0, 1000);
|
|
|
+ }else{
|
|
|
+ mTVPreGameHint.setVisibility(View.GONE);
|
|
|
+ startGame(false);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
+ mPreGameHandler.sendEmptyMessage(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startFeedBack(){
|
|
|
+ hideAllView();
|
|
|
+ mCLForm.setVisibility(View.VISIBLE);
|
|
|
+ mEndTime = System.currentTimeMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getGameHint(){
|
|
|
+ String result;
|
|
|
+ int clickResult = getClickResult();
|
|
|
+
|
|
|
+ result = clickResult == GameInfo.CLICK_RIGHT ? "正確" : (clickResult == GameInfo.CLICK_WRONG ? "錯誤" : "錯失");
|
|
|
+
|
|
|
+ if (mClickTime != 0 && clickResult == GameInfo.CLICK_RIGHT)
|
|
|
+ result += String.format(Locale.TRADITIONAL_CHINESE, "<br/>反應時間: <font color='#cc0000'>%1$d</font> 毫秒", mClickTime - mCurrentTime);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int getClickResult(){
|
|
|
+ int preNumber = mCurrentPractiseIndex == 0 ? -1 : mPractiseNumbers[mCurrentPractiseIndex - 1];
|
|
|
+ int currentNumber = mPractiseNumbers[mCurrentPractiseIndex];
|
|
|
+ if (mGameInfo.gameVersion == GameInfo.VERSION_GAME_ONE){
|
|
|
+ if (mClickTime == 0){
|
|
|
+ if (mSpecialNumberOne == currentNumber)
|
|
|
+ return GameInfo.CLICK_MISS;
|
|
|
+ else
|
|
|
+ return GameInfo.CLICK_RIGHT;
|
|
|
+ }else{
|
|
|
+ if (mSpecialNumberOne == currentNumber)
|
|
|
+ return GameInfo.CLICK_RIGHT;
|
|
|
+ else
|
|
|
+ return GameInfo.CLICK_WRONG;
|
|
|
+ }
|
|
|
+ }else if(mGameInfo.gameVersion == GameInfo.VERSION_GAME_TWO){
|
|
|
+ if (preNumber < 0 && mClickTime != 0)
|
|
|
+ return GameInfo.CLICK_WRONG;
|
|
|
+
|
|
|
+ if (mClickTime == 0){
|
|
|
+ if (preNumber == mSpecialNumberOne && currentNumber == mSpecialNumberTwo)
|
|
|
+ return GameInfo.CLICK_MISS;
|
|
|
+ else
|
|
|
+ return GameInfo.CLICK_RIGHT;
|
|
|
+ }else{
|
|
|
+ if (preNumber == mSpecialNumberOne && currentNumber == mSpecialNumberTwo)
|
|
|
+ return GameInfo.CLICK_RIGHT;
|
|
|
+ else
|
|
|
+ return GameInfo.CLICK_WRONG;
|
|
|
+ }
|
|
|
+ }else if(mGameInfo.gameVersion == GameInfo.VERSION_GAME_THREE){
|
|
|
+ if (mClickTime == 0){
|
|
|
+ if (mSpecialNumberOne == currentNumber)
|
|
|
+ return GameInfo.CLICK_RIGHT;
|
|
|
+ else
|
|
|
+ return GameInfo.CLICK_MISS;
|
|
|
+ }else{
|
|
|
+ if (mSpecialNumberOne == currentNumber)
|
|
|
+ return GameInfo.CLICK_WRONG;
|
|
|
+ else
|
|
|
+ return GameInfo.CLICK_RIGHT;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return GameInfo.CLICK_MISS;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer[] getPractiseNumbers(int otherNumberOccurrence){
|
|
|
+ List<Integer> result = new ArrayList<>();
|
|
|
+ for (int i = 0; i < otherNumberOccurrence; i ++)
|
|
|
+ result.add(getRandomNumber(mSpecialNumberOne));
|
|
|
+
|
|
|
+ for (int i = 0; i < mGameInfo.correctNumberOccurrenceNumber; i ++) {
|
|
|
+ int index = (int) (Math.random() * result.size());
|
|
|
+ if (mGameInfo.gameVersion != GameInfo.VERSION_GAME_TWO)
|
|
|
+ result.add(index, mSpecialNumberOne);
|
|
|
+ else{
|
|
|
+ if (index != 0 && index != result.size() - 1 && result.size() > 2 && ((result.get(index - 1) == mSpecialNumberOne && result.get(index) == mSpecialNumberTwo) || (result.get(index) == mSpecialNumberOne && result.get(index + 1) == mSpecialNumberTwo))){
|
|
|
+ i--;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ result.add(index, mSpecialNumberOne);
|
|
|
+ result.add(index + 1, mSpecialNumberTwo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result.toArray(new Integer[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void restoreClickInfo(boolean clickable){
|
|
|
+ mClickable = clickable;
|
|
|
+ mClickTime = 0;
|
|
|
+ mCurrentTime = 0;
|
|
|
}
|
|
|
|
|
|
private int getRandomNumber(){
|
|
@@ -139,7 +399,7 @@ public class GameActivity extends BaseActivity {
|
|
|
}
|
|
|
|
|
|
private void hideAllView(){
|
|
|
- ConstraintLayout rootView = ((ConstraintLayout)getWindow().getDecorView().findViewById(R.id.root));
|
|
|
+ ConstraintLayout rootView = getWindow().getDecorView().findViewById(R.id.root);
|
|
|
for (int i = 0; i < rootView.getChildCount(); i ++){
|
|
|
if (rootView.getChildAt(i).getId() != R.id.click_area)
|
|
|
rootView.getChildAt(i).setVisibility(View.GONE);
|
|
@@ -151,4 +411,11 @@ public class GameActivity extends BaseActivity {
|
|
|
intent.putExtra(PARAM_GAME_INFO, gameInfo);
|
|
|
context.startActivity(intent);
|
|
|
}
|
|
|
+
|
|
|
+ public static void LAUNCH_GAME(Activity context, ResGameInfo resGameInfo){
|
|
|
+ Intent intent = new Intent(context, GameActivity.class);
|
|
|
+ intent.putExtra(PARAM_GAME_INFO, resGameInfo.getGameInfo());
|
|
|
+ intent.putExtra(PARAM_RES_GAME_INFO, resGameInfo);
|
|
|
+ context.startActivity(intent);
|
|
|
+ }
|
|
|
}
|