Skip to content

Commit

Permalink
Clean up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszwasacz committed Mar 21, 2020
1 parent 5236326 commit c15e051
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 196 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,42 @@ open class CommunicationViewModel(application: Application): AndroidViewModel(ap
val versionDialogId = 1

//IP i port serwera
val serverIP = (PreferenceManager.getDefaultSharedPreferences(application).getString(application.getString(R.string.key_ip), application.getString(R.string.default_ip)) ?: application.getString(R.string.default_ip)).toString()
val serverPORT = (PreferenceManager.getDefaultSharedPreferences(application).getString(application.getString(R.string.key_port), application.getString(R.string.default_port)) ?: application.getString(R.string.default_port)).toInt()
private val serverIP = (PreferenceManager.getDefaultSharedPreferences(application).getString(application.getString(R.string.key_ip), application.getString(R.string.default_ip)) ?: application.getString(R.string.default_ip)).toString()
private val serverPORT = (PreferenceManager.getDefaultSharedPreferences(application).getString(application.getString(R.string.key_port), application.getString(R.string.default_port)) ?: application.getString(R.string.default_port)).toInt()

//Socket
val client = OkHttpClient()
var socket: WebSocket? = null
//lateinit var output: OutputStream
//lateinit var inputStream: InputStreamReader
private val client = OkHttpClient()
private var socket: WebSocket? = null
val NORMAL_CLOSURE_STATUS = 1000

//Listy AsyncTask'ów
//val communicationTaskList = ArrayList<CommunicationTask>()
//val sendTaskList = ArrayList<SendTask>()
val interpretationTaskList = ArrayList<InterpretationTask>()

var currentGameState = MutableLiveData<Event<BoardModel>>()
var wrongSocket = MutableLiveData<Event<Boolean>>()
var versionPacket: PacketVER? = null

//Łączenie z serwerem
fun connect(roomName: String) {
//ConnectTask(this@CommunicationViewModel, roomName).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
dialogId.value = Event(connectDialogId)
wrongSocket.value = Event(false)
val request = Request.Builder().url("ws://$serverIP:$serverPORT").build()
val listener = EchoWebSocketListener(this@CommunicationViewModel, roomName)
socket = client.newWebSocket(request, listener)
}

/*
//Odbieranie pakietu
fun communicate(packet: Packet?) {
val task = CommunicationTask(this@CommunicationViewModel, packet)
communicationTaskList.add(task)
task.execute()
}*/

//Wysyłanie ruchu gracza
fun sendMove(x: Int, y: Int) {
/*val task = SendTask(this@CommunicationViewModel, PacketSET(params = ParamsSET(x, y), time = (System.currentTimeMillis()/1000L).toInt()))
sendTaskList.add(task)
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)*/
val packet = PacketSET(params = ParamsSET(x, y), time = (System.currentTimeMillis()/1000L).toInt())
socket?.send(Gson().toJson(packet))
}

//Wysyłanie odpowiedzi na Ping
fun sendPOG() {
/*val task = SendTask(this@CommunicationViewModel, PacketGetPngPog(method = "POG", params = ParamsGetPngPog(), time = (System.currentTimeMillis()/1000L).toInt()))
sendTaskList.add(task)
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)*/
val packet = PacketGetPngPog(method = "POG", params = ParamsGetPngPog(), time = (System.currentTimeMillis()/1000L).toInt())
socket?.send(Gson().toJson(packet))
}

//Zamykanie Socketa
override fun onCleared() {
//CloseSocketTask(this@CommunicationViewModel).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
for(task in interpretationTaskList) {
if(task.status == AsyncTask.Status.RUNNING || task.status == AsyncTask.Status.PENDING)
task.cancel(false)
Expand All @@ -87,8 +65,6 @@ open class CommunicationViewModel(application: Application): AndroidViewModel(ap

//Deserializacja pakietu z serwera w obiekt
fun deserializePacketFromServer(input: String): Packet {
//val packetTypeJON = object: TypeToken<PacketJON>() {}.type
//val packetTypeSET = object: TypeToken<PacketSET>() {}.type
val packetTypeSTT = object: TypeToken<PacketSTT>() {}.type
val packetTypeVER = object: TypeToken<PacketVER>() {}.type
val packetTypeBadErrDbgUin = object: TypeToken<PacketBadErrDbgUin>() {}.type
Expand All @@ -97,8 +73,6 @@ open class CommunicationViewModel(application: Application): AndroidViewModel(ap
val tempPacket = Gson().fromJson<PacketGetPngPog>(input, packetTypeGetPngPog)

return when(tempPacket.method) {
//"JON" -> Gson().fromJson<PacketJON>(input, packetTypeJON)
//"SET" -> Gson().fromJson<PacketSET>(input, packetTypeSET)
"STT" -> Gson().fromJson<PacketSTT>(input, packetTypeSTT)
"VER" -> Gson().fromJson<PacketVER>(input, packetTypeVER)
"BAD" -> Gson().fromJson<PacketBadErrDbgUin>(input, packetTypeBadErrDbgUin)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class EchoWebSocketListener(private val viewModel: CommunicationViewModel, priva
val task = InterpretationTask(viewModel, text)
viewModel.interpretationTaskList.add(task)
task.execute()
//Log.i("Receiving", text)
}

override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class MainActivity: AppCompatActivity() {
//Obsługa ikonek
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id: Int = item.itemId
//Ustawienia
if(id == R.id.action_settings) {
// launch settings activity
startActivity(Intent(this@MainActivity, SettingsActivity::class.java))
return true
}
Expand Down
14 changes: 0 additions & 14 deletions app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/SendTask.kt

This file was deleted.

3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
tools:ignore="Autofill" />

<Button
android:id="@+id/buttonJoin"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<!-- Reply Preference -->
<!-- Theme Preference -->
<string-array name="theme_entries">
<item>@string/theme_name_light</item>
<item>@string/theme_name_dark</item>
Expand Down

0 comments on commit c15e051

Please sign in to comment.