<commit_before>package jsettlers.main;

import jsettlers.common.map.MapLoadException;
import jsettlers.graphics.ISettlersGameDisplay;
import jsettlers.graphics.action.Action;
import jsettlers.graphics.action.EActionType;
import jsettlers.graphics.map.IMapInterfaceListener;
import jsettlers.graphics.map.MapInterfaceConnector;
import jsettlers.graphics.map.UIState;
import jsettlers.graphics.map.draw.ImageProvider;
import jsettlers.graphics.progress.EProgressState;
import jsettlers.graphics.progress.ProgressConnector;
import jsettlers.input.GuiInterface;
import jsettlers.input.GuiTaskExecutor;
import jsettlers.logic.map.newGrid.MainGrid;
import jsettlers.logic.timer.MovableTimer;
import jsettlers.logic.timer.Timer100Milli;
import network.NetworkManager;
import random.RandomSingleton;
import synchronic.timer.NetworkTimer;

/**
 * This is a running jsettlers game. It can be started and then stopped once.
 * 
 * @author michael
 */
public class JSettlersGame {

	private final long randomSeed;

	private boolean stopped = false;
	private final Object stopMutex = new Object();
	private final ISettlersGameDisplay content;
	private IGameCreator mapcreator;
	private final NetworkManager networkManager;
	private final byte playerNumber;

	private Listener listener;
	private MapInterfaceConnector gameConnector;
	private boolean started = false;

	public JSettlersGame(ISettlersGameDisplay content, IGameCreator map, long randomSeed, NetworkManager networkManager, byte playerNumber) {
		this.content = content;
		this.mapcreator = map;
		this.randomSeed = randomSeed;
		this.networkManager = networkManager;
		this.playerNumber = playerNumber;
	}

	/**
	 * Starts the game in a new thread. Returns immeadiately
	 */
	public synchronized void start() {
		if (!started) {
			started = true;
			new Thread(new GameRunner()).start();
		}
	}

	private class GameRunner implements Runnable, IMapInterfaceListener {

		@Override
		public void run() {
			ProgressConnector progress = content.showProgress();

			NetworkTimer.get().setPausing(true);
			RandomSingleton.load(randomSeed);


			progress.setProgressState(EProgressState.LOADING_MAP, 0.1f);

			ImageProvider.getInstance().startPreloading();

			MainGrid grid;

			UIState uiState;
			try {
				grid = mapcreator.getMainGrid();
				uiState = mapcreator.getUISettings(playerNumber);
				mapcreator = null;
			} catch (MapLoadException e1) {
				e1.printStackTrace();
				listener.gameEnded();
				return;
			}

			NetworkTimer.get().setPausing(false);

			/** random map */
			// RandomMapFile file =
			// RandomMapFile.getByName(mapSettings.getMap().getName());
			// RandomMapEvaluator evaluator = new
			// RandomMapEvaluator(file.getInstructions(), (byte)
			// mapSettings.getPlayerCount());
			// evaluator.createMap(RandomSingleton.get());
			// IMapData mapGrid = evaluator.getGrid();

			// GameSerializer gameSerializer = new GameSerializer();
			// try {
			// grid = gameSerializer.load();
			// } catch (Exception e) {
			// e.printStackTrace();
			// grid = null;
			// }

			progress.setProgressState(EProgressState.LOADING_IMAGES, 0.8f);

			final MapInterfaceConnector connector = content.showGameMap(grid.getGraphicsGrid(), null);
			GuiInterface guiInterface = new GuiInterface(connector, networkManager, grid.getGuiInputGrid(), playerNumber);

			connector.addListener(this);
			connector.loadUIState(uiState);

			networkManager.startGameTimer(new GuiTaskExecutor(grid.getGuiInputGrid(), guiInterface));

			gameConnector = connector;

			synchronized (stopMutex) {
				while (!stopped) {
					try {
						stopMutex.wait();
					} catch (InterruptedException e) {
					}
				}
			}

			networkManager.stop();
			Timer100Milli.stop();
			MovableTimer.stop();
			grid.stopThreads();

			listener.gameEnded();
		}

		@Override
		public void action(Action action) {
			if (action.getActionType() == EActionType.EXIT) {
				stop();
			}
		}

	}

	public void stop() {
		synchronized (stopMutex) {
			stopped = true;
			stopMutex.notifyAll();
		}
	}

	/**
	 * Defines a listener for this game.
	 * 
	 * @param managedJSettlers
	 */
	public void setListener(Listener listener) {
		this.listener = listener;
	}

	public interface Listener {
		void gameEnded();
	}

