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
31 changes: 11 additions & 20 deletions alone_2048.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import time, pickle
from app import app, db
from game import *
from flask import request, render_template, jsonify, session
from flask import request, render_template, jsonify


global_dict = {}
# global_dict = {}
@app.route("/")
def main():
return render_template('index.html')
Expand All @@ -15,7 +15,8 @@ def play_the_game():
resp = request.get_json()
uId = str(resp['uId'])
direction = resp['direction']
b = pickle.loads(session['uId'])
b = db.get_game(uId)
print(b, "**")
board = b.x
moved = b.process_move(direction)
legit = b.next_step_check()
Expand All @@ -25,27 +26,23 @@ def play_the_game():
b.add_number()
game_data = {"board": board, "c_score": c_score, "uId": uId, "game_over": False}
game_dict = jsonify(game_data)
session['uId'] = pickle.dumps(b)
return game_dict
elif moved:
game_data = {"board": board, "c_score": c_score, "uId": uId, "game_over": False}
game_dict = jsonify(game_data)
session['uId'] = pickle.dumps(b)
return game_dict
else:
game_data = {"board": board, "c_score": c_score, "uId": uId, "game_over": False}
game_dict = jsonify(game_data)
session['uId'] = pickle.dumps(b)
return game_dict
game_data = {"board": board, "c_score": c_score, "uId": uId, "game_over": True}
game_dict = jsonify(game_data)
# session['uId'] = pickle.dumps(b)
return game_dict


@app.route('/api/games')
def games():
return str(global_dict)
# @app.route('/api/games')
# def games():
# return str(global_dict)


@app.route('/api/new_game')
Expand All @@ -57,22 +54,16 @@ def new_game():
c_score = b.c_score
game_data = {"board": board, "c_score": c_score, "uId": uId}
game_dict = jsonify(game_data)
session['uId'] = pickle.dumps(b)
# global_dict[uId] = b
db.save_to_games_db(uId, pickle.dumps(b))
return game_dict


# @app.route('/save_user_highscore', methods=['POST']) #curl -X POST -F 'u_name=Try_1' -F 'c_score=1500' 127.0.0.1:5000/save_user_highscore
# def save_user_highscore():
# u_name = request.form.get('u_name')
# c_score = request.form.get('c_score')
# db.save_to_db(u_name, c_score)
# return print(c_score, u_name)

@app.route('/save_user_highscore', methods=['POST', 'GET']) #curl -H 'Content-Type: application/json' -X GET 127.0.0.1:5000/save_user_highscore -d '{"u_name": "test_1", "c_score": 1000}'
@app.route('/save_user_highscore', methods=['POST', 'GET'])
def save_user_highscore():
resp = request.get_json()
u_name = resp['u_name']
c_score = resp['c_score']
db.save_to_db(u_name, c_score)
db.save_to_scores_db(u_name, c_score)
msg = "Saved!"
return msg
2 changes: 0 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from flask import Flask


app = Flask(__name__)
app.config['SECRET_KEY'] = 'so amazingly secret' # for session values


import alone_2048
28 changes: 24 additions & 4 deletions app/db.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
import sqlite3
import sqlite3, pickle


def create_db():
conn = sqlite3.connect('scores.db')
conn = sqlite3.connect('games.db')
c = conn.cursor()
c.execute("CREATE TABLE high_scores (name text, score integer)")
c.execute("CREATE TABLE game_list (id text, game text)")
conn.commit()
conn.close()


def save_to_db(u_name, c_score):
def save_to_scores_db(u_name, c_score):
conn = sqlite3.connect('scores.db')
c = conn.cursor()
c.execute("INSERT INTO high_scores VALUES(?, ?)", (u_name, c_score))
conn.commit()
conn.close()


def save_to_games_db(uId, b):
conn = sqlite3.connect('games.db')
c = conn.cursor()
c.execute("INSERT INTO game_list VALUES(?, ?)", (uId, b))
conn.commit()
conn.close()


def get_game(uId):
conn = sqlite3.connect('games.db')
c = conn.cursor()
c.execute("SELECT game FROM game_list WHERE id=?", (uId,))
gam = c.fetchone()
b = pickle.loads(gam[0])
conn.commit()
conn.close()
print(b)
return b
6 changes: 0 additions & 6 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ def __init__(self):
self.c_score = self.c_score
self.copy_board = self.copy_board

def print_inline(self, s):
print(s, end='')

def count_zeroes(self):
return sum([sum([1 for c in r if c == 0]) for r in self.x])

def max_value(self):
return max([max(r) for r in self.x])

def add_number(self):
list_of_num = [2, 2, 2, 2, 4]
num = random.choice(list_of_num)
Expand Down
Binary file added games.db
Binary file not shown.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Flask==1.0.2
gunicorn==19.7.1