# UncommonApp
# Dennis Chen, T Fabiha, Addison Huang, Michelle Tang
# P#02: The End

from flask import Flask,render_template,request,session,url_for,redirect,flash

from os import urandom
from util import db_updater as update
from util import db_search as search
from util import db_builder as builder
from util import colors
from util import weatherColors
from passlib.hash import sha256_crypt

import ssl
import urllib
import json
import random

import sqlite3 #imports sqlite

app = Flask(__name__)
app.secret_key = urandom(32)
#----------------------------------------------------------home--------------------------------------------------------
@app.route("/",methods=['GET','POST'])
def home():
    builder.main()
    if 'username' in session: #if user is logged in
        return redirect(url_for('authPage'))
    else:
        return render_template('auth.html')

#----------------------------------------------------------login/register/logout--------------------------------------------------------
@app.route("/logout")
def logout():
    '''
    Logs user out of session by popping them from the session. Returns user to log-in screen
    '''
    if 'username' in session:
        session.pop('username')
        return redirect(url_for('authPage'))
    else:
        return redirect(url_for('home'))

@app.route("/auth",methods=['GET','POST'])
def authPage():
    '''
    Authenticates user signing in. Checks to see if password is correct or not;
    if correct, logs user in. If not, flashes "incorrect credentials"
    '''
    if 'username' in session:
        username = session['username']
        '''score = search.score(username)[0]
        scores = search.highScores()
        counter = 0
        highScores = []
        userNames = []

        while counter < len(scores):
            highScores.append(scores[counter][0])
            userNames.append(scores[counter][1])
            counter += 1
        '''
        return redirect(url_for('genWeather'))
    else:
        try:
            username=request.form['username'] #username
            password = search.password(username) #password that matches the username
            print(password)
            if password == None: #if credentials are incorrect
                flash('Wrong Username or Password!')
                return redirect(url_for('home')) #redirects
            elif sha256_crypt.verify(request.form['password'], password[0]): #if password is correct, login
                session['username'] = username
                return redirect(url_for('authPage'))
            else: #else credentials are wrong
                flash('Wrong Username or Password!')
                return redirect(url_for('home'))
        except:
            return redirect(url_for('home'))

@app.route("/reg",methods=['GET','POST'])
def reg():
    '''
    Loads the template that takes information and allows user to register,
    creating a new account that they can sign into session with
    '''
    if 'username' in session:
        return redirect(url_for('authPage'))

    return render_template('reg.html')

#----------------------------------------------------------database--------------------------------------------------------
@app.route("/added",methods=['GET','POST'])
def added():
    '''
    Checks to see if username is unique,
    flashes "username taken" if it is,
    adds user and password to database if not and sends to home
    '''
    try:
        if request.form['password'] == request.form['confirmpassword']:
            newUsername = request.form['username']
            newPassword = sha256_crypt.encrypt(request.form['password']) #encrypts password
            userList = search.username(newUsername)
            if userList == [] : #if username isn't taken
                update.adduser(newUsername,newPassword) #add to database
                flash('Register Successful')
                return redirect(url_for('tutorial'))
            else: #if username is taken
                flash('Username Taken')
                return redirect(url_for('reg'))
        else:
            flash("Passwords don't match")
            return redirect(url_for('reg'))
    except:
        return redirect(url_for('home'))

#-------------------------------------------featured puzzle--------------------------------------------------------
@app.route('/weatherPuz',methods = ['GET','POST'])
def genWeather():
    '''
    Generates a featured puzzle based on the current weather of the user and puts
    it on the home page
    '''
    if 'username' not in session:
        return redirect(url_for('home'))
    else:
        name = session['username']
        rows = 8
        columns = 8
        palette = weatherColors.makePuzOnWeather()
        puzzle = colors.puzzleGen(rows,columns,palette[0],palette[1],palette[2],palette[3])
        print('-------weather----------')
        print("{},{},{}".format(palette[0][0],palette[0][1],palette[0][2]))
        return render_template('home.html',
                               Name = name,
                               colors = puzzle,
                               tile_size = "{}x{}".format(rows, columns), # size "widthxheigth"
                               UL = "{},{},{}".format(palette[0][0],
                                                      palette[0][1],
                                                      palette[0][2]), # upper-left color
                               UR = "{},{},{}".format(palette[1][0],
                                                      palette[1][1],
                                                      palette[1][2]), # upper-right color
                               LL = "{},{},{}".format(palette[2][0],
                                                      palette[2][1],
                                                      palette[2][2]), # lower-left color
                               LR = "{},{},{}".format(palette[3][0],
                                                      palette[3][1],
                                                      palette[3][2])) # lower-right color
    #-------------------------------------------create puzzle--------------------------------------------------------
@app.route('/random',methods = ['GET','POST'])
def random():
    '''
    Creates a random puzzle for users to play.
    '''
    if 'username' not in session:
        return redirect(url_for('home'))
    rows = 8
    columns = 8

    palette = colors.getpalette(4)
    puzzle = colors.puzzleGen(rows, columns,
                              palette[0], palette[1],
                              palette[2], palette[3])
    dbString = "%s;%s;%s;%s;%s;%s" % (rows,columns, palette[0], palette[1], palette[2], palette[3])
    dbString = "".join(dbString.split(" "))
    return render_template('testpuzzle.html',
                           colors = puzzle,
                           tile_size = "{}x{}".format(rows, columns), # size "widthxheigth"
                           UL = "{},{},{}".format(palette[0][0],
                                                  palette[0][1],
                                                  palette[0][2]), # upper-left color
                           UR = "{},{},{}".format(palette[1][0],
                                                  palette[1][1],
                                                  palette[1][2]), # upper-right color
                           LL = "{},{},{}".format(palette[2][0],
                                                  palette[2][1],
                                                  palette[2][2]), # lower-left color
                           LR = "{},{},{}".format(palette[3][0],
                                                  palette[3][1],
                                                  palette[3][2]), # lower-right color
                           puzzleInfo = dbString)
                           # avgMoves = search.highScores(search.getPuzzleID(dbString)))
                           #
                           # updateAverageMovesPuzzle(moves,puzzleID):
#-------------------------------------------liked puzzles page--------------------------------------------------------
@app.route('/puzzles', methods = ["GET","POST"])
def puzzles():
    if 'username' in session: #if user is logged in

        # likedPuz = db_search.getLikedPuzzles(session['username'])
        # puzzles = []
        # for puz in likedPuz:
        #     puzzle = puzzle.split(';')
        #     rows = int(puzzle[0])
        #     cols = int(puzzle[1])
        #     ULC = json.loads(puzzle[2])
        #     URC = json.loads(puzzle[3])
        #     LLC = json.loads(puzzle[4])
        #     LRC = json.loads(puzzle[5])
        #     puzzle = colors.puzzleGen(rows,cols,ULC,URC,LLC,LRC)
        #     puzzles.append(puzzle)

        puzzles=search.getAllPuzzles()
        i = 0;
        while i < len(puzzles):
            puzzles[i]=puzzles[i].split(';')
            puzzles[i]=','.join(puzzles[i])
            print(puzzles[i])
            i += 1
        return render_template('puzzles.html',puzzles = puzzles)
    else:
        return redirect(url_for('home'))

#-------------------------------------------save puzzle--------------------------------------------------------
@app.route('/save', methods = ["GET","POST"])
def save():
    if 'username' not in session:
        return redirect(url_for('home'))
    dbString = request.form['dbStr']
    moves = request.form['moves']
    if dbString not in search.getAllPuzzles():
        update.addPuzzle(dbString,moves)
        puzId = search.getPuzzleID(dbString)
        update.updateAverageMovesPuzzle(moves,puzId)
        update.addLog(session['username'],moves,puzId)
    return redirect(url_for('authPage'))

#-------------------------------------------customize--------------------------------------------------------
@app.route('/customize',methods = ["GET","POST"])
def custom():
    if 'username' in session:
        #palette=colors.getpalette(4)
        #print(palette[0])
        colorList = [[],[],[],[]]

        for i in range(4):
            palette = colors.getpalette(4);

            for j in range(4):
                colorList[i].append('rgb('+str(palette[j][0]) +","+str(palette[j][1])+"," +str(palette[j][2]) +')')


        """
        colorList0=[]
        i=0
        while i<4:
            colorList0.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList1=[]
        while i<4:
            colorList1.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList2=[]
        while i<4:
            colorList2.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList3=[]
        while i<4:
            colorList3.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
"""
        return render_template('customize.html', colors0=colorList[0], colors1=colorList[1], colors2=colorList[2], colors3=colorList[3])
    else:
        return redirect(url_for('home'))

