-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServerAdapter.kt
134 lines (117 loc) · 3.94 KB
/
ServerAdapter.kt
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
package at.dropical.wolliAI
import at.dropical.client.DropicalListener
import at.dropical.client.DropicalProxy
import at.dropical.shared.GameState
import at.dropical.shared.PlayerAction
import at.dropical.shared.net.container.CountDownContainer
import at.dropical.shared.net.container.GameDataContainer
import at.dropical.shared.net.container.GameOverContainer
import at.dropical.shared.net.container.ListDataContainer
import at.dropical.shared.net.requests.HandleInputRequest
import at.dropical.shared.net.requests.JoinRequest
// Created by julian on 11.01.18.
/**
* This is the Bridge/Interface that one player uses
* to communicate to any server.
* It has convenience methods that allow easy access
* to the DropicalProxy.
*
* The concrete Server implementation/communication is managed
* in this class.
*/
class ServerAdapter(
player: String = "Wolli AI "+ Math.random(),
hostName: String = "localhost",
port: Int = 45000,
gameName: String? = null
): DropicalListener {
private var newestGameDataContainer: GameDataContainer? = null
/** Which index in the Arrays in the container is this player. */
private var index: Int = 0
private val server: DropicalProxy = DropicalProxy(hostName, port, this)
private val playerName = player
init {
/** Auto-queue to a game on the server. */
server.writeToServer(JoinRequest(gameName, playerName))
println("gejoint")
}
// Needed because Java.
constructor(gameID: String) : this(gameName = gameID)
/* The DropicalProxy calls these functions. */
override fun updateUI(container: GameDataContainer?) {
if (container != null) {
var i = 0
while (i < container.playernames.size) {
if (container.playernames[i] == playerName) {
index = i
break
}
i++;
}
newestGameDataContainer = container
}
}
override fun countDown(container: CountDownContainer?) {
println("Game starts in ${container?.seconds}")
}
override fun somebodyJoinedTheLobby(container: ListDataContainer?) {
println("Players:")
container?.names?.forEach{ println(it)}
}
override fun onGameOver(container: GameOverContainer?) {
println("Game Over")
}
/* ----- Alte Schnittstelle ------------------- */
fun getGameState(): GameState {
val container = newestGameDataContainer
if(container != null)
return container.currentState
else
return GameState.LOADING
}
fun getArena(): Array<IntArray> {
val container = newestGameDataContainer
if(container != null)
return container.arenas[index]
else
return emptyArena
}
fun getTetromino(): Array<IntArray> {
val container = newestGameDataContainer
if(container != null)
return container.currTrocks[index]
else
return emptyTetromino
}
fun getNextTetromino(): Array<IntArray> {
val container = newestGameDataContainer
if(container != null)
return container.nextTrocks[index]
else
return emptyTetromino
}
fun getXPos(): Int {
val container = newestGameDataContainer
if(container != null)
return container.currTrockXs[index]
else
return 0
}
fun getYPos(): Int {
val container = newestGameDataContainer
if(container != null)
return container.currTrockYs[index]
else
return 0
}
fun sendInput(input: PlayerAction) {
server.writeToServer(HandleInputRequest(playerName, input))
}
/** For performance and readability */
companion object {
// Size: 20*10
val emptyArena = Array<IntArray>(20, { IntArray(10) })
//4*4
val emptyTetromino = Array<IntArray>(4, { IntArray(4) })
}
}