diff options
| author | Leonardo Bishop <me@leonardobishop.com> | 2024-11-17 00:11:08 +0000 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.com> | 2024-11-17 00:11:17 +0000 |
| commit | 25a3947a1dc4233b43b2e4e8e79b127fa7dc57f7 (patch) | |
| tree | c19258e9d8d2d4edbb604ee18ca0895c049aae10 /app/src/main/java/com | |
| parent | a0fd80340f93b04eba585254dc2a718770a30e92 (diff) | |
genting
Diffstat (limited to 'app/src/main/java/com')
5 files changed, 138 insertions, 172 deletions
diff --git a/app/src/main/java/com/example/alcagotchi/AlcoGotchi.kt b/app/src/main/java/com/example/alcagotchi/AlcoGotchi.kt index d1c5d7a..bbc5a5b 100644 --- a/app/src/main/java/com/example/alcagotchi/AlcoGotchi.kt +++ b/app/src/main/java/com/example/alcagotchi/AlcoGotchi.kt @@ -1,5 +1,73 @@ package com.example.alcagotchi -class AlcoGotchi { +import okhttp3.Call +import okhttp3.Callback +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.Response +import org.json.JSONObject +import java.io.IOException + +class AlcoGotchi private constructor() { + + private val client = OkHttpClient() + private val jsonMediaType = "application/json; charset=utf-8".toMediaType() + + var baseUrl = "http://192.168.4.1:80" var coins = 100 -}
\ No newline at end of file + + companion object { + @Volatile private var instance: AlcoGotchi? = null + + fun getInstance() = + instance ?: synchronized(this) { + instance ?: AlcoGotchi().also { instance = it } + } + } + + private fun buildUrl(path: String): String { + return "$baseUrl/$path" + } + + val basicCallback = object : Callback { + override fun onFailure(call: Call, e: IOException) { + e.printStackTrace() + } + + override fun onResponse(call: Call, response: Response) { + response.use { + handleStateResponse(response) + } + } + } + + fun handleStateResponse(response: Response) { + if (!response.isSuccessful) throw IOException("Unexpected code $response") + + val json = JSONObject(response.body!!.string()) + + coins = json.getJSONObject("data").getInt("alco_coin") + } + + fun getState() { + val request = Request.Builder() + .url(buildUrl("")) + .build() + + client.newCall(request).enqueue(basicCallback) + } + + fun postGamble(amount: Int) { + val body = JSONObject() + body.put("bet", amount) + + val request = Request.Builder() + .url(buildUrl("gamble")) + .post(body.toString().toRequestBody(jsonMediaType)) + .build() + + client.newCall(request).enqueue(basicCallback) + } +} diff --git a/app/src/main/java/com/example/alcagotchi/CasinoActivity.kt b/app/src/main/java/com/example/alcagotchi/CasinoActivity.kt index a28f42b..bb97951 100644 --- a/app/src/main/java/com/example/alcagotchi/CasinoActivity.kt +++ b/app/src/main/java/com/example/alcagotchi/CasinoActivity.kt @@ -30,6 +30,10 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -42,10 +46,13 @@ import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import androidx.lifecycle.MutableLiveData import com.example.alcagotchi.ui.theme.AlcaGotchiTheme import com.example.alcagotchi.ui.theme.AlcaGotchiTheme class CasinoActivity : ComponentActivity() { + private val coin = MutableLiveData<Int>() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { @@ -63,61 +70,46 @@ class CasinoActivity : ComponentActivity() { } } -fun gamble(amount: Int) { +fun gamble(amount: Int): Int { + val alcoGotchi = AlcoGotchi.getInstance() + alcoGotchi.postGamble(amount) + return alcoGotchi.coins } @Composable -fun CasinoGreetingText(message: String, from: String, modifier: Modifier = Modifier) { - // Create a column so that texts don't overlap +fun CasinoOptions(modifier: Modifier = Modifier) { + val coin = remember { mutableIntStateOf(0) } + Column( verticalArrangement = Arrangement.Center, - modifier = modifier + horizontalAlignment = Alignment.CenterHorizontally ) { + val context = LocalContext.current as Activity + Text( - text = message, - fontSize = 100.sp, - lineHeight = 116.sp, - textAlign = TextAlign.Center, - color = Color(0xFF00FF00), - modifier = Modifier.padding(top = 16.dp) - ) - Text( - text = from, + text = "Your coins:", fontSize = 36.sp, color = Color(0xFF00FF00), modifier = Modifier .padding(top = 16.dp) .padding(end = 16.dp) - .align(alignment = Alignment.End) - ) - } -} - -@Composable -fun CasinoOptions(modifier: Modifier = Modifier) { - Column( - verticalArrangement = Arrangement.Center, - horizontalAlignment = Alignment.CenterHorizontally - ) { - val context = LocalContext.current as Activity - Text( - text = "Your balance: many coin", + text = coin.value.toString(), fontSize = 36.sp, color = Color(0xFF00FF00), modifier = Modifier .padding(top = 16.dp) .padding(end = 16.dp) ) - Button(onClick = { /*TODO*/ }) { + Button(onClick = { coin.intValue = gamble(10) }) { Text("Gamble 10 coins") } - Button(onClick = { /*TODO*/ }) { + Button(onClick = { coin.intValue = gamble(100) }) { Text("Gamble 100 coins") } - Button(onClick = { /*TODO*/ }) { + Button(onClick = { coin.intValue = gamble(1000) }) { Text("Gamble 1000 coins") } Button(onClick = { context.finish() }) { diff --git a/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt b/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt index 4c5e3aa..1d02fea 100644 --- a/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt +++ b/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt @@ -45,18 +45,12 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.example.alcagotchi.ui.theme.AlcaGotchiTheme -import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.HttpRequest -import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.HttpClient -import com.google.gson.Gson import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import okhttp3.OkHttpClient import java.io.BufferedReader -import java.io.IOException import java.io.InputStreamReader import java.net.HttpURLConnection -import java.net.URI import java.net.URL //import java.net.HttpURLConnection //import java.net.URL @@ -67,97 +61,8 @@ import java.net.URL //var client: OkHttpClient? = OkHttpClient() fun PostHttpRequest(url_name: String = "http://httpbin.org/post", json_string: String = """{"name":"John Doe", "age": 30}"""){ - val url = URL(url_name) - val json = json_string - val connection = url.openConnection() as HttpURLConnection - connection.apply { - requestMethod = "POST" - doOutput = true - setRequestProperty("Content-Type", "application/json") - } - - val outputStream = connection.outputStream - outputStream.write(json.toByteArray()) - outputStream.flush() - outputStream.close() - - val response = connection.inputStream.use { it.reader().use { reader -> reader.readText() } } - println(response) - connection.disconnect() } -fun asyncGetHttpRequest( - endpoint: String, - onSuccess: (ApiResponse<AmiiboResponse>) -> Unit, - onError: (Exception) -> Unit -) { - CoroutineScope(Dispatchers.IO).launch { - val url = URL(endpoint) - val openedConnection = url.openConnection() as HttpURLConnection - openedConnection.requestMethod = "GET" - - val responseCode = openedConnection.responseCode - try { - val reader = BufferedReader(InputStreamReader(openedConnection.inputStream)) - val response = reader.readText() - val apiResponse = ApiResponse( - responseCode, - parseJson<AmiiboResponse>(response) - ) - print(response) - reader.close() - // Call the success callback on the main thread - launch(Dispatchers.Main) { - onSuccess(apiResponse) - } - } catch (e: Exception) { - Log.d("Error", e.message.toString()) - // Handle error cases and call the error callback on the main thread - launch(Dispatchers.Main) { - onError(Exception("HTTP Request failed with response code $responseCode")) - } - } finally { - - } - } -} - - -//fun sendPost(urlString: String = "http://127.0.0.1:8080/test", -// bodyContent: String = "test") { -// -// val client = HttpClient.newBuilder().build(); -// val request = HttpRequest.newBuilder() -// .uri(URI.create(urlString)) -// //.header("Content-Type", "application/json") -// .POST(HttpRequest.BodyPublishers.ofString(bodyContent)) -// .build() -// val response = client.send(request, HttpResponse.BodyHandlers.ofString()) -// val answer = response.body() -// println(answer) -//} - -private inline fun <reified T>parseJson(text: String): T = - Gson().fromJson(text, T::class.java) - -data class ApiResponse<T>( - val responseCode: Int, - val response: T -) - -data class AmiiboResponse( - val amiibo: List<AmiiboItem> -) -data class AmiiboItem( - val amiiboSeries: String, - val character: String, - val gameSeries: String, - val head: String, - val image: String, - val name: String, - val tail: String, - val type: String -) class DrinkingActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/com/example/alcagotchi/MainActivity.kt b/app/src/main/java/com/example/alcagotchi/MainActivity.kt index 34afa63..99c8f4f 100644 --- a/app/src/main/java/com/example/alcagotchi/MainActivity.kt +++ b/app/src/main/java/com/example/alcagotchi/MainActivity.kt @@ -60,6 +60,7 @@ import com.example.alcagotchi.ui.theme.AlcaGotchiTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { + AlcoGotchi.getInstance().getState() super.onCreate(savedInstanceState) setContent { AlcaGotchiTheme { diff --git a/app/src/main/java/com/example/alcagotchi/RequestTest.kt b/app/src/main/java/com/example/alcagotchi/RequestTest.kt index 513bb69..4f4535e 100644 --- a/app/src/main/java/com/example/alcagotchi/RequestTest.kt +++ b/app/src/main/java/com/example/alcagotchi/RequestTest.kt @@ -17,48 +17,48 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember -@Composable -fun GetAmiiboItems(modifier: Modifier) { - val amiiboList = remember { mutableStateOf<List<AmiiboItem>>(listOf()) } - Column { - Button(onClick = { - asyncGetHttpRequest( - endpoint = "https://www.amiiboapi.com/api/amiibo/", - onSuccess = { - amiiboList.value = it.response.amiibo - Log.d("SUCCESS", amiiboList.toString()) - }, - onError = { - Log.d("ERROR", it.message.toString()) - } - ) - }) - { - Text( - text = "Get Amiibos" - ) - } - } - Column( -// modifier = Modifier.fillMaxSize(), -// contentPadding = PaddingValues(16.dp) - ) { - Text(text = amiiboList.value.toString()) - } -} -class RequestTest : ComponentActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContent { - AlcaGotchiTheme { - // A surface container using the 'background' color from the theme - Surface( - color = MaterialTheme.colorScheme.background - ) { - // Let's create a composable function named GetAmiiboItems - GetAmiiboItems(modifier = Modifier()) - } - } - } - } -}
\ No newline at end of file +// @Composable +// fun GetAmiiboItems(modifier: Modifier) { +// val amiiboList = remember { mutableStateOf<List<AmiiboItem>>(listOf()) } +// Column { +// Button(onClick = { +// asyncGetHttpRequest( +// endpoint = "https://www.amiiboapi.com/api/amiibo/", +// onSuccess = { +// amiiboList.value = it.response.amiibo +// Log.d("SUCCESS", amiiboList.toString()) +// }, +// onError = { +// Log.d("ERROR", it.message.toString()) +// } +// ) +// }) +// { +// Text( +// text = "Get Amiibos" +// ) +// } +// } +// Column( +// // modifier = Modifier.fillMaxSize(), +// // contentPadding = PaddingValues(16.dp) +// ) { +// Text(text = amiiboList.value.toString()) +// } +// } +// class RequestTest : ComponentActivity() { +// override fun onCreate(savedInstanceState: Bundle?) { +// super.onCreate(savedInstanceState) +// setContent { +// AlcaGotchiTheme { +// // A surface container using the 'background' color from the theme +// Surface( +// color = MaterialTheme.colorScheme.background +// ) { +// // Let's create a composable function named GetAmiiboItems +// GetAmiiboItems(modifier = Modifier()) +// } +// } +// } +// } +// }
\ No newline at end of file |
