Skip to content

Commit

Permalink
Futurize stage2
Browse files Browse the repository at this point in the history
  • Loading branch information
jkenlooper committed Apr 13, 2019
1 parent 6ba692c commit f5e2d5e
Show file tree
Hide file tree
Showing 26 changed files with 87 additions and 41 deletions.
1 change: 1 addition & 0 deletions api/api/artist.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import map
import os

import redis
Expand Down
3 changes: 2 additions & 1 deletion api/api/bit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import str
from random import randint

from flask import current_app, abort, json, redirect, make_response, request
Expand Down Expand Up @@ -56,7 +57,7 @@ def get(self):
# List of bit icon names that are available
result = cur.execute(fetch_query_string('select_random_bit_batch.sql'), {'offset_time': offset_time}).fetchall()
(result, col_names) = rowify(result, cur.description)
bits = map(lambda x: x['icon'], result)
bits = [x['icon'] for x in result]


response = make_response(encoder.encode({'data':bits}), 200)
Expand Down
3 changes: 2 additions & 1 deletion api/api/database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import
from builtins import zip
import os

from flask import current_app
Expand Down Expand Up @@ -37,7 +38,7 @@ def rowify(l, description):
if l != None and description != None:
col_names = [x[0] for x in description]
for row in l:
d.append(dict(zip(col_names, row)))
d.append(dict(list(zip(col_names, row))))
return (d, col_names)

# TODO: deprecate
Expand Down
6 changes: 5 additions & 1 deletion api/api/flask_secure_cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

from __future__ import absolute_import

from builtins import zip
from builtins import str
from past.builtins import basestring
from builtins import object
import time
import re
import logging
Expand Down Expand Up @@ -49,7 +53,7 @@ def u(s):
return s.decode('unicode_escape')
# These names don't exist in py3, so use noqa comments to disable
# warnings in flake8.
unicode_type = unicode # noqa
unicode_type = str # noqa
basestring_type = basestring # noqa

def utf8(value):
Expand Down
1 change: 1 addition & 0 deletions api/api/janitor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import map
query_idle_puzzles = """
select * from Puzzle where m_date < datetime('now', '-10 minutes') order by m_date
"""
Expand Down
3 changes: 2 additions & 1 deletion api/api/jobs/convertPiecesToDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# This job should be ran by a janitor worker. It should find all puzzles in
# redis that haven't had any activity in the last week or so.

from builtins import input
import sys
import os.path
import math
Expand Down Expand Up @@ -97,6 +98,6 @@ def transferAll():
print('used_memory: {used_memory_human}'.format(**memory))

if __name__ == '__main__':
confirm = raw_input("Transfer all puzzle data out of redis and into sqlite database? y/n\n")
confirm = input("Transfer all puzzle data out of redis and into sqlite database? y/n\n")
if confirm == 'y':
transferAll()
2 changes: 1 addition & 1 deletion api/api/jobs/convertPiecesToRedis.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def convert(puzzle, db_file=None):
for piece in all_pieces:
#print('convert piece {id} for puzzle: {puzzle}'.format(**piece))
offsets = {}
for (k, v) in map(lambda x: x.split(':'), piece.get('adjacent', '').split(' ')):
for (k, v) in [x.split(':') for x in piece.get('adjacent', '').split(' ')]:
offsets[k] = v
#print offsets
# Add Piece Properties
Expand Down
3 changes: 2 additions & 1 deletion api/api/jobs/migrateFromDBv1_1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function
from builtins import map
import os.path
import math

