<commit_before><!DOCTYPE html>
<html>
	<head>
		<title>Mars Rover</title>
		<!--<link rel="icon" type="img/png" href="style/favicon.png" style="width:30px;">-->
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<link rel="stylesheet" type="text/css" href="assets/reset.css">
		<!-- <link rel="stylesheet" type="text/less" href="assets/main.less"> -->
		<link rel="stylesheet" type="text/css" href="assets/main.css">
		<script type="text/javascript" src="lib/promise.min.js"></script>
		<link href="https://fonts.googleapis.com/css?family=Share+Tech+Mono" rel="stylesheet">
		<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>
		<script src="lib/promise.min.js"></script>
		<script>
			// Initialize Firebase
			var config = {
				apiKey: "<KEY>",
				authDomain: "marsteam-6522c.firebaseapp.com",
				databaseURL: "https://marsteam-6522c.firebaseio.com",
				storageBucket: "marsteam-6522c.appspot.com",
				messagingSenderId: "487026784695"
			};
			firebase.initializeApp(config);
		</script>
		<style>

			html {
				background: url('assets/camera_final.png') no-repeat center center fixed; 
				-webkit-background-size: cover;
				-moz-background-size: cover;
				-o-background-size: cover;
				background-size: cover;
			}

			body {
				background: transparent;
			}

			div#entry{
				background: transparent;
			}

			div#entry, div#start {
				display: block;
				margin: 0 auto;
			}

			p {
				text-align: center;
			}

			p#form-errors {
				color: red;
			}

		</style>
	</head>

	<body>

		<div class="container">
			<div id="entry" class="panel">
				<h1>Transmission</h1>
				<p>Test your collaboration skills under pressure with a team of scientists to re-establish communication with a wary Mars rover.</p>
				<p>Create a new mission below and share the Mission ID with your friends. Players can join from laptop or mobile.</p>
			</div>
			<div id="start" class="panel">
				<h2>Start Your Team Mission</h2>
				<p id="note">For collaboration, for science.</p>
				<div id="form-join" class="center">
					<p id="form-errors"></p>
					<input id="key" type="text" placeholder="Mission ID">
					<input id="pid" type="text" placeholder="Player Name">
					<button id="join">Join Mission</button>
				</div>
				<div id="team-container" class="center hide">
					<h2>Mission: <span id="team-mid">test</span></h2>
					<div id="team-space"></div>
					<button id="enter">Enter Mission Control</button>
				</div>
			</div>
			<br>
			<p style="color: white;">Created at Boilermake IV by <NAME> and <NAME></p>
			<br>
		</div>
		
		<script type="text/javascript">

			var db = firebase.database();
			var mission_id = false;
			var player_id = false;

			function cleanse(str){
				if(str){
					return str.replace(/\W/g, '');
				}
				else{
					return false;
				}
			}

			function getQueryParams(qs) {
				qs = qs.split('+').join(' ');
				var params = {},
					tokens,
					re = /[?&]?([^=]+)=([^&]*)/g;
				while (tokens = re.exec(qs)) {
					params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
				}
				return params;
			}

			var params = getQueryParams(document.location.search);
			mission_id = cleanse(params.mid) || false;
			player_id = cleanse(params.pid) || false;

			var midInput = document.getElementById('key');
			var pidInput = document.getElementById('pid');
			var joinForm = document.getElementById('form-join');
			var errorBox = document.getElementById('form-errors');
			var teamContainer = document.getElementById('team-container');
			var teamSpace = document.getElementById('team-space');

			if(mission_id){
				midInput.value = mission_id;
			}
			if(player_id){
				pidInput.value = player_id;
			}

			function removeNode(key){
				db.ref('join/' + mission_id + '/' + key).remove();
			}

			function initTeamFormation(){
				var teamRef = db.ref('join/' + mission_id);
				teamRef.push({
					id: player_id
				});
				teamRef.on('value', function(snapshot){
					var team = snapshot.val();
					teamSpace.innerHTML = '';
					var count = 0;
					var joined = false;
					for(var i in team){
						var node = team[i];
						var player = node.id;
						if(node.start){
							window.location = 'player.html?mid=' + mission_id + '&pid=' + player_id;
							break;
						}
						else{
							if(player === player_id){
								joined = true;
							}
							var html = '<div>' + player + ' joined the mission. <button onclick="removeNode(&quot;' + i + '&quot;);">Remove</button></div>';
							teamSpace.innerHTML += html;
							count++;
						}
					}
					if(count < 1 || !joined){
						joinForm.style.display = 'block';
						teamContainer.style.display = 'none';
					}
					else{
						joinForm.style.display = 'none';
						teamContainer.style.display = 'block';
					}
				});
			}

			function checkNode(mid){
				return new Promise((resolve, reject) => {
					var free = true;
					db.ref('missions/' + mid).once('value', function(snapshot){
						if(snapshot.exists()){
							free = false;
						}
						resolve(free);
					});
				});
			}

			var joinB = document.getElementById('join');
			joinB.addEventListener('click', function(e){
				if(cleanse(midInput.value) && cleanse(pidInput.value)){
					mission_id = cleanse(midInput.value);
					player_id = cleanse(pidInput.value);
					checkNode(mission_id).then(function(free){
						if(free){
							initTeamFormation();<commit_msg>Identify and clean old unfinished games<commit_after><!DOCTYPE html>
<html>
	<head>
		<title>Mars Rover</title>
		<!--<link rel="icon" type="img/png" href="style/favicon.png" style="width:30px;">-->
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<link rel="stylesheet" type="text/css" href="assets/reset.css">
		<!-- <link rel="stylesheet" type="text/less" href="assets/main.less"> -->
		<link rel="stylesheet" type="text/css" href="assets/main.css">
		<script type="text/javascript" src="lib/promise.min.js"></script>
		<link href="https://fonts.googleapis.com/css?family=Share+Tech+Mono" rel="stylesheet">
		<script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>
		<script src="lib/promise.min.js"></script>
		<script>
			// Initialize Firebase
			var config = {
				apiKey: "<KEY>",
				authDomain: "marsteam-6522c.firebaseapp.com",
				databaseURL: "https://marsteam-6522c.firebaseio.com",
				storageBucket: "marsteam-6522c.appspot.com",
				messagingSenderId: "487026784695"
			};
			firebase.initializeApp(config);
		</script>
		<style>

			html {
				background: url('assets/camera_final.png') no-repeat center center fixed; 
				-webkit-background-size: cover;
				-moz-background-size: cover;
				-o-background-size: cover;
				background-size: cover;
			}

			body {
				background: transparent;
			}

			div#entry{
				background: transparent;
			}

			div#entry, div#start {
				display: block;
				margin: 0 auto;
			}

			p {
				text-align: center;
			}

			p#form-errors {
				color: red;
			}

		</style>
	</head>

	<body>

		<div class="container">
			<div id="entry" class="panel">
				<h1>Transmission</h1>
				<p>Test your collaboration skills under pressure with a team of scientists to re-establish communication with a wary Mars rover.</p>
				<p>Create a new mission below and share the Mission ID with your friends. Players can join from laptop or mobile.</p>
			</div>
			<div id="start" class="panel">
				<h2>Start Your Team Mission</h2>
				<p id="note">For collaboration, for science.</p>
				<div id="form-join" class="center">
					<p id="form-errors"></p>
					<input id="key" type="text" placeholder="Mission ID">
					<input id="pid" type="text" placeholder="Player Name">
					<button id="join">Join Mission</button>
				</div>
				<div id="team-container" class="center hide">
					<h2>Mission: <span id="team-mid">test</span></h2>
					<div id="team-space"></div>
					<button id="enter">Enter Mission Control</button>
				</div>
			</div>
			<br>
			<p style="color: white;">Created at Boilermake IV by <NAME> and <NAME></p>
			<br>
		</div>
		
		<script type="text/javascript">

			var db = firebase.database();
			var mission_id = false;
			var player_id = false;

			function cleanse(str){
				if(str){
					return str.replace(/\W/g, '');
				}
				else{
					return false;
				}
			}

			function getQueryParams(qs) {
				qs = qs.split('+').join(' ');
				var params = {},
					tokens,
					re = /[?&]?([^=]+)=([^&]*)/g;
				while (tokens = re.exec(qs)) {
					params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
				}
				return params;
			}

			var params = getQueryParams(document.location.search);
			mission_id = cleanse(params.mid) || false;
			player_id = cleanse(params.pid) || false;

			var midInput = document.getElementById('key');
			var pidInput = document.getElementById('pid');
			var joinForm = document.getElementById('form-join');
			var errorBox = document.getElementById('form-errors');
			var teamContainer = document.getElementById('team-container');
			var teamSpace = document.getElementById('team-space');

			if(mission_id){
				midInput.value = mission_id;
			}
			if(player_id){
				pidInput.value = player_id;
			}

			function removeNode(key){
				db.ref('join/' + mission_id + '/' + key).remove();
			}

			function initTeamFormation(){
				var teamRef = db.ref('join/' + mission_id);
				teamRef.push({
					id: player_id
				});
				teamRef.on('value', function(snapshot){
					var team = snapshot.val();
					teamSpace.innerHTML = '';
					var count = 0;
					var joined = false;
					for(var i in team){
						var node = team[i];
						var player = node.id;
						if(node.start){
							window.location = 'player.html?mid=' + mission_id + '&pid=' + player_id;
							break;
						}
						else{
							if(player === player_id){
								joined = true;
							}
							var html = '<div>' + player + ' joined the mission. <button onclick="removeNode(&quot;' + i + '&quot;);">Remove</button></div>';
							teamSpace.innerHTML += html;
							count++;
						}
					}
					if(count < 1 || !joined){
						joinForm.style.display = 'block';
						teamContainer.style.display = 'none';
					}
					else{
						joinForm.style.display = 'none';
						teamContainer.style.display = 'block';
					}
				});
			}

			var EXPIRED = 1000 * 60 * 10; // 10 minutes in ms

			function checkNode(mid){
				return new Promise((resolve, reject) => {
					var free = false;
					db.ref('missions/' + mid).once('value', function(snapshot){
						if(snapshot.exists()){
							var events = snapshot.val();
							var last = Object.keys(events).map(key => events[key]).sort((a, b) => { return b.timestamp - a.timestamp })[0];
							var diff = Date.now() - last.timestamp;
							if(diff > EXPIRED){
								db.ref('missions/' + mid).remove().then((done) => {
									free = true;
									resolve(free);
								});
							}
							else{
								console.log(diff)
								free = false;
								resolve(free);
							}
						}
						else{
							free = true;
							resolve(free);
						}
					});
				});
			}

			var joinB = document.getElementById('join');
			joinB.addEventListener('click', function(e){
				if(cleanse(midInput.value) && cleanse(pidInput.value)){
					mission_id = cleanse(midInput.value);
					player_id = cleanse(pidInput.value);
					checkNode(mission_id).then(function(free){
						if(free){
							initTeamFormation();
{'fraction_non_alphanumeric': 0.16921320688443975, 'fraction_numerical': 0.008342114506498069, 'mean_word_length': 2.9503988900450917, 'pattern_counts': {'":': 0, '<': 147, '<?xml version=': 0, '>': 151, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, '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': '441185', 'n_tokens_mistral': 4939, 'n_tokens_neox': 3723, 'n_words': 780}