@app.route('/play', methods=["GET","POST"])
def play():
    if 'username' in session:
        try:
            size = request.form['size']
            size1=size.split('x')
            rows = int(size1[0])
            columns = int(size1[1])
            colorTL = request.form['tlcolor'][4:len(request.form['tlcolor'])-1]
            colorTL = colorTL.split(",")
            colorTL = [int(i) for i in colorTL]
            colorTR = request.form['trcolor'][4:len(request.form['trcolor'])-1]
            colorTR = colorTR.split(",")
            colorTR = [int(i) for i in colorTR]
            colorBL = request.form['blcolor'][4:len(request.form['blcolor'])-1]
            colorBL = colorBL.split(",")
            colorBL = [int(i) for i in colorBL]
            colorBR = request.form['brcolor'][4:len(request.form['brcolor'])-1]
            colorBR = colorBR.split(",")
            colorBR = [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,colorBL, colorBR)
            dbString = "%s;%s;%s;%s;%s;%s" % (rows,columns, colorTL, colorTR, colorBL, colorBR)
            dbString = "".join(dbString.split(" "))
            print('-------colorTL--------')
            print("{},{},{}".format(colorTL[0],colorTL[1],colorTL[2]))
            return render_template('playpuzzle.html',
                                   colors = puzzle,
                                   tile_size = "{}x{}".format(rows, columns), # size "widthxheigth"
                                   UL = "{},{},{}".format(colorTL[0],
                                                  colorTL[1],
                                                  colorTL[2]), # upper-left color
                                   UR = "{},{},{}".format(colorTR[0],
                                                  colorTR[1],
                                                  colorTR[2]), # upper-right color
                                   LL = "{},{},{}".format(colorBL[0],
                                                  colorBL[1],
                                                  colorBL[2]), # lower-left color
                                   LR = "{},{},{}".format(colorBR[0],
                                                  colorBR[1],
                                                  colorBR[2]), # lower-right color
                                   puzzleInfo = dbString)
        except:
            value = request.form['value']
            rows = int(value[0])
            columns = int(value[2])
            colorTL = value[value.find("[")+1:value.find("]")]
            colorTL = colorTL.split(",")
            colorTL= [int(i) for i in colorTL]
            value = value[value.find("]")+1:]
            colorTR = value[value.find("[")+1:value.find("]")]
            colorTR = colorTR.split(",")
            colorTR= [int(i) for i in colorTR]
            value = value[value.find("]")+1:]
            colorBL = value[value.find("[")+1:value.find("]")]
            colorBL = colorBL.split(",")
            colorBL= [int(i) for i in colorBL]
            value = value[value.find("]")+1:]
            colorBR = value[value.find("[")+1:value.find("]")]
            colorBR = colorBR.split(",")
            colorBR= [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,colorBL, colorBR)
            dbString = "%s;%s;%s;%s;%s;%s" % (rows,columns, colorTL, colorTR, colorBL, colorBR)
            dbString = "".join(dbString.split(" "))
            return render_template('testpuzzle.html',
                                   colors = puzzle,
                                   tile_size = "{}x{}".format(rows, columns), # size "widthxheigth"
                                   UL = "{},{},{}".format(colorTL[0],
                                                  colorTL[1],
                                                  colorTL[2]), # upper-left color
                                   UR = "{},{},{}".format(colorTR[0],
                                                  colorTR[1],
                                                  colorTR[2]), # upper-right color
                                   LL = "{},{},{}".format(colorBL[0],
                                                  colorBL[1],
                                                  colorBL[2]), # lower-left color
                                   LR = "{},{},{}".format(colorBR[0],
                                                  colorBR[1],
                                                  colorBR[2]), # lower-right color
                                   puzzleInfo = dbString)


    else:
        return redirect(url_for('home'))

@app.route('/tutorial', methods=["GET","POST"])
def tutorial():
    rows=4
    columns = 4
    colorTL = [124,10,2]
    colorTR = [255,8,0]
    colorBL = [237,41,57]
    colorBR = [250,128,114]
    puzzle = colors.puzzleGen(rows,columns,colorTL,colorTR,colorBL,colorBR)
    return render_template('tutorial.html',
                                   colors = puzzle,
                                   tile_size = "{}x{}".format(rows, columns), # size "widthxheigth"
                                   UL = "{},{},{}".format(colorTL[0],
                                                  colorTL[1],
                                                  colorTL[2]), # upper-left color
                                   UR = "{},{},{}".format(colorTR[0],
                                                  colorTR[1],
                                                  colorTR[2]), # upper-right color
                                   LL = "{},{},{}".format(colorBL[0],
                                                  colorBL[1],
                                                  colorBL[2]), # lower-left color
                                   LR = "{},{},{}".format(colorBR[0],
                                                  colorBR[1],
                                                  colorBR[2])) # lower-right color)

@app.route('/completed', methods=["GET","POST"])
def completed():
    flash('Tutorial Complete')
    return redirect(url_for('home'))




if __name__ == '__main__':
    app.run(debug=True)

48e4967d3554b4613e47ece6220e14586f02c7ea
<mask token>


@app.route('/', methods=['GET', 'POST'])
def home():
    builder.main()
    if 'username' in session:
        return redirect(url_for('authPage'))
    else:
        return render_template('auth.html')


@app.route('/logout')
def logout():
    """
    Logs user out of session by popping them from the session. Returns user to log-in screen
    """
    if 'username' in session:
        session.pop('username')
        return redirect(url_for('authPage'))
    else:
        return redirect(url_for('home'))


@app.route('/auth', methods=['GET', 'POST'])
def authPage():
    """
    Authenticates user signing in. Checks to see if password is correct or not;
    if correct, logs user in. If not, flashes "incorrect credentials"
    """
    if 'username' in session:
        username = session['username']
        """score = search.score(username)[0]
        scores = search.highScores()
        counter = 0
        highScores = []
        userNames = []

        while counter < len(scores):
            highScores.append(scores[counter][0])
            userNames.append(scores[counter][1])
            counter += 1
        """
        return redirect(url_for('genWeather'))
    else:
        try:
            username = request.form['username']
            password = search.password(username)
            print(password)
            if password == None:
                flash('Wrong Username or Password!')
                return redirect(url_for('home'))
            elif sha256_crypt.verify(request.form['password'], password[0]):
                session['username'] = username
                return redirect(url_for('authPage'))
            else:
                flash('Wrong Username or Password!')
                return redirect(url_for('home'))
        except:
            return redirect(url_for('home'))


@app.route('/reg', methods=['GET', 'POST'])
def reg():
    """
    Loads the template that takes information and allows user to register,
    creating a new account that they can sign into session with
    """
    if 'username' in session:
        return redirect(url_for('authPage'))
    return render_template('reg.html')


<mask token>


@app.route('/random', methods=['GET', 'POST'])
def random():
    """
    Creates a random puzzle for users to play.
    """
    if 'username' not in session:
        return redirect(url_for('home'))
    rows = 8
    columns = 8
    palette = colors.getpalette(4)
    puzzle = colors.puzzleGen(rows, columns, palette[0], palette[1],
        palette[2], palette[3])
    dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, palette[0], palette[1],
        palette[2], palette[3])
    dbString = ''.join(dbString.split(' '))
    return render_template('testpuzzle.html', colors=puzzle, tile_size=
        '{}x{}'.format(rows, columns), UL='{},{},{}'.format(palette[0][0],
        palette[0][1], palette[0][2]), UR='{},{},{}'.format(palette[1][0],
        palette[1][1], palette[1][2]), LL='{},{},{}'.format(palette[2][0],
        palette[2][1], palette[2][2]), LR='{},{},{}'.format(palette[3][0],
        palette[3][1], palette[3][2]), puzzleInfo=dbString)


@app.route('/puzzles', methods=['GET', 'POST'])
def puzzles():
    if 'username' in session:
        puzzles = search.getAllPuzzles()
        i = 0
        while i < len(puzzles):
            puzzles[i] = puzzles[i].split(';')
            puzzles[i] = ','.join(puzzles[i])
            print(puzzles[i])
            i += 1
        return render_template('puzzles.html', puzzles=puzzles)
    else:
        return redirect(url_for('home'))


