Untitled

 avatar
unknown
plain_text
9 months ago
3.3 kB
10
Indexable
import socket
import threading
import random
import time

# Configuration
HOST = '0.0.0.0'
PORT = 5555
MAX_PLAYERS = 4
TOPICS = [
    "Climate Change",
    "AI Regulation",
    "Global Refugee Crisis",
    "Nuclear Non-Proliferation",
    "Economic Inequality"
]

# Store clients
clients = []
countries = []
player_names = []

# Send message to all clients
def broadcast(message):
    for client in clients:
        try:
            client.send(message.encode())
        except:
            pass

# Handle a single client
def handle_client(client):
    while True:
        try:
            message = client.recv(1024).decode()
            if message.startswith("NAME:"):
                name = message.split(":")[1]
                player_names.append(name)
                broadcast(f"SERVER: {name} has joined the debate!")
            elif message.startswith("COUNTRY:"):
                country = message.split(":")[1]
                countries.append(country)
            else:
                broadcast(message)
        except:
            index = clients.index(client)
            broadcast(f"SERVER: {player_names[index]} has left the debate.")
            clients.remove(client)
            client.close()
            break

# Main server loop
def start_server():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((HOST, PORT))
    server.listen(MAX_PLAYERS)
    print(f"[SERVER STARTED] Waiting for players on {HOST}:{PORT}...")

    # Assign AI if slots empty
    ai_countries = ["AI_USA", "AI_China", "AI_Brazil", "AI_Europe"]
    
    def ai_turns():
        while True:
            time.sleep(20)  # AI speaks every 20 seconds
            if len(countries) < MAX_PLAYERS:
                ai_name = random.choice(ai_countries)
                ai_country = ai_name
                ai_message = f"{ai_country}: I strongly support international cooperation on {random.choice(TOPICS)}."
                broadcast(ai_message)
    
    threading.Thread(target=ai_turns, daemon=True).start()

    while len(clients) < MAX_PLAYERS:
        client, address = server.accept()
        print(f"[NEW CONNECTION] {address} connected.")
        clients.append(client)
        threading.Thread(target=handle_client, args=(client,)).start()

    broadcast("SERVER: Debate will start shortly!")
    time.sleep(3)
    broadcast("SERVER: Debate Topic: " + random.choice(TOPICS))
    broadcast("SERVER: You may now start your opening statements.")

start_server()import socket
import threading

# Server IP and port
SERVER_IP = input("Enter host IP: ")
PORT = 5555

# Connect to server
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((SERVER_IP, PORT))

# Choose name and country
name = input("Enter your name: ")
country = input("Enter your country: ")
client.send(f"NAME:{name}".encode())
client.send(f"COUNTRY:{country}".encode())

# Listen for messages from server
def receive():
    while True:
        try:
            message = client.recv(1024).decode()
            print(message)
        except:
            print("Disconnected from server.")
            break

# Send messages to server
def write():
    while True:
        msg = input()
        client.send(f"{country}: {msg}".encode())

# Start threads
threading.Thread(target=receive).start()
threading.Thread(target=write).start()
Editor is loading...
Leave a Comment