Exemplo n.º 1
0
def handle_disconnect():
    restart_db()
    global pong_thread
    #tell all clients to SCRAM!
    socker.emit('scram', {'get': 'the f**k out'}, namespace='/pong')

    pong_thread.kill()
Exemplo n.º 2
0
def handle_player_connect(data):
    global pong_thread

    print('received player_connect event')
    data_base = get_db()
    paddle_stats = get_paddle_stats()
    if data_base['count'] == 0:
        data_base[data['id']] = {'player_number': 1}
        data_base[data['id']]['x'] = 10
        data_base[data['id']]['y'] = 50
        data_base['count'] += 1
        socker.emit('what_player', {
            'id': data['id'],
            'player_number': data_base['count']
        },
                    namespace='/pong')
    elif data_base['count'] == 1:
        data_base[data['id']] = {'player_number': 2}
        data_base[data['id']]['x'] = 375
        data_base[data['id']]['y'] = 50
        data_base['count'] += 1
        socker.emit('what_player', {
            'id': data['id'],
            'player_number': data_base['count']
        },
                    namespace='/pong')

        #all players connected
        print('SENDING ALL PLAYERS CONNECTED SIGNAL')
        socker.emit('all_players', data_base, namespace='/pong')
        socker.emit('recv_paddle_stats', paddle_stats, namespace='/pong')

        pong_thread = eventlet.spawn(run_pong)  #how to end?
Exemplo n.º 3
0
def handle_connect():
    global survivor_thread
    db = get_db()
    # Start the game thread only when the first player joins
    if db['total_players'] == 0:
        survivor_thread = eventlet.spawn(run_survivor)

    # Each connected player gets a new hero
    existing_ids = get_hero_ids()
    attempt_id = 1
    while attempt_id in existing_ids:
        attempt_id += 1
    new_hero = Hero(attempt_id, db['map'])
    db['heros'].append(new_hero)

    socker.emit('your_hero_id', {'your_hero_id': attempt_id},
                namespace='/stay_alive')
    db['total_players'] += 1
Exemplo n.º 4
0
def run_pong_inner():
    ball = get_ball()
    db = get_db()
    rally = get_rally_count()
    paddle_stats = get_paddle_stats()
    #add logic that changes the paddle sizes with upgrades
    rally['count'] += 1

    #determine if ball hits walls of space
    if ball['y'] > 400 or ball['y'] < 0:
        ball['y_s'] *= -1

    if ball['x'] > 400:
        ball['x'] = 200
        ball['y'] = 200
        ball['player_1_score'] += 1
        rally['count'] = 0
        increment_rally(paddle_stats, 'p1')
        paddle_stats['p2'] = paddle_stats['defaults'].copy()
        socker.emit('recv_paddle_stats', paddle_stats, namespace='/pong')

    if ball['x'] < 0:
        ball['x'] = 200
        ball['y'] = 200
        ball['player_2_score'] += 1
        rally['count'] = 0
        increment_rally(paddle_stats, 'p2')
        paddle_stats['p1'] = paddle_stats['defaults'].copy()
        socker.emit('recv_paddle_stats', paddle_stats, namespace='/pong')

    #make ball speed faster if rally has been continuing
    if rally['count'] % 100 == 0 and rally['count'] != 0:
        ball['x_s'] *= 1.1
        ball['y_s'] *= 1.1
        socker.emit('testing', {'data': 'ball faster'}, namespace='/pong')
    elif rally['count'] == 0:
        ball['x_s'] = 5
        ball['y_s'] = 5

    #determine if ball hits paddles
    key_list = db.keys()

    for key in key_list:
        if key != 'count' and key != 'player_1_score' and key != 'player_2_score':
            cur_paddle = db[key]
            player_num = db[key]['player_number']
            x = cur_paddle['x']
            y = cur_paddle['y']
            if ball['x'] > x and ball['x'] < x + 15:

                if player_num == 1:
                    if ball['y'] > y and ball[
                            'y'] < y + paddle_stats['p1']['ysize']:
                        ball['x_s'] *= -1
                else:
                    if ball['y'] > y and ball[
                            'y'] < y + paddle_stats['p2']['ysize']:
                        ball['x_s'] *= -1

    ball['x'] += ball['x_s']
    ball['y'] += ball['y_s']

    socker.emit('recieve_ball_loc', ball, namespace='/pong')
Exemplo n.º 5
0
def handle_connect():
    print('THE HANDLE CONNECT FUNCTION WAS WRITTEN')
    socker.emit('testing', {'hello': 'hi'}, namespace='/pong')

    print('Connected to stay_alive')
    socker.emit('con_test', {'stay alive': 'from pong'}, namespace='/pong')
Exemplo n.º 6
0
def handle_being_sent(ball_loc):
    socker.emit('recieve_ball_loc', ball_loc, namespace='/pong')
Exemplo n.º 7
0
def handle_chatting(data):
    socker.emit('recv_chat', data, namespace='/pong')
Exemplo n.º 8
0
def handle_move_request(data):
    data_base = get_db()
    data_base[data['id']]['y'] += data['y']

    socker.emit('all_info', data_base, namespace='/pong')
Exemplo n.º 9
0
def game_loop():

    db = get_db()

    #
    # Client input layer ---------------------------------------
    #

    #
    # Process if inputs are valid ------------------------------
    #

    #
    # Do game logic --------------------------------------------
    #
    for hero in db['heros']:
        hero.update()

    db['daylight'].progress()

    # Remove depleated resources
    for resource_node in db['resource_nodes']:
        resource_node.update()
        if resource_node.amount == 0:
            db['resource_nodes'].remove(resource_node)

    # 10% chance of a new resource node as long as there are less than 15 on the map
    if random.random() < 0.05 and len(db['resource_nodes']) < 10:
        db['resource_nodes'].append(
            ResourceNode(random.choice(list(ResourceNode.color_dict.keys())),
                         db['map']))

    #
    # Extraction Layer ----------------------------------------
    #
    # Get all data that needs to be sent to clients
    heros_data = []
    for hero in db['heros']:
        heros_data.append(hero.export())

    daylight_data = db['daylight'].export()

    resource_nodes_data = []
    for resource_node in db['resource_nodes']:
        resource_nodes_data.append(resource_node.export())

    structures_data = []
    for structure in db['structures']:
        structures_data.append(structure.export())

    #
    # Replication layer ---------------------------------------
    #
    # Send all data to clients
    # TO DO only send data to players if that object is within 400 distance units from them
    socker.emit('hero_data', {'hero_data': heros_data},
                namespace='/stay_alive')
    socker.emit('daylight_data', {'daylight_data': daylight_data},
                namespace='/stay_alive')
    socker.emit('resource_nodes_data',
                {'resource_nodes_data': resource_nodes_data},
                namespace='/stay_alive')
    socker.emit('structure_data', {'structure_data': structures_data},
                namespace='/stay_alive')