Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-Dunne committed Mar 6, 2022
2 parents f98220b + e575489 commit 26ce5b4
Show file tree
Hide file tree
Showing 43 changed files with 246 additions and 9 deletions.
2 changes: 2 additions & 0 deletions RWM-P2-TEAM-C/Assets/Scripts/BulletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public void shootBullet()
bulletPrefab.GetComponent<Bullet>().bulletManager = this;
bulletPrefab.GetComponent<Bullet>().speed = bulletMoveSpeed;
bulletPrefab.GetComponent<Bullet>().lifetime = bulletLifeTime;

AnalyticsManager.instance.data.bulletsFired++;
}
}

Expand Down
1 change: 1 addition & 0 deletions RWM-P2-TEAM-C/Assets/Scripts/OnDeath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void LateUpdate()

public void hasDied()
{
AnalyticsManager.instance.data.deathCount++;
StartCoroutine("deathAnimationPlaying");
}

Expand Down
39 changes: 35 additions & 4 deletions RWM-P2-TEAM-C/Assets/Scripts/Server/AnalyticsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,48 @@
using UnityEngine;
using UnityEngine.Networking;
using System.Net;
using UnityEngine.SceneManagement;

[System.Serializable]
public class GameState
{
public int completion_time;
public int bulletsFired;
public int deathCount;
public int defeatedEnemies;
public int level;
}

