Example #1
0
def do_client(new_sock):
    """
    ~main server's loop~
    for each client -
    send his shooter and all shooters in arena after some update, and checks
    """
    me = Shooter(None, 500, 30)
    me.set_new_color()
    while not get_approval(me):
        me.set_new_color()

    me.set_random_position(X_BOUND, Y_BOUND)
    shooters.append(me)

    Done = False
    while not Done:
        try:
            to_send = shooters_to_send(me)

            if me.life <= 0:
                Done = True
                to_send = "LOS"
            elif me.life < 100:
                me.life += 0.01

            to_send = json.dumps(to_send)
            new_sock.send(to_send)

            recv = new_sock.recv(1024)

            if recv == "CLD":  #stands for - CLient Disconnected
                print "Client Disconnected"
                Done = True
            else:
                recv = json.loads(recv)
                me.angle = recv[0]
                presses = recv[1::]
                control_player(presses, me)
                me.move_shot()
                update_shooters(me)
                check_hits(me)

        except:
            print "closing current socket because of technical issues"
            Done = True
    shooters.remove(me)
    new_sock.close()