import pickle
import socket
import _thread
from scripts.multiplayer import game, board, tetriminos

server = "192.168.29.144"
port = 5555

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.bind((server, port))
except socket.error as e:
    print(e)

s.listen()
print("Waiting for connection")

connected = set()
games = {}
idCount = 0


def threaded_client(conn, p, gameId):
    global idCount
    conn.send(str.encode(str(p)))

    reply = ""
    while True:
        try:
            data = conn.recv(4096).decode()

            if gameId in games:
                game = games[gameId]

                if not data:
                    break
                else:
                    game.update(p, data)

                    reply = game
                    conn.sendall(pickle.dumps(reply))
            else:
                break
        except:
            break

    print("Lost Connection!")

    try:
        del games[gameId]
        print("Closing Game", gameId)
    except:
        pass
    idCount -= 1
    conn.close()


while True:
    conn, addr = s.accept()
    print("Connected to: ", addr)

    idCount += 1
    p = 0
    game_id = (idCount - 1) // 2

    if idCount % 2 == 1:
        games[game_id] = game.Game((0, 0, 0), None, board)
    else:
        games[game_id].ready = True
        p = 1

    _thread.start_new_thread(threaded_client, (conn, p, game_id))

[(9, 'socket.socket', 'socket.socket', False, 'import socket\n'), (73, '_thread.start_new_thread', '_thread.start_new_thread', False, 'import _thread\n'), (68, 'scripts.multiplayer.game.Game', 'game.Game', False, 'from scripts.multiplayer import game, board, tetriminos\n'), (39, 'scripts.multiplayer.game.update', 'game.update', False, 'from scripts.multiplayer import game, board, tetriminos\n'), (42, 'pickle.dumps', 'pickle.dumps', False, 'import pickle\n')]