public class AnalyticsManager : MonoBehaviour
{
public static IEnumerator PostMethod(GameState t_data)
public static AnalyticsManager instance = null;
public GameState data;

void Awake()
{
if (instance == null) instance = this;
else if (instance != this) DestroyImmediate(gameObject);

data.level = SceneManager.GetActiveScene().buildIndex;
DontDestroyOnLoad(gameObject);
}

/// <summary>
/// Used to reset our Singleton's data back to the start
/// To be used when a new game is started.
/// </summary>
void resetdata()
{
string jsonData = JsonUtility.ToJson(t_data);
data.bulletsFired = 0;
data.deathCount = 0;
data.defeatedEnemies = 0;
data.level = SceneManager.GetActiveScene().buildIndex;
}

public IEnumerator PostMethod()
{
string jsonData = JsonUtility.ToJson(data); // uses pre-made data object

string url = "http://localhost:5000/upload_data";
string url = "http://34.242.150.74/upload_data";
using (UnityWebRequest request = UnityWebRequest.Put(url, jsonData))
{
request.method = UnityWebRequest.kHttpVerbPOST;
Expand All @@ -32,4 +59,8 @@ public static IEnumerator PostMethod(GameState t_data)
Debug.Log("Error sending data to the server: Error " + request.responseCode);
}
}

void Start() { }

void Update() { }
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions server/Mantas_Josh.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAjiv6TR75vN9WH6KuOF7iwoMj3Kqo0Rc02Rz9fDVODQ/RGF9X
sQPCalUUk2HZAb0e00r9kfGf9s4XDCwKVnv9SZNNacA4pUMKAhSzvWITZtL/I8zM
iIp54agpl2bJ6lqYsrIMv0syIxBDE1+BB5KFJXaYvNh0lHkEfmFkNdJO8X1Rg0FA
aVFhEBj+n8fupribXabG0+17qhsH85F1q2pYvbxqxLVvQ9JeO4ZWeJs8WwQspClm
N7Ogf3uJmWxv99CQq7Mwv1lV3ozXWdkOpFQgFvRNsdwk3vUfV7WF9zg64LhMts5G
3sFNRnblTVW28AzEQTX7rZWCKrgMCZO4BMWldwIDAQABAoIBAEQwUZ/1sGBnWf5Y
X2lIEUir72JiNA/9KmrL3cRaKLJZGLew2vVa0T+AyRqbPqL32zYKbK44DrooRKOg
5+BtHaHRVbrX6BNToRQ4a7D0vkt9/cNk8eC3UhfqOPCV61hM1Zo3jaeeYFCy9wju
sIs6OkQqqr1AsEGZ/Y7EFXU/BoAKHD2xP0EvavxNOlaxCOO/hp79n9bpt7Yu1sQR
YvoCTggru/S1isqYpOEFYdGg2Twcx8zNCHeWaRokdfsJoH8uYWfDUfESA2Bvgy8L
mP+5zMjIrPev4rrkw+MfQFhRLHF9S59UoLwEVNEb5bwL8IYdvDgsBqNqtmAKQhne
E1XEhIkCgYEA00odDbBynAwkOwarHrW2QzAZFq2xMkCUfvybe3qbtB9hCn8tjo32
j1Y8c2d2M5fq4ideTF5HS0Ea26TSG5Wee7udCtu7cWILB3O+YjftEMLuRwchZaAE
v5HvgrlYUIaZdqcc0Hetv2LWLAtR/ykgjCpCXWywXLTbD/Jy8moTLLsCgYEArEGl
8g2GWHAWGgSbMN5dC2nkhBN9rB0m4Wu3ZWYqmZ6xWKlFIY/3v1e/ICYxOB8fBng5
y9R226LydivKt/+HQiEiPqUHM/JbKY4S/CU7KoJYOGz47v9q39nQxHofPUe1U2Wn
gned736/IsJ6MlUFwcUZbwmPUK8rZ7VMEtJdXHUCgYAYAkjw4Eb1sm9/gY96St2u
Hs4LBKuo8SuujlfAnTsKKbKmNbNgioMk4IC4AF6vGCkpj52ogeIKDBjYMo9pcQP0
PWpn7HYNOT3Vl/JfSMmYaP4Ydhf2a0t+1T9pFFSTuefAUtb0XVUR9VIu6Q5lNzAq
nTazVQFpV2e5FSy4nRMWtwKBgCmUEwUxJIt2Sq9vCzKpidX2cnU66yu/QaJJyNPN
7Fx2RKJ9ATXPtWJXQMf20AyW3ntGS9UabLIpS9MgUOdG45QfwU9TsEBmsICATa7V
Icnp+ndsPk4aEucDgXPbcmztQSRnNh601sd3VZWDUdGSWjhiZfg6W4CDN7vps/ob
EvP9AoGBAJpyrN11VrCgsMJGvHeu6pm9ugUFebYrBEF4T4awsG9hujI8AV/fdDPF
hdvKNOShHxZU7IGetA9KKIpm64ct73TrTBSTzuMd3XErItcF45nTDSt3eAug2VLJ
KPB7pSVV4bO3rsdZlZP3udjg2QyFcl7vFhYvrW1aV6hprg8x5Zgg
-----END RSA PRIVATE KEY-----
17 changes: 17 additions & 0 deletions server/PythonDockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use an official Python runtime as a parent image
FROM python:3.8

RUN mkdir /app

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD /requirements.txt .

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Run app.py when the container launches
# Note -u means unbuffered, otherwise there is no output to the logs
CMD ["python", "-u", "./basic_api.py"]
Binary file added server/__pycache__/api_basic_upload.cpython-38.pyc
Binary file not shown.
Binary file added server/__pycache__/game_analytics.cpython-38.pyc
Binary file not shown.
Binary file added server/__pycache__/game_analytics.cpython-39.pyc
Binary file not shown.
14 changes: 14 additions & 0 deletions server/api_basic_upload.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
from flask import Flask, request, Blueprint, make_response
import json
import datetime
import game_analytics

api_basic_upload = Blueprint('api_basic_upload', __name__)

@api_basic_upload.route('/upload_data', methods=['POST'])
def post_upload():
try:
print("Grabbing data via POST method in post_upload")
data = request.get_json()
print("Adding timestamp to data dict in post_upload")
data['timestamp'] = datetime.datetime.utcnow()
print(data)

print("Attempting connection to database in post_upload")
game_analytics.connect_to_database()

print("Attempting to add data to database in post_upload")
game_analytics.add_data(data)

print("Attempts successful, returning success.")
return "{'status': 'success'}"
except Exception as e:
print(e)
Expand Down
2 changes: 1 addition & 1 deletion server/basic_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

app.register_blueprint(api_basic_upload)

app.run(host='0.0.0.0', debug=True, use_reloader=True)
app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=True)

31 changes: 31 additions & 0 deletions server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
version: "3.3"

services:

game_analytics:
build:
context: .
dockerfile: PythonDockerfile
ports:
- "80:5000"
tty: true
volumes:
- ./:/app
networks:
- net

mongo:
image: mongo:4.2.2
ports:
- "27017:27017"
environment:
- MONGO_INITDB_ROOT_USERNAME=test
- MONGO_INITDB_ROOT_PASSWORD=test
networks:
- net
volumes:
- ./mongo_db_data_container:/data/db

networks:
net:
driver: bridge
49 changes: 49 additions & 0 deletions server/game_analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pandas as pd
from pymongo import MongoClient

db = {}

def connect_to_database():
print("Creating client in connect_to_database")
client = MongoClient('mongo', 27017, username="test", password="test")


