예제 #1
0
def application(environ, start_response):

    try:
        if environ['REQUEST_METHOD'] == 'POST':
            request_body_size = int(environ.get('CONTENT_LENGTH', '0'))
            request_body = environ['wsgi.input'].read(request_body_size)
            data = json.loads(request_body.decode())

            WORLDS = {}

            world = data['world']
            if world not in WORLDS:
                with open('public/' + world + '/OBJECTS.json') as f:
                    objects = json.load(f)
                with open('public/' + world + '/ROOMS.json') as f:
                    rooms = json.load(f)
                WORLDS['world'] = [objects, rooms]

            world = WORLDS['world']
            game = GameEngine(world[0], world[1])

            game.set_state(data['state'])

            cmd = game.decode_button_command(data['user_command'])
            prs = game.process_command(cmd)

            audio_prs = game.prompts_only(prs)
            text_prs = game.text_only(prs)

            ndata = {
                'text': text_prs,
                'audio': audio_prs,
                'state': game.get_state()
            }

            start_response('200 OK', [('Content-Type', 'application/json')])
            return [json.dumps(ndata).encode()]

        else:
            # If we are running alongside a web server then the server will handle static files.
            # This is for the stand-alone case (like running on the Farmer Says)
            fn = environ['PATH_INFO']
            if fn == '/':
                fn = '/index.html'
            print('*', fn, '*')
            with open('public' + fn, 'rb') as f:
                data = f.read()
            if fn.endswith('.json'):
                start_response('200 OK', [('Content-Type', 'application/json')])
            else:
                start_response('200 OK', [('Content-Type', 'text/html')])
            return [data]
    except Exception:
        with open('ex.txt', 'w') as f:
            f.write(traceback.format_exc())
        raise
예제 #2
0
class Application:

    cmd_map = {
        '12_oclock': 'NORTH',
        '1_oclock': 'USERIGHT',
        '2_oclock': 'GETRIGHT',
        '3_oclock': 'EAST',
        '4_oclock': 'DROPRIGHT',
        '5_oclock': 'LOOK',
        '6_oclock': 'SOUTH',
        '7_oclock': 'ACTION',
        '8_oclock': 'DROPLEFT',
        '9_oclock': 'WEST',
        '10_oclock': 'GETLEFT',
        '11_oclock': 'USELEFT'
    }

    GAMES = ['cooks', 'vertiv', 'says']

    def __init__(self):

        self._hardware = FarmerSays(self.command_pull_callback)
        self._game_number = 0

        self.reinit()

        self._hardware.start_audio_thread()

    def reinit(self):
        game = Application.GAMES[self._game_number]
        with open('public/' + game + '/ROOMS.json') as f:
            rooms = json.load(f)

        with open('public/' + game + '/OBJECTS.json') as f:
            objects = json.load(f)

        self._engine = GameEngine(objects, rooms)

        self._hardware.set_audio_path('public/' + game + '/audio/')

    def play_prompts(self, prs):
        # stop any existing audio
        self._hardware.stop_audio()
        for pr in prs:
            self._hardware.queue_prompt([pr, 0.25])

    def command_pull_callback(self, cmd):

        if cmd == 'red':
            self.reinit()
            self.command_pull_callback('5_oclock')
            return

        elif cmd == 'yellow':
            self._game_number += 1
            if self._game_number >= len(Application.GAMES):
                self._game_number = 0
            self._hardware.stop_audio()
            self._hardware.queue_prompt([Application.GAMES[self._game_number] + '.wav', 0.25], path='public/')
            return

        cmd = Application.cmd_map[cmd]
        cmd = self._engine.decode_button_command(cmd)
        prs = self._engine.process_command(cmd)

        if prs:
            aud_prs = self._engine.prompts_only(prs)
            self.play_prompts(aud_prs)
예제 #3
0
class Application:
    def __init__(self, game, use_audio=True):

        if use_audio:
            print('Loading hardware ...')
            from farmer_says import FarmerSays
            print('... done')
            self._hardware = FarmerSays()
            self._hardware.start_audio_thread()
        else:
            self._hardware = None

        with open('public/' + game + '/ROOMS.json') as f:
            rooms = json.load(f)

        with open('public/' + game + '/OBJECTS.json') as f:
            objects = json.load(f)

        self._engine = GameEngine(objects, rooms)

        if self._hardware:
            self._hardware.set_audio_path('public/' + game + '/audio/')

    def print_long(self, txt):
        paras = txt.split('\n')
        for para in paras:
            print(textwrap.fill(para.strip(), COLUMNS))

    def play_prompts(self, prs):

        if not self._hardware:
            return

        # stop any existing audio
        self._hardware.stop_audio()
        for pr in prs:
            self._hardware.queue_prompt([pr, 0.25])

    def console_loop(self):

        known = [
            'NORTH', 'SOUTH', 'EAST', 'WEST', 'ACTION', 'LOOK', 'GETLEFT',
            'GETRIGHT', 'DROPLEFT', 'DROPRIGHT', 'USELEFT', 'USERIGHT'
        ]

        replace = {'E': 'EAST', 'W': 'WEST', 'N': 'NORTH', 'S': 'SOUTH'}

        first_cmd = True
        while True:
            while True:

                if first_cmd:
                    cmd = 'LOOK'
                    first_cmd = False
                else:
                    cmd = input('> ').upper().replace(' ', '')
                    if cmd in replace:
                        cmd = replace[cmd]
                    if cmd not in known:
                        print('## Unknown command:', cmd)
                        continue
                    cmd = self._engine.decode_button_command(cmd.upper())

                prs = self._engine.process_command(cmd)

                if prs:
                    aud_prs = self._engine.prompts_only(prs)
                    self.play_prompts(aud_prs)
                    prs = self._engine.text_only(prs)
                    self.print_long(prs)