@app.route('/save', methods=['GET', 'POST'])
def save():
    if 'username' not in session:
        return redirect(url_for('home'))
    dbString = request.form['dbStr']
    moves = request.form['moves']
    if dbString not in search.getAllPuzzles():
        update.addPuzzle(dbString, moves)
        puzId = search.getPuzzleID(dbString)
        update.updateAverageMovesPuzzle(moves, puzId)
        update.addLog(session['username'], moves, puzId)
    return redirect(url_for('authPage'))


@app.route('/customize', methods=['GET', 'POST'])
def custom():
    if 'username' in session:
        colorList = [[], [], [], []]
        for i in range(4):
            palette = colors.getpalette(4)
            for j in range(4):
                colorList[i].append('rgb(' + str(palette[j][0]) + ',' + str
                    (palette[j][1]) + ',' + str(palette[j][2]) + ')')
        """
        colorList0=[]
        i=0
        while i<4:
            colorList0.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList1=[]
        while i<4:
            colorList1.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList2=[]
        while i<4:
            colorList2.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList3=[]
        while i<4:
            colorList3.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
"""
        return render_template('customize.html', colors0=colorList[0],
            colors1=colorList[1], colors2=colorList[2], colors3=colorList[3])
    else:
        return redirect(url_for('home'))


@app.route('/play', methods=['GET', 'POST'])
def play():
    if 'username' in session:
        try:
            size = request.form['size']
            size1 = size.split('x')
            rows = int(size1[0])
            columns = int(size1[1])
            colorTL = request.form['tlcolor'][4:len(request.form['tlcolor']
                ) - 1]
            colorTL = colorTL.split(',')
            colorTL = [int(i) for i in colorTL]
            colorTR = request.form['trcolor'][4:len(request.form['trcolor']
                ) - 1]
            colorTR = colorTR.split(',')
            colorTR = [int(i) for i in colorTR]
            colorBL = request.form['blcolor'][4:len(request.form['blcolor']
                ) - 1]
            colorBL = colorBL.split(',')
            colorBL = [int(i) for i in colorBL]
            colorBR = request.form['brcolor'][4:len(request.form['brcolor']
                ) - 1]
            colorBR = colorBR.split(',')
            colorBR = [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,
                colorBL, colorBR)
            dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, colorTL,
                colorTR, colorBL, colorBR)
            dbString = ''.join(dbString.split(' '))
            print('-------colorTL--------')
            print('{},{},{}'.format(colorTL[0], colorTL[1], colorTL[2]))
            return render_template('playpuzzle.html', colors=puzzle,
                tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.
                format(colorTL[0], colorTL[1], colorTL[2]), UR='{},{},{}'.
                format(colorTR[0], colorTR[1], colorTR[2]), LL='{},{},{}'.
                format(colorBL[0], colorBL[1], colorBL[2]), LR='{},{},{}'.
                format(colorBR[0], colorBR[1], colorBR[2]), puzzleInfo=dbString
                )
        except:
            value = request.form['value']
            rows = int(value[0])
            columns = int(value[2])
            colorTL = value[value.find('[') + 1:value.find(']')]
            colorTL = colorTL.split(',')
            colorTL = [int(i) for i in colorTL]
            value = value[value.find(']') + 1:]
            colorTR = value[value.find('[') + 1:value.find(']')]
            colorTR = colorTR.split(',')
            colorTR = [int(i) for i in colorTR]
            value = value[value.find(']') + 1:]
            colorBL = value[value.find('[') + 1:value.find(']')]
            colorBL = colorBL.split(',')
            colorBL = [int(i) for i in colorBL]
            value = value[value.find(']') + 1:]
            colorBR = value[value.find('[') + 1:value.find(']')]
            colorBR = colorBR.split(',')
            colorBR = [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,
                colorBL, colorBR)
            dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, colorTL,
                colorTR, colorBL, colorBR)
            dbString = ''.join(dbString.split(' '))
            return render_template('testpuzzle.html', colors=puzzle,
                tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.
                format(colorTL[0], colorTL[1], colorTL[2]), UR='{},{},{}'.
                format(colorTR[0], colorTR[1], colorTR[2]), LL='{},{},{}'.
                format(colorBL[0], colorBL[1], colorBL[2]), LR='{},{},{}'.
                format(colorBR[0], colorBR[1], colorBR[2]), puzzleInfo=dbString
                )
    else:
        return redirect(url_for('home'))


@app.route('/tutorial', methods=['GET', 'POST'])
def tutorial():
    rows = 4
    columns = 4
    colorTL = [124, 10, 2]
    colorTR = [255, 8, 0]
    colorBL = [237, 41, 57]
    colorBR = [250, 128, 114]
    puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR, colorBL, colorBR
        )
    return render_template('tutorial.html', colors=puzzle, tile_size=
        '{}x{}'.format(rows, columns), UL='{},{},{}'.format(colorTL[0],
        colorTL[1], colorTL[2]), UR='{},{},{}'.format(colorTR[0], colorTR[1
        ], colorTR[2]), LL='{},{},{}'.format(colorBL[0], colorBL[1],
        colorBL[2]), LR='{},{},{}'.format(colorBR[0], colorBR[1], colorBR[2]))


<mask token>

<mask token>


@app.route('/', methods=['GET', 'POST'])
def home():
    builder.main()
    if 'username' in session:
        return redirect(url_for('authPage'))
    else:
        return render_template('auth.html')


@app.route('/logout')
def logout():
    """
    Logs user out of session by popping them from the session. Returns user to log-in screen
    """
    if 'username' in session:
        session.pop('username')
        return redirect(url_for('authPage'))
    else:
        return redirect(url_for('home'))


@app.route('/auth', methods=['GET', 'POST'])
def authPage():
    """
    Authenticates user signing in. Checks to see if password is correct or not;
    if correct, logs user in. If not, flashes "incorrect credentials"
    """
    if 'username' in session:
        username = session['username']
        """score = search.score(username)[0]
        scores = search.highScores()
        counter = 0
        highScores = []
        userNames = []

        while counter < len(scores):
            highScores.append(scores[counter][0])
            userNames.append(scores[counter][1])
            counter += 1
        """
        return redirect(url_for('genWeather'))
    else:
        try:
            username = request.form['username']
            password = search.password(username)
            print(password)
            if password == None:
                flash('Wrong Username or Password!')
                return redirect(url_for('home'))
            elif sha256_crypt.verify(request.form['password'], password[0]):
                session['username'] = username
                return redirect(url_for('authPage'))
            else:
                flash('Wrong Username or Password!')
                return redirect(url_for('home'))
        except:
            return redirect(url_for('home'))


@app.route('/reg', methods=['GET', 'POST'])
def reg():
    """
    Loads the template that takes information and allows user to register,
    creating a new account that they can sign into session with
    """
    if 'username' in session:
        return redirect(url_for('authPage'))
    return render_template('reg.html')


@app.route('/added', methods=['GET', 'POST'])
def added():
    """
    Checks to see if username is unique,
    flashes "username taken" if it is,
    adds user and password to database if not and sends to home
    """
    try:
        if request.form['password'] == request.form['confirmpassword']:
            newUsername = request.form['username']
            newPassword = sha256_crypt.encrypt(request.form['password'])
            userList = search.username(newUsername)
            if userList == []:
                update.adduser(newUsername, newPassword)
                flash('Register Successful')
                return redirect(url_for('tutorial'))
            else:
                flash('Username Taken')
                return redirect(url_for('reg'))
        else:
            flash("Passwords don't match")
            return redirect(url_for('reg'))
    except:
        return redirect(url_for('home'))


@app.route('/weatherPuz', methods=['GET', 'POST'])
def genWeather():
    """
    Generates a featured puzzle based on the current weather of the user and puts
    it on the home page
    """
    if 'username' not in session:
        return redirect(url_for('home'))
    else:
        name = session['username']
        rows = 8
        columns = 8
        palette = weatherColors.makePuzOnWeather()
        puzzle = colors.puzzleGen(rows, columns, palette[0], palette[1],
            palette[2], palette[3])
        print('-------weather----------')
        print('{},{},{}'.format(palette[0][0], palette[0][1], palette[0][2]))
        return render_template('home.html', Name=name, colors=puzzle,
            tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.format(
            palette[0][0], palette[0][1], palette[0][2]), UR='{},{},{}'.
            format(palette[1][0], palette[1][1], palette[1][2]), LL=
            '{},{},{}'.format(palette[2][0], palette[2][1], palette[2][2]),
            LR='{},{},{}'.format(palette[3][0], palette[3][1], palette[3][2]))


