<commit_before>part of coUserver;

//handle player update events
class PlayerUpdateHandler
{
	static Map<String,Identifier> users = {};

	static void handle(WebSocket ws)
	{
		ws.listen((message)
		{
			processMessage(ws, message);
	    },
		onError: (error)
		{
			cleanupList(ws);
		},
		onDone: ()
		{
			cleanupList(ws);
		});
	}

	static void cleanupList(WebSocket ws)
	{
		String leavingUser;

		users.forEach((String username, Identifier id)
		{
			if(ws == id.webSocket)
			{
				id.webSocket = null;
				leavingUser = username;
			}
		});

		Identifier leavingID = users.remove(leavingUser);
		if(leavingID != null)
		{
			Map map = new Map();
			map["disconnect"] = "true";
			map["username"] = leavingUser;
			map["street"] = leavingID.currentStreet;
			sendAll(map);
		}
	}

	static Future processMessage(WebSocket ws, String message)
	{
		try
		{
			Completer c = new Completer();
			Map map = JSON.decode(message);

			if(map['clientVersion'] != null)
			{
				if(map['clientVersion'] < minClientVersion)
					ws.add(JSON.encode({'error':'version too low'}));
				c.complete();
			}
			else
			{
				String username = map["username"];
    			if(users[username] != null) { //we've had an update for this user before
    				if(users[username].currentStreet != map["street"]) {
					    //the user must have switched streets
    					map["changeStreet"] = map["street"];
    					users[username].currentStreet = map["street"];
					    map["letter"] = PLAYER_LETTERS.newPlayerLetter(username);
    				} else {
					    map["letter"] = PLAYER_LETTERS.getPlayerLetter(username);
				    }

				    try {
	    				num prevX = users[username].currentX;
	    				num prevY = users[username].currentY;
	    				num currentX = num.parse(map['xy'].split(',')[0]);
	    				num currentY = num.parse(map['xy'].split(',')[1]);
	    				num xDiff = (currentX-prevX).abs();
	    				num yDiff = (currentY-prevY).abs();
	    				StatBuffer.incrementStat("stepsTaken", (xDiff+yDiff)/22);
	    				users[username].currentX = currentX;
	    				users[username].currentY = currentY;<commit_msg>send previousStreet along with changeStreet message<commit_after>part of coUserver;

//handle player update events
class PlayerUpdateHandler
{
	static Map<String,Identifier> users = {};

	static void handle(WebSocket ws)
	{
		ws.listen((message)
		{
			processMessage(ws, message);
	    },
		onError: (error)
		{
			cleanupList(ws);
		},
		onDone: ()
		{
			cleanupList(ws);
		});
	}

	static void cleanupList(WebSocket ws)
	{
		String leavingUser;

		users.forEach((String username, Identifier id)
		{
			if(ws == id.webSocket)
			{
				id.webSocket = null;
				leavingUser = username;
			}
		});

		Identifier leavingID = users.remove(leavingUser);
		if(leavingID != null)
		{
			Map map = new Map();
			map["disconnect"] = "true";
			map["username"] = leavingUser;
			map["street"] = leavingID.currentStreet;
			sendAll(map);
		}
	}

	static Future processMessage(WebSocket ws, String message)
	{
		try
		{
			Completer c = new Completer();
			Map map = JSON.decode(message);

			if(map['clientVersion'] != null)
			{
				if(map['clientVersion'] < minClientVersion)
					ws.add(JSON.encode({'error':'version too low'}));
				c.complete();
			}
			else
			{
				String username = map["username"];
    			if(users[username] != null) { //we've had an update for this user before
				    String previousStreet = users[username].currentStreet;
    				if(previousStreet != map["street"]) {
					    //the user must have switched streets
    					map["changeStreet"] = map["street"];
					    map['previousStreet'] = previousStreet;
    					users[username].currentStreet = map["street"];
					    map["letter"] = PLAYER_LETTERS.newPlayerLetter(username);
    				} else {
					    map["letter"] = PLAYER_LETTERS.getPlayerLetter(username);
				    }

				    try {
	    				num prevX = users[username].currentX;
	    				num prevY = users[username].currentY;
	    				num currentX = num.parse(map['xy'].split(',')[0]);
	    				num currentY = num.parse(map['xy'].split(',')[1]);
	    				num xDiff = (currentX-prevX).abs();
	    				num yDiff = (currentY-prevY).abs();
	    				StatBuffer.incrementStat("stepsTaken", (xDiff+yDiff)/22);
	    				users[username].currentX = currentX;
	    				users[username].currentY = currentY;
{'fraction_non_alphanumeric': 0.1479248782749826, 'fraction_numerical': 0.0018548574078367725, 'mean_word_length': 2.972375690607735, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '4219786', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1402, 'n_words': 277}