Skip to content

Commit

Permalink
#15 Рейтинг пользователя
Browse files Browse the repository at this point in the history
Добавлен подсчет рейтнга пользователя
  • Loading branch information
Neomer committed May 5, 2019
1 parent 598097a commit a0e8beb
Show file tree
Hide file tree
Showing 31 changed files with 830 additions and 143 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="UnusedAttribute">
<activity android:name=".PrivacyPolicyActivity"></activity>
<activity android:name=".RatingActivity"></activity>
<activity android:name=".PrivacyPolicyActivity" />
<activity android:name=".GameModeSelectionActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_game_mode_selection);

ApplicationResources.getInstance().setDebug(0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));

ApplicationResources.getInstance().loadPreferences(this);
ApplicationResources.getInstance().loadSounds(this, null);
ButterKnife.bind(this);
}
Expand All @@ -41,6 +41,11 @@ void SinglePlayerGameMode() {
runGame(new SinglePlayerGameMode());
}

@OnClick(R.id.btnRating)
void StartRatingActivity() {
startActivity(new Intent(this, RatingActivity.class));
}

private void runGame(IGameMode gameMode) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(GAMEMODE_TAG, gameMode);
Expand Down
95 changes: 3 additions & 92 deletions app/src/main/java/my/neomer/sixtyseconds/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,13 @@ public class MainActivity
extends AppCompatActivity {

private static final String TAG = "MainActivity";
private static final int SKIP_ADS_COUNT = 5; // Сколько вопросов показывать без рекламы

// Configuration keys
private static final String USER_UUID_KEY = "USER_UUID";
private static final String DIFFICULTY_KEY = "DIFFICULTY";
private static final String ADS_COUNTER_KEY = "ADS";

private ConstraintLayout voteLayout, mainLayout;
private CardView cardGuess, cardCorrect;
private ImageView imgCorrect;
private TextView txtCorrect;

private InterstitialAd mInterstitialAd;
private int ad_skip = 0;

private IGameMode gameMode;

private enum GameState {
DisplayAds
}
private GameState state;

@Override
protected void onPause() {
YandexMetrica.getReporter(getApplicationContext(), AppMetricaHelper.AppKey).pauseSession();
saveAdsState();
gameMode.getCurrentState().pause();
super.onPause();
}

Expand All @@ -74,16 +55,14 @@ protected void onStop() {
* Сохраняет количество пропущенных вопросов для показа рекламы
*/
private void saveAdsState() {
SharedPreferences pref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor ed = pref.edit();
ed.putInt(ADS_COUNTER_KEY, ad_skip);
ed.apply();
ApplicationResources.getInstance().savePreferences(this);
}

@Override
protected void onResume() {
super.onResume();
YandexMetrica.getReporter(getApplicationContext(), AppMetricaHelper.AppKey).resumeSession();
gameMode.getCurrentState().proceed();
}

@Override
Expand All @@ -98,16 +77,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mainLayout = findViewById(R.id.mainLayout);
voteLayout = findViewById(R.id.layoutVote);

cardCorrect = findViewById(R.id.cardCorrect);
imgCorrect = findViewById(R.id.imgCorrect);
txtCorrect = findViewById(R.id.txtCorrect);

loadGameMode();
loadAds();
loadPreferences();
}

@Override
Expand All @@ -125,49 +96,13 @@ private void loadGameMode() {
gameMode.getGameContext().setActivity(this);
}

private void showCardCorrect(boolean correct) {
if (correct) {
imgCorrect.setImageResource(R.drawable.ic_correct_w48);
txtCorrect.setText(R.string.correct_label);
} else {
imgCorrect.setImageResource(R.drawable.ic_wrong_w48);
txtCorrect.setText(R.string.wrong_label);
}
cardCorrect.setVisibility(View.VISIBLE);
}

private void hideCardCorrect() {
cardCorrect.setVisibility(View.INVISIBLE);
}

/**
* Подготовить систему отображения рекламы
*/
private void loadAds() {
ApplicationResources.getInstance().loadAds(this);
}

/**
* Загрузить настройки
*/
private void loadPreferences() {
SharedPreferences pref = getPreferences(MODE_PRIVATE);

String user = pref.getString(USER_UUID_KEY, null);
int difficulty = pref.getInt(DIFFICULTY_KEY, 5);
gameMode.getGameContext().setAdsSkipped(pref.getInt(ADS_COUNTER_KEY, 0));

if (user == null) {
user = UUID.randomUUID().toString();
SharedPreferences.Editor ed = pref.edit();
ed.putString(USER_UUID_KEY, user);
ed.apply();
}
ApplicationResources.getInstance()
.getQuestionProvider()
.setConfiguration(new TransportConfiguration(user, difficulty));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
Expand All @@ -182,28 +117,4 @@ public boolean onOptionsItemSelected(MenuItem item) {

}
}

/**
* Спрятать окно для оценки вопроса
*/
private void hideVoting() {
voteLayout.setVisibility(View.INVISIBLE);

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(mainLayout);
constraintSet.connect(R.id.fragment, ConstraintSet.BOTTOM, R.id.btnStart, ConstraintSet.TOP,0);
constraintSet.applyTo(mainLayout);
}

/**
* Отобразить окно для оценки вопроса
*/
private void showVoting() {
voteLayout.setVisibility(View.VISIBLE);

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(mainLayout);
constraintSet.connect(R.id.fragment, ConstraintSet.BOTTOM, R.id.layoutVote, ConstraintSet.TOP,0);
constraintSet.applyTo(mainLayout);
}
}
92 changes: 92 additions & 0 deletions app/src/main/java/my/neomer/sixtyseconds/RatingActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package my.neomer.sixtyseconds;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import org.w3c.dom.Text;

