def start_game(self, **kwargs): userid = kwargs['userid'] del kwargs['userid'] robots = [] if 'user' in kwargs: robots.append(kwargs['user'] + '@' + userid) del kwargs['user'] # If there is only one checked robot, it will be returned as a str, # not a list. for bot_list in kwargs.values(): if isinstance(bot_list, basestring): robots.append(bot_list) else: robots.extend(bot_list) # Maintain a separate list for the name without the ids split_list = [x.split('@')[0] for x in robots] split_bots = ' '.join(split_list) robots = ' '.join(robots) game_id = str(uuid.uuid4()) # Try to detect OpenShiftiness base = os.environ.get('OPENSHIFT_REPO_DIR') or '../../' subprocess.Popen(['python', 'main.py', '-g', '-I', game_id, '-R', robots], cwd=base+'pybotwar') new_game = model.Game(id=game_id, name=split_bots, userid=userid, date=datetime.now()) DBSession.add(new_game) sleep(1) redirect('/game?game_id=%s' % (game_id))
def start_game(self, **kwargs): userid = kwargs['userid'] robots = [] if 'user' in kwargs: robots.append(kwargs['user'] + '@' + userid) # If there is only one checked robot, it will be returned as a str, # not a list. examples = kwargs.get('example', []) if isinstance(examples, basestring): robots.append(examples) else: robots.extend(kwargs['example']) robots = ' '.join(robots) game_id = str(uuid.uuid4()) # Try to detect OpenShiftiness base = os.environ.get('OPENSHIFT_REPO_DIR') or '../../' subprocess.Popen(['python', 'main.py', '-g', '-I', game_id, '-R', robots], cwd=base+'pybotwar') new_game = model.Game(id=game_id, name=robots, userid=userid, date=datetime.now()) DBSession.add(new_game) sleep(1) redirect('/game?game_id=%s' % (game_id))
def make_friends(self, **kwargs): import json data = json.loads(kwargs['data']) user_id = kwargs['uid'] for item in data['data']: uid = item['uid2'] if DBSession.query(model.Login).filter_by(id=uid).all(): if not DBSession.query(model.Friend).filter_by(uid_left=user_id).filter_by(uid_right=uid).all(): friendship = model.Friend(uid_left=user_id, uid_right=uid) DBSession.add(friendship) friendship = model.Friend(uid_left=uid, uid_right=user_id) DBSession.add(friendship)
def setUp(self): """Prepare model test fixture.""" try: new_attrs = {} new_attrs.update(self.attrs) new_attrs.update(self.do_get_dependencies()) self.obj = self.klass(**new_attrs) DBSession.add(self.obj) DBSession.flush() return self.obj except: DBSession.rollback() raise
def start_game(self, **kwargs): robots = '' for key in kwargs.keys(): robots += key + ' ' robots = robots[:-1] game_id = str(uuid.uuid3(uuid.NAMESPACE_DNS, robots + str(clock()))) # Try to detect OpenShiftiness base = os.environ.get('OPENSHIFT_REPO_DIR') if not base: base = '../../' subprocess.Popen(['python', 'main.py', '-g', '-I', game_id, '-R', robots], cwd=base+'pybotwar') new_game = model.Game(id=game_id, name=robots) DBSession.add(new_game) sleep(1) redirect('/game?game_id=%s' % (game_id))
def upload_code(self, **kw): code = kw['code'].value name = kw['name'] uid = kw['userid'] if not uid: flash('Must be logged in to upload robots.') else: # Try to detect OpenShiftiness base = os.environ.get('OPENSHIFT_REPO_DIR') or '../../' with open("%spybotwar/robots/%s@%s.py" % (base, name, uid), 'w') as local_file: local_file.write(code) # Save a ref to the file in the DB robot = model.Robot(userid=uid, name=name) DBSession.add(robot) redirect("/")