@app.route('/random', methods=['GET', 'POST'])
def random():
    """
    Creates a random puzzle for users to play.
    """
    if 'username' not in session:
        return redirect(url_for('home'))
    rows = 8
    columns = 8
    palette = colors.getpalette(4)
    puzzle = colors.puzzleGen(rows, columns, palette[0], palette[1],
        palette[2], palette[3])
    dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, palette[0], palette[1],
        palette[2], palette[3])
    dbString = ''.join(dbString.split(' '))
    return render_template('testpuzzle.html', colors=puzzle, tile_size=
        '{}x{}'.format(rows, columns), UL='{},{},{}'.format(palette[0][0],
        palette[0][1], palette[0][2]), UR='{},{},{}'.format(palette[1][0],
        palette[1][1], palette[1][2]), LL='{},{},{}'.format(palette[2][0],
        palette[2][1], palette[2][2]), LR='{},{},{}'.format(palette[3][0],
        palette[3][1], palette[3][2]), puzzleInfo=dbString)


@app.route('/puzzles', methods=['GET', 'POST'])
def puzzles():
    if 'username' in session:
        puzzles = search.getAllPuzzles()
        i = 0
        while i < len(puzzles):
            puzzles[i] = puzzles[i].split(';')
            puzzles[i] = ','.join(puzzles[i])
            print(puzzles[i])
            i += 1
        return render_template('puzzles.html', puzzles=puzzles)
    else:
        return redirect(url_for('home'))


@app.route('/save', methods=['GET', 'POST'])
def save():
    if 'username' not in session:
        return redirect(url_for('home'))
    dbString = request.form['dbStr']
    moves = request.form['moves']
    if dbString not in search.getAllPuzzles():
        update.addPuzzle(dbString, moves)
        puzId = search.getPuzzleID(dbString)
        update.updateAverageMovesPuzzle(moves, puzId)
        update.addLog(session['username'], moves, puzId)
    return redirect(url_for('authPage'))


@app.route('/customize', methods=['GET', 'POST'])
def custom():
    if 'username' in session:
        colorList = [[], [], [], []]
        for i in range(4):
            palette = colors.getpalette(4)
            for j in range(4):
                colorList[i].append('rgb(' + str(palette[j][0]) + ',' + str
                    (palette[j][1]) + ',' + str(palette[j][2]) + ')')
        """
        colorList0=[]
        i=0
        while i<4:
            colorList0.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList1=[]
        while i<4:
            colorList1.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList2=[]
        while i<4:
            colorList2.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList3=[]
        while i<4:
            colorList3.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
"""
        return render_template('customize.html', colors0=colorList[0],
            colors1=colorList[1], colors2=colorList[2], colors3=colorList[3])
    else:
        return redirect(url_for('home'))


@app.route('/play', methods=['GET', 'POST'])
def play():
    if 'username' in session:
        try:
            size = request.form['size']
            size1 = size.split('x')
            rows = int(size1[0])
            columns = int(size1[1])
            colorTL = request.form['tlcolor'][4:len(request.form['tlcolor']
                ) - 1]
            colorTL = colorTL.split(',')
            colorTL = [int(i) for i in colorTL]
            colorTR = request.form['trcolor'][4:len(request.form['trcolor']
                ) - 1]
            colorTR = colorTR.split(',')
            colorTR = [int(i) for i in colorTR]
            colorBL = request.form['blcolor'][4:len(request.form['blcolor']
                ) - 1]
            colorBL = colorBL.split(',')
            colorBL = [int(i) for i in colorBL]
            colorBR = request.form['brcolor'][4:len(request.form['brcolor']
                ) - 1]
            colorBR = colorBR.split(',')
            colorBR = [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,
                colorBL, colorBR)
            dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, colorTL,
                colorTR, colorBL, colorBR)
            dbString = ''.join(dbString.split(' '))
            print('-------colorTL--------')
            print('{},{},{}'.format(colorTL[0], colorTL[1], colorTL[2]))
            return render_template('playpuzzle.html', colors=puzzle,
                tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.
                format(colorTL[0], colorTL[1], colorTL[2]), UR='{},{},{}'.
                format(colorTR[0], colorTR[1], colorTR[2]), LL='{},{},{}'.
                format(colorBL[0], colorBL[1], colorBL[2]), LR='{},{},{}'.
                format(colorBR[0], colorBR[1], colorBR[2]), puzzleInfo=dbString
                )
        except:
            value = request.form['value']
            rows = int(value[0])
            columns = int(value[2])
            colorTL = value[value.find('[') + 1:value.find(']')]
            colorTL = colorTL.split(',')
            colorTL = [int(i) for i in colorTL]
            value = value[value.find(']') + 1:]
            colorTR = value[value.find('[') + 1:value.find(']')]
            colorTR = colorTR.split(',')
            colorTR = [int(i) for i in colorTR]
            value = value[value.find(']') + 1:]
            colorBL = value[value.find('[') + 1:value.find(']')]
            colorBL = colorBL.split(',')
            colorBL = [int(i) for i in colorBL]
            value = value[value.find(']') + 1:]
            colorBR = value[value.find('[') + 1:value.find(']')]
            colorBR = colorBR.split(',')
            colorBR = [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,
                colorBL, colorBR)
            dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, colorTL,
                colorTR, colorBL, colorBR)
            dbString = ''.join(dbString.split(' '))
            return render_template('testpuzzle.html', colors=puzzle,
                tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.
                format(colorTL[0], colorTL[1], colorTL[2]), UR='{},{},{}'.
                format(colorTR[0], colorTR[1], colorTR[2]), LL='{},{},{}'.
                format(colorBL[0], colorBL[1], colorBL[2]), LR='{},{},{}'.
                format(colorBR[0], colorBR[1], colorBR[2]), puzzleInfo=dbString
                )
    else:
        return redirect(url_for('home'))


@app.route('/tutorial', methods=['GET', 'POST'])
def tutorial():
    rows = 4
    columns = 4
    colorTL = [124, 10, 2]
    colorTR = [255, 8, 0]
    colorBL = [237, 41, 57]
    colorBR = [250, 128, 114]
    puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR, colorBL, colorBR
        )
    return render_template('tutorial.html', colors=puzzle, tile_size=
        '{}x{}'.format(rows, columns), UL='{},{},{}'.format(colorTL[0],
        colorTL[1], colorTL[2]), UR='{},{},{}'.format(colorTR[0], colorTR[1
        ], colorTR[2]), LL='{},{},{}'.format(colorBL[0], colorBL[1],
        colorBL[2]), LR='{},{},{}'.format(colorBR[0], colorBR[1], colorBR[2]))


@app.route('/completed', methods=['GET', 'POST'])
def completed():
    flash('Tutorial Complete')
    return redirect(url_for('home'))


<mask token>

<mask token>


@app.route('/', methods=['GET', 'POST'])
def home():
    builder.main()
    if 'username' in session:
        return redirect(url_for('authPage'))
    else:
        return render_template('auth.html')


@app.route('/logout')
def logout():
    """
    Logs user out of session by popping them from the session. Returns user to log-in screen
    """
    if 'username' in session:
        session.pop('username')
        return redirect(url_for('authPage'))
    else:
        return redirect(url_for('home'))


@app.route('/auth', methods=['GET', 'POST'])
def authPage():
    """
    Authenticates user signing in. Checks to see if password is correct or not;
    if correct, logs user in. If not, flashes "incorrect credentials"
    """
    if 'username' in session:
        username = session['username']
        """score = search.score(username)[0]
        scores = search.highScores()
        counter = 0
        highScores = []
        userNames = []

        while counter < len(scores):
            highScores.append(scores[counter][0])
            userNames.append(scores[counter][1])
            counter += 1
        """
        return redirect(url_for('genWeather'))
    else:
        try:
            username = request.form['username']
            password = search.password(username)
            print(password)
            if password == None:
                flash('Wrong Username or Password!')
                return redirect(url_for('home'))
            elif sha256_crypt.verify(request.form['password'], password[0]):
                session['username'] = username
                return redirect(url_for('authPage'))
            else:
                flash('Wrong Username or Password!')
                return redirect(url_for('home'))
        except:
            return redirect(url_for('home'))


