def POST(self): var = web.input() if 'fb' in var: xsrf = util.select_one('xsrf', where='token=$tok', vars={'tok': var.xsrf}) if xsrf is None: raise status.ApiError('401 Unauthorized') try: xsrf = util.select_one('xsrf', where='token=$tok', vars={'tok': var.xsrf}) if xsrf is None: raise status.ApiError('401 Unauthorized') user = self.user_auth(var.email, var.pword) if user is None: print "this one" raise status.ApiError('401 Unauthorized') sess = str(uuid.uuid4())[:64] values = { 'sess': sess, 'uid': user['id'] } util.insert('sessions', **values) web.setcookie('wsid_login', sess, path='/') except AttributeError as err: print "that one" raise status.ApiError('401 Unauthorized (%s)' % err) web.redirect('/')
def GET(self, gid): db = util.connect_db() row = util.select_one(db, gid) fields = copy.deepcopy(config.fields) for item in fields: item[-1] = row[item[0]] del db return render.editor(fields, '/edit/' + gid, web.input(msg='').msg)
def user_auth(self, email, pword): user = util.select_one('users', where='email=$e', vars={'e':email}) if user is None: return None hashed = user.pword if bcrypt.hashpw(pword, hashed) == hashed: return user return None
def POST(self, pid): qx = util.select_one('qlist', where='id=$id', vars={'id': pid}) if qx is None: raise status.ApiError('401 Invalid Question') sess = util.get_sess() data = web.data() util.insert('alist', ans=data, up=0, down=0, qid=qx.id) raise status.ApiError('200 OK')
def POST(self): try: sess_key = web.cookies().wsid_login sess = util.select_one('sessions', where='sess=$s', vars={'s': sess_key}) web.setcookie('wsid_login',sess_key,expires=-1) print "del", util.delete('sessions', where='sess=$s', vars={'s': sess_key}) except AttributeError: raise status.ApiError('401 Not logged in') except KeyError: raise status.ApiError('401 Invalid session') raise status.ApiError('200 OK')
def GET(self, gid): web.header('Cache-Control', 'no-cache') db = util.connect_db() trans = db.transaction() try: #还原余额 orig = util.select_one(db, gid) util.change_remain(db, orig['direction'], orig['amount'], cancel=True) db.delete(config.table, where='gid=%d'%int(gid)) except Exception, e: trans.rollback() logging.warn('remove failed: %s\n%s', str(e), str(i)) return render.msg('remove failed')
def GET(self, pid): params = web.input(limit=20, offset=0) qx = util.select_one('qlist JOIN users ON qlist.uid = users.id', where='qlist.id=$id', vars={'id': pid}) if qx is None: raise status.ApiError('401 Invalid Question') res2 = util.select('alist JOIN qlist ON qlist.id = alist.qid JOIN users ON qlist.uid = users.id', where='qid=$id', limit=params.limit, offset=params.offset, order='alist.id DESC', vars={'id': pid}) res = [r for r in res2] if len(res) < 1 and params.offset>0: raise status.ApiError('403 Invalid Page') sess = util.get_sess() raise status.ApiReturn('templates/question', qx, res)
def get_move(game_state: Othello): """ interface function of all players Asks the user for a move and returns the selection :param game_state: actual game state :return: best move in available moves """ # Create a data structure to use with util.select_one possibilities = [] for move in game_state.get_available_moves(): (row, col) = move description = f"({COLUMN_NAMES[col]}{row + 1})" possibilities.append((description, move)) # Return the users selection return util.select_one(possibilities, "Select your move:")
def POST(self, gid): i = util.filter_readonly(web.input()) db = util.connect_db() trans = db.transaction() try: #还原余额 orig = util.select_one(db, gid) util.change_remain(db, orig['direction'], orig['amount'], cancel=True) #更新余额 util.change_remain(db, i['direction'], i['amount'], cancel = False) i['remain'] = util.get_remain(db) db.update(config.table, where='gid=%d'%int(gid), **i) except Exception, e: trans.rollback() logging.warn('edit failed: %s\n%s', str(e), str(i)) return render.msg('edit failed')
def GET(self, gid): web.header('Cache-Control', 'no-cache') db = util.connect_db() trans = db.transaction() try: #还原余额 orig = util.select_one(db, gid) util.change_remain(db, orig['direction'], orig['amount'], cancel=True) db.delete(config.table, where='gid=%d' % int(gid)) except Exception, e: trans.rollback() logging.warn('remove failed: %s\n%s', str(e), str(i)) return render.msg('remove failed')
def select_heuristic(player_string): """ Asks the user to select one of the heuristics known to the function :param player_string: 'W' or 'B' to ask specific player to choose a heuristic :return: function reference of the selected heuristic """ # Create a list of all Heuristics available_heuristics = list() # Use pairs of the form (description: String, class: Player) to store a player type available_heuristics.append(("Nijssen 07 Heuristic", NijssenHeuristic.heuristic)) available_heuristics.append(("Field Heuristic", StoredMonteCarloHeuristic.heuristic)) available_heuristics.append(("Cowthello Heuristic", CowthelloHeuristic.heuristic)) if len(available_heuristics) > 1: return util.select_one(available_heuristics, f"[{player_string}] Please select a heuristic") else: return available_heuristics[0][1]
def POST(self, gid): i = util.filter_readonly(web.input()) db = util.connect_db() trans = db.transaction() try: #还原余额 orig = util.select_one(db, gid) util.change_remain(db, orig['direction'], orig['amount'], cancel=True) #更新余额 util.change_remain(db, i['direction'], i['amount'], cancel=False) i['remain'] = util.get_remain(db) db.update(config.table, where='gid=%d' % int(gid), **i) except Exception, e: trans.rollback() logging.warn('edit failed: %s\n%s', str(e), str(i)) return render.msg('edit failed')
def GET(self, rid): room = util.select_one('rooms', where='id=$rid', vars={'rid': rid}) house = util.select_one('houses', where='id=$hid', vars={'hid': room.house_id}) coms = util.select('room_com', where='room_id=$rid', vars={'rid': rid}) raise status.ApiReturn('templates/room_detail', house, room, list(coms))
def GET(self, hid): house = util.select_one('houses', where='id=$hid', vars={'hid': hid}) hcom = util.select('house_com', where='house_id=$hid', vars={'hid': hid}) rooms = util.select('rooms', where='house_id=$hid', vars={'hid': hid}, order='room_no ASC') raise status.ApiReturn('templates/house_detail', house, list(hcom), list(rooms))
if __name__ == '__main__': print("Welcome to Othello") # Create a list of Player Types available_players = list() # Use pairs of the form (description: String, class: Player) to store a player type available_players.append(("Human Player", Human)) available_players.append(("AI Player - Random", Random)) available_players.append(("AI Player - Monte Carlo", MonteCarlo)) available_players.append( ("AI Player - Alpha-Beta Pruning", AlphaBetaPruning)) # Ask the user to select a type of player as first player. selection_player_one = util.select_one( available_players, f"Select Mode for Player {PRINT_SYMBOLS[PLAYER_ONE]}") # Initialize a new player of the selected type and store it. player_one = selection_player_one() # Ask the user to select a type of player as second player. selection_player_two = util.select_one( available_players, f"Select Mode for Player {PRINT_SYMBOLS[PLAYER_TWO]}") # Initialize a new player of the selected type and store it. player_two = selection_player_two() # Store the players in a dict with the internal player codes as key to allow easy access and maintaining the correct order players = {PLAYER_ONE: player_one, PLAYER_TWO: player_two} # Create a new game state game = Othello()