def saveGame(team_one,team_two,date,time,location): game = Game() game.team_one = team_one game.team_two = team_two game.game_date = date game.game_time = time game.location = location num = datetime.datetime.strptime(date, "%Y-%m-%d") game.weeknum = str(num.isocalendar()[0])+str(num.isocalendar()[1]) game.week_index = num.weekday() game.save() return True
def populate_challenges(self): challenge = Challenge() challenge.title = "Dummiest Challenge created ever" challenge.start_time = timezone.now() challenge.end_time = timezone.now() + timedelta(days=1) challenge.registration_start_time = challenge.start_time challenge.registration_end_time = challenge.end_time challenge.registration_open = True challenge.team_size = 3 challenge.entrance_price = 1000 game = Game() game.name = "AIC Game 2018" game.save() challenge.game = game challenge.save()
def create_game(cls, **kwargs): try: game = Game.create_game(**kwargs) return game.canonical() except Exception as e: print('create_game failed:%s' % str(e)) return None
def get_game_list(cls, page_num): try: print "logic begin:" if(page_num<1): raise Exception(u'F**K!PAGENUM<1!') game_id_list = Game.get_game_list(page_num) print game_id_list game_dict_list = [] if len(game_id_list) > 0: for game_id in game_id_list: game_dict_list.append(Game.get_one_game(game_id).canonical_trim_game()) return game_dict_list else: return [] except Exception as e: print e return []
def start_game(self, snake_ids): """ Start a game given a tuple of snake id's. Returning a game id. """ if len(snake_ids) == 1: return snakes = [vars(s) for s in Snake.objects.filter(id__in=snake_ids)] game = Game(width=10, height=10, food=5, snakes=snakes) game.create() game.is_leaderboard_game = True game.run() GameLeaderboard(game=game).save()
def update_game(cls, **kwargs): """ Update game property base on the argument game_id """ try: game_id = kwargs.pop('game_id') game = Game.get_one_game(game_id) if game and game.user_id == int(kwargs.pop('user_id')): for key, value in kwargs.items(): setattr(game, key, value) game.save() return game.canonical() else: raise Exception("No exist Game:game_id: %s" %game_id) except Exception as e: print(e) return 0
def create(self, *args, **kwargs): heat = kwargs.get("heat") previous_game = heat.latest_game skip = [w.snake.id for w in heat.winners] print(skip) if previous_game is not None: skip.append(previous_game.winner.snake.id) next_snakes = [s for s in previous_game.snakes if s.id not in skip] else: next_snakes = heat.snakes snake_ids = [{"id": snake.id} for snake in next_snakes] from apps.game.models import Game game = Game(width=20, height=20, food=10, snakes=snake_ids) game.create() game.save() return super(HeatGameManager, self).create(*args, **kwargs, game=game)
def basic(self): return Game(width=20, height=20, food=5)
def get_total_num(cls): return Game.get_total_num()
def get_one_game(cls, game_id): game_dict = Game.get_one_game(game_id).canonical() return game_dict
def post(self, request): post_data = self.DATA valid_data = True session_user = request.user test_user = User.objects.get(id=1) session_user = test_user if not session_user.is_authenticated(): response = Response(403, {'statusCode': 403, 'message' : 'Not authorized'}) return self.render(response) name_list = post_data.getlist('name', None) # Title of the game type_list = post_data.getlist('type', None) # Game's type (game_type:slug) description_list = post_data.getlist('description', None) # some info cloned_list = post_data.getlist('cloned', None) # cloned game's slug authors_list = post_data.getlist('authors', None) # usernames of the game creators (array) # game title title = None if name_list and len(name_list): title = name_list[0] # description description = None if description_list and len(description_list): description = description_list[0] # game type type_slug = None if type_list and len(type_list): type_slug = type_list[0] try: type_slug = GameType.objects.get(slug=type_slug) except GameType.DoesNotExist: valid_data = False # cloned cloned_slug = None if cloned_list and len(cloned_list): cloned_slug = cloned_list[0] try: print cloned_slug cloned_slug = Game.objects.get(slug=cloned_slug) except Game.DoesNotExist: valid_data = False if valid_data: try: slug = slugify(title) cloned = None if cloned_slug: cloned = cloned_slug.id instance = Game(title=title, slug=slug, type=type_slug, description=description, cloned=cloned) instance.save() # handle authors if authors_list and len(authors_list): for author in authors_list: try: author_user = User.objects.get(username=author) author_instance = Author(user=author_user, game=instance) author_instance.save() except User.DoesNotExist: pass response=Response(200,{'statusCode' : 200 }) return self.render(response) except (ValueError, IntegrityError) as e: pass response=Response(400,{'message':'Invalid data'}) return self.render(response)
def submit(self): game = Game(**self.cleaned_data) game.create() return game.run()