global db

print("Grabbing test_database in connect_to_database")
db = client.test_database

print("Returning test_database in connect_to_database")
return db

def connect_to_database_outside_docker():
client = MongoClient('34.242.150.74', 80, username="test", password="test")

global db
db = client.test_database
return db


def add_data(data):
print("Getting posts in database in add_data")
posts = db.posts

print("Grabbing post_id using data in add_data")
post_id = posts.insert_one(data).inserted_id

print("Post ID: " + str(post_id))

def get_dataframe():
print("Initializing cursor in get_dataframe")
cursor = db.posts.find() # Expand the cursor and construct the DataFrame

print(cursor)

print("Creating pandas Dataframe in get_dataframe")
df = pd.DataFrame(list(cursor))

return df




34 changes: 34 additions & 0 deletions server/game_analytics.py.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pandas as pd
from pymongo import MongoClient

db = {}

def connect_to_database():
client = MongoClient('mongo', 80, username="test", password="test")

global db
db = client.test_database
return db

def connect_to_database_outside_docker():
client = MongoClient('http://34.242.150.74/upload_data', 80, username="test", password="test")

global db
db = client.test_database
return db


def add_data(data):
posts = db.posts

post_id = posts.insert_one(data).inserted_id

def get_dataframe():
cursor = db.posts.find()
# Expand the cursor and construct the DataFrame
df = pd.DataFrame(list(cursor))
return df




2 changes: 2 additions & 0 deletions server/mongo_db_data_container/WiredTiger
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WiredTiger
WiredTiger 3.2.2: (August 28, 2019)
1 change: 1 addition & 0 deletions server/mongo_db_data_container/WiredTiger.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WiredTiger lock file
6 changes: 6 additions & 0 deletions server/mongo_db_data_container/WiredTiger.turtle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WiredTiger version string
WiredTiger 3.2.2: (August 28, 2019)
WiredTiger version
major=3,minor=2,patch=2
file:WiredTiger.wt
access_pattern_hint=none,allocation_size=4KB,app_metadata=,assert=(commit_timestamp=none,durable_timestamp=none,read_timestamp=none),block_allocation=best,block_compressor=,cache_resident=false,checksum=uncompressed,collator=,columns=,dictionary=0,encryption=(keyid=,name=),format=btree,huffman_key=,huffman_value=,id=0,ignore_in_memory_cache_size=false,internal_item_max=0,internal_key_max=0,internal_key_truncate=true,internal_page_max=4KB,key_format=S,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=32KB,leaf_value_max=0,log=(enabled=true),memory_page_image_max=0,memory_page_max=5MB,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=false,prefix_compression_min=4,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,value_format=S,version=(major=1,minor=1),checkpoint=(WiredTigerCheckpoint.14=(addr="018d81e47ca5c6b18e81e4c57ebada8f81e422b771c6808080e2efc0e22fc0",order=14,time=1646493484,size=24576,newest_durable_ts=0,oldest_start_ts=0,oldest_start_txn=0,newest_stop_ts=-1,newest_stop_txn=-11,write_gen=36)),checkpoint_lsn=(3,6144)
Binary file added server/mongo_db_data_container/WiredTiger.wt
Binary file not shown.
Binary file not shown.
Binary file added server/mongo_db_data_container/_mdb_catalog.wt
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file added server/mongo_db_data_container/sizeStorer.wt
Binary file not shown.
Binary file added server/mongo_db_data_container/storage.bson
Binary file not shown.
11 changes: 11 additions & 0 deletions server/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Flask_Cors==3.0.7
Werkzeug==0.14.1
passlib==1.7.1
requests==2.24.0
httplib2==0.12.1
Flask==1.0.2
python_dateutil==2.8.0
pymongo==3.11.0
pandas==1.1.3
notebook==6.1.5
itsdangerous==2.0.1
17 changes: 14 additions & 3 deletions server/test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import game_analytics
import requests

def test_post_data():
url = 'http://localhost:5000/upload_data'
url = 'http://34.242.150.74/upload_data'
payload = {"completion_time": 2000,
"level": 7
}
}
r = requests.post(url, json=payload)
print(r)
test_post_data()

def test_get_data():
#game_analytics.connect_to_database_outside_docker()
print("Connecting to database in test_get_data")
game_analytics.connect_to_database()

df = game_analytics.get_dataframe()
print(df)

test_post_data()
test_get_data()

0 comments on commit 26ce5b4

Please sign in to comment.