Skip to content

Commit

Permalink
🚀 Time and strike dependent score update
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbatovK committed Mar 26, 2022
1 parent fdfaf68 commit b54c205
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 50 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/com/albatros/kquiz/model/api/ApiService.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.albatros.kquiz.model.api

import com.albatros.kquiz.model.data.AnswerSubmit
import com.albatros.kquiz.model.data.ClientInfo
import com.albatros.kquiz.model.data.Quiz
import com.albatros.kquiz.model.data.ResponseData
import com.albatros.kquiz.model.data.response.AnswerSubmit
import com.albatros.kquiz.model.data.info.ClientInfo
import com.albatros.kquiz.model.data.pojo.Quiz
import com.albatros.kquiz.model.data.response.ResponseData
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.albatros.kquiz.model.data
package com.albatros.kquiz.model.data.info

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
Expand All @@ -19,6 +19,6 @@ data class ClientInfo(

@SerializedName("questionMap")
@Expose
var questionMap: HashMap<Int, Boolean> = HashMap()
var questionMap: HashMap<Int, QuestionInfo> = HashMap()

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.albatros.kquiz.model.data.info

import android.os.Parcelable
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize

@Parcelize
data class QuestionInfo(

@SerializedName("right")
@Expose
var right: Boolean = false,

@SerializedName("time")
@Expose
var time: Int = 0

) : Parcelable
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.albatros.kquiz.model.data
package com.albatros.kquiz.model.data.pojo

import android.os.Parcelable
import com.google.gson.annotations.Expose
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.albatros.kquiz.model.data
package com.albatros.kquiz.model.data.pojo

import android.os.Parcelable
import com.albatros.kquiz.model.data.pojo.Question
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.albatros.kquiz.model.data
package com.albatros.kquiz.model.data.response

import android.os.Parcelable
import com.google.gson.annotations.Expose
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.albatros.kquiz.model.data
package com.albatros.kquiz.model.data.response

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/albatros/kquiz/model/repo/ClientRepo.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.albatros.kquiz.model.repo

import com.albatros.kquiz.model.data.ClientInfo
import com.albatros.kquiz.model.data.Quiz
import com.albatros.kquiz.model.data.info.ClientInfo
import com.albatros.kquiz.model.data.pojo.Quiz

class ClientRepo {
var quiz: Quiz = Quiz()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.albatros.kquiz.databinding.ClientLayoutBinding
import com.albatros.kquiz.model.data.ClientInfo
import com.albatros.kquiz.model.data.info.ClientInfo

class ClientAdapter(private val clients: MutableList<ClientInfo>) :
RecyclerView.Adapter<ClientAdapter.ClientViewHolder>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.albatros.kquiz.R
import com.albatros.kquiz.databinding.ResultClientLayoutBinding
import com.albatros.kquiz.domain.playFadeInAnimation
import com.albatros.kquiz.model.data.ClientInfo
import com.albatros.kquiz.model.data.info.ClientInfo

class ResultAdapter(private val clients: MutableList<ClientInfo>) :
RecyclerView.Adapter<ResultAdapter.ResultClientViewHolder>() {
Expand Down Expand Up @@ -38,7 +38,22 @@ class ResultAdapter(private val clients: MutableList<ClientInfo>) :
with(binding) {
binding.root.playFadeInAnimation(R.anim.fade_in_animation)
name.text = it.name
score.text = (it.questionMap.count { item -> item.value } * 1000).toString()

val base = it.questionMap.count { item -> item.value.right } * 1000
var timeDependent = 0

val from = if (it.questionMap.size == 0) 0 else it.questionMap.size - 1
val to = if (it.questionMap.size > 2) it.questionMap.size - 3 else 0

val lastCount = it.questionMap.filterKeys { it in from downTo to }.count { item -> item.value.right }

if (lastCount == 3) {
name.text = it.name + " " + "\uD83D\uDD25"
timeDependent = 200 - (it.questionMap[it.questionMap.size - 1]?.time ?: 20) * 10
}

val res = base + timeDependent
score.text = res.toString()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.albatros.kquiz.R
import com.albatros.kquiz.databinding.QuizLayoutBinding
import com.albatros.kquiz.domain.playFadeInAnimation
import com.albatros.kquiz.model.data.Quiz
import com.albatros.kquiz.model.data.pojo.Quiz

class QuizAdapter(
private val quizzes: MutableList<Quiz>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.albatros.kquiz.ui.adapter.quiz

import android.view.View
import com.albatros.kquiz.model.data.Quiz
import com.albatros.kquiz.model.data.pojo.Quiz

interface QuizAdapterListener {
fun onQuizSelected(quiz: Quiz, view: View)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package com.albatros.kquiz.ui.fragments.client
import android.os.Bundle
import android.transition.TransitionInflater
import android.view.*
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.albatros.kquiz.R
import com.albatros.kquiz.databinding.ClientFragmentBinding
import com.albatros.kquiz.model.data.ClientInfo
import com.albatros.kquiz.model.data.info.ClientInfo
import com.albatros.kquiz.ui.activity.MainActivity
import com.albatros.kquiz.ui.adapter.client.ClientAdapter
import com.albatros.kquiz.ui.fragments.host.HostFragmentDirections
import org.koin.androidx.viewmodel.ext.android.viewModel

class ClientFragment : Fragment(), MainActivity.IOnBackPressed {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.albatros.kquiz.model.api.ApiService
import com.albatros.kquiz.model.data.ClientInfo
import com.albatros.kquiz.model.data.info.ClientInfo
import com.albatros.kquiz.model.repo.ClientRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -45,14 +45,6 @@ class ClientViewModel(private val api: ApiService, private val repo: ClientRepo)

val started: LiveData<Boolean> = _started



val usersInfo: LiveData<List<ClientInfo>> = _usersInfo

fun updateUserInfo() {
viewModelScope.launch(Dispatchers.Main) {
_usersInfo.value = api.getClientsInfo(repo.sessionId)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.albatros.kquiz.model.api.ApiService
import com.albatros.kquiz.model.data.AnswerSubmit
import com.albatros.kquiz.model.data.Question
import com.albatros.kquiz.model.data.response.AnswerSubmit
import com.albatros.kquiz.model.data.pojo.Question
import com.albatros.kquiz.model.repo.ClientRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand All @@ -20,7 +20,10 @@ class GameViewModel(

private val _onActionEnd = MutableLiveData<Boolean>().apply {
viewModelScope.launch(Dispatchers.Main) {
delay(20_000)
repeat(20) {
delay(1000)
times++
}
value = false
}
}
Expand All @@ -40,12 +43,12 @@ class GameViewModel(

val onActionEnd: LiveData<Boolean> = _onActionEnd

private val _onSuccessSubmit = MutableLiveData<Boolean>()
var times = 0

val onSuccessSubmit: LiveData<Boolean> = _onSuccessSubmit
private val _onSuccessSubmit = MutableLiveData<Boolean>()

fun submitData(answer: String) {
val data = AnswerSubmit(repo.clientInfo.id, repo.currentPos, answer == question.answer, 0)
val data = AnswerSubmit(repo.clientInfo.id, repo.currentPos, answer == question.answer, times)
repo.currentPos++
viewModelScope.launch(Dispatchers.Main) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.albatros.kquiz.R
import com.albatros.kquiz.databinding.HostFragmentBinding
import com.albatros.kquiz.databinding.InfoDialogBinding
import com.albatros.kquiz.model.data.ClientInfo
import com.albatros.kquiz.model.data.info.ClientInfo
import com.albatros.kquiz.ui.activity.MainActivity
import com.albatros.kquiz.ui.adapter.client.ClientAdapter
import com.albatros.kquiz.ui.fragments.enter.EnterFragmentDirections
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.koin.androidx.viewmodel.ext.android.viewModel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.albatros.kquiz.model.api.ApiService
import com.albatros.kquiz.model.data.ClientInfo
import com.albatros.kquiz.model.data.info.ClientInfo
import com.albatros.kquiz.model.repo.ClientRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.albatros.kquiz.model.api.ApiService
import com.albatros.kquiz.model.data.Quiz
import com.albatros.kquiz.model.data.pojo.Quiz
import com.albatros.kquiz.model.repo.ClientRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.albatros.kquiz.ui.fragments.list

import android.content.Intent
import android.os.Bundle
import android.transition.TransitionInflater
import android.view.LayoutInflater
Expand All @@ -15,10 +14,9 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.albatros.kquiz.R
import com.albatros.kquiz.databinding.InfoDialogBinding
import com.albatros.kquiz.databinding.NameDialogBinding
import com.albatros.kquiz.databinding.QuizListFragmentBinding
import com.albatros.kquiz.model.data.Quiz
import com.albatros.kquiz.model.data.pojo.Quiz
import com.albatros.kquiz.ui.adapter.quiz.QuizAdapter
import com.albatros.kquiz.ui.adapter.quiz.QuizAdapterListener
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package com.albatros.kquiz.ui.fragments.result
import android.os.Bundle
import android.transition.TransitionInflater
import android.view.*
import android.widget.LinearLayout
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import com.albatros.kquiz.R
import com.albatros.kquiz.databinding.ResultFragmentBinding
import com.albatros.kquiz.model.data.ClientInfo
import com.albatros.kquiz.model.data.info.ClientInfo
import com.albatros.kquiz.ui.adapter.client.ResultAdapter
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.albatros.kquiz.model.api.ApiService
import com.albatros.kquiz.model.data.ClientInfo
import com.albatros.kquiz.model.data.Question
import com.albatros.kquiz.model.data.info.ClientInfo
import com.albatros.kquiz.model.data.pojo.Question
import com.albatros.kquiz.model.repo.ClientRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -35,7 +35,7 @@ class ResultViewModel(
private val _usersInfo = MutableLiveData<List<ClientInfo>>().apply {
viewModelScope.launch(Dispatchers.Main) {
value = api.getClientsInfo(repo.sessionId).sortedByDescending {
it.questionMap.count { item -> item.value }
it.questionMap.count { item -> item.value.right }
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

<argument
android:name="question"
app:argType="com.albatros.kquiz.model.data.Question" />
app:argType="com.albatros.kquiz.model.data.pojo.Question" />

</fragment>

Expand Down Expand Up @@ -109,7 +109,7 @@

<argument
android:name="question"
app:argType="com.albatros.kquiz.model.data.Question" />
app:argType="com.albatros.kquiz.model.data.pojo.Question" />

</fragment>

Expand Down

0 comments on commit b54c205

Please sign in to comment.