	public boolean isPaused() {
		return true; // TODO: how do we know that the game is paused
	}
<commit_msg>Gave the game thread a name.<commit_after>package jsettlers.main;

import jsettlers.common.map.MapLoadException;
import jsettlers.graphics.ISettlersGameDisplay;
import jsettlers.graphics.action.Action;
import jsettlers.graphics.action.EActionType;
import jsettlers.graphics.map.IMapInterfaceListener;
import jsettlers.graphics.map.MapInterfaceConnector;
import jsettlers.graphics.map.UIState;
import jsettlers.graphics.map.draw.ImageProvider;
import jsettlers.graphics.progress.EProgressState;
import jsettlers.graphics.progress.ProgressConnector;
import jsettlers.input.GuiInterface;
import jsettlers.input.GuiTaskExecutor;
import jsettlers.logic.map.newGrid.MainGrid;
import jsettlers.logic.timer.MovableTimer;
import jsettlers.logic.timer.Timer100Milli;
import network.NetworkManager;
import random.RandomSingleton;
import synchronic.timer.NetworkTimer;

/**
 * This is a running jsettlers game. It can be started and then stopped once.
 *
 * @author michael
 */
public class JSettlersGame {

	private final long randomSeed;

	private boolean stopped = false;
	private final Object stopMutex = new Object();
	private final ISettlersGameDisplay content;
	private IGameCreator mapcreator;
	private final NetworkManager networkManager;
	private final byte playerNumber;

	private Listener listener;
	private MapInterfaceConnector gameConnector;
	private boolean started = false;

	public JSettlersGame(ISettlersGameDisplay content, IGameCreator map, long randomSeed, NetworkManager networkManager, byte playerNumber) {
		this.content = content;
		this.mapcreator = map;
		this.randomSeed = randomSeed;
		this.networkManager = networkManager;
		this.playerNumber = playerNumber;
	}

	/**
	 * Starts the game in a new thread. Returns immeadiately
	 */
	public synchronized void start() {
		if (!started) {
			started = true;
			new Thread(null, new GameRunner(), "game", 128 * 1024).start();
		}
	}

	private class GameRunner implements Runnable, IMapInterfaceListener {

		@Override
		public void run() {
			ProgressConnector progress = content.showProgress();

			NetworkTimer.get().setPausing(true);
			RandomSingleton.load(randomSeed);


			progress.setProgressState(EProgressState.LOADING_MAP, 0.1f);

			ImageProvider.getInstance().startPreloading();

			MainGrid grid;

			UIState uiState;
			try {
				grid = mapcreator.getMainGrid();
				uiState = mapcreator.getUISettings(playerNumber);
				mapcreator = null;
			} catch (MapLoadException e1) {
				e1.printStackTrace();
				listener.gameEnded();
				return;
			}

			NetworkTimer.get().setPausing(false);

			/** random map */
			// RandomMapFile file =
			// RandomMapFile.getByName(mapSettings.getMap().getName());
			// RandomMapEvaluator evaluator = new
			// RandomMapEvaluator(file.getInstructions(), (byte)
			// mapSettings.getPlayerCount());
			// evaluator.createMap(RandomSingleton.get());
			// IMapData mapGrid = evaluator.getGrid();

			// GameSerializer gameSerializer = new GameSerializer();
			// try {
			// grid = gameSerializer.load();
			// } catch (Exception e) {
			// e.printStackTrace();
			// grid = null;
			// }

			progress.setProgressState(EProgressState.LOADING_IMAGES, 0.8f);

			final MapInterfaceConnector connector = content.showGameMap(grid.getGraphicsGrid(), null);
			GuiInterface guiInterface = new GuiInterface(connector, networkManager, grid.getGuiInputGrid(), playerNumber);

			connector.addListener(this);
			connector.loadUIState(uiState);

			networkManager.startGameTimer(new GuiTaskExecutor(grid.getGuiInputGrid(), guiInterface));

			gameConnector = connector;

			synchronized (stopMutex) {
				while (!stopped) {
					try {
						stopMutex.wait();
					} catch (InterruptedException e) {
					}
				}
			}

			networkManager.stop();
			Timer100Milli.stop();
			MovableTimer.stop();
			grid.stopThreads();

			listener.gameEnded();
		}

		@Override
		public void action(Action action) {
			if (action.getActionType() == EActionType.EXIT) {
				stop();
			}
		}

	}

	public void stop() {
		synchronized (stopMutex) {
			stopped = true;
			stopMutex.notifyAll();
		}
	}

	/**
	 * Defines a listener for this game.
	 *
	 * @param managedJSettlers
	 */
	public void setListener(Listener listener) {
		this.listener = listener;
	}

	public interface Listener {
		void gameEnded();
	}

	public boolean isPaused() {
		return true; // TODO: how do we know that the game is paused
	}

{'fraction_non_alphanumeric': 0.10571526743786508, 'fraction_numerical': 0.003550566945367083, 'mean_word_length': 4.2162485065710875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4547939', 'n_tokens_mistral': 3039, 'n_tokens_neox': 2601, 'n_words': 583}