本文整理汇总了Python中games.models.Game.save方法的典型用法代码示例。如果您正苦于以下问题:Python Game.save方法的具体用法?Python Game.save怎么用?Python Game.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类games.models.Game
的用法示例。
在下文中一共展示了Game.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ship_creation
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def test_ship_creation(self):
"""Test that Ship instances are created correctly."""
game = Game()
game.save()
user = User(username='user', password='password')
user.save()
player = Player(user=user)
player.save()
team = Team(player=player, game=game)
team.save()
ship = Ship(
team=team,
x=0,
y=0,
length=3,
direction=Ship.CARDINAL_DIRECTIONS['WEST']
)
self.assertTrue(isinstance(ship, Ship))
self.assertEqual(
str(ship),
'Game 1 - user\'s 3L at (0, 0) facing West'
)
示例2: invite
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def invite(hometeam,awayteam):
game=Game(player_1=hometeam, player_2=awayteam)
game.save()
message = client.messages.create(to=hometeam, from_="+16502156107",
body="Your game has started. Make your first move...")
示例3: create
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def create(self, request):
serializer = GameSerializer(data=request.data)
if serializer.is_valid():
user = request.user
player = user.player
player.hosted_count += 1
player.game_count += 1
if request.data['nickname'] != "":
player.nickname = request.data['nickname']
elif player.nickname == "":
player.nickname = player.user.username
player.save()
new_game = Game()
new_game.host = player
new_game.name = request.data['name']
new_game.motto = request.data['motto']
new_game.passcode = request.data['passcode']
new_game.save()
new_game_player_detail = GamePlayerDetail()
new_game_player_detail.game = new_game
new_game_player_detail.player = player
new_game_player_detail.save()
return Response({'status': 'game set', 'game_id': new_game.id})
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例4: TeamPresenterTestCase
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
class TeamPresenterTestCase(TestCase):
def setUp(self):
self.game = Game()
self.game.save()
self.user = User.objects.create_user('user', '', 'password')
self.player = Player(user=self.user)
self.player.save()
self.team = Team(player=self.player, game=self.game)
self.team.save()
def test_from_team(self):
presenter = TeamPresenter.from_team(team=self.team, game=self.game)
self.assertEqual(presenter.player.username, self.user.username)
self.assertTrue(presenter.is_next)
self.assertEqual(presenter.winner, self.team.winner)
self.assertEqual(presenter.alive, self.team.alive)
self.assertEqual(len(presenter.tiles), GAME_SIZE)
self.assertEqual(len(presenter.tiles[0]), GAME_SIZE)
def test_make_tiles(self):
tiles = TeamPresenter.make_tiles(team=self.team, game=self.game)
self.assertEqual(len(tiles), GAME_SIZE)
for i in range(0, GAME_SIZE):
self.assertEqual(len(tiles[i]), GAME_SIZE)
示例5: new_game
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def new_game(request):
game = Game(
title = 'New Game',
description = 'Write a description here',
author = request.user,
pubDate = datetime.datetime.now(),
lastEditDate = datetime.datetime.now())
game.save()
return render_to_response('build.html',{'game_id':game.id}, context_instance=RequestContext(request))
示例6: save_game
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def save_game(request):
if request.method == 'POST':
info = json.loads(request.body);
newGame = Game(title=info.get('title'), description=info.get('description'))
newGame.pubDate = datetime.datetime.now()
newGame.author = User.objects.get(username='chrismcmath')
newGame.numberQuestions = 0
newGame.save()
pdb.set_trace()
return HttpResponse(request.body, mimetype="application/json")
示例7: create
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def create(request):
if request.method == "POST":
game = Game()
game.name = request.POST['name']
game.player_one = request.user.get_profile()
couch = couchdb.Server(settings.COUCHDB_HOST)
db = couch[settings.COUCHDB_NAME]
game.couch_id = db.create({'game': db['game_start']['game']})
game.save()
return HttpResponseRedirect(game.get_absolute_url())
else:
raise Http404
示例8: post
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def post(self, request, *args, **kwargs):
if request.user.is_authenticated():
form = CreateGameForm(request.POST, max_players=MAX_PLAYERS)
if form.is_valid():
opponent_usernames = []
for i in range(0, MAX_PLAYERS):
field_name = "opponent_username_{}".format(i)
opponent_usernames.append(form.cleaned_data[field_name])
try:
opponent_users = []
for opponent_username in opponent_usernames:
if len(opponent_username) > 0:
opponent_users.append(User.objects.get(username=opponent_username))
except User.DoesNotExist:
error_message = "User does not exist! " "Are you sure the username is correct?"
messages.error(request, error_message)
context = {"form": form}
return render(request, self.template_name, context)
user_player = Player.objects.get(user=request.user)
opponent_players = [Player.objects.get(user=opponent_user) for opponent_user in opponent_users]
# Create a game plus teams and ships for both players
# Creation in Game -> Team -> Ships order is important
# to satisfy ForeignKey dependencies.
game = Game()
game.save()
user_team = Team(player=user_player, game=game, last_turn=-2)
opponent_teams = [
Team(player=opponent_player, game=game, last_turn=-1) for opponent_player in opponent_players
]
user_team.save()
for opponent_team in opponent_teams:
opponent_team.save()
user_ships = make_ships(user_team, Ship.LENGTHS)
for opponent_team in opponent_teams:
opponent_ships = make_ships(opponent_team, Ship.LENGTHS)
for user_ship in user_ships:
user_ship.save()
for opponent_ship in opponent_ships:
opponent_ship.save()
return HttpResponseRedirect(reverse("game", args=[game.id]))
else:
messages.error(request, "Invalid form.")
context = {"form": form}
return render(request, self.template_name, context)
else:
return HttpResponseRedirect("/login")
示例9: test_team_creation
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def test_team_creation(self):
"""Test that Team instances are created correctly."""
game = Game()
game.save()
user = User.objects.create_user('user', '', 'password')
player = Player(user=user)
player.save()
team = Team(player=player, game=game)
self.assertTrue(isinstance(team, Team))
self.assertEqual(str(team), 'Game 1 - user (last_turn=0)')
示例10: import_games
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def import_games(csv_file):
with open(csv_file, 'r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
g = Game()
g.location = row["location"]
g.name = row["name"]
hour = row["time"].split(":")[0]
minute = row["time"].split(":")[1]
year = row["date"].split("-")[0]
month = row["date"].split("-")[1]
day = row["date"].split("-")[2]
g.date = date(int(year), int(month), int(day))
g.time = time(int(hour), int(minute))
g.save()
示例11: create_game
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def create_game(game_num, roster_soup):
# pull team name data from txt - first two instances of class teamHeading
teams = roster_soup.find_all("td", class_="teamHeading")
away_name = teams[0].text.encode("utf-8")
home_name = teams[1].text.encode("utf-8")
# Creates TeamGame objects home, away
away = TeamGame.objects.create(team=Team.objects.get(name=away_name))
home = TeamGame.objects.create(team=Team.objects.get(name=home_name))
game = Game(game_id=game_num, team_home=home, team_away=away, date=import_date(roster_soup))
game.save()
return game
示例12: test_game_requires_players
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def test_game_requires_players(self):
arena = make_a_whole_arena()
course = make_course(arena)[0]
player = make_players()[0]
game = Game(
course=course,
creator=player,
created=datetime.now(),
)
game.save()
game.players = [player]
game.start()
# Check that game doesn't finish without players
self.assertRaises(ValidationError, game.finish)
示例13: _create_games
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def _create_games(self, n=1):
if Color.objects.count() < Game.AVAILABLE_COLORS:
print "Impossible to create games. No enough colors."
print "ESCAPING..."
return
print "Populating Games"
for i in range(n):
available_colors = Color.objects.all().order_by('?')[:Game.AVAILABLE_COLORS]
game = Game()
game.initial_amount = random.randint(0, 100)
game.percentage = random.randint(0, 100)
game.payment = random.uniform(0, 5)
game.start_date = timezone.now()
game.end_date = game.start_date + datetime.timedelta(days=random.randint(3, 30))
game.save()
game.available_colors.add(*available_colors)
print "SUCCESS"
示例14: create_game
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def create_game(game):
""" Create game object from Steam API call """
from games.models import Game
slug = slugify(game['name'])[:50]
LOGGER.info("Adding %s to library" % game['name'])
steam_game = Game(
name=game['name'],
steamid=game['appid'],
slug=slug,
)
if game['img_logo_url']:
steam_game.get_steam_logo(game['img_logo_url'])
steam_game.get_steam_icon(game['img_icon_url'])
try:
steam_game.save()
except Exception as ex:
LOGGER.error("Error while saving game %s: %s", game, ex)
raise
return steam_game
示例15: test_game_creation
# 需要导入模块: from games.models import Game [as 别名]
# 或者: from games.models.Game import save [as 别名]
def test_game_creation(self):
"""Test that Game instances are created correctly."""
game = Game()
game.save()
user1 = User.objects.create_user('user1', '', 'password')
user2 = User.objects.create_user('user2', '', 'password')
player1 = Player(user=user1)
player2 = Player(user=user2)
player1.save()
player2.save()
team1 = Team(player=player1, game=game)
team2 = Team(player=player2, game=game)
team1.save()
team2.save()
self.assertTrue(isinstance(game, Game))
self.assertEqual(str(game), '1 - user1 user2')