Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 55 additions & 17 deletions venv/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def index_page():
return render_template('index.html', default_people=DATA.DEFAULT_SIZE)


@app.route('/gameList')
def gameList_page():
return render_template('gameList.html', filteredGames =DATA.filteredGames)

@app.route('/checkGameOver', methods=["POST"])
def check_game_over():
''' Given a game ID, checks if the spy was discovered or not based on
Expand All @@ -27,10 +31,11 @@ def check_game_over():
def new_game():
''' Creates a new game '''
# make new game

print('test1')
# L[0] = numPlayers
# L[1] = isFancy
L = request.get_json()
print(L)
isFancy = L[1]
try:
num_players = int(L[0])
Expand Down Expand Up @@ -73,7 +78,6 @@ def new_game():
@app.route('/game/<id_num>')
def join_game(id_num):
''' Connects user to existing game - if possible '''
print(id_num)
try:
id_num = int(id_num)
except:
Expand Down Expand Up @@ -121,7 +125,7 @@ def __init__(self):
self.GAME_CAP = 10
self.DEFAULT_SIZE = 5
self.MAX_SIZE = 100

self.activePlayers = {}
self.adjectives_list = []
self.colors_list = []
self.locations_dict = {}
Expand All @@ -131,6 +135,7 @@ def __init__(self):
self.playerList = {}
self.playerID = 0
self.MAX_PLAYER_ID = 10000
self.filteredGames = []

def load_json_adjectives(self):
''' Loads all of the adjectives and colors from the external info.JSON
Expand All @@ -157,8 +162,7 @@ def next_game_id(self):

class Game:
''' Each game will be an instance of this object '''
def __init__(self, num_people, fancy=True,
location_type="random", time_limit=0, location=None):
def __init__(self, num_people, fancy=True, location_type="random", time_limit=0, location=None):
''' Initializer for a new game '''
self.num_people = num_people
self.current_players = 0
Expand Down Expand Up @@ -192,6 +196,9 @@ def join_game(self, gameid):

session['gameid'] = gameid
session['userid'] = self.current_players
if 'playerID' in session:
activePlayers[session['playerID']] = {session['gameid']}


# When a new person joins a game, we need to increment the number of
# people in the game, and then give them a role that is available
Expand Down Expand Up @@ -248,11 +255,30 @@ def gpsdata():
data = request.data
playerID = getID()
DATA.playerList[playerID] = {data}
print(playerID)
session['playerID'] = playerID
#newDistEntry = updatePlayerMatrix(playerID)
#DATA.distanceMatrix.append(newDistEntry)
return "Player is in the database"
newDistEntry = updatePlayerMatrix(playerID)
DATA.distanceMatrix.append(newDistEntry)
players = findUsersWithinXMiles(playerID,500)
activeUsers = filterIDs(players)
activeGames = findGames(activeUsers)
return "Player Database Updated"

def findGames(users):
gameIDs = []
DATA.filteredGames = []
for i in range(len(users)):
tempGame = DATA.activePlayers[users[i]]
gameIDs.append(tempGame)
DATA.filteredGames.append(gameIDs[i])
return gameIDs


def filterIDs(users):
activeUsers = []
for i in range(len(users)):
if users[i] in list(DATA.activePlayers.keys()):
activeUsers.append(users[i])
return activeUsers

