Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: moved the distance and answer validation to backend #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 52 additions & 28 deletions app/src/main/java/edu/nitt/delta/orientation22/compose/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package edu.nitt.delta.orientation22.compose

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.net.Uri
import android.provider.Settings
import android.widget.Toast
import androidx.activity.viewModels
import androidx.annotation.DrawableRes
import androidx.compose.animation.animateColor
import androidx.compose.animation.core.animateFloat
Expand Down Expand Up @@ -56,6 +58,7 @@ import androidx.compose.ui.window.Dialog
import androidx.core.app.ActivityCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.MultiplePermissionsState
Expand All @@ -65,8 +68,18 @@ import com.google.android.gms.maps.model.LatLng
import edu.nitt.delta.orientation22.ArActivity
import edu.nitt.delta.orientation22.R
import edu.nitt.delta.orientation22.compose.navigation.NavigationRoutes
import edu.nitt.delta.orientation22.di.api.ApiInterface
import edu.nitt.delta.orientation22.di.api.ApiRoutes
import edu.nitt.delta.orientation22.di.api.ResponseConstants
import edu.nitt.delta.orientation22.di.viewModel.uiState.ArStateViewModel
import edu.nitt.delta.orientation22.models.game.LocationRequest
import edu.nitt.delta.orientation22.ui.theme.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