@app.route('/reg', methods=['GET', 'POST'])
def reg():
    """
    Loads the template that takes information and allows user to register,
    creating a new account that they can sign into session with
    """
    if 'username' in session:
        return redirect(url_for('authPage'))
    return render_template('reg.html')


@app.route('/added', methods=['GET', 'POST'])
def added():
    """
    Checks to see if username is unique,
    flashes "username taken" if it is,
    adds user and password to database if not and sends to home
    """
    try:
        if request.form['password'] == request.form['confirmpassword']:
            newUsername = request.form['username']
            newPassword = sha256_crypt.encrypt(request.form['password'])
            userList = search.username(newUsername)
            if userList == []:
                update.adduser(newUsername, newPassword)
                flash('Register Successful')
                return redirect(url_for('tutorial'))
            else:
                flash('Username Taken')
                return redirect(url_for('reg'))
        else:
            flash("Passwords don't match")
            return redirect(url_for('reg'))
    except:
        return redirect(url_for('home'))


@app.route('/weatherPuz', methods=['GET', 'POST'])
def genWeather():
    """
    Generates a featured puzzle based on the current weather of the user and puts
    it on the home page
    """
    if 'username' not in session:
        return redirect(url_for('home'))
    else:
        name = session['username']
        rows = 8
        columns = 8
        palette = weatherColors.makePuzOnWeather()
        puzzle = colors.puzzleGen(rows, columns, palette[0], palette[1],
            palette[2], palette[3])
        print('-------weather----------')
        print('{},{},{}'.format(palette[0][0], palette[0][1], palette[0][2]))
        return render_template('home.html', Name=name, colors=puzzle,
            tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.format(
            palette[0][0], palette[0][1], palette[0][2]), UR='{},{},{}'.
            format(palette[1][0], palette[1][1], palette[1][2]), LL=
            '{},{},{}'.format(palette[2][0], palette[2][1], palette[2][2]),
            LR='{},{},{}'.format(palette[3][0], palette[3][1], palette[3][2]))


@app.route('/random', methods=['GET', 'POST'])
def random():
    """
    Creates a random puzzle for users to play.
    """
    if 'username' not in session:
        return redirect(url_for('home'))
    rows = 8
    columns = 8
    palette = colors.getpalette(4)
    puzzle = colors.puzzleGen(rows, columns, palette[0], palette[1],
        palette[2], palette[3])
    dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, palette[0], palette[1],
        palette[2], palette[3])
    dbString = ''.join(dbString.split(' '))
    return render_template('testpuzzle.html', colors=puzzle, tile_size=
        '{}x{}'.format(rows, columns), UL='{},{},{}'.format(palette[0][0],
        palette[0][1], palette[0][2]), UR='{},{},{}'.format(palette[1][0],
        palette[1][1], palette[1][2]), LL='{},{},{}'.format(palette[2][0],
        palette[2][1], palette[2][2]), LR='{},{},{}'.format(palette[3][0],
        palette[3][1], palette[3][2]), puzzleInfo=dbString)


@app.route('/puzzles', methods=['GET', 'POST'])
def puzzles():
    if 'username' in session:
        puzzles = search.getAllPuzzles()
        i = 0
        while i < len(puzzles):
            puzzles[i] = puzzles[i].split(';')
            puzzles[i] = ','.join(puzzles[i])
            print(puzzles[i])
            i += 1
        return render_template('puzzles.html', puzzles=puzzles)
    else:
        return redirect(url_for('home'))


@app.route('/save', methods=['GET', 'POST'])
def save():
    if 'username' not in session:
        return redirect(url_for('home'))
    dbString = request.form['dbStr']
    moves = request.form['moves']
    if dbString not in search.getAllPuzzles():
        update.addPuzzle(dbString, moves)
        puzId = search.getPuzzleID(dbString)
        update.updateAverageMovesPuzzle(moves, puzId)
        update.addLog(session['username'], moves, puzId)
    return redirect(url_for('authPage'))


@app.route('/customize', methods=['GET', 'POST'])
def custom():
    if 'username' in session:
        colorList = [[], [], [], []]
        for i in range(4):
            palette = colors.getpalette(4)
            for j in range(4):
                colorList[i].append('rgb(' + str(palette[j][0]) + ',' + str
                    (palette[j][1]) + ',' + str(palette[j][2]) + ')')
        """
        colorList0=[]
        i=0
        while i<4:
            colorList0.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList1=[]
        while i<4:
            colorList1.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList2=[]
        while i<4:
            colorList2.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList3=[]
        while i<4:
            colorList3.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
"""
        return render_template('customize.html', colors0=colorList[0],
            colors1=colorList[1], colors2=colorList[2], colors3=colorList[3])
    else:
        return redirect(url_for('home'))


@app.route('/play', methods=['GET', 'POST'])
def play():
    if 'username' in session:
        try:
            size = request.form['size']
            size1 = size.split('x')
            rows = int(size1[0])
            columns = int(size1[1])
            colorTL = request.form['tlcolor'][4:len(request.form['tlcolor']
                ) - 1]
            colorTL = colorTL.split(',')
            colorTL = [int(i) for i in colorTL]
            colorTR = request.form['trcolor'][4:len(request.form['trcolor']
                ) - 1]
            colorTR = colorTR.split(',')
            colorTR = [int(i) for i in colorTR]
            colorBL = request.form['blcolor'][4:len(request.form['blcolor']
                ) - 1]
            colorBL = colorBL.split(',')
            colorBL = [int(i) for i in colorBL]
            colorBR = request.form['brcolor'][4:len(request.form['brcolor']
                ) - 1]
            colorBR = colorBR.split(',')
            colorBR = [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,
                colorBL, colorBR)
            dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, colorTL,
                colorTR, colorBL, colorBR)
            dbString = ''.join(dbString.split(' '))
            print('-------colorTL--------')
            print('{},{},{}'.format(colorTL[0], colorTL[1], colorTL[2]))
            return render_template('playpuzzle.html', colors=puzzle,
                tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.
                format(colorTL[0], colorTL[1], colorTL[2]), UR='{},{},{}'.
                format(colorTR[0], colorTR[1], colorTR[2]), LL='{},{},{}'.
                format(colorBL[0], colorBL[1], colorBL[2]), LR='{},{},{}'.
                format(colorBR[0], colorBR[1], colorBR[2]), puzzleInfo=dbString
                )
        except:
            value = request.form['value']
            rows = int(value[0])
            columns = int(value[2])
            colorTL = value[value.find('[') + 1:value.find(']')]
            colorTL = colorTL.split(',')
            colorTL = [int(i) for i in colorTL]
            value = value[value.find(']') + 1:]
            colorTR = value[value.find('[') + 1:value.find(']')]
            colorTR = colorTR.split(',')
            colorTR = [int(i) for i in colorTR]
            value = value[value.find(']') + 1:]
            colorBL = value[value.find('[') + 1:value.find(']')]
            colorBL = colorBL.split(',')
            colorBL = [int(i) for i in colorBL]
            value = value[value.find(']') + 1:]
            colorBR = value[value.find('[') + 1:value.find(']')]
            colorBR = colorBR.split(',')
            colorBR = [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,
                colorBL, colorBR)
            dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, colorTL,
                colorTR, colorBL, colorBR)
            dbString = ''.join(dbString.split(' '))
            return render_template('testpuzzle.html', colors=puzzle,
                tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.
                format(colorTL[0], colorTL[1], colorTL[2]), UR='{},{},{}'.
                format(colorTR[0], colorTR[1], colorTR[2]), LL='{},{},{}'.
                format(colorBL[0], colorBL[1], colorBL[2]), LR='{},{},{}'.
                format(colorBR[0], colorBR[1], colorBR[2]), puzzleInfo=dbString
                )
    else:
        return redirect(url_for('home'))


@app.route('/tutorial', methods=['GET', 'POST'])
def tutorial():
    rows = 4
    columns = 4
    colorTL = [124, 10, 2]
    colorTR = [255, 8, 0]
    colorBL = [237, 41, 57]
    colorBR = [250, 128, 114]
    puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR, colorBL, colorBR
        )
    return render_template('tutorial.html', colors=puzzle, tile_size=
        '{}x{}'.format(rows, columns), UL='{},{},{}'.format(colorTL[0],
        colorTL[1], colorTL[2]), UR='{},{},{}'.format(colorTR[0], colorTR[1
        ], colorTR[2]), LL='{},{},{}'.format(colorBL[0], colorBL[1],
        colorBL[2]), LR='{},{},{}'.format(colorBR[0], colorBR[1], colorBR[2]))


