<s> import logging <EOL> import os <EOL> import random <EOL> import struct <EOL> from google . appengine . ext import webapp <EOL> from google . appengine . ext . webapp import util <EOL> import member <EOL> class AssignUser ( webapp . RequestHandler ) : <EOL> def post ( self ) : <EOL> self . response . headers . add_header ( "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> STR_VERSERVER = '<STR_LIT>' <EOL> INT_VERCLIENT = <NUM_LIT> <EOL> STR_VERCLIENT = '<STR_LIT>' <EOL> if not os . environ . has_key ( '<STR_LIT>' ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> if not os . environ . has_key ( '<STR_LIT>' ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> HTTPS = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> CURRENT_VERSION_ID = os . environ . get ( '<STR_LIT>' , STR_VERSERVER ) <EOL> if HTTPS . __str__ ( ) != '<STR_LIT>' : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> minlen = <NUM_LIT:4> + <NUM_LIT:32> <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> data = self . request . body <EOL> size = str . __len__ ( data ) <EOL> if size < minlen : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> server = int ( CURRENT_VERSION_ID [ <NUM_LIT:0> : <NUM_LIT:8> ] , <NUM_LIT:16> ) <EOL> client = ( struct . unpack ( "<STR_LIT>" , data [ <NUM_LIT:0> : <NUM_LIT:4> ] ) ) [ <NUM_LIT:0> ] <EOL> data = data [ <NUM_LIT:4> : ] <EOL> if client < INT_VERCLIENT : <EOL> self . resp_simple ( <NUM_LIT:0> , ( '<STR_LIT>' % STR_VERCLIENT ) ) <EOL> return <EOL> num = <NUM_LIT:1> <EOL> while ( num > <NUM_LIT:0> ) : <EOL> query = member . Member . all ( ) <EOL> num = query . count ( ) <EOL> if ( num >= <NUM_LIT> ) : <EOL> maxUsers = <NUM_LIT> <EOL> elif ( num >= <NUM_LIT> ) : <EOL> maxUsers = <NUM_LIT> <EOL> else : <EOL> maxUsers = <NUM_LIT> <EOL> used = [ ] <EOL> for m in query : <EOL> used . append ( m . usr_id ) <EOL> if str ( m . commitment ) == str ( data ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> full = set ( xrange ( <NUM_LIT:11> , maxUsers + <NUM_LIT:1> ) ) <EOL> avail = sorted ( full - set ( used ) ) <EOL> r = random . SystemRandom ( ) <EOL> usrid = r . choice ( avail ) <EOL> query = member . Member . all ( ) <EOL> query . filter ( '<STR_LIT>' , usrid ) <EOL> num = query . count ( ) <EOL> if num > <NUM_LIT:0> : <EOL> logging . info ( "<STR_LIT>" + str ( usrid ) + "<STR_LIT>" ) <EOL> mem = member . Member ( usr_id = usrid , commitment = data , client_ver = client ) <EOL> mem . put ( ) <EOL> key = mem . key ( ) <EOL> if not key . has_id_or_name ( ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> self . response . out . write ( '<STR_LIT:%s>' % struct . pack ( '<STR_LIT>' , server ) ) <EOL> self . response . out . write ( '<STR_LIT:%s>' % struct . pack ( '<STR_LIT>' , usrid ) ) <EOL> def resp_simple ( self , code , msg ) : <EOL> self . response . out . write ( '<STR_LIT>' % ( struct . pack ( '<STR_LIT>' , code ) , msg ) ) <EOL> def main ( ) : <EOL> application = webapp . WSGIApplication ( [ ( '<STR_LIT>' , AssignUser ) , <EOL> ] , <EOL> debug = True ) <EOL> util . run_wsgi_app ( application ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
