summaryrefslogtreecommitdiff
path: root/robots/client.py
blob: 8832506d245102b3dcea9758230f7343be08598b (about) (plain)
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
import argparse
import os
import random
from string import ascii_lowercase as lowercase
import zmq

import logbook
import urwid

from network.client import Discoverer

from robots.cursesviewer import CursesViewer
from robots.game import Game
from robots.utils import read_map

log = logbook.Logger(__name__)

def button_width(btn):
    return len(btn.label) + 4

def focusable(widget):
    return urwid.AttrMap(widget, None, focus_map='reversed')

class Browser(Discoverer):
    SERVER_KIND = 'robots'

    def __init__(self, avail_walker, added_walker, ctx=None):
        super(Browser, self).__init__(self.SERVER_KIND)

        if ctx is None:
            ctx = zmq.Context.instance()

        self.ctx = ctx

        self.targets = {}
        self.added = []

        self.avail_walker = avail_walker
        self.added_walker = added_walker

    def make_server_widget(self, target, info):
        return urwid.AttrMap(
            urwid.Text(info['name']),
            'bright',
        )

    def make_bot_widget(self, key, server_info, name):
        button = urwid.Button('Add')

        def on_button_pressed(_):
            self.added.append((key, server_info, name))
            self.update_added()

        urwid.connect_signal(button, 'click', on_button_pressed)

        return focusable(urwid.Columns([
            urwid.Text(' ' * 4 + name),
            (button_width(button), button),
        ]))

    def make_added_widget(self, key, server_info, name):
        button = urwid.Button('Remove')

        widget = focusable(urwid.Columns([
            urwid.Text(server_info['name'] + ' / ' + name),
            (button_width(button), button),
        ]))

        def on_button_pressed(_):
            self.added.remove((key, server_info, name))
            self.update_added()

        urwid.connect_signal(button, 'click', on_button_pressed)

        return widget

    def on_services_changed(self):
        current_targets = set()

        for service in self.services.values():
            if service is None:
                continue

            target = service['target']
            current_targets.add(target)

            if target in self.targets:
                continue

            sock = self.ctx.socket(zmq.REQ)
            sock.connect(target)
            sock.send_json({'action': 'list'})
            response = sock.recv_json()

            if response['status'] == 200:
                self.targets[target] = {
                    'name': str(service['name']),
                    'bots': response['bots'],
                }

        for target in list(self.targets.keys()):
            if target not in current_targets:
                del self.targets[target]

        self.update_avail()

    def update_avail(self):
        things = []
        for target, info in sorted(
            self.targets.items(),
            key=lambda item: item[1]['name'],
        ):
            things.append(self.make_server_widget(target, info))
            for name in sorted(info['bots']):
                key = (target, name)
                things.append(self.make_bot_widget(key, info, name))
            things.append(urwid.Text(''))

        self.avail_walker[:] = things

    def update_added(self):
        things = []
        for key, server_info, name in self.added:
            things.append(self.make_added_widget(key, server_info, name))
        self.added_walker[:] = things

def make_ui(avail_walker, added_walker):
    start_game = urwid.Button('Start Game')

    def on_start_game(_):
        raise urwid.ExitMainLoop()
    urwid.connect_signal(start_game, 'click', on_start_game)

    main = urwid.Columns([
        urwid.Frame(
            urwid.LineBox(urwid.ListBox(avail_walker)),
            header=urwid.Text('Available bots:'),
        ),
        urwid.Frame(
            urwid.Pile([
                urwid.LineBox(urwid.ListBox(added_walker)),
                ('pack', urwid.Padding(
                    focusable(start_game),
                    align='right',
                    width=button_width(start_game),
                )),
            ]),
            header=urwid.Text('Bots in game:'),
        ),
    ])

    stdout = urwid.Text('')

    def update_stdout(data):
        content = stdout.text + data.decode('utf-8')
        lines = content.split('\n')
        stdout.set_text('\n'.join(lines[-5:]))

    frame = urwid.Frame(main, footer=urwid.LineBox(stdout))
    return frame, update_stdout

def choose_bots():
    avail_walker = urwid.SimpleFocusListWalker([])
    added_walker = urwid.SimpleFocusListWalker([])

    frame, update_stdout = make_ui(avail_walker, added_walker)
    mainloop = urwid.MainLoop(
        frame,
        palette=[('reversed', 'standout', '')],
        event_loop=urwid.GLibEventLoop(),
    )

    pipe = mainloop.watch_pipe(update_stdout)
    handler = logbook.StreamHandler(os.fdopen(pipe, 'w'))

    with handler.applicationbound():
        browser = Browser(avail_walker, added_walker)
        mainloop.run()

    return browser.added

class RemoteBot(object):
    def __init__(self, target, name, ctx=None):
        if ctx is None:
            ctx = zmq.Context.instance()

        self.sock = ctx.socket(zmq.REQ)
        self.sock.connect(target)

        self.debug_name = target + '/' + name

        self.sock.send_json({
            'action': 'create',
            'name': name,
            'options': {},
        })
        resp = self.sock.recv_json()
        if resp['status'] != 200:
            raise RuntimeError(
                'Failed to create remote bot %s/%s: %s' % (
                    target, name, resp.get('message'),
                )
            )
        self.instance_id = resp['instance_id']

    def __call__(self, whoami, state):
        self.sock.send_json({
            'action': 'process',
            'instance_id': self.instance_id,
            'whoami': whoami,
            'state': state.to_json(),
        })
        result = self.sock.recv_json()
        if result['status'] != 200:
            raise RuntimeError(
                'Failed to run %s: %s' % (
                    self.debug_name, result.get('message'),
                )
            )
        return result['result']

    def close(self):
        self.sock.send_json({
            'action': 'destroy',
            'instance_id': self.instance_id,
        })
        self.sock.recv_json()

    def __del__(self):
        self.close()

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('map', type=argparse.FileType('r'))
    args = parser.parse_args()

    with args.map:
        map_ = read_map(args.map)

    bots = choose_bots()

    game = Game(
        map_,
        victory_by_combat=len(bots) != 1,
    )
    for key, server_info, name in bots:
        suffix = ''.join(random.sample(lowercase, 3))
        fullname = '%s.%s.%s' % (
            server_info['name'],
            name,
            suffix,
        )
        game.add_bot(
            RemoteBot(*key),
            fullname,
        )

    viewer = CursesViewer(game)
    viewer.run()

if __name__ == '__main__':
    main()