@app.route('/completed', methods=['GET', 'POST'])
def completed():
    flash('Tutorial Complete')
    return redirect(url_for('home'))


if __name__ == '__main__':
    app.run(debug=True)

from flask import Flask, render_template, request, session, url_for, redirect, flash
from os import urandom
from util import db_updater as update
from util import db_search as search
from util import db_builder as builder
from util import colors
from util import weatherColors
from passlib.hash import sha256_crypt
import ssl
import urllib
import json
import random
import sqlite3
app = Flask(__name__)
app.secret_key = urandom(32)


@app.route('/', methods=['GET', 'POST'])
def home():
    builder.main()
    if 'username' in session:
        return redirect(url_for('authPage'))
    else:
        return render_template('auth.html')


@app.route('/logout')
def logout():
    """
    Logs user out of session by popping them from the session. Returns user to log-in screen
    """
    if 'username' in session:
        session.pop('username')
        return redirect(url_for('authPage'))
    else:
        return redirect(url_for('home'))


@app.route('/auth', methods=['GET', 'POST'])
def authPage():
    """
    Authenticates user signing in. Checks to see if password is correct or not;
    if correct, logs user in. If not, flashes "incorrect credentials"
    """
    if 'username' in session:
        username = session['username']
        """score = search.score(username)[0]
        scores = search.highScores()
        counter = 0
        highScores = []
        userNames = []

        while counter < len(scores):
            highScores.append(scores[counter][0])
            userNames.append(scores[counter][1])
            counter += 1
        """
        return redirect(url_for('genWeather'))
    else:
        try:
            username = request.form['username']
            password = search.password(username)
            print(password)
            if password == None:
                flash('Wrong Username or Password!')
                return redirect(url_for('home'))
            elif sha256_crypt.verify(request.form['password'], password[0]):
                session['username'] = username
                return redirect(url_for('authPage'))
            else:
                flash('Wrong Username or Password!')
                return redirect(url_for('home'))
        except:
            return redirect(url_for('home'))


@app.route('/reg', methods=['GET', 'POST'])
def reg():
    """
    Loads the template that takes information and allows user to register,
    creating a new account that they can sign into session with
    """
    if 'username' in session:
        return redirect(url_for('authPage'))
    return render_template('reg.html')


@app.route('/added', methods=['GET', 'POST'])
def added():
    """
    Checks to see if username is unique,
    flashes "username taken" if it is,
    adds user and password to database if not and sends to home
    """
    try:
        if request.form['password'] == request.form['confirmpassword']:
            newUsername = request.form['username']
            newPassword = sha256_crypt.encrypt(request.form['password'])
            userList = search.username(newUsername)
            if userList == []:
                update.adduser(newUsername, newPassword)
                flash('Register Successful')
                return redirect(url_for('tutorial'))
            else:
                flash('Username Taken')
                return redirect(url_for('reg'))
        else:
            flash("Passwords don't match")
            return redirect(url_for('reg'))
    except:
        return redirect(url_for('home'))


@app.route('/weatherPuz', methods=['GET', 'POST'])
def genWeather():
    """
    Generates a featured puzzle based on the current weather of the user and puts
    it on the home page
    """
    if 'username' not in session:
        return redirect(url_for('home'))
    else:
        name = session['username']
        rows = 8
        columns = 8
        palette = weatherColors.makePuzOnWeather()
        puzzle = colors.puzzleGen(rows, columns, palette[0], palette[1],
            palette[2], palette[3])
        print('-------weather----------')
        print('{},{},{}'.format(palette[0][0], palette[0][1], palette[0][2]))
        return render_template('home.html', Name=name, colors=puzzle,
            tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.format(
            palette[0][0], palette[0][1], palette[0][2]), UR='{},{},{}'.
            format(palette[1][0], palette[1][1], palette[1][2]), LL=
            '{},{},{}'.format(palette[2][0], palette[2][1], palette[2][2]),
            LR='{},{},{}'.format(palette[3][0], palette[3][1], palette[3][2]))


@app.route('/random', methods=['GET', 'POST'])
def random():
    """
    Creates a random puzzle for users to play.
    """
    if 'username' not in session:
        return redirect(url_for('home'))
    rows = 8
    columns = 8
    palette = colors.getpalette(4)
    puzzle = colors.puzzleGen(rows, columns, palette[0], palette[1],
        palette[2], palette[3])
    dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, palette[0], palette[1],
        palette[2], palette[3])
    dbString = ''.join(dbString.split(' '))
    return render_template('testpuzzle.html', colors=puzzle, tile_size=
        '{}x{}'.format(rows, columns), UL='{},{},{}'.format(palette[0][0],
        palette[0][1], palette[0][2]), UR='{},{},{}'.format(palette[1][0],
        palette[1][1], palette[1][2]), LL='{},{},{}'.format(palette[2][0],
        palette[2][1], palette[2][2]), LR='{},{},{}'.format(palette[3][0],
        palette[3][1], palette[3][2]), puzzleInfo=dbString)


@app.route('/puzzles', methods=['GET', 'POST'])
def puzzles():
    if 'username' in session:
        puzzles = search.getAllPuzzles()
        i = 0
        while i < len(puzzles):
            puzzles[i] = puzzles[i].split(';')
            puzzles[i] = ','.join(puzzles[i])
            print(puzzles[i])
            i += 1
        return render_template('puzzles.html', puzzles=puzzles)
    else:
        return redirect(url_for('home'))


@app.route('/save', methods=['GET', 'POST'])
def save():
    if 'username' not in session:
        return redirect(url_for('home'))
    dbString = request.form['dbStr']
    moves = request.form['moves']
    if dbString not in search.getAllPuzzles():
        update.addPuzzle(dbString, moves)
        puzId = search.getPuzzleID(dbString)
        update.updateAverageMovesPuzzle(moves, puzId)
        update.addLog(session['username'], moves, puzId)
    return redirect(url_for('authPage'))


@app.route('/customize', methods=['GET', 'POST'])
def custom():
    if 'username' in session:
        colorList = [[], [], [], []]
        for i in range(4):
            palette = colors.getpalette(4)
            for j in range(4):
                colorList[i].append('rgb(' + str(palette[j][0]) + ',' + str
                    (palette[j][1]) + ',' + str(palette[j][2]) + ')')
        """
        colorList0=[]
        i=0
        while i<4:
            colorList0.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList1=[]
        while i<4:
            colorList1.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList2=[]
        while i<4:
            colorList2.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList3=[]
        while i<4:
            colorList3.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
"""
        return render_template('customize.html', colors0=colorList[0],
            colors1=colorList[1], colors2=colorList[2], colors3=colorList[3])
    else:
        return redirect(url_for('home'))


