def __init__( self ): webapp.RequestHandler.__init__( self ) self.log = getLogger() self.dao = DAO() self.xml_gen = XMLGenerator()
def __init__( self ): webapp.RequestHandler.__init__( self ) self.log = getLogger() self.dao = DAO() self.config = ConfigObj("Config/skajserver.cfg")
class GameHandler( webapp.RequestHandler ): def __init__( self ): webapp.RequestHandler.__init__( self ) self.log = getLogger() self.dao = DAO() self.config = ConfigObj("Config/skajserver.cfg") def handleCreateGame(self, url): """ Create a game... .. DUH NO SECURITY YET..... """ response = "sorry... operation failed" data = "<game>FAILED</game>" try: self.log.debug("GameHandler:handleCreateGame start") name = self.request.get("name") password = self.request.get("password") num_players = int( self.request.get("num_players") ) map_name = self.request.get("map_name", "default") (stat, game) = self.dao.createGame( name, password, num_players, map_name ) if stat == StatusCodes.SUCCESSFUL: self.log.debug("SUCCESSFULLY CREATED GAME " + name ) data = "<game name=%s></game>"%( name ) except: self.log.error("GameHandler:handleCreateGame ex " + traceback.format_exc() ) self.response.out.write( data ) def handleModifyGameState(self, url): """ Create a game... .. DUH NO SECURITY YET..... """ response = "sorry... operation failed" data = "<game>FAILED</game>" try: self.log.debug("GameHandler:handleModifyGameState start") name = self.request.get("name") state = self.request.get("state") (stat, game) = self.dao.modifyGameState( name, state ) if stat == StatusCodes.SUCCESSFUL: self.log.debug("SUCCESSFULLY MODIFIED STATE GAME " + name ) data = "<game name=%s></game>"%( name ) except: self.log.error("GameHandler:handleModifyGameState ex " + traceback.format_exc() ) self.response.out.write( data ) # will eventually go into a proper object for this. def populateGame( self, game ): """ Assigns countries to players and does initial populating of armies. """ # get list of countries for map name game_name = game.game_name (game_status, countries_list) = self.dao.getMasterCountries( game.map_name ) # randomize list. random.shuffle( countries_list ) # get players. (players_status, players_list) = self.dao.getAllPlayersForGame( game.game_name ) # assign equals num of countries to players starting_armies = int( self.config["starting_armies"] ) starting_countries = int( self.config["starting_countries"] ) # need to check if enough countries. if len( players_list) * starting_countries > countries_list: self.log.error("NOT ENOUGH COUNTRIES FOR NUMBER OF PLAYERS") else: for player in players_list: for i in xrange( starting_countries ): country_name = countries_list.pop() self.dao.createCountryForPlayer( game_name, player.user_name, country_name, starting_armies ) def handleStartGame(self, url): """ Create a game... .. DUH NO SECURITY YET..... """ response = "sorry... operation failed" data = "<game>FAILED</game>" try: self.log.debug("GameHandler:handleStartGame start") name = self.request.get("name") (stat, game) = self.dao.modifyGameState( name, "STARTED" ) if stat == StatusCodes.SUCCESSFUL: self.log.debug("SUCCESSFULLY MODIFIED STATE GAME " + name ) # populate game with countries. self.populateGame( game ) data = "<game name=%s>%s</game>"%( name, "STARTED" ) except: self.log.error("GameHandler:handleStartGame ex " + traceback.format_exc() ) self.response.out.write( data ) def handleJoinGame(self, url): """ Join a game... .. DUH NO SECURITY YET..... 1) Get username 2) get token 3) authenticate 4) get game 5) create player for game """ response = "sorry... operation failed" data = "<game>FAILED</game>" try: self.log.debug("GameHandler:handleJoinGame start") user_name = self.request.get("username") token = self.request.get("token") state = self.request.get("state") game_name = self.request.get("gamename") ( stat, user ) = self.dao.authenticateUser( user_name, token ) if stat == StatusCodes.SUCCESSFUL: (game_status, game) = self.dao.getGame( game_name ) if game_status == StatusCodes.SUCCESSFUL and game != None: # check if user has already joined the game. ( stat, player ) = self.dao.getPlayerForGame( user_name, game ) if stat != StatusCodes.PLAYER_NOT_IN_GAME: # create the player. ( player_status, player) = self.dao.createPlayer( user, game ) if player_status == StatusCodes.SUCCESSFUL: data = "<game>JOINED</game>" except: self.log.error("GameHandler:handleJoinGame ex " + traceback.format_exc() ) self.response.out.write( data ) def get(self, url): """ handle various User based gets """ self.log.debug("URL is " + url) path = self.request.path self.log.debug("path is " + path) # dict string --> method. # FIXME if path == "/1/creategame": self.handleCreateGame( url ) else: if path == "/1/modifygamestate": self.handleModifyGameState( url ) else: if path == "/1/joingame": self.handleJoinGame( url ) else: if path == "/1/startgame": self.handleStartGame( url ) else: if path == "/foo": self.dao.generateCountryMaster()
class UserHandler( webapp.RequestHandler ): def __init__( self ): webapp.RequestHandler.__init__( self ) self.log = getLogger() self.dao = DAO() self.config = ConfigObj("Config/skajserver.cfg") def handleLogin(self, url): """ Create a user... .. DUH NO SECURITY YET..... MARKS the user as logged in (on the server?) """ response = "sorry... operation failed" data = "<user>FAILED</user>" try: self.log.debug("UserHandler:handleLogin") user_name = self.request.get("user_name") password = self.request.get("password") self.log.debug("user URL is " + user_name ) (stat, user ) = self.dao.getUserWithPassword( user_name, password ) print "stat is "+str(stat) #print "fooooooooooooo" if stat == StatusCodes.SUCCESSFUL: self.log.debug("SUCCESSFULLY RETRIEVED USER " + user_name ) # get token. new_token = str( random.randrange(0,1000000000) ) user.token = new_token # store it self.dao.storeUser( user) data = "<user id=%s token=%s></user>"%( user_name, new_token ) except: self.log.error("UserHandler:handleLogin ex " + traceback.format_exc() ) self.response.out.write( data ) def handleGetUser(self, url): """ Retrieve User. NO SECURITY YET..... MARKS the user as logged in (on the server?) """ response = "sorry... operation failed" data = "<user>FAILED</user>" try: self.log.debug("UserHandler:handleGetUser") user_name = self.request.get("user_name") password = self.request.get("password") self.log.debug("user URL is " + user_name ) (stat, user ) = self.dao.getUserWithPassword( user_name, password ) print "stat is "+str(stat) #print "fooooooooooooo" if stat == StatusCodes.SUCCESSFUL: self.log.debug("SUCCESSFULLY RETRIEVED USER " + user_name ) data = "<user id=%s token=%s></user>"%( user_name, user.token ) except: self.log.error("UserHandler:handleGetUser ex " + traceback.format_exc() ) self.response.out.write( data ) def handleCreateUser(self, url): """ Create a user... .. DUH NO SECURITY YET..... """ response = "sorry... operation failed" data = "<user>FAILED</user>" try: d = str(dir(self.request )) self.log.debug("D " + d ) self.log.debug("path "+self.request.path) self.log.debug("path_url " + self.request.path_url) self.log.debug("UserHandler:handleCreateUser") data = "success2" user_name = self.request.get("user_name") password = self.request.get("password") data = "success3" self.log.debug("user URL is " + user_name ) (status, user ) = self.dao.createUser( user_name, password ) data = "success4" #print "fooooooooooooo" if status == StatusCodes.SUCCESSFUL: self.log.debug("SUCCESSFULLY CREATED USER " + user_name ) data = "<user name=%s></user>"%(user_name) except: self.log.error("UserHandler:handleCreateUser ex " + traceback.format_exc() ) self.response.out.write( data ) def get(self, url): """ handle various User based gets """ self.log.debug("URL is " + url) path = self.request.path self.log.debug("path is " + path) # dict string --> method. # FIXME if path == "/1/createuser": self.handleCreateUser( url ) else: if path == "/1/login": self.handleLogin( url ) else: if path == "/1/getuser": self.handleGetUser( url )
class PlayerHandler( webapp.RequestHandler ): def __init__( self ): webapp.RequestHandler.__init__( self ) self.log = getLogger() self.dao = DAO() self.config = ConfigObj("Config/skajserver.cfg") def createPlayerHandler(self, url): """ create player based on user that is logged in. """ response = "sorry... operation failed" data = "<player>FAILED</player>" try: self.log.debug("PlayerHandler:createPlayerHandler") user_name = self.request.get("user_name") token = self.request.get("token") # need to know which game we're joining. game_name = self.request.get("game_name") if self.dao.authenticateUser( user_name, token ): # check if user has already joined the game. ( stat, player ) = self.dao.getPlayerForGame( user_name, game_name ) if stat == StatusCodes.PLAYER_NOT_IN_GAME: self.log.debug("player does not exist in game") ( user_status, user) = self.dao.getUser( user_name ) if user_status == StatusCodes.SUCCESSFUL: self.log.debug("have user successfully") # get the game. ( game_stat, game) = self.dao.getGame( game_name ) self.log.debug("game name is " + game.game_name) # create the player. player = self.dao.createPlayer( user, game ) data = "<player id=%s game_name=%s></user>"%( user_name, game_name ) else: self.log.debug("player already in game") data = "<player id=%s game_name=%s></user>"%( user_name, game_name ) except: self.log.error("PlayerHandler:createPlayerHandler ex " + traceback.format_exc() ) self.response.out.write( data ) def getPlayerHandlerForGame( self, url): """ Retrieve User. NO SECURITY YET..... MARKS the user as logged in (on the server?) """ response = "sorry... operation failed" data = "<player>FAILED</player>" try: self.log.debug("PlayerHandler:getPlayerHandlerForGame") user_name = self.request.get("user_name") token = self.request.get("token") game_name = self.request.get("game_name") (stat, player ) = self.dao.getPlayerForGame( user_name, game_name ) if stat == StatusCodes.SUCCESSFUL: self.log.debug("SUCCESSFULLY RETRIEVED PLAYER " + user_name ) data = "<player id=%s game_name=%s></user>"%( user_name, game_name ) except: self.log.error("PlayerHandler:getPlayerHandlerForGame ex " + traceback.format_exc() ) self.response.out.write( data ) def get(self, url): """ handle various User based gets """ self.log.debug("URL is " + url) path = self.request.path self.log.debug("path is " + path) # dict string --> method. # FIXME if path == "/1/createplayer": self.createPlayerHandler( url ) else: if path == "/1/getplayer": self.getPlayerHandlerForGame( url )
class ActionHandler( webapp.RequestHandler ): def __init__( self ): webapp.RequestHandler.__init__( self ) self.log = getLogger() self.dao = DAO() self.config = ConfigObj("Config/skajserver.cfg") def canAttack( self, attacking_country, attacked_country ): """ Confirms if countries can attack eachother. """ # FIXME return true def attack( self, attacking_country, attacked_country ): """ Confirms if countries can attack eachother. """ # FIXME return true def handleAttack(self, url): """ Create a game... .. DUH NO SECURITY YET..... """ response = "sorry... operation failed" data = "<action>FAILED</action>" try: self.log.debug("ActionHandler:handleAttack start") name = self.request.get("name") token = self.request.get("token") if self.dao.authenticateUser( name, token): # get country doing the attacking attacking_country = self.request.get("attacking_name") # get country being attacked. attacked_country = self.request.get("attacked_name") # confirm they're near eachother. if self.canAttack( attacking_country, attacked_country ): # attack self.attack( attacking_country, attacked_country ) except: self.log.error("GameHandler:handleCreateGame ex " + traceback.format_exc() ) self.response.out.write( data ) def handlePopulate(self, url): """ Create a game... .. DUH NO SECURITY YET..... """ response = "sorry... operation failed" data = "<action>FAILED</action>" try: self.log.debug("ActionHandler:handlePopulate start") name = self.request.get("name") token = self.request.get("token") if self.dao.authenticateUser( name, token): # get country doing the attacking country_name = self.request.get("country") # get country being attacked. attacked_country = self.request.get("attacked_name") # confirm they're near eachother. if self.canAttack( attacking_country, attacked_country ): except: self.log.error("GameHandler:handlePopulate ex " + traceback.format_exc() ) self.response.out.write( data ) def get(self, url): """ handle various User based gets """ self.log.debug("URL is " + url) path = self.request.path self.log.debug("path is " + path) # dict string --> method. # FIXME if path == "/1/attack": self.handleAttack( url ) else: if path == "/1/populate": self.handleModifyGameState( url )
class APIHandler(webapp.RequestHandler): def __init__( self ): webapp.RequestHandler.__init__( self ) self.log = getLogger() self.dao = DAO() self.xml_gen = XMLGenerator() def decode_short_url(self, url ): """ Decodes short url. This is really duplicated code, which is bad. But its really only a 1 liner. """ full_url = None self.log.info("APIHandler::decode_short_url start") try: self.log.debug("short url " + str( url ) ) if url != None and url != "": full_url = self.dao.get_long_url( short_url ) self.log.debug("long url " + str( full_url ) ) except: self.log.error("APIHandler::decode_short_url ex " + traceback.format_exc() ) return full_url def get(self, command ): """ Can issue 3 types of commands. 1) Get a LONG_URL from a short url 2) Assign a LONG_URL to a given short_url 3) Assign a LONG_URL to a random url. """ try: self.log.debug("command is " + str( command ) ) if command == "get": url = self.request.get("short_url") if url != None and url != "": full_url = self.dao.get_long_url( url ) if full_url != None and full_url != "": res = self.xml_gen.generate_successful_get( full_url ) else: res = self.xml_gen.generate_unsuccessful_get( url ) self.response.out.write( res ) else: if command == "set": short_url = self.request.get("short_url") full_url = self.request.get("full_url") creator = self.request.get("creator") tags = self.request.get("tags") if short_url != None and short_url != "" and full_url != None and full_url != "" and creator != None and creator != "" : result_url, status_code = self.dao.replace_entry( short_url, full_url, creator, tags ) if status_code == StatusCodes.SUCCESSFUL: res = self.xml_gen.generate_successful_set( ) else: res = self.xml_gen.generate_unsuccessful_set( status_code ) self.response.out.write( res ) else: if command == "list": creator = self.request.get("creator") if creator != None and creator != "": entry_list = self.dao.get_all_entries_for_creator( creator ) self.log.debug("entry list " + str( entry_list ) ) if entry_list != None and entry_list != [] : res = self.xml_gen.generate_successful_list( entry_list) else: res = self.xml_gen.generate_unsuccessful_List( creator ) self.response.out.write( res ) except: self.log.error("APIHandler::get ex " + traceback.format_exc() )