import butterknife.BindView;
import butterknife.ButterKnife;
import my.neomer.sixtyseconds.helpers.ApplicationResources;
import my.neomer.sixtyseconds.model.UserRating;
import my.neomer.sixtyseconds.transport.Callback;

public class RatingActivity extends AppCompatActivity
implements Callback<UserRating>
{

@BindView(R.id.txtPoints)
TextView txtPoints;

@BindView(R.id.txtPlace)
TextView txtPlace;

@BindView(R.id.imvPlaceIcon)
ImageView imvPlaceIcon;

@BindView(R.id.loadUserRatingProgressBar)
ProgressBar loadUserRatingProgressBar;

@BindView(R.id.txtFirstPlace)
TextView txtFirstPlace;

@BindView(R.id.txtSecondPlace)
TextView txtSecondPlace;

@BindView(R.id.txtThirdPlace)
TextView txtThirdPlace;

@BindView(R.id.txtFourthPlace)
TextView txtFourthPlace;

@BindView(R.id.txtFifthPlace)
TextView txtFifthPlace;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);

ButterKnife.bind(this);
}

@Override
protected void onResume() {
super.onResume();

loadUserRatingProgressBar.setVisibility(View.VISIBLE);

ApplicationResources.getInstance()
.getQuestionProvider()
.loadUserRating(this);

}

@Override
public void onReady(UserRating data) {
loadUserRatingProgressBar.setVisibility(View.INVISIBLE);
imvPlaceIcon.setVisibility(View.INVISIBLE);
txtPlace.setText(String.valueOf(data.getPlace()));
txtPoints.setText(String.valueOf(data.getPoints()));

txtFirstPlace.setText(String.valueOf(data.getLeaderboard().get(0).getPoints()));
txtSecondPlace.setText(String.valueOf(data.getLeaderboard().get(1).getPoints()));
txtThirdPlace.setText(String.valueOf(data.getLeaderboard().get(2).getPoints()));
txtFourthPlace.setText(String.valueOf(data.getLeaderboard().get(3).getPoints()));
txtFifthPlace.setText(String.valueOf(data.getLeaderboard().get(4).getPoints()));
}

@Override
public void onFailure(Throwable t) {

ApplicationResources.getInstance()
.getQuestionProvider()
.loadUserRating(this);

}
}
2 changes: 2 additions & 0 deletions app/src/main/java/my/neomer/sixtyseconds/dao/AnswerDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class AnswerDTO {
private int result;
private String answer;
private String comment;
private int points;

public int isCorrect() {
return result;
Expand Down Expand Up @@ -37,6 +38,7 @@ public Answer toAnswer() {
answer.setAnswer(this.answer);
answer.setComment(this.comment);
answer.setCorrect(result == 1);
answer.setPoints(points);
return answer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package my.neomer.sixtyseconds.dao;

import my.neomer.sixtyseconds.model.LeaderboardRecord;

public class LeaderboardRecordDTO {

private String user;
private int points;

public LeaderboardRecord toLeaderBoardRecord() {
LeaderboardRecord record = new LeaderboardRecord();
record.setPoints(points);
return record;
}


}
32 changes: 32 additions & 0 deletions app/src/main/java/my/neomer/sixtyseconds/dao/UserRatingDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package my.neomer.sixtyseconds.dao;

import java.util.ArrayList;
import java.util.List;

import my.neomer.sixtyseconds.model.LeaderboardRecord;
import my.neomer.sixtyseconds.model.UserRating;

public class UserRatingDTO {

private String user_id;
private long points;
private int place;
private List<LeaderboardRecordDTO> leaderboard;

public UserRating toUserRating() {
UserRating userRating = new UserRating();
userRating.setPlace(place);
userRating.setPoints(points);

List<LeaderboardRecord> list = new ArrayList<>();
if (leaderboard != null) {
for (LeaderboardRecordDTO rec : leaderboard) {
list.add(rec.toLeaderBoardRecord());
}
}
userRating.setLeaderboard(list);
return userRating;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public BaseGameContext() {
@SuppressLint("StaticFieldLeak")
private FragmentActivity activity;
private String guess;
private int adsSkipped;
private Question.Difficulty difficulty;

public MutableLiveData<Question> getQuestion() {
Expand All @@ -76,13 +75,6 @@ public String getGuess() {
public void setGuess(String guess) {
this.guess = guess;
}
public int getAdsSkipped() {
return adsSkipped;
}
public void setAdsSkipped(int adsSkipped) {
this.adsSkipped = adsSkipped;
}
public void adsSkipped() { this.adsSkipped++; }
public Question.Difficulty getDifficulty() {
return difficulty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import my.neomer.sixtyseconds.core.InfinitePipeline;
import my.neomer.sixtyseconds.states.AdsDisplayingState;
import my.neomer.sixtyseconds.states.AnswerReceivingState;
import my.neomer.sixtyseconds.states.AnswerState;
import my.neomer.sixtyseconds.states.GuessInputState;
import my.neomer.sixtyseconds.states.IState;
import my.neomer.sixtyseconds.states.QuestionReceivingState;
import my.neomer.sixtyseconds.states.QuestionState;
import my.neomer.sixtyseconds.states.RatingAnswerState;

public class SinglePlayerWithRatesGameMode extends BaseGameMode {

Expand Down Expand Up @@ -47,7 +47,7 @@ public SinglePlayerWithRatesGameMode() {
new QuestionState(),
new GuessInputState(),
new AnswerReceivingState(),
new AnswerState(),
new RatingAnswerState(),
new AdsDisplayingState()

));
Expand Down
Loading

0 comments on commit a0e8beb

Please sign in to comment.