Esempio n. 1
0
async def test_register_player_new(thegame):
    g = HatGame()
    s = MockWebSocket()
    n = 'test user'
    assert g.register_player(name=n, socket=s)
    assert n in g.players_map
    assert id(s) in g.sockets_map
    assert g.players_map[n] == g.sockets_map[id(s)]
    p = g.players_map[n]
    assert p in g.players
    assert p.socket == s
    assert p.name == n
Esempio n. 2
0
async def test_name_fail_connect():
    g = HatGame()
    m = message.Name(name='test1')
    ws = MockWebSocket()

    for st in (HatGame.ST_PLAY, HatGame.ST_FINISH):
        g.state = st
        await g.name(ws, m)
        e = message.Error(
            code=104,
            message="Can't login new user test1 while game in progress")
        ws.send_json.assert_awaited_once_with(e.data())
        ws.send_json.reset_mock()
Esempio n. 3
0
async def test_name_connect_first():
    g = HatGame()
    m = message.Name(name='test2')
    ws = MockWebSocket()

    g.state = HatGame.ST_SETUP
    await g.name(ws, m)
    print(ws.send_json.await_args_list)
    ws.send_json.assert_has_awaits([
        call(g.game_msg().data()),
        call(message.Prepare(players={
            m.name: 0
        }).data())
    ],
                                   any_order=False)
Esempio n. 4
0
async def test_name_connect_second():
    g = HatGame()
    ws1 = MockWebSocket()
    g.register_player(name="test1", socket=ws1)

    m = message.Name(name='test2')
    ws2 = MockWebSocket()

    g.state = HatGame.ST_SETUP
    await g.name(ws2, m)
    print(ws2.send_json.await_args_list)
    pm = message.Prepare(players={m.name: 0, 'test1': 0})
    ws2.send_json.assert_has_awaits(
        [call(g.game_msg().data()), call(pm.data())], any_order=False)
    ws1.send_json.assert_awaited_once_with(pm.data())
Esempio n. 5
0
from game.views import Login, Words, WebSocket
from settings import *
from game.hat import HatGame

async def on_shutdown(app):
    for ws in app['websockets']:
        await ws.close(code=1001, message='Server shutdown')


async def shutdown(server, app, handler):
    server.close()
    await server.wait_closed()
    await app.shutdown()
    await handler.finish_connections(10.0)
    await app.cleanup()


app = web.Application()
setattr(app, 'websockets', [])
setattr(app, 'game', HatGame())

app.add_routes([
                web.get ('/',       Login,      name='login'),
                web.post('/words',  Words,      name='words'),
                web.get('/ws',      WebSocket,  name='game')
])

if __name__ == '__main__':
    web.run_app(app)
Esempio n. 6
0
                log.info(
                    f"Expire game ID={g.id} name='{g.game_name}', inactivity = {inactive}"
                )

            await g.reset()  # disconnect all, reset state

            if g.id != default_id:  # do not delete default game
                del app.games[g.id]


app = web.Application()
app.websockets = weakref.WeakSet()
app.games = {}

# default game, temporary, hackish
app.games[default_id] = HatGame(name='Secret Tea')
app.games[default_id].id = default_id

app.add_routes((
    web.get('/', Login, name='login'),
    web.get('/games/{id}', GetGame, name='get_game'),
    web.get('/games', ListGames, name='list_games'),
    web.post('/games', NewGame, name='new_game'),
    web.get('/ws/{id}', WebSocket, name='game'),
    web.get('/ws', WebSocket),  # default game
))

if settings.NEED_CORS:
    app.add_routes([web.options('/games', NewGame, name='cors')])

cron = Scheduler(60, expire_games, app)
Esempio n. 7
0
from aiohttp import web

from game.views import Login, Words, WebSocket
from game.hat import HatGame


async def on_shutdown(app):
    for ws in app['websockets']:
        await ws.close(code=1001, message='Server shutdown')


async def shutdown(server, app, handler):
    server.close()
    await server.wait_closed()
    await app.shutdown()
    await handler.finish_connections(10.0)
    await app.cleanup()

app = web.Application()
app.websockets = []
app.game = HatGame()

app.add_routes((
    web.get('/', Login, name='login'),
    web.post('/words', Words, name='words'),
    web.get('/ws', WebSocket, name='game')
))

if __name__ == '__main__':
    web.run_app(app)
Esempio n. 8
0
def thegame():
    g = HatGame()
    g.register_player(name="user1", socket=MockWebSocket())
    g.register_player(name="user2", socket=MockWebSocket())
    g.register_player(name="user3", socket=MockWebSocket())
    return g