def _inventlist(self, request, user): check_invent=Inventory.query(Inventory.name== \ request.user_name).get() if check_invent: check_invent.key.delete() invent=Inventory(name=user.name, user=user.key, \ flint=items.get("flint"), grass=items.get("grass"), \ boulder=items.get("boulder"), hay=items.get("hay"), \ tree=items.get("tree"), sapling=items.get("sapling")) invent.put()
def checkInventory(self, request, user): """Used to pull inventory on a item""" if not user: raise endpoints.NotFoundException( 'A User with that name does not exist!') chklist=Inventory.query( Inventory.user==user.key).get() if not chklist: raise endpoints.NotFoundException( 'This user does not have any Inventory') if user.key==chklist.user: if items.has_key(request.item_name)== False: raise endpoints.NotFoundException('Invalid item name') itemname=request.item_name value=getattr( chklist, itemname) return StringMessage1(message='You have {} {} '.format \ (value, itemname))
def craftItemNew(self, request, user): """Craft an item""" if not user: raise endpoints.NotFoundException( 'A User with that name does not exist!') ingamecheck=Game.query(Game.user==user.key).filter \ (Game.game_over == False).get() if not ingamecheck: raise endpoints.NotFoundException \ ('User is not in a game. Please create a new game for this user.') #Check for if out of time if ingamecheck.timeout == True: setattr(ingamecheck, "game_over", True) ingamecheck.put() raise endpoints.ConflictException\ ('Player has run out of time and did not survive! Start a new game.') #Starts the game timer if ingamecheck.game_started == False: t1=int(time.time()) gamediff=getattr(ingamecheck, "difficulty") setattr(ingamecheck, "timer", t1) setattr(ingamecheck, "game_started", True) ingamecheck.put() #Calls gamecheck for game timer if ingamecheck.game_started == True: if ingamecheck.difficulty > 1: gamecheck(ingamecheck) # Make a dict of inventory from ndb inventory_items=Inventory.query( Inventory.user==user.key)\ .get() # Create a dict of what is needed to craft the item takesToCraft=craft.get(request.itemcraft) if takesToCraft == None: raise endpoints.NotFoundException('Invalid item name.') # Make a copy of takesToCraft to re-populate with ndb values. copycraft=takesToCraft.copy() #Calls a function to populate copycraft invenOfCraft(copycraft, inventory_items) #return of invenOfCraft function. inven_ndb=copycraft #Compares what is needed to craft an item to what exist in inventory. #Determines if required items are present in inventory #Flags canbeMade T or F #Tells player if not enough resources to craft item canBeMade=True for i in craft[request.itemcraft]: if craft[request.itemcraft] [i]>inven_ndb[i]: canBeMade=False return StringMessage1 \ (message='Sorry, item can not be crafted. Takes {}, you only have {}' \ .format(takesToCraft, inven_ndb)) if canBeMade==True: # Adds 1 to the quantity of a crafted item in datastore. increment=1+getattr(inventory_items, request.itemcraft) setattr(inventory_items, request.itemcraft, increment) #Decrement inventory items used to craft a new item neededForCraft= takesToCraft.copy() for w in neededForCraft: if hasattr(inventory_items, w)==True: setattr(inventory_items, w, getattr \ (inventory_items, w)-neededForCraft[w]) inventory_items.put() ingamecheck.history.append(request.itemcraft) ingamecheck.put() #Checks to see if you have survived and won the game if inventory_items.tent >= 1 and inventory_items.firepit >= 1: setattr(ingamecheck, "survived", True) setattr(ingamecheck, "game_over", True) setattr(user, "wins", 1 + getattr(user, "wins")) setattr(user, "total_played", 1 + getattr \ (user, "total_played")) setattr(user, "score", 20 * ingamecheck.difficulty+ \ user.score) ingamecheck.put() user.put() return StringMessage1(message='Congrats {}, you survived! Game over.' \ .format(inventory_items.name)) else: return StringMessage1(message='{} Can be crafted! {}, You have {}' \ .format(request.itemcraft, takesToCraft, inven_ndb))