#!/usr/bin/python
from redis import Redis
from random import shuffle, choice
import uuid
import time

def getPickyness(redis,searchers):
    picky = {}
    allchars = redis.smembers('all-chars')
    for session in searchers:
        picky[session] = redis.smembers('session.%s.picky' % session)
        if len(picky[session])==0:
            picky[session] = allchars
    return picky

def shuffled(seq):
    local = list(seq)
    shuffle(local)
    return local

def match(picky, first, second):
    chat=str(uuid.uuid4()).replace('-','')
    redis.hset('chat.'+chat+'.meta', 'type', 'unsaved')
    redis.set('session.'+first+'.match', chat)
    redis.set('session.'+second+'.match', chat)
    redis.zrem('searchers', first)
    redis.zrem('searchers', second)
    del picky[first]
    del picky[second]

def matchUser(session, picky, identities):
    acceptable = []
    try:
        wants = picky[session]
    except KeyError:
        return # we've already been matched
    whoiam = identities[session]
    for other, their_wants in picky.items():
        if other!=session and identities[other] in wants and whoiam in picky[other]:
            acceptable.append(other)

    if acceptable:
        selected = choice(acceptable)
        match(picky, session, selected)

if __name__=='__main__': 

    redis = Redis(unix_socket_path='/tmp/redis.sock')

    while True:
        searchers = redis.zrange('searchers', 0, -1)
        print 'searchers: ', searchers
        
        if len(searchers)>=2: # if there aren't at least 2 people, there can't be matches
            identities = dict((session, redis.hget('session.'+session, 'character')) for session in searchers)
            picky = getPickyness(redis, searchers)
            for session in shuffled(picky.keys()):
                matchUser(session, picky, identities)

        time.sleep(1)


@@ -39,1237 +39,167 @@
 dis%0A
-from random import shuffle, choice%0Aimport uuid%0Aimport time%0A%0Adef getPickyness(redis,searchers):%0A    picky = %7B%7D%0A    allchars = redis.smembers('all-chars')%0A    for session in searchers:%0A        picky%5Bsession%5D = redis.smembers('session.%25s.picky' %25 session)%0A        if len(picky%5Bsession%5D)==0:%0A            picky%5Bsession%5D = allchars%0A    return picky%0A%0Adef shuffled(seq):%0A    local = list(seq)%0A    shuffle(local)%0A    return local%0A%0Adef match(picky, first, second):%0A    chat=str(uuid.uuid4()).replace('-','')%0A    redis.hset('chat.'+chat+'.meta', 'type', 'unsaved')%0A    redis.set('session.'+first+'.match', chat)%0A    redis.set('session.'+second+'.match', chat)%0A    redis.zrem('searchers', first)%0A    redis.zrem('searchers', second)%0A    del picky%5Bfirst%5D%0A    del picky%5Bsecond%5D%0A%0Adef matchUser(session, picky, identities):%0A    acceptable = %5B%5D%0A    try:%0A        wants = picky%5Bsession%5D%0A    except KeyError:%0A        return # we've already been matched%0A    whoiam = identities%5Bsession%5D%0A    for other, their_wants in picky.items():%0A        if other!=session and identities%5Bother%5D in wants and whoiam in picky%5Bother%5D:%0A            acceptable.append(other)%0A%0A    if acceptable:%0A        selected = choice(acceptable)%0A        match(picky, session, selected)
+import uuid%0Aimport time%0A%0Adef check_compatibility(first, second):%0A    return first%5B'char'%5D in second%5B'wanted_chars'%5D and second%5B'char'%5D in first%5B'wanted_chars'%5D
 %0A%0Aif
@@ -351,47 +351,8 @@
 -1)%0A
-        print 'searchers: ', searchers%0A
     
@@ -446,16 +446,17 @@
 matches%0A
+%0A
         
@@ -463,35 +463,131 @@
     
-identities = dict((session,
+all_chars = redis.smembers('all-chars')%0A            sessions = %5B%7B%0A                'id': session_id,%0A                'char':
  red
@@ -612,16 +612,19 @@
 +session
+_id
 , 'chara
@@ -629,17 +629,124 @@
 racter')
-)
+,%0A                'wanted_chars': redis.smembers('session.'+session_id+'.picky') or all_chars,%0A            %7D
  for ses
@@ -745,24 +745,27 @@
  for session
+_id
  in searcher
@@ -757,33 +757,34 @@
 _id in searchers
-)
+%5D%0A
 %0A            pic
@@ -784,150 +784,940 @@
     
-picky = getPickyness(redis, searchers)%0A            for session in shuffled(picky.keys()):%0A                matchUser(session, picky, identities
+already_matched = set()%0A            for n in range(len(sessions)):%0A                for m in range(n+1, len(sessions)):%0A                    if (%0A                        sessions%5Bn%5D%5B'id'%5D not in already_matched%0A                        and sessions%5Bm%5D%5B'id'%5D not in already_matched%0A                        and check_compatibility(sessions%5Bn%5D, sessions%5Bm%5D)%0A                    ):%0A                        chat = str(uuid.uuid4()).replace('-','')%0A                        redis.hset('chat.'+chat+'.meta', 'type', 'unsaved')%0A                        redis.set('session.'+sessions%5Bn%5D%5B'id'%5D+'.match', chat)%0A                        redis.set('session.'+sessions%5Bm%5D%5B'id'%5D+'.match', chat)%0A                        redis.zrem('searchers', sessions%5Bn%5D%5B'id'%5D)%0A                        redis.zrem('searchers', sessions%5Bm%5D%5B'id'%5D)%0A                        already_matched.add(sessions%5Bn%5D%5B'id'%5D)%0A                        already_matched.add(sessions%5Bm%5D%5B'id'%5D
 )%0A%0A 

Refactor the matchmaker.