Esempio n. 1
0
def play_token(ws, user, game_id, location, color):
    game=db_session.query(models.Game).filter_by(game_id=game_id).first()

    game_status = json.loads(game.game_status_json)
    if game_status['to_move'] != color:
        return
    if user is not current_user(game, color):
        return
    
    query = db_session.query(models.Token)
    query = query.filter_by(game_id=game_id).filter_by(location=location)
    if query.count() > 0:
        return
    
    game_status['moves_left'] -= 1
    if game_status['moves_left'] == 0:
        game_status['to_move'] = 2 if game_status['to_move'] == 1 else 1
        game_status['moves_left'] = 2
    game.tokens.append(models.Token(player=color, location=location))
    check_game(game, game_status)
    game.game_status_json=json.dumps(game_status)
    db_session.commit()
    message = {
        'action': 'PLAY_TOKEN',
        'location': location,
        'color': color,
    }
    broadcast(ws, game_id, message)
    message = {
        'action': 'NEW_GAME_STATUS',
        'status': game_status
    }
    broadcast(ws, game_id, message)
Esempio n. 2
0
def new_game():
    if 'user_id' not in session:
        #TODO: better interface
        return 'Log in to challenge opponent'
    user_id = session['user_id']
    user = db_session.query(models.User).filter_by(user_id=user_id).first()
    game_name = request.form.get('game_name', None)
    board_id = int(request.form['board_id'])
    opponent_id = int(request.form['opponent_id'])
    opponent = db_session.query(models.User).filter_by(user_id=opponent_id).first()

    if user is None or opponent is None:
        return 'Error in user info'

    if game_name is None:
        game_name = user.display_name + ' vs ' + opponent.display_name

    default_status = json.dumps({
        'to_move': 1,
        'moves_left': 1,
    })
    new_game = models.Game(
        game_name=game_name,
        board_id=board_id,
        game_status_json=default_status,
        player1=user,
        player2=opponent,
    )
    db_session.add(new_game)
    db_session.commit()

    return redirect(url_for('voro.view_game',id=new_game.game_id))
Esempio n. 3
0
def login():
    current_app.logger.info('session: %s', session)
    if request.method == 'POST':
        username = request.form['username']
        user = db_session.query(
            models.User).filter_by(username=username).first()
        if user is None:
            return 'User does not exist'
        session['user_id'] = user.user_id
    elif 'user_id' in session:
        user = db_session.query(
            models.User).filter_by(user_id=session['user_id']).first()
    else:
        user = None
    return render_template('login_null.html', user=user)
Esempio n. 4
0
def layout_board(id):
    board_model = db_session.query(models.Board).filter_by(board_id=id).first()
    board = json.loads(board_model.board_json)
    # with open('board.json', 'r') as f:
    #     board = json.load(f)
    trueboard = np.array(board['tokens'])
    edges = board['edges']

    def edge(i, j):
        p1 = trueboard[i]
        p2 = trueboard[j]
        return {
            'x1': p1[0]+22,
            'y1': p1[1]+22,
            'x2': p2[0]+22,
            'y2': p2[1]+22,
        }

    def cell(i):
        p = trueboard[i]
        return {
            'num': i,
            'x': p[0]+22,
            'y': p[1]+22,
        }
    
    return {
        'board_id': id,
        'board_name': board_model.board_name,
        'edges': [edge(i, j) for (i, j) in edges],
        'cells': [cell(i) for i in range(len(trueboard))],
        'radius': min(np.linalg.norm(trueboard[i]-trueboard[j])
                      for (i, j) in edges) * 0.45,
    }
Esempio n. 5
0
def game_socket(ws, id):
    client_address = ws.handler.client_address
    server = ws.handler.server
    client = server.clients[client_address]
    client.game_id = id
    current_app.logger.info('Opening socket at %s for game %d', client_address, id)
    current_app.logger.info('Session: %s', session)
    if 'user_id' in session:
        user = (db_session.query(models.User)
        .filter_by(user_id=session['user_id'])
        .first())
    else:
        user = None
    while not ws.closed:
        try:
            raw_message = ws.receive()
            if raw_message is None:
                current_app.logger.info('None message received and ignored...')
                continue
            message = json.loads(raw_message)
            if message['action'] == 'PLAY_TOKEN':
                location = int(message['location'])
                color = int(message['color'])
                play_token(ws, user, id, location, color)
            else:
                current_app.logger.warning('Unknown message %s', message)
        except Exception as e:
            current_app.logger.exception('Error in websocket', e)
            current_app.logger.warning('Raw message was: %s', raw_message)
    current_app.logger.info('Closing socket...')
Esempio n. 6
0
def show_single_product(product_id):
    product = db_session.query(Product).get(product_id)
    tempalte_path = "products/product.html"
    context = {
        "product": product,
        "single_product_page": True,
    }
    return render_template(tempalte_path, **context)
Esempio n. 7
0
def login_row():
    current_app.logger.info('session: %s', session)
    if 'user_id' in session:
        user = db_session.query(
            models.User).filter_by(user_id=session['user_id']).first()
    else:
        user = None
    return jinja2.Markup(render_template('login_row_null.html', user=user))
Esempio n. 8
0
def signup():
    #TODO: more informative errors (or not, for this is for testing)
    if 'username' not in request.form:
        return 'No username entered'
    username = request.form['username']
    if db_session.query(models.User).filter_by(username=username).count() > 0:
        return 'User already exists'
    new_user = models.User(username=username, display_name=username)
    db_session.add(new_user)
    db_session.commit()
    return redirect(url_for('login_null.login'))
Esempio n. 9
0
def view_game(id):
    game = db_session.query(models.Game).filter_by(game_id=id).first()
    board_id = game.board_id
    board = layout_board(board_id)
    board['game_name'] = game.game_name
    for token in game.tokens:
        location = token.location
        player = token.player
        board['cells'][location]['color'] = player
    return dict(
        game_status_json=game.game_status_json,
        **board)
Esempio n. 10
0
def user_view(id):
    user = db_session.query(models.User).filter_by(user_id=id).first()
    #TODO: current games
    boards = db_session.query(models.Board).all()
    return dict(user=user, boards=boards)
Esempio n. 11
0
def user_list():
    users = db_session.query(models.User).all()
    return dict(users=users)
Esempio n. 12
0
def board_list():
    boards = db_session.query(models.Board).all()
    games = db_session.query(models.Game).all()
    return dict(boards=boards, games=games)