@app.route('/play', methods=['GET', 'POST'])
def play():
    if 'username' in session:
        try:
            size = request.form['size']
            size1 = size.split('x')
            rows = int(size1[0])
            columns = int(size1[1])
            colorTL = request.form['tlcolor'][4:len(request.form['tlcolor']
                ) - 1]
            colorTL = colorTL.split(',')
            colorTL = [int(i) for i in colorTL]
            colorTR = request.form['trcolor'][4:len(request.form['trcolor']
                ) - 1]
            colorTR = colorTR.split(',')
            colorTR = [int(i) for i in colorTR]
            colorBL = request.form['blcolor'][4:len(request.form['blcolor']
                ) - 1]
            colorBL = colorBL.split(',')
            colorBL = [int(i) for i in colorBL]
            colorBR = request.form['brcolor'][4:len(request.form['brcolor']
                ) - 1]
            colorBR = colorBR.split(',')
            colorBR = [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,
                colorBL, colorBR)
            dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, colorTL,
                colorTR, colorBL, colorBR)
            dbString = ''.join(dbString.split(' '))
            print('-------colorTL--------')
            print('{},{},{}'.format(colorTL[0], colorTL[1], colorTL[2]))
            return render_template('playpuzzle.html', colors=puzzle,
                tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.
                format(colorTL[0], colorTL[1], colorTL[2]), UR='{},{},{}'.
                format(colorTR[0], colorTR[1], colorTR[2]), LL='{},{},{}'.
                format(colorBL[0], colorBL[1], colorBL[2]), LR='{},{},{}'.
                format(colorBR[0], colorBR[1], colorBR[2]), puzzleInfo=dbString
                )
        except:
            value = request.form['value']
            rows = int(value[0])
            columns = int(value[2])
            colorTL = value[value.find('[') + 1:value.find(']')]
            colorTL = colorTL.split(',')
            colorTL = [int(i) for i in colorTL]
            value = value[value.find(']') + 1:]
            colorTR = value[value.find('[') + 1:value.find(']')]
            colorTR = colorTR.split(',')
            colorTR = [int(i) for i in colorTR]
            value = value[value.find(']') + 1:]
            colorBL = value[value.find('[') + 1:value.find(']')]
            colorBL = colorBL.split(',')
            colorBL = [int(i) for i in colorBL]
            value = value[value.find(']') + 1:]
            colorBR = value[value.find('[') + 1:value.find(']')]
            colorBR = colorBR.split(',')
            colorBR = [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,
                colorBL, colorBR)
            dbString = '%s;%s;%s;%s;%s;%s' % (rows, columns, colorTL,
                colorTR, colorBL, colorBR)
            dbString = ''.join(dbString.split(' '))
            return render_template('testpuzzle.html', colors=puzzle,
                tile_size='{}x{}'.format(rows, columns), UL='{},{},{}'.
                format(colorTL[0], colorTL[1], colorTL[2]), UR='{},{},{}'.
                format(colorTR[0], colorTR[1], colorTR[2]), LL='{},{},{}'.
                format(colorBL[0], colorBL[1], colorBL[2]), LR='{},{},{}'.
                format(colorBR[0], colorBR[1], colorBR[2]), puzzleInfo=dbString
                )
    else:
        return redirect(url_for('home'))


@app.route('/tutorial', methods=['GET', 'POST'])
def tutorial():
    rows = 4
    columns = 4
    colorTL = [124, 10, 2]
    colorTR = [255, 8, 0]
    colorBL = [237, 41, 57]
    colorBR = [250, 128, 114]
    puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR, colorBL, colorBR
        )
    return render_template('tutorial.html', colors=puzzle, tile_size=
        '{}x{}'.format(rows, columns), UL='{},{},{}'.format(colorTL[0],
        colorTL[1], colorTL[2]), UR='{},{},{}'.format(colorTR[0], colorTR[1
        ], colorTR[2]), LL='{},{},{}'.format(colorBL[0], colorBL[1],
        colorBL[2]), LR='{},{},{}'.format(colorBR[0], colorBR[1], colorBR[2]))


@app.route('/completed', methods=['GET', 'POST'])
def completed():
    flash('Tutorial Complete')
    return redirect(url_for('home'))


if __name__ == '__main__':
    app.run(debug=True)

# UncommonApp
# Dennis Chen, T Fabiha, Addison Huang, Michelle Tang
# P#02: The End

from flask import Flask,render_template,request,session,url_for,redirect,flash

from os import urandom
from util import db_updater as update
from util import db_search as search
from util import db_builder as builder
from util import colors
from util import weatherColors
from passlib.hash import sha256_crypt

import ssl
import urllib
import json
import random

import sqlite3 #imports sqlite

app = Flask(__name__)
app.secret_key = urandom(32)
#----------------------------------------------------------home--------------------------------------------------------
@app.route("/",methods=['GET','POST'])
def home():
    builder.main()
    if 'username' in session: #if user is logged in
        return redirect(url_for('authPage'))
    else:
        return render_template('auth.html')

#----------------------------------------------------------login/register/logout--------------------------------------------------------
@app.route("/logout")
def logout():
    '''
    Logs user out of session by popping them from the session. Returns user to log-in screen
    '''
    if 'username' in session:
        session.pop('username')
        return redirect(url_for('authPage'))
    else:
        return redirect(url_for('home'))

@app.route("/auth",methods=['GET','POST'])
def authPage():
    '''
    Authenticates user signing in. Checks to see if password is correct or not;
    if correct, logs user in. If not, flashes "incorrect credentials"
    '''
    if 'username' in session:
        username = session['username']
        '''score = search.score(username)[0]
        scores = search.highScores()
        counter = 0
        highScores = []
        userNames = []

        while counter < len(scores):
            highScores.append(scores[counter][0])
            userNames.append(scores[counter][1])
            counter += 1
        '''
        return redirect(url_for('genWeather'))
    else:
        try:
            username=request.form['username'] #username
            password = search.password(username) #password that matches the username
            print(password)
            if password == None: #if credentials are incorrect
                flash('Wrong Username or Password!')
                return redirect(url_for('home')) #redirects
            elif sha256_crypt.verify(request.form['password'], password[0]): #if password is correct, login
                session['username'] = username
                return redirect(url_for('authPage'))
            else: #else credentials are wrong
                flash('Wrong Username or Password!')
                return redirect(url_for('home'))
        except:
            return redirect(url_for('home'))

@app.route("/reg",methods=['GET','POST'])
def reg():
    '''
    Loads the template that takes information and allows user to register,
    creating a new account that they can sign into session with
    '''
    if 'username' in session:
        return redirect(url_for('authPage'))

    return render_template('reg.html')

#----------------------------------------------------------database--------------------------------------------------------
@app.route("/added",methods=['GET','POST'])
def added():
    '''
    Checks to see if username is unique,
    flashes "username taken" if it is,
    adds user and password to database if not and sends to home
    '''
    try:
        if request.form['password'] == request.form['confirmpassword']:
            newUsername = request.form['username']
            newPassword = sha256_crypt.encrypt(request.form['password']) #encrypts password
            userList = search.username(newUsername)
            if userList == [] : #if username isn't taken
                update.adduser(newUsername,newPassword) #add to database
                flash('Register Successful')
                return redirect(url_for('tutorial'))
            else: #if username is taken
                flash('Username Taken')
                return redirect(url_for('reg'))
        else:
            flash("Passwords don't match")
            return redirect(url_for('reg'))
    except:
        return redirect(url_for('home'))

#-------------------------------------------featured puzzle--------------------------------------------------------
@app.route('/weatherPuz',methods = ['GET','POST'])
def genWeather():
    '''
    Generates a featured puzzle based on the current weather of the user and puts
    it on the home page
    '''
    if 'username' not in session:
        return redirect(url_for('home'))
    else:
        name = session['username']
        rows = 8
        columns = 8
        palette = weatherColors.makePuzOnWeather()
        puzzle = colors.puzzleGen(rows,columns,palette[0],palette[1],palette[2],palette[3])
        print('-------weather----------')
        print("{},{},{}".format(palette[0][0],palette[0][1],palette[0][2]))
        return render_template('home.html',
                               Name = name,
                               colors = puzzle,
                               tile_size = "{}x{}".format(rows, columns), # size "widthxheigth"
                               UL = "{},{},{}".format(palette[0][0],
                                                      palette[0][1],
                                                      palette[0][2]), # upper-left color
                               UR = "{},{},{}".format(palette[1][0],
                                                      palette[1][1],
                                                      palette[1][2]), # upper-right color
                               LL = "{},{},{}".format(palette[2][0],
                                                      palette[2][1],
                                                      palette[2][2]), # lower-left color
                               LR = "{},{},{}".format(palette[3][0],
                                                      palette[3][1],
                                                      palette[3][2])) # lower-right color
    #-------------------------------------------create puzzle--------------------------------------------------------
@app.route('/random',methods = ['GET','POST'])
def random():
    '''
    Creates a random puzzle for users to play.
    '''
    if 'username' not in session:
        return redirect(url_for('home'))
    rows = 8
    columns = 8

    palette = colors.getpalette(4)
    puzzle = colors.puzzleGen(rows, columns,
                              palette[0], palette[1],
                              palette[2], palette[3])
    dbString = "%s;%s;%s;%s;%s;%s" % (rows,columns, palette[0], palette[1], palette[2], palette[3])
    dbString = "".join(dbString.split(" "))
    return render_template('testpuzzle.html',
                           colors = puzzle,
                           tile_size = "{}x{}".format(rows, columns), # size "widthxheigth"
                           UL = "{},{},{}".format(palette[0][0],
                                                  palette[0][1],
                                                  palette[0][2]), # upper-left color
                           UR = "{},{},{}".format(palette[1][0],
                                                  palette[1][1],
                                                  palette[1][2]), # upper-right color
                           LL = "{},{},{}".format(palette[2][0],
                                                  palette[2][1],
                                                  palette[2][2]), # lower-left color
                           LR = "{},{},{}".format(palette[3][0],
                                                  palette[3][1],
                                                  palette[3][2]), # lower-right color
                           puzzleInfo = dbString)
                           # avgMoves = search.highScores(search.getPuzzleID(dbString)))
                           #
                           # updateAverageMovesPuzzle(moves,puzzleID):
