-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayground.kt
265 lines (213 loc) · 8.02 KB
/
Playground.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package eu.metatools.wepwawet
import com.google.common.collect.ComparisonChain
import com.googlecode.lanterna.TextColor
import com.googlecode.lanterna.gui2.*
import com.googlecode.lanterna.gui2.table.Table
import eu.metatools.common.*
import eu.metatools.wepwawet.tools.IndexFunction0
import eu.metatools.wepwawet.tools.Recorder
import eu.metatools.wepwawet.tools.recordFrom
import eu.metatools.wepwawet.tools.recorder
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.Channel.Factory.UNLIMITED
import java.lang.Math.round
import java.util.*
import kotlin.properties.Delegates.notNull
class Y(container: Container, i: Int) : Entity(container) {
var i by key(i)
val xRecorder = recorder<Float>(5000)
var x by prop(0, xRecorder::recordFrom)
val cmd: IndexFunction0<Double, Unit> by impulse { ->
x /= 2
}
override fun toStringMembers() = "i=$i, x=$x(i=${xRecorder.exin(container.time)?.round(2)})"
}
class Root(container: Container) : Entity(container, AutoKeyMode.PER_CLASS) {
var children by holdMany<Y>()
var ct by prop(0)
val clear by impulse { ->
children = listOf()
}
val cmd by impulse { arg: String ->
ct += 1
when (arg) {
"add" -> children += create(::Y, children.size).also {
}
"inc" -> for (c in children)
c.x += 1
"del" -> if (children.isNotEmpty()) {
children -= children.last()
}
}
}
override fun toStringMembers() = "size=${children.size}, ct=$ct"
}
fun <T> Map<List<Any?>, T>.softSort() = entries.sortedWith(Comparator { xs, ys ->
val c = ComparisonChain.start()
c.compare(xs.key.size, ys.key.size)
for ((x, y) in xs.key zip ys.key) {
if (x == null && y == null)
c.compare(0, 0)
else if (x == null)
c.compare(0, 1)
else if (y == null)
c.compare(1, 0)
else {
val xc = x.javaClass
val yc = y.javaClass
c.compare(xc.name, yc.name)
if (x is Comparable<*> && y is Comparable<*>)
c.compare(x, y)
}
}
c.result()
})
var pause = false
data class CallComponents(
val revision: Revision,
val id: List<Any?>,
val call: Byte,
val arg: Any?
)
var allContainers by notNull<List<Container>>()
val simulatePing = true
val pingMin = 15
val pingMax = 75
fun playGame(gui: MultiWindowTextGUI, container: Container, calls: Channel<CallComponents>, root: Root)
: Pair<Panel, Job> {
val s = System.currentTimeMillis()
var timeLabel by notNull<Label>()
var apmLabel by notNull<Label>()
var tasLabel by notNull<Label>()
var entityTable by notNull<Table<Any>>()
var cmdTable by notNull<Table<Any>>()
val result = Panel().apply {
layoutManager = GridLayout(2)
addComponent(Label("Time"))
addComponent(Label("...").also { timeLabel = it })
addComponent(Label("APM"))
addComponent(Label("...").also { apmLabel = it })
addComponent(Button("Clear now") {
root.clear()
})
addComponent(Button("Clear in 3 seconds") {
root.clear[3.0]()
})
addComponent(Label("#TA"))
addComponent(Label("...").also { tasLabel = it })
entityTable = Table<Any>("Key", "Values")
cmdTable = Table<Any>("Pending", "When", "Action")
addComponent(cmdTable)
addComponent(entityTable)
}
val job = launch(CommonPool) {
var ac = 0
while (isActive) {
if (pause) {
yield()
continue
}
container.apply {
for ((r, i, c, a) in generateSequence { calls.poll() })
receive(r, i, c, a)
time = (System.currentTimeMillis() - s).toInt()
repo.softUpper = rev()
val minutes = (time / 1000 / 60).toString().padStart(2, '0')
val seconds = (time / 1000).rem(60).toString().padStart(2, '0')
val millis = time.rem(1000).toString().padStart(3, '0')
timeLabel.text = "$minutes:$seconds.$millis"
apmLabel.text = "${1000 * 60 * ac / time}"
gui.guiThread.invokeAndWait {
entityTable.tableModel.apply {
while (rowCount > 0)
removeRow(0)
for ((k, v) in index.softSort())
addRow(k, v.toStringMembers())
}
cmdTable.tableModel.apply {
while (rowCount > 0)
removeRow(0)
fun whenString(r: Revision) =
(round((r.time - time).toDouble() / 100.0) / 10.0).toString()
for ((r, a) in repo.pendingActions.descendingMap())
addRow("Yes", whenString(r), a.toString())
for ((r, a) in repo.doneActions.descendingMap())
addRow("", whenString(r), a.toString())
}
}
if (randomTrue(.60)) {
root.cmd(randomOf("add", "add", "inc", "inc", "inc", "del"))
ac++
}
val mr = allContainers.map(Container::rev).min()!!
val dr = Revision(mr.time - (pingMax * 2), 0, 0)
repo.drop(dr)
tasLabel.text = "${repo.revisions.size}"
}
(result.parent.size.rows - 6).let {
entityTable.visibleRows = it
cmdTable.visibleRows = it
}
delay(100)
}
}
return result to job
}
fun main(args: Array<String>) = runBlocking {
term {
// Create the container locations
var x by notNull<Container>()
var y by notNull<Container>()
val toX = Channel<CallComponents>(UNLIMITED)
val toY = Channel<CallComponents>(UNLIMITED)
val randomLatency = (pingMin..pingMax).toList().toTypedArray()
// Construct the containers
x = object : Container(0) {
override fun dispatch(time: Revision, id: List<Any?>, call: Byte, arg: Any?) {
if (simulatePing)
launch(CommonPool) {
delay(randomOf(*randomLatency).toLong())
toY.send(CallComponents(time, id, call, arg))
}
else
runBlocking {
toY.send(CallComponents(time, id, call, arg))
}
}
}
y = object : Container(1) {
override fun dispatch(time: Revision, id: List<Any?>, call: Byte, arg: Any?) {
if (simulatePing)
launch(CommonPool) {
delay(randomOf(*randomLatency).toLong())
toX.send(CallComponents(time, id, call, arg))
}
else
runBlocking {
toX.send(CallComponents(time, id, call, arg))
}
}
}
allContainers = listOf(x, y)
// Initialize
val a = x.init(::Root)
val b = y.init(::Root)
val gui = MultiWindowTextGUI(this, DefaultWindowManager(), EmptySpace(TextColor.ANSI.WHITE))
val xw = playGame(gui, x, toX, a)
val yw = playGame(gui, y, toY, b)
val window = BasicWindow().apply {
setHints(setOf(Window.Hint.FULL_SCREEN))
component = Panel().apply {
layoutManager = GridLayout(2)
addComponent(Button("pause") { pause = true })
addComponent(Button("play") { pause = false })
addComponent(xw.first)
addComponent(yw.first)
}
}
gui.addWindowAndWait(window)
xw.second.cancel()
yw.second.cancel()
}
}