<commit_before>package jdungeonquest.network;

import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.minlog.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import jdungeonquest.enums.ClientState;
import static jdungeonquest.enums.NetworkMessageType.RegistrationRequest;
import jdungeonquest.gui.GUI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NetworkClient implements Runnable {

    private Client client;
    private int serverPort;
    private String serverIP;
    private GUI gui;
    private ClientState state = ClientState.NOT_CONNECTED;
    //list of players connected to this client
    List<PlayerData> players = new ArrayList<>();
    Logger logger = LoggerFactory.getLogger(NetworkClient.class);

    public NetworkClient(String ip, int port, GUI gui) {
        client = new Client();
        Network.registerClasses(client);
        Log.set(Log.LEVEL_DEBUG);
        
        this.serverIP = ip;
        this.serverPort = port;
        this.gui = gui;
        logger.debug("NetworkClient " + this.serverIP + ":" + this.serverPort);
    }

    public void connectToServer() {
        try {
            client.connect(5000, this.serverIP, this.serverPort);
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(NetworkClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (!client.isConnected()) {
            return;
        }
        sendMessage("he");
    }
    
    public void registerOnServer(String name) {
        sendMessage(new RegistrationRequest(name));
    }

    public void sendMessage(Message msg) {
        int a = this.client.sendTCP(msg);
        logger.debug("Sent message " + msg + ", the size is " + a + " bytes.");
    }
    
    public void sendMessage(String msg) {
        int a = this.client.sendTCP(msg);
        logger.debug("Sent string " + msg + ", the size is " + a + " bytes.");
    }
    
    private void changeState(ClientState newState) {
        logger.debug("Changing game state from " + state + " to " + newState);
        state = newState;
    }

    @Override
    public void run() {
        client.addListener(new Listener() {
            @Override
            public void connected(Connection connection) {
                logger.debug("Connected to " + connection);
            }

            @Override
            public void disconnected(Connection connection) {
                changeState(ClientState.NOT_CONNECTED);
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            logger.debug("Reconnecting.");
                            client.reconnect();
                            //send name of all players on this client
                            //client.sendTCP(new PlayerList(players));
                        } catch (IOException ex) {
                            logger.debug(ex.toString());
                        }
                    }
                }.start();

                logger.debug("Disconnected");
            }

            @Override
            public void received(Connection c, Object object) {
                logger.debug("Recieved package: " + object);
                if (object instanceof String) {
                    String obj = (String)object;
                    if(obj.equals("lo")){
                        logger.debug("Confirmed connection with a server.");
                        changeState(ClientState.IN_LOBBY);
                        gui.showLobby();
                    }
                }else if (object instanceof Message) {
                    switch (((Message) object).msgType) {
                        
                        case RegistrationRequest:
                            String playerName = ((RegistrationRequest)object).getName();
                            if (havePlayer(playerName)) {
                                gui.playerRegistered(true, playerName);
                            } else {
                                gui.playerRegistered(false, playerName);
                            }
                            break;
                            <commit_msg>Client should send list of its players on reconnect<commit_after>package jdungeonquest.network;

import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.minlog.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import jdungeonquest.enums.ClientState;
import static jdungeonquest.enums.NetworkMessageType.RegistrationRequest;
import jdungeonquest.gui.GUI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NetworkClient implements Runnable {

    private Client client;
    private int serverPort;
    private String serverIP;
    private GUI gui;
    private ClientState state = ClientState.NOT_CONNECTED;
    //list of players connected to this client
    List<PlayerData> players = new ArrayList<>();
    Logger logger = LoggerFactory.getLogger(NetworkClient.class);

    public NetworkClient(String ip, int port, GUI gui) {
        client = new Client();
        Network.registerClasses(client);
        Log.set(Log.LEVEL_DEBUG);
        
        this.serverIP = ip;
        this.serverPort = port;
        this.gui = gui;
        logger.debug("NetworkClient " + this.serverIP + ":" + this.serverPort);
    }

    public void connectToServer() {
        try {
            client.connect(5000, this.serverIP, this.serverPort);
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(NetworkClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (!client.isConnected()) {
            return;
        }
        sendMessage("he");
    }
    
    public void registerOnServer(String name) {
        sendMessage(new RegistrationRequest(name));
    }

    public void sendMessage(Message msg) {
        int a = this.client.sendTCP(msg);
        logger.debug("Sent message " + msg + ", the size is " + a + " bytes.");
    }
    
    public void sendMessage(String msg) {
        int a = this.client.sendTCP(msg);
        logger.debug("Sent string " + msg + ", the size is " + a + " bytes.");
    }
    
    private void changeState(ClientState newState) {
        logger.debug("Changing game state from " + state + " to " + newState);
        state = newState;
    }

    @Override
    public void run() {
        client.addListener(new Listener() {
            @Override
            public void connected(Connection connection) {
                logger.debug("Connected to " + connection);
            }

            @Override
            public void disconnected(Connection connection) {
                changeState(ClientState.NOT_CONNECTED);
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            logger.debug("Reconnecting.");
                            client.reconnect();

                            String[] playerList = new String[players.size()];
                            for (int i = 0; i < players.size(); i++) {
                                playerList[i] = players.get(i).getName();
                            }
                            sendMessage(new PlayerList(playerList));
                        } catch (IOException ex) {
                            logger.debug(ex.toString());
                        }
                    }
                }.start();

                logger.debug("Disconnected");
            }

            @Override
            public void received(Connection c, Object object) {
                logger.debug("Recieved package: " + object);
                if (object instanceof String) {
                    String obj = (String)object;
                    if(obj.equals("lo")){
                        logger.debug("Confirmed connection with a server.");
                        changeState(ClientState.IN_LOBBY);
                        gui.showLobby();
                    }
                }else if (object instanceof Message) {
                    switch (((Message) object).msgType) {
                        
                        case RegistrationRequest:
                            String playerName = ((RegistrationRequest)object).getName();
                            if (havePlayer(playerName)) {
                                gui.playerRegistered(true, playerName);
                            } else {
                                gui.playerRegistered(false, playerName);
                            }
                            break;
                            
{'fraction_non_alphanumeric': 0.0949027498323273, 'fraction_numerical': 0.001453163424994411, 'mean_word_length': 1.5888310185185186, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '976088', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2147, 'n_words': 497}