#-------------------------------------------liked puzzles page--------------------------------------------------------
@app.route('/puzzles', methods = ["GET","POST"])
def puzzles():
    if 'username' in session: #if user is logged in

        # likedPuz = db_search.getLikedPuzzles(session['username'])
        # puzzles = []
        # for puz in likedPuz:
        #     puzzle = puzzle.split(';')
        #     rows = int(puzzle[0])
        #     cols = int(puzzle[1])
        #     ULC = json.loads(puzzle[2])
        #     URC = json.loads(puzzle[3])
        #     LLC = json.loads(puzzle[4])
        #     LRC = json.loads(puzzle[5])
        #     puzzle = colors.puzzleGen(rows,cols,ULC,URC,LLC,LRC)
        #     puzzles.append(puzzle)

        puzzles=search.getAllPuzzles()
        i = 0;
        while i < len(puzzles):
            puzzles[i]=puzzles[i].split(';')
            puzzles[i]=','.join(puzzles[i])
            print(puzzles[i])
            i += 1
        return render_template('puzzles.html',puzzles = puzzles)
    else:
        return redirect(url_for('home'))

#-------------------------------------------save puzzle--------------------------------------------------------
@app.route('/save', methods = ["GET","POST"])
def save():
    if 'username' not in session:
        return redirect(url_for('home'))
    dbString = request.form['dbStr']
    moves = request.form['moves']
    if dbString not in search.getAllPuzzles():
        update.addPuzzle(dbString,moves)
        puzId = search.getPuzzleID(dbString)
        update.updateAverageMovesPuzzle(moves,puzId)
        update.addLog(session['username'],moves,puzId)
    return redirect(url_for('authPage'))

#-------------------------------------------customize--------------------------------------------------------
@app.route('/customize',methods = ["GET","POST"])
def custom():
    if 'username' in session:
        #palette=colors.getpalette(4)
        #print(palette[0])
        colorList = [[],[],[],[]]

        for i in range(4):
            palette = colors.getpalette(4);

            for j in range(4):
                colorList[i].append('rgb('+str(palette[j][0]) +","+str(palette[j][1])+"," +str(palette[j][2]) +')')


        """
        colorList0=[]
        i=0
        while i<4:
            colorList0.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList1=[]
        while i<4:
            colorList1.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList2=[]
        while i<4:
            colorList2.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
            palette=colors.getpalette(4)
        i=0
        colorList3=[]
        while i<4:
            colorList3.append('rgb('+str(palette[i][0]) +","+str(palette[i][1])+"," +str(palette[i][2]) +')')
            i = i + 1
"""
        return render_template('customize.html', colors0=colorList[0], colors1=colorList[1], colors2=colorList[2], colors3=colorList[3])
    else:
        return redirect(url_for('home'))

@app.route('/play', methods=["GET","POST"])
def play():
    if 'username' in session:
        try:
            size = request.form['size']
            size1=size.split('x')
            rows = int(size1[0])
            columns = int(size1[1])
            colorTL = request.form['tlcolor'][4:len(request.form['tlcolor'])-1]
            colorTL = colorTL.split(",")
            colorTL = [int(i) for i in colorTL]
            colorTR = request.form['trcolor'][4:len(request.form['trcolor'])-1]
            colorTR = colorTR.split(",")
            colorTR = [int(i) for i in colorTR]
            colorBL = request.form['blcolor'][4:len(request.form['blcolor'])-1]
            colorBL = colorBL.split(",")
            colorBL = [int(i) for i in colorBL]
            colorBR = request.form['brcolor'][4:len(request.form['brcolor'])-1]
            colorBR = colorBR.split(",")
            colorBR = [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,colorBL, colorBR)
            dbString = "%s;%s;%s;%s;%s;%s" % (rows,columns, colorTL, colorTR, colorBL, colorBR)
            dbString = "".join(dbString.split(" "))
            print('-------colorTL--------')
            print("{},{},{}".format(colorTL[0],colorTL[1],colorTL[2]))
            return render_template('playpuzzle.html',
                                   colors = puzzle,
                                   tile_size = "{}x{}".format(rows, columns), # size "widthxheigth"
                                   UL = "{},{},{}".format(colorTL[0],
                                                  colorTL[1],
                                                  colorTL[2]), # upper-left color
                                   UR = "{},{},{}".format(colorTR[0],
                                                  colorTR[1],
                                                  colorTR[2]), # upper-right color
                                   LL = "{},{},{}".format(colorBL[0],
                                                  colorBL[1],
                                                  colorBL[2]), # lower-left color
                                   LR = "{},{},{}".format(colorBR[0],
                                                  colorBR[1],
                                                  colorBR[2]), # lower-right color
                                   puzzleInfo = dbString)
        except:
            value = request.form['value']
            rows = int(value[0])
            columns = int(value[2])
            colorTL = value[value.find("[")+1:value.find("]")]
            colorTL = colorTL.split(",")
            colorTL= [int(i) for i in colorTL]
            value = value[value.find("]")+1:]
            colorTR = value[value.find("[")+1:value.find("]")]
            colorTR = colorTR.split(",")
            colorTR= [int(i) for i in colorTR]
            value = value[value.find("]")+1:]
            colorBL = value[value.find("[")+1:value.find("]")]
            colorBL = colorBL.split(",")
            colorBL= [int(i) for i in colorBL]
            value = value[value.find("]")+1:]
            colorBR = value[value.find("[")+1:value.find("]")]
            colorBR = colorBR.split(",")
            colorBR= [int(i) for i in colorBR]
            puzzle = colors.puzzleGen(rows, columns, colorTL, colorTR,colorBL, colorBR)
            dbString = "%s;%s;%s;%s;%s;%s" % (rows,columns, colorTL, colorTR, colorBL, colorBR)
            dbString = "".join(dbString.split(" "))
            return render_template('testpuzzle.html',
                                   colors = puzzle,
                                   tile_size = "{}x{}".format(rows, columns), # size "widthxheigth"
                                   UL = "{},{},{}".format(colorTL[0],
                                                  colorTL[1],
                                                  colorTL[2]), # upper-left color
                                   UR = "{},{},{}".format(colorTR[0],
                                                  colorTR[1],
                                                  colorTR[2]), # upper-right color
                                   LL = "{},{},{}".format(colorBL[0],
                                                  colorBL[1],
                                                  colorBL[2]), # lower-left color
                                   LR = "{},{},{}".format(colorBR[0],
                                                  colorBR[1],
                                                  colorBR[2]), # lower-right color
                                   puzzleInfo = dbString)


    else:
        return redirect(url_for('home'))

@app.route('/tutorial', methods=["GET","POST"])
def tutorial():
    rows=4
    columns = 4
    colorTL = [124,10,2]
    colorTR = [255,8,0]
    colorBL = [237,41,57]
    colorBR = [250,128,114]
    puzzle = colors.puzzleGen(rows,columns,colorTL,colorTR,colorBL,colorBR)
    return render_template('tutorial.html',
                                   colors = puzzle,
                                   tile_size = "{}x{}".format(rows, columns), # size "widthxheigth"
                                   UL = "{},{},{}".format(colorTL[0],
                                                  colorTL[1],
                                                  colorTL[2]), # upper-left color
                                   UR = "{},{},{}".format(colorTR[0],
                                                  colorTR[1],
                                                  colorTR[2]), # upper-right color
                                   LL = "{},{},{}".format(colorBL[0],
                                                  colorBL[1],
                                                  colorBL[2]), # lower-left color
                                   LR = "{},{},{}".format(colorBR[0],
                                                  colorBR[1],
                                                  colorBR[2])) # lower-right color)

@app.route('/completed', methods=["GET","POST"])
def completed():
    flash('Tutorial Complete')
    return redirect(url_for('home'))




if __name__ == '__main__':
    app.run(debug=True)