Expand Down Expand Up @@ -108,7 +109,7 @@ def get_offset_of_adjacent_pieces(piece, mask_width, piece_width):
#print('convert piece {id} for puzzle: {puzzle}'.format(**piece))
offsets = get_offset_of_adjacent_pieces(piece, mask_width, piece_width)
# Create the adjacent pieces string from offsets
adjacent = ' '.join(map(lambda k, v: '{0}:{1}'.format(k, v), offsets.keys(), offsets.values()))
adjacent = ' '.join(map(lambda k, v: '{0}:{1}'.format(k, v), list(offsets.keys()), list(offsets.values())))
#print offsets
# Add Piece Properties
pc = {
Expand Down
4 changes: 3 additions & 1 deletion api/api/jobs/migratePuzzleFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- If source.unsplash; update description with links to photographer and unsplash. Include photo description on next line.
"""

from future import standard_library
standard_library.install_aliases()
import re
import sqlite3
import os
Expand All @@ -13,7 +15,7 @@
import time
import math
import random
from urlparse import urlparse
from urllib.parse import urlparse
import requests
import logging

Expand Down
21 changes: 13 additions & 8 deletions api/api/jobs/pieceRenderer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from __future__ import print_function
from __future__ import division
from builtins import map
from builtins import str
from builtins import range
from past.utils import old_div
import os
import json
import sys
Expand Down Expand Up @@ -190,15 +195,15 @@ def render(*args):

# Update the css file with dimensions for puzzle outline
cssfile = open(os.path.join(puzzle_dir, 'scale-100', 'raster.css'), 'a')
cssfile.write("[id=puzzle-outline]{{width:{width}px;height:{height}px;left:{left}px;top:{top}px;}}".format(width=pieces.width, height=pieces.height, left=int(round((tw - pieces.width) / 2)), top=int(round((th - pieces.height) / 2))))
cssfile.write("[id=puzzle-outline]{{width:{width}px;height:{height}px;left:{left}px;top:{top}px;}}".format(width=pieces.width, height=pieces.height, left=int(round(old_div((tw - pieces.width), 2))), top=int(round(old_div((th - pieces.height), 2)))))
cssfile.close()


# Get the top left piece by checking the bounding boxes
top_left_piece = "0"
minLeft = piece_bboxes[top_left_piece][0]
minTop = piece_bboxes[top_left_piece][1]
for key in piece_bboxes.keys():
for key in list(piece_bboxes.keys()):
if piece_bboxes[key][0] <= minLeft and piece_bboxes[key][1] <= minTop:
top_left_piece = key
minLeft = piece_bboxes[key][0]
Expand Down Expand Up @@ -226,8 +231,8 @@ def render(*args):
})

# Set the top left piece to the top left corner and make it immovable
piece_properties[top_left_piece]["x"] = int(round((tw - pieces.width) / 2))
piece_properties[top_left_piece]["y"] = int(round((th - pieces.height) / 2))
piece_properties[top_left_piece]["x"] = int(round(old_div((tw - pieces.width), 2)))
piece_properties[top_left_piece]["y"] = int(round(old_div((th - pieces.height), 2)))
piece_properties[top_left_piece]["status"] = 1
piece_properties[top_left_piece]["g"] = top_left_piece
# set row and col for finding the top left piece again after reset of puzzle
Expand Down Expand Up @@ -267,10 +272,10 @@ def render(*args):
filtered_adjacent_pieces = {}

# filter out the corner adjacent pieces
for target_id, target_adjacent_list in adjacent_pieces.items():
for target_id, target_adjacent_list in list(adjacent_pieces.items()):
target_bbox = piece_bboxes[target_id] # [0, 0, 499, 500]
target_center_x = target_bbox[0] + int(round((target_bbox[2] - target_bbox[0]) / 2))
target_center_y = target_bbox[1] + int(round((target_bbox[3] - target_bbox[1]) / 2))
target_center_x = target_bbox[0] + int(round(old_div((target_bbox[2] - target_bbox[0]), 2)))
target_center_y = target_bbox[1] + int(round(old_div((target_bbox[3] - target_bbox[1]), 2)))
filtered_adjacent_list = []
for adjacent_id in target_adjacent_list:
adjacent_bbox = piece_bboxes[adjacent_id] # [0, 347, 645, 996]
Expand Down Expand Up @@ -314,7 +319,7 @@ def render(*args):
x = piece_bboxes[adj_pc][0] - origin_x
y = piece_bboxes[adj_pc][1] - origin_y
offsets[adj_pc] = '{x},{y}'.format(x=x, y=y)
adjacent_str = ' '.join(map(lambda k, v: '{0}:{1}'.format(k, v), offsets.keys(), offsets.values()))
adjacent_str = ' '.join(map(lambda k, v: '{0}:{1}'.format(k, v), list(offsets.keys()), list(offsets.values())))
pc['adjacent'] = adjacent_str

# The original.jpg is assumed to be available locally because of migratePuzzleFile.py
Expand Down
19 changes: 12 additions & 7 deletions api/api/jobs/pieceTranslate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from __future__ import division
from builtins import map
from builtins import zip
from builtins import str
from past.utils import old_div
import os.path
import math
import time
Expand All @@ -15,7 +20,7 @@
PIECE_GROUP_MOVE_MAX_BEFORE_PENALTY = 5
MAX_RECENT_POINTS = 25
MAX_KARMA = 25
MIN_KARMA = (int(MAX_KARMA/2) * -1) # -12
MIN_KARMA = (int(old_div(MAX_KARMA,2)) * -1) # -12

POINTS_CAP = 15000

Expand Down Expand Up @@ -177,7 +182,7 @@ def updateGroupedPiecesPositions(puzzle, piece, pieceGroup, offsetX, offsetY, ne
pipe = redisConnection.pipeline(transaction=True)
for groupedPiece in allOtherPiecesInPieceGroup:
pipe.hmget('pc:{puzzle}:{groupedPiece}'.format(puzzle=puzzle, groupedPiece=groupedPiece), ['x', 'y'])
groupedPiecesXY = dict(zip(allOtherPiecesInPieceGroup, pipe.execute()))
groupedPiecesXY = dict(list(zip(allOtherPiecesInPieceGroup, pipe.execute())))
#print 'groupedPiecesXY'
#print groupedPiecesXY

Expand Down Expand Up @@ -233,12 +238,12 @@ def updateGroupedPiecesPositions(puzzle, piece, pieceGroup, offsetX, offsetY, ne
y = puzzleData['table_height']

# Save the origin position
(originX, originY) = map(int, redisConnection.hmget('pc:{puzzle}:{piece}'.format(puzzle=puzzle, piece=piece), ['x', 'y']))
(originX, originY) = list(map(int, redisConnection.hmget('pc:{puzzle}:{piece}'.format(puzzle=puzzle, piece=piece), ['x', 'y'])))

pieceGroup = redisConnection.hget('pc:{puzzle}:{piece}'.format(puzzle=puzzle, piece=piece), 'g')

# Check proximity to other pieces with unique groups
tolerance = int(100/2)
tolerance = int(old_div(100,2))
#print('{0} {1} {2}'.format('pcx:{puzzle}'.format(**locals()), x - tolerance, x + tolerance))
proximityX = set(map(int, redisConnection.zrangebyscore('pcx:{puzzle}'.format(puzzle=puzzle), x - tolerance, x + tolerance)))
proximityY = set(map(int, redisConnection.zrangebyscore('pcy:{puzzle}'.format(puzzle=puzzle), y - tolerance, y + tolerance)))
Expand Down Expand Up @@ -317,11 +322,11 @@ def updateGroupedPiecesPositions(puzzle, piece, pieceGroup, offsetX, offsetY, ne
p += formatPieceMovementString(piece, **pieceProperties)

# Get Adjacent Piece Properties
adjacentPiecesList = map(int, filter(lambda x: x not in ('x', 'y', 'r', 'w', 'h', 'b', 'rotate', 'g', 's'), pieceProperties.keys()))
adjacentPiecesList = list(map(int, [x for x in list(pieceProperties.keys()) if x not in ('x', 'y', 'r', 'w', 'h', 'b', 'rotate', 'g', 's')]))
pipe = redisConnection.pipeline(transaction=False)
for adjacentPiece in adjacentPiecesList:
pipe.hgetall('pc:{puzzle}:{adjacentPiece}'.format(puzzle=puzzle, adjacentPiece=adjacentPiece))
adjacentPieceProperties = dict(zip(adjacentPiecesList, pipe.execute()))
adjacentPieceProperties = dict(list(zip(adjacentPiecesList, pipe.execute())))
#print adjacentPieceProperties

# Check if piece is close enough to any adjacent piece
Expand All @@ -335,7 +340,7 @@ def updateGroupedPiecesPositions(puzzle, piece, pieceGroup, offsetX, offsetY, ne
#print('Skipping since adjacent piece in same group')
continue

(offsetFromPieceX, offsetFromPieceY) = map(int, pieceProperties.get(str(adjacentPiece)).split(','))
(offsetFromPieceX, offsetFromPieceY) = list(map(int, pieceProperties.get(str(adjacentPiece)).split(',')))
targetX = offsetFromPieceX + int(pieceProperties['x'])
targetY = offsetFromPieceY + int(pieceProperties['y'])
adjacentPieceProps = adjacentPieceProperties.get(adjacentPiece)
Expand Down
3 changes: 2 additions & 1 deletion api/api/piece.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import
from builtins import zip
from flask import abort, json, current_app
from flask.views import MethodView
import redis
Expand Down Expand Up @@ -44,5 +45,5 @@ def get(self, puzzle_id, piece):
# Fetch just the piece properties
publicPieceProperties = ('x', 'y', 'rotate', 's', 'w', 'h', 'b')
pieceProperties = redisConnection.hmget('pc:{puzzle}:{piece}'.format(puzzle=puzzle, piece=piece), *publicPieceProperties)
pieceData = dict(zip(publicPieceProperties, pieceProperties))
pieceData = dict(list(zip(publicPieceProperties, pieceProperties)))
return encoder.encode(pieceData)
3 changes: 2 additions & 1 deletion api/api/pieces.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import print_function
from __future__ import absolute_import
from builtins import zip
import time
import uuid

Expand Down Expand Up @@ -120,7 +121,7 @@ def get(self, puzzle_id):
pipe.hmget('pc:{puzzle}:{piece}'.format(puzzle=puzzle, piece=piece), *publicPieceProperties)
allPublicPieceProperties = pipe.execute()
# convert the list of lists into list of dicts. Only want to return the public piece props.
pieces = map(lambda properties: dict(zip(publicPieceProperties, properties)), allPublicPieceProperties)
pieces = [dict(list(zip(publicPieceProperties, properties))) for properties in allPublicPieceProperties]
# TODO: Change piece properties to int type instead of string
for item in all_pieces:
piece = item.get('id')
Expand Down
16 changes: 10 additions & 6 deletions api/api/publish.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from __future__ import absolute_import
from __future__ import division
from builtins import str
from builtins import map
from past.utils import old_div
import datetime
import time
import uuid
Expand All @@ -25,7 +29,7 @@

BLOCKEDPLAYER_EXPIRE_TIMEOUT = HOUR
MAX_KARMA = 25
MIN_KARMA = (int(MAX_KARMA/2) * -1) # -12
MIN_KARMA = (int(old_div(MAX_KARMA,2)) * -1) # -12
MOVES_BEFORE_PENALTY = 12
STACK_PENALTY = 1
HOTSPOT_EXPIRE = 30
Expand Down Expand Up @@ -249,7 +253,7 @@ def get(self, puzzle_id, piece):
redisConnection.set('token:{}'.format(user), '{puzzle}:{piece}:{mark}'.format(puzzle=puzzle, piece=piece, mark=mark), ex=TOKEN_LOCK_TIMEOUT)

# Claim the piece by showing the bit icon next to it.
(x, y) = map(int, redisConnection.hmget('pc:{puzzle}:{piece}'.format(puzzle=puzzle, piece=piece), ['x', 'y']))
(x, y) = list(map(int, redisConnection.hmget('pc:{puzzle}:{piece}'.format(puzzle=puzzle, piece=piece), ['x', 'y'])))
msg = formatBitMovementString(user, x, y)
redisConnection.publish(u'move:{0}'.format(puzzle_id), msg)

Expand Down Expand Up @@ -303,7 +307,7 @@ def patch(self, puzzle_id, piece):
if request.form:
args.update(request.form.to_dict(flat=True))

if len(args.keys()) == 0:
if len(list(args.keys())) == 0:
err_msg = {
'msg': "invalid args",
'type': "invalid",
Expand All @@ -312,7 +316,7 @@ def patch(self, puzzle_id, piece):
}
return make_response(encoder.encode(err_msg), 400)
# check if args are only in acceptable set
if len(self.ACCEPTABLE_ARGS.intersection(set(args.keys()))) != len(args.keys()):
if len(self.ACCEPTABLE_ARGS.intersection(set(args.keys()))) != len(list(args.keys())):
err_msg = {
'msg': "invalid args",
'type': "invalid",
Expand All @@ -321,7 +325,7 @@ def patch(self, puzzle_id, piece):
}
return make_response(encoder.encode(err_msg), 400)
# validate that all values are int
for key, value in args.items():
for key, value in list(args.items()):
if not isinstance(value, int):
try:
args[key] = int(value)
Expand Down Expand Up @@ -544,7 +548,7 @@ def patch(self, puzzle_id, piece):
# what pieceTranslate does. This includes immovable pieces, and the
# pieces own group which would normally be filtered out when checking
# if the piece can be joined.
(pieceWidth, pieceHeight) = map(int, redisConnection.hmget('pc:{puzzle}:{piece}'.format(puzzle=puzzle, piece=piece), ['w', 'h']))
(pieceWidth, pieceHeight) = list(map(int, redisConnection.hmget('pc:{puzzle}:{piece}'.format(puzzle=puzzle, piece=piece), ['w', 'h'])))
toleranceX = min(pieceWidth, 200)
toleranceY = min(pieceHeight, 200)
proximityX = set(map(int, redisConnection.zrangebyscore('pcx:{puzzle}'.format(puzzle=puzzle), int(x) - toleranceX, int(x) + toleranceX)))
Expand Down
4 changes: 3 additions & 1 deletion api/api/rebuild.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from __future__ import absolute_import
from __future__ import division
from past.utils import old_div
import os
from random import randint

Expand Down Expand Up @@ -96,7 +98,7 @@ def post(self):
im = Image.open(imagefile)
(width, height) = im.size
im.close()
max_pieces_that_will_fit = int((width/MIN_PIECE_SIZE)*(height/MIN_PIECE_SIZE))
max_pieces_that_will_fit = int((old_div(width,MIN_PIECE_SIZE))*(old_div(height,MIN_PIECE_SIZE)))

# The user points for rebuilding the puzzle is decreased by the piece
# count for the puzzle. Use at least 200 points for smaller puzzles.
Expand Down
3 changes: 2 additions & 1 deletion api/api/reset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import print_function
from __future__ import absolute_import
from builtins import range
from random import randint

from flask import current_app, redirect, request, make_response, abort, request
Expand Down Expand Up @@ -102,7 +103,7 @@ def post(self):

(result, col_names) = rowify(cur.execute(query_select_top_left_piece, {'puzzle': puzzleData['id']}).fetchall(), cur.description)
topLeftPiece = result[0]
allPiecesExceptTopLeft = range(0, puzzleData['pieces'])
allPiecesExceptTopLeft = list(range(0, puzzleData['pieces']))
allPiecesExceptTopLeft.remove(topLeftPiece['id'])

# Create a pipe for buffering commands and disable atomic transactions
Expand Down
4 changes: 2 additions & 2 deletions api/api/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def broadcast(puzzle, message):
print('clients: {0}'.format(len(ws.handler.server.clients)))
# TODO: With redis set the count of total players on the site as well as for each puzzle.

for (key, client) in ws.handler.server.clients.items():
for (key, client) in list(ws.handler.server.clients.items()):
if client.ws.closed:
# TODO: I don't think this happens. This code block hasn't been tested.
print('remove closed client {0}'.format(key))
Expand Down Expand Up @@ -120,7 +120,7 @@ def broadcast(puzzle, message):
# Close all connections for this puzzle if it's stale
if (ws.handler.environ['last_activity'] + 60) < int(time.time()):
print("close all stale connections for puzzle {0}".format(puzzle))
for (key, client) in ws.handler.server.clients.items():
for (key, client) in list(ws.handler.server.clients.items()):
if client.ws.path.find(puzzle) != -1:
ws.handler.server.clients.pop(key)

Expand Down
3 changes: 2 additions & 1 deletion api/api/test_publish.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import range
import unittest
from random import randint
from time import sleep
Expand Down Expand Up @@ -29,7 +30,7 @@ def make_puzzle(**kw):
}
puzzle.update(kw)
pieces = []
for p in xrange(0, puzzle['pieces']):
for p in range(0, puzzle['pieces']):
pieces.append({
'id': p + 1,
'puzzle': 1,
Expand Down
Loading

0 comments on commit f5e2d5e

Please sign in to comment.