def robots(self, userid=None): """List all the available robots.""" #TODO: get robot list from somewhere (user?) user_list = DBSession.query(model.Robot).filter_by(userid=userid).all() user_list = [x.name.split('@')[0] for x in user_list] friends = DBSession.query(model.Friend).filter_by(uid_left=userid).all() friend_list = [] for friend in friends: robots = DBSession.query(model.Robot).filter_by(userid=friend.uid_right).all() if robots: friend = DBSession.query(model.Login).filter_by(id=friend.uid_right).one() for robot in robots: friend_list.append("%s's %s" % (friend.name, robot.name.split('@')[0])) other_list = [] robo_list = [u'Ninja', u'Pirate', u'Robot', u'Wizard', u'Velociraptor', u'Zombie', u'robot07', u'robot08'] # Build a custom widget to hold the form.x class RoboForm(WebbotForm): class user(twf.RadioButtonList): options = user_list class example(twf.CheckBoxList): options = robo_list class friends(twf.CheckBoxList): options = friend_list class others(twf.CheckBoxList): options = other_list return dict(form=RoboForm(action='start_game'), page_title='', form_title='Here are all the robots you can play with:')
def games(self, userid=None): """List all the available games.""" user_games = DBSession.query(model.Game).filter_by(userid=userid).all() game_list = DBSession.query(model.Game) \ .filter(model.Game.userid != userid) \ .order_by(desc(model.Game.date)).limit(10).all() return dict(your_games=user_games, games=game_list)
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 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("/")
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 robots(self, userid=None): """List all the available robots.""" #TODO: get robot list from somewhere (user?) user_list = DBSession.query(model.Robot).filter_by(userid=userid).all() user_list = [x.name.split('@')[0] for x in user_list] robo_list = [u'Ninja', u'Pirate', u'Robot', u'Wizard', u'Velociraptor', u'Zombie', u'robot07', u'robot08'] # Build a custom widget to hold the form.x class RoboForm(WebbotForm): class user(twf.RadioButtonList()): options = user_list class example(twf.CheckBoxList()): options = robo_list return dict(form=RoboForm(action='start_game'), page_title='', form_title='Here are all the robots you can play with:')
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 test_query_obj(self): """Model objects can be queried""" obj = DBSession.query(self.klass).one() for key, value in self.attrs.iteritems(): assert_equals(getattr(obj, key), value)
def tearDown(self): """Finish model test fixture.""" DBSession.rollback()
def games(self): """List all the available games.""" game_list = DBSession.query(model.Game).all() print(game_list[0]) return dict(games=game_list)
def by_display_name(cls, dname): return DBSession.query(cls).filter_by(display_name=dname).first()
def by_user_id(self, userid): return DBSession.query(self).filter_by(user_id=userid).first()
def by_user_name(cls, username): """Return the user object whose user name is ``username``.""" return DBSession.query(cls).filter_by(user_name=username).first()
def by_email_address(cls, email): """Return the user object whose email address is ``email``.""" return DBSession.query(cls).filter_by(email_address=email).first()