-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayGameActivity.kt
More file actions
138 lines (117 loc) · 3.88 KB
/
PlayGameActivity.kt
File metadata and controls
138 lines (117 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.example.cs193a_hw5_devansh
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.ContactsContract
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.google.firebase.FirebaseApp
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.*
class PlayGameActivity : AppCompatActivity()
{
lateinit var mainDB: DatabaseReference
lateinit var questions: DatabaseReference
lateinit var currentQ: DatabaseReference
var id = 0
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_play_game)
mainDB = FirebaseDatabase.getInstance().reference.child("animalgame")
questions = mainDB.child("nodes")
currentQ = questions.child("1")
id = 0
updateQ()
}
fun processData (): Int
{
var x =0
currentQ.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(data: DataSnapshot)
{
val currentQuestion = data.getValue(DBNode::class.java)!!
x=currentQuestion.id
}
})
return x
}
fun updateQ ()
{
var question: String = ""
currentQ.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(data: DataSnapshot)
{
val currentQuestion = data.getValue(DBNode::class.java)!!
question = currentQuestion.text
}
})
val displayedQuestion = findViewById<TextView>(R.id.currentQuestion)
displayedQuestion.text = question
}
fun responseYes (view: View)
{
var b = true
currentQ.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(data: DataSnapshot)
{
val currentQuestion = data.getValue(DBNode::class.java)!!
if (currentQuestion!!.isQuestion)
{
id = currentQuestion!!.id
val newID = currentQuestion!!.yes
currentQ = questions.child("$newID")
processData()
updateQ()
}
else
{
b = false
}
}
})
if (!b)
{
val myIntent = Intent(this, WinGame::class.java)
startActivity(myIntent)
}
}
fun responseNo (view: View)
{
var b = true
currentQ.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(data: DataSnapshot)
{
val currentQuestion = data.getValue(DBNode::class.java)!!
if (currentQuestion!!.isQuestion)
{
id = currentQuestion!!.id
val newID = currentQuestion!!.no
currentQ = questions.child("$newID")
updateQ()
}
else
{
b = false
}
}
})
if(!b)
{
val myIntent = Intent(this, LoseGameActivity::class.java)
myIntent.putExtra("quesID", id)
myIntent.putExtra("ansID", processData())
startActivity(myIntent)
}
}
}