fun Context.toast(message: CharSequence)
Expand Down Expand Up @@ -122,7 +135,16 @@ fun openAr(
) {
when{
permissionState.hasPermission -> {
distanceCalculator(fusedLocationProviderClient, mContext, currentClueLocation,glbUrl,anchorHash,currentScale, currentLevel)
CoroutineScope(Dispatchers.IO).launch {
distanceCalculator(
fusedLocationProviderClient,
mContext,
glbUrl,
anchorHash,
currentScale,
currentLevel
)
}
}
permissionState.shouldShowRationale -> {
mContext.toast("Camera Access is required for AR Explore.")
Expand Down Expand Up @@ -237,47 +259,49 @@ fun ClueAlertBox(clueName: String,
}
}

fun distanceCalculator(
@SuppressLint("SuspiciousIndentation")
suspend fun distanceCalculator(
fusedLocationProviderClient: FusedLocationProviderClient,
mContext: Context,
currentClueLocation: LatLng,
glbUrl: String,
anchorHash: String,
currentScale: Double,
currentLevel: Int,
){
val innerRadius = 40
val outerRadius = 70

if (
ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
) {
val location = fusedLocationProviderClient.lastLocation
location.addOnSuccessListener {
try {
val results = FloatArray(1)
Location.distanceBetween(
it.latitude,
it.longitude,
currentClueLocation.latitude,
currentClueLocation.longitude,
results
val retrofit=Retrofit.Builder()
.client(OkHttpClient())
.baseUrl(ApiRoutes.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val apiInterface=retrofit.create(ApiInterface::class.java)
val locationRequest = LocationRequest(
latitude = it.latitude,
longitude = it.longitude
)
val distanceInMeters = results[0]
// mContext.toast(distanceInMeters.toString())
if (distanceInMeters <= innerRadius) {
val intent = Intent(mContext, ArActivity::class.java)
intent.putExtra("glb", glbUrl)
intent.putExtra("anchorHash", anchorHash)
intent.putExtra("anchorScale", currentScale)
intent.putExtra("level", currentLevel)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
mContext.startActivity(intent)
} else if (distanceInMeters <= outerRadius){
mContext.toast("You are almost there.")
} else {
mContext.toast("You are too far from your current clue.")
}
CoroutineScope(Dispatchers.IO).launch {
val response=apiInterface.checkDistance(locationRequest)
if(response.message==ResponseConstants.NEAR_OBJECT){
val intent = Intent(mContext, ArActivity::class.java)
intent.putExtra("glb", glbUrl)
intent.putExtra("anchorHash", anchorHash)
intent.putExtra("anchorScale", currentScale)
intent.putExtra("level", currentLevel)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
mContext.startActivity(intent)
}
else if(response.message==ResponseConstants.IN_RADIUS){
mContext.toast("You are almost there. Keep looking!")
}
else {
mContext.toast("You are too far from your current clue!")
}
}
} catch (e:Exception){
mContext.toast("Turn On Location Service")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@ import edu.nitt.delta.orientation22.R
import edu.nitt.delta.orientation22.compose.SpeedDialData
import edu.nitt.delta.orientation22.compose.SpeedDialFloatingActionButton
import edu.nitt.delta.orientation22.compose.toast
import edu.nitt.delta.orientation22.di.api.ApiInterface
import edu.nitt.delta.orientation22.di.api.ApiRoutes
import edu.nitt.delta.orientation22.models.game.CheckAnswerRequest
import edu.nitt.delta.orientation22.ui.theme.*
import io.github.sceneview.ar.ArSceneView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

@Composable
fun ArScreen(
Expand Down Expand Up @@ -162,18 +171,27 @@ fun SubmitPopUp(isPopUp : MutableState<Boolean>, onClick: () -> Unit, answer: St
Spacer(modifier = Modifier.height((screenHeight / 40).dp))
TextButton(
onClick = {
if (answer.equals(text, ignoreCase = true)){
isPopUp.value = false
mContext.toast("Congratulations!! You've completed this level.")
if (!isCorrect.value){
onClick()
isCorrect.value = true
}
onBack()
}
else{
mContext.toast("Wrong code. Please check and try again!")
val retrofit= Retrofit.Builder()
.client(OkHttpClient())
.baseUrl(ApiRoutes.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val apiInterface = retrofit.create(ApiInterface::class.java)
CoroutineScope(Dispatchers.IO).launch {
val response = apiInterface.checkAnswer(CheckAnswerRequest(text))
if(response.message=="correct"){
isPopUp.value = false
mContext.toast("Congratulations! You've completed this level.")
if (!isCorrect.value){
onClick()
isCorrect.value = true
}
onBack()
}
else{
mContext.toast("Wrong code. Please try again!")
}
}
},
shape = RoundedCornerShape(5.dp),
colors = ButtonDefaults.buttonColors(backgroundColor = cyan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package edu.nitt.delta.orientation22.di.api
import edu.nitt.delta.orientation22.models.IsLiveResponse
import edu.nitt.delta.orientation22.models.IsRegisteredResponse
import edu.nitt.delta.orientation22.models.auth.*
import edu.nitt.delta.orientation22.models.game.CheckAnswerRequest
import edu.nitt.delta.orientation22.models.game.AnswerResponse
import edu.nitt.delta.orientation22.models.game.CurrentLocationResponse
import edu.nitt.delta.orientation22.models.game.DistanceCheckResponse
import edu.nitt.delta.orientation22.models.game.LocationRequest
import edu.nitt.delta.orientation22.models.game.PostAnswerRequest
import edu.nitt.delta.orientation22.models.game.RouteResponse
import edu.nitt.delta.orientation22.models.leaderboard.LeaderboardResponse
Expand Down Expand Up @@ -57,4 +60,14 @@ interface ApiInterface {
suspend fun isLive(
@Body token: TokenRequestModel
):IsLiveResponse

@POST(ApiRoutes.CHECK_DISTANCE)
suspend fun checkDistance(
@Body coordinates: LocationRequest
):DistanceCheckResponse

@POST(ApiRoutes.CHECK_ANSWER)
suspend fun checkAnswer(
@Body answer: CheckAnswerRequest
): DistanceCheckResponse
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.nitt.delta.orientation22.di.api

object ApiRoutes {
const val BASE_URL = "https://utopia.nitt.edu/"
const val BASE_URL = "http://localhost:8000/"

const val LEADERBOARD = "api/leaderboard"
const val REGISTER_TEAM ="api/registerTeam"
Expand All @@ -12,4 +12,6 @@ object ApiRoutes {
const val AUTH_CALLBACK = "api/auth/callback"
const val IS_REGISTERED = "api/isRegistered"
const val IS_LIVE="api/isLive"
const val CHECK_DISTANCE="api/checkDistance"
const val CHECK_ANSWER="api/checkAnswer"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ object ResponseConstants {
// const val CALLBACK_RESPONSE_SUCCESS = "User has been authenticated"
const val GET_USER_RESPONSE_SUCCESS = "success"
const val IS_REGISTERED_SUCCESS = "success"
const val NEAR_OBJECT = "success"
const val IN_RADIUS = "warning"
const val FAR_AWAY = "failure"
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package edu.nitt.delta.orientation22.models

open class ClientCredentials (val param : String) {
object ClientID : ClientCredentials("nu7.9dItKsL0AEzQ")
object RedirectUri : ClientCredentials("https://utopia.nitt.edu/api/auth/callback")
object ClientID : ClientCredentials("m3yrmTh1gfRhRptR")
object RedirectUri : ClientCredentials("http://localhost:8000/api/auth/callback")
object State : ClientCredentials("state")
object Nonce : ClientCredentials("123456")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package edu.nitt.delta.orientation22.models.game

import com.google.gson.annotations.SerializedName

data class CheckAnswerRequest (
@SerializedName("answer")
val answer: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package edu.nitt.delta.orientation22.models.game

import com.google.gson.annotations.SerializedName

data class DistanceCheckResponse (
@SerializedName("message")
var message: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package edu.nitt.delta.orientation22.models.game

import com.google.gson.annotations.SerializedName

data class LocationRequest (
@SerializedName("latitude")
val latitude: Double,
@SerializedName("longitude")
val longitude: Double,
)
Loading