def get(self): Utils.reset(self) # reset/clean standard variables # validate and assign parameters uuid = Utils.required(self, 'uuid') guid = self.request.get('guid') itid = Utils.required(self, 'itid') data = self.request.get('data') start_time = time.time() # start count # set variables default item = None player = None storeitem = None # if error, skip this if self.error == '': player = Player.getplayer_as_obj(self, uuid) if self.error == '' and player is not None and guid != '': if guid != player.state_obj['guid']: player = None self.error = config.error_message['dup_login'] # if error or player is none, skip this if self.error == '' and player is not None: storeitem = Data.getstoreitem_as_obj(self, config.softstore['version']) # if storeitem is not none if storeitem is not None: try: # try to get item with id=itid item = storeitem[str(itid)] except KeyError: # if item with that id/key doesn't exist storeitem = None # throw it to None self.error = 'given item id doesn\'t exist!' # and inform user that # if error or storeitem is none, skip to the end // return if self.error == '' and storeitem is not None: myitems = Item.getitems( self, uuid) # get all items user owned from Item model class helper add = True # this variable indicates is user eligible to purchase if item['dependencies'] != '': # check if the item has dependencies add = False # set item to unavailable first, we will change it available after we check all its dependencies self.respn = '{"warning":"you\'re not qualified to purchase this item!"}' # also inform user some good reason, not just die. this will eliminate after we check all its dependencies anyway depc = 0 # A variable stand from dependencies counter deps = item['dependencies'].replace(' ', '').split( ',' ) # and eliminate space from dependencies and split by commas ',' for dep in deps: # run through all dependencies of the item for myitem in myitems: # run through all user's list of items if myitem.itid == dep: # check if user have dependency item depc += 1 # increment counter if depc >= len( deps ): # alter all counting, if counter is more than all dependencies, it probably means user has all dependencies add = True # and then yeah!, we turn item to available if add is True and item[ 'maximum'] != '': # check if the item has maximum limitation depc = 0 # set counter to 0 again, we will use for next logic for myitem in myitems: # run through myitems if myitem.itid == item[ 'id']: # check if user has same item in hand depc += 1 # increment counter if int(depc) >= int( item['maximum'] ): # if counter is more than or equal the item's maximum add = False # that means user has reached the maximum, and should not available for him any more self.respn = '{"warning":"you\'ve reached maximum of this item!"}' # and yes! should inform him that # now if user is eligible to purchase this item, then do it if add is True: if player.state_obj['gold'] >= item[ 'gold']: # check if user has gold enough to purchase player.state_obj['gold'] -= item[ 'gold'] # deduct gold from user by item price itemobj = Item(parent=db.Key.from_path( 'Item', config.db['itemdb_name'] )) # now create an item and reward/deliver to user itemobj.itid = item[ 'id'] # assign an itemid.. to identiy item type itemobj.inid = Utils.genitemid( self) # generate and assign unique inventory id, itemobj.uuid = player.uuid # assign uuid, indicate who is the owner of this item itemobj.userData = '' if storeitem[ itemobj.itid]['type'] == Item.ItemType.BUILDING: itemobj.userData = data itemobj.status = 'pending' # assign status, probably this is pending itemobj.timestamp = int(start_time) + int( item['time'] ) # calculate time to deliver or reward, while status pending, user can't use it if itemobj.put(): # put it into database if player.state_obj[ 'token'] != '': # check if user already has token apns.add( player.state_obj['token'], storeitem[str(itemobj.itid)]['title'] + ' has been delivered to you!', itemobj.timestamp ) # if so, set a push notofication to be sent if Player.setplayer_as_obj( self, player ): # put player state back into the database self.respn = '{' self.respn += '"' + itemobj.inid + '":{' self.respn += '"itid":"' + itemobj.itid + '",' self.respn += '"type":"' + storeitem[ itemobj.itid]['type'] + '",' self.respn += '"title":"' + storeitem[ itemobj.itid]['title'] + '",' self.respn += '"desc":"' + storeitem[ itemobj.itid]['description'] + '",' self.respn += '"imgurl":"' + storeitem[ itemobj.itid]['image_url_sd'] + '",' self.respn += '"data":"' + itemobj.userData + '",' self.respn += '"status":"' + itemobj.status + '",' self.respn += '"timestamp":' + str( itemobj.timestamp) self.respn += '},' self.respn = self.respn.rstrip(',') + '}' """ myitems = Item.getspecificitems(self, uuid, item['id']) # get user items (only same type of purchased item), we want to list them all and return if myitems is not None: # make sure we have everything ready self.respn = '{' for myitem in myitems: # run through myitems if storeitem[str(myitem.itid)]: # check if item does exist save = False # this variable indicates should we update user's item data if myitem.status == 'pending' and start_time >= myitem.timestamp: # if item status is pending and it is time to reward myitem.status = 'reward' # then reward it, by changing item status myitem.timestamp = int(start_time) + int(storeitem[str(myitem.itid)]['produce_time']) # calculate next time to produce resource save = True # cause we change item status, so we need to update database elif myitem.status == 'reward': # if status is reward, myitem.status = 'rewarded' # we should change it to rewarded, so system won't duplicate reward save = True # and again, save it any time we change something # now, compose list of same type of purchased item self.respn += '"' + myitem.inid + '":{' self.respn += '"itid" : "' + myitem.itid + '",' self.respn += '"type" : "' + storeitem[myitem.itid]['type'] + '",' self.respn += '"title" : "' + storeitem[myitem.itid]['title'] + '",' self.respn += '"desc" : "' + storeitem[myitem.itid]['description'] + '",' self.respn += '"imgurl" : "' + storeitem[myitem.itid]['image_url_sd'] + '",' self.respn += '"data" : "' + myitem.userData + '",' self.respn += '"status" : "' + myitem.status + '",' self.respn += '"timestamp" : ' + str(myitem.timestamp) self.respn += '},' if save is True: # now we should know, should we update item data myitem.put() # if yes, please do so self.respn = self.respn.rstrip(',') + '}' else: # if we can't get my item, self.respn = '{}' # then return emtpy object """ self.respn = '{"uuid":"' + player.uuid + '", "state":' + player.state + ', "items":' + self.respn + '}' # compose final result else: # if user has not enough gold self.respn = '{"warning":"not enough gold!"}' # then tell him # calculate time taken and return result time_taken = time.time() - start_time self.response.headers['Content-Type'] = 'text/html' self.response.write(Utils.RESTreturn(self, time_taken))
def get(self): # reset/clean standard variables Utils.reset(self) # validate and assign parameters passwd = Utils.required(self, 'passwd') version = config.data_version['buildings'] if self.request.get('version'): version = self.request.get('version') uuid = Utils.required(self, 'uuid') guid = self.request.get('guid') inid = Utils.required(self, 'inid') # start count start_time = time.time() # set default parameters player = None researches = None myresearch = None economy = None # if error, skip this if self.error == '': player = Player.getplayer_as_obj(self, uuid) if self.error == '' and player is not None and guid != '': if guid != player.state_obj['guid']: player = None self.error = config.error_message['dup_login'] # if error or player is not, then skip to the end if self.error == '' and player is not None: researches = Data.getresearches(self, version) if self.error == '' and researches is not None: myresearch = Research.getmyresearch(self, uuid, inid) # if any error or myresearch is none, then skip to the end if self.error == '' and myresearch is not None: if myresearch.status != Research.ResearchStatus.PENDING: self.respn = '{"warning":"research=' + inid + ' has been finished."}' else: economy = Data.getDataAsObj(self, 'economy', config.data_version['economy']) if self.error == '' and self.respn == '' and economy is not None: _name = str(myresearch.itid) _pos = myresearch.itid.find('.', len(myresearch.itid) - 4) _bui = myresearch.itid[0:_pos] _lev = myresearch.itid[_pos + 1:len(myresearch.itid)] _upd = False time_left = researches.as_obj[_bui][_lev]['wait'] - int( (start_time - myresearch.timestamp) / 60) if myresearch.status == Research.ResearchStatus.PENDING: if time_left > 0: sele = economy.obj[0] for list in economy.obj: logging.info( str(time_left) + ' > ' + str(list['time_in_minutes'])) if time_left >= list['time_in_minutes']: sele = list else: logging.info('--->break') break logging.info( str(sele['time_in_minutes']) + '==>' + str(sele['gold_value'])) if player.state_obj['gold'] >= sele['gold_value']: player.state_obj['gold'] -= sele['gold_value'] myresearch.status = Research.ResearchStatus.REWARD _upd = True Player.setplayer_as_obj(self, player) else: self.respn = '{"warning":"not enough gold!"}' else: myresearch.status = Research.ResearchStatus.REWARD _upd = True if _upd is True: Research.setmyresearch(self, myresearch) if self.error == '' and self.respn == '': self.respn = '{"state":' + player.state + ', "buildings":[' self.respn = Research.compose_myresearch( self.respn, myresearch) self.respn = self.respn.rstrip(',') + ']' self.respn += '}' # calculate time taken and return the result time_taken = time.time() - start_time self.response.headers['Content-Type'] = 'text/html' self.response.write(Utils.RESTreturn(self, time_taken))
def get(self): Utils.reset(self) # reset/clean standard variables # validate and assign parameters passwd = Utils.required(self, 'passwd') version = config.data_version['buildings'] if self.request.get('version'): version = self.request.get('version') lang = config.server["defaultLanguage"] if self.request.get('lang'): lang = self.request.get('lang') uuid = Utils.required(self, 'uuid') guid = self.request.get('guid') itid = Utils.required(self, 'itid') # check password if self.error == '' and passwd != config.testing['passwd']: self.error = 'passwd is incorrect.' start_time = time.time() # start count # logical variables player = None researches = None research = None # if error, skip this if self.error == '': player = Player.getplayer_as_obj(self, uuid) if self.error == '' and player is not None and guid != '': if guid != player.state_obj['guid']: player = None self.error = config.error_message['dup_login'] if self.error == '' and player is not None: researches = Data.getresearches(self, float(version)) if self.error == '' and researches is not None: _name = str(itid) _pos = itid.find('.', len(itid) - 4) _bui = itid[0:_pos] _lev = itid[_pos + 1:len(itid)] try: research = researches.as_obj[_bui][_lev] except KeyError: self.error = itid + " was not found!" if self.error == '' and research is not None: add = True if player.state_obj['cash'] < research['cost'] and self.respn == '': self.respn = '{"warning":"not enough cash!"}' if player.state_obj['fuel'] < research['fuel'] and self.respn == '': self.respn = '{"warning":"not enough fuel!"}' if player.state_obj['tire'] < research['tire'] and self.respn == '': self.respn = '{"warning":"not enough tire!"}' if player.state_obj['battery'] < research[ 'battery'] and self.respn == '': self.respn = '{"warning":"not enough battery!"}' if player.state_obj['oil'] < research['oil'] and self.respn == '': self.respn = '{"warning":"not enough oil!"}' if player.state_obj['brake'] < research[ 'brake'] and self.respn == '': self.respn = '{"warning":"not enough brake!"}' if self.respn == '' and self.error == '': player.state_obj['cash'] -= research['cost'] player.state_obj['fuel'] -= research['fuel'] player.state_obj['tire'] -= research['tire'] player.state_obj['battery'] -= research['battery'] player.state_obj['oil'] -= research['oil'] player.state_obj['brake'] -= research['brake'] if Player.setplayer_as_obj(self, player): myresearch = Research.newresearch(self) myresearch.uuid = uuid myresearch.itid = itid myresearch.inid = Utils.genanyid(self, 'r') myresearch.status = Research.ResearchStatus.PENDING myresearch.timestamp = int(start_time) Research.setmyresearch(self, myresearch) self.respn = '{"state":' + player.state + ',' self.respn += '"researches":[' self.respn = Research.compose_myresearch( self.respn, myresearch) self.respn = self.respn.rstrip(',') + ']' self.respn += '}' # calculate time taken and return the result time_taken = time.time() - start_time self.response.headers['Content-Type'] = 'text/html' self.response.write(Utils.RESTreturn(self, time_taken))