def getID():
'''Takes the data variable playerID, assigns it to a user, and
Expand All @@ -268,22 +294,35 @@ def updateNumPlayers():
DATA.numPlayers = len(DATA.playerList.keys())
return

''' def updatePlayerMatrix(playerID):
def updatePlayerMatrix(playerID):
keyList = list(DATA.playerList.keys())
print(keyList)
if len(keyList) == 1:
keyList = []
else:
keyList.remove(playerID)
print(keyList)
if len(keyList) <= 0:
return
newPlayerMatrix = []
for i in range (DATA.numPlayers):
print(i)
newPlayerMatrix[i] = calculateDist(playerID,keyList[i])
newPlayerMatrix.append(calculateDist(playerID,keyList[i]))
return newPlayerMatrix
'''

def findUsersWithinXMiles(playerID, x):
keyList = list(DATA.playerList.keys())
playerIndexinMatrix = keyList.index(playerID)
outputPlayers = []
for i in range (DATA.numPlayers):
if i<playerIndexinMatrix:
if DATA.distanceMatrix[playerIndexinMatrix][i] < x:
closePlayer = keyList[i]
outputPlayers.append(closePlayer)
else:
if DATA.distanceMatrix[i+1][playerIndexinMatrix] < x:
closePlayer = keyList[i+1]
outputPlayers.append(closePlayer)
return outputPlayers


def calculateDist(userID1,userID2):
'''Finds all the locations in the list that are within x miles of
user. Uses the formula from '''
Expand All @@ -300,7 +339,6 @@ def calculateDist(userID1,userID2):
lat2 = gps2[0]
lon2 = gps2[1]
distance = distFromLats(lat1,lon1,lat2,lon2)
print(distance)
return distance

def degreesToRadians(degrees):
Expand Down Expand Up @@ -361,4 +399,4 @@ def testing():
DATA.load_json_roles()
DATA.debugging = debugging

app.run(host='0.0.0.0')
app.run(host='127.0.0.1')
80 changes: 80 additions & 0 deletions venv/Templates/gameList.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<html>
<head>
<title>SAD</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<p id="mapholder">
Below are the list of games people around you are active in!
</p>
<p id="game1"> Game 1
</p><p id="game2"> Game 2
</p><p id="game3"> Game 3
</p><p id="game4"> Game 4
</p><p id="game5"> Game 5
</p><p id="game6"> Game 6
</p><p id="game7"> Game 7
</p><p id="game8"> Game 8
</p><p id="game9"> Game 9
</p><p id="game10"> Game 10
</p>
<script>
var games = {{ filteredGames }};
var arrayLength = games.length;
for (var i = 0; i < arrayLength; i++) {
displayGame(games[i])
}


function displayGame(gameID,i){
document.getElementById("game"+i).innerHTML = "/game/"+gameID
}


var outputgames = document.getElementById("gps");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
navigator.geolocation.getCurrentPosition(showMap);
navigator.geolocation.getCurrentPosition(sendPosition);
} else {
outputgames.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
outputgames.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}

function showMap(position) {
var latlon = position.coords.latitude + "," + position.coords.longitude;
var img_url = "https://maps.googleapis.com/maps/api/staticmap?center="+latlon+"&zoom=14&size=400x300&sensor=false&key=AIzaSyCUa7DrgBVZwYBVJOFrpjauc7DQ64h9D_Y";
document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>" + "<br/> Searching for players... within a few miles";
}

function sendPosition(position){
/*
var http = new XMLHttpRequest();
var url = "198.199.94.88:5000/gpsdata";
var params = 'lat=' +position.coords.latitude+'&amp;lon='+position.coords.longitude
http.open("POST", url, true);
http.send(params);
*/
$.ajax({
type : "POST",
url : "http://127.0.0.1:5000/gpsdata",
data: JSON.stringify(position.coords),
contentType: 'application/json;charset=UTF-8',
success: function(result) {
console.log(result);
}
}
);
console.log(position.coords.latitude);
console.log(position.coords.longitude)
}

</script>
</body>
</html>
7 changes: 0 additions & 7 deletions venv/templates/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ <h1>
Enter Your Guess <input type="text">
<button>Confirm </button>
</h1>
<p id="gps">Latitude: ??? <br/>
Longitude: ???
</p>
<button onclick="getLocation()">Get Location</button>
<p id="mapholder">
This is a placeholder for the badass goddamn map that will appear here
</p>
</body>
<script>
var x = document.getElementById("gps");
Expand Down
18 changes: 12 additions & 6 deletions venv/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ <h5 class="modal-title" id="exampleModalLabel">Enter the Game ID</h5>
</button>
</div>
<div class="col">
<button class="btn btn-lg btn-danger" onclick="getLan()">
See Local Games
<button class="btn btn-lg btn-danger" onclick="getLocation()">
Find Games
</button>
</div>
<div class="col">
Expand Down Expand Up @@ -154,7 +154,12 @@ <h5 class="modal-title" id="exampleModalLabel">Enter the Game ID</h5>
</div>
</div>
<div class="col-sm-1">

<p id="gps">Latitude: ??? <br/>
Longitude: ???
</p>
<p id="mapholder">
This is a placeholder for the badass goddamn map that will appear here
</p>
</div>
</div>
</div>
Expand Down Expand Up @@ -226,16 +231,17 @@ <h5 class="modal-title" id="exampleModalLabel">Enter the Game ID</h5>

var x = document.getElementById("gps");
function getLocation() {
var x = document.getElementById("gps");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
navigator.geolocation.getCurrentPosition(showMap);
navigator.geolocation.getCurrentPosition(sendPosition);
navigator.geolocation.getCurrentPosition(sendID);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var x = document.getElementById("gps");
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
Expand All @@ -253,7 +259,7 @@ <h5 class="modal-title" id="exampleModalLabel">Enter the Game ID</h5>
data: latlon,
contentType: 'application/json;charset=UTF-8',
success: function(result) {
console.log(result);
document.location.href = "/gameList";
}
}
);
Expand All @@ -262,6 +268,6 @@ <h5 class="modal-title" id="exampleModalLabel">Enter the Game ID</h5>
}
</script>

</script>
</body>

</html>