本文整理汇总了Python中models.Game.save方法的典型用法代码示例。如果您正苦于以下问题:Python Game.save方法的具体用法?Python Game.save怎么用?Python Game.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Game
的用法示例。
在下文中一共展示了Game.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def create(request):
""" User will can create games.
Keyword arguments:
request -
"""
if request.method == 'POST':
form = GameForm(request.POST)
if form.is_valid():
print form.cleaned_data
sport = Sport.objects.get(sport=form.cleaned_data['sport'])
player = Player.objects.get(user=request.user)
game = Game()
game.sport = sport
game.owner = request.user
game.start_date_and_time = datetime.combine(form.cleaned_data['start_date'], form.cleaned_data['start_time'])
game.address = form.cleaned_data['address']
game.minimum_players = form.cleaned_data['minimum_players']
game.restrictions = form.cleaned_data['restrictions']
game.active=True
if request.POST['lng'] and request.POST['lat']:
game.longitude = request.POST['lng']
game.latitude = request.POST['lat']
game.save()
return HttpResponseRedirect('/game/%s/' % game.id)
else:
form = GameForm()
return render(request, 'create.html', {'form': form})
示例2: test_points_multiple_games
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def test_points_multiple_games(self):
game1 = Game(
#tournament = self.tournament1,
time = self.time,
location = self.location,
teamA=self.team1,
teamB=self.team2,
score_teamA=3,
score_teamB=1,
)
game2 = Game(
#tournament = self.tournament1,
time = self.time,
location = self.location,
teamA=self.team1,
teamB=self.team2,
score_teamA=1,
score_teamB=1,
)
game1.save()
game2.save()
self.assertEquals(self.team1.points, 4)
self.assertEquals(self.team2.points, 1)
game1.delete()
game2.delete()
示例3: GameView
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
class GameView(TemplateView):
template_name = 'gameshow/game.html'
def _start_game(self):
player = Player.objects.create(name="Some Name", score=0)
self.thegame = Game()
self.thegame.players = player
self.thegame.title = 'My Game Title'
self.thegame.description = "Blah Blah Blah"
self.thegame.save()
def post(self, request, *args, **kwargs):
form = GameScore(request.POST)
if form.is_valid():
messages.add_message(self.request, messages.INFO, message="Enjoy your game!")
return redirect('game_main')
else:
return self.get(request, form=form)
def get_context_data(self, **kwargs):
self._start_game()
return {
'form': GameScore() if not kwargs.get('form') else kwargs['form'],
'game': self.thegame,
}
示例4: game
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def game(request):
#check if authed
# print "user " + str(request.user)
if not request.user.is_authenticated():
return HttpResponse('Unauthorized', status=401)
if request.method == 'GET':
#return a list of games available
return get_json(list(Game.objects.all()))
elif request.method == 'POST':
# make sure authed
try:
json_data = json.loads(request.body)
# user = this user
new_game = Game(status = Game.OPEN)
new_game.save()
new_game.players.add(request.user)
amounts = json_data['amounts']
for i, amount in enumerate(amounts):
new_pile = Pile(
position = i,
amount = amount,
game = new_game
)
new_pile.save()
return get_json(new_game)
except Exception, e:
return HttpResponseServerError("Malformed data!")
示例5: next_elimination_stage
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def next_elimination_stage(request, tournament_id, current_code=None):
tournament = Tournament.objects.get(pk=tournament_id)
if not current_code:
tg = TournamentGroup.objects.filter(tournament=tournament)[0]
tg.add_to_elimination()
return HttpResponseRedirect(reverse('view-tournament', args=[tournament_id]))
new_code = EliminationStatus
if tournament.has_double_elimination:
tes = list(TournamentElimination.objects.filter(game__tournament=tournament, status__code__startswith=current_code))
while tes:
first_game = tes.pop(0).game
second_game = tes.pop(0).game
if list(current_code)[0] == "W":
te = TournamentElimination.objects.get_or_create(game__tournament=tournament, status=list(current_code)[0] + str(int(list(current_code)[1]) + 1))
game = Game(home_team=first_game.loser(), away_team=second_game.loser(), tournament=tournament)
game.save()
te = TournamentElimination(game=game, status='L' + list(current_code)[1])
te.save()
else:
tes = list(TournamentElimination.objects.filter(game__tournament=tournament, status__code=current_code))
new_code = EliminationStatus.objects.get(code=(int(current_code) / 2))
while tes:
first_game = tes.pop(0).game
second_game = tes.pop(0).game
game = Game(home_team=first_game.winner(), away_team=second_game.winner(), tournament=tournament)
game.save()
te = TournamentElimination(game=game, status=new_code)
te.save()
return HttpResponseRedirect(reverse('view-tournament', args=[tournament_id]))
示例6: open_or_new_games
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def open_or_new_games(request):
if not request.COOKIES.get('nickname', ''):
return HttpResponseRedirect(reverse(set_nickname))
if request.method == 'POST':
form = NewGameForm(request.POST)
if form.is_valid():
nickname = re.sub(r'\W', '', request.COOKIES.get('nickname', ''))
num_players = form.cleaned_data['number_of_players']
game = Game(name=nickname, num_players=num_players)
game.save()
try:
player = game.add_player(nickname, True)
request.session['player_id'] = player.id
request.session['game_id'] = game.id
except Game.InvalidPlayer as x:
pass
#TODO: add session error message
return HttpResponseRedirect(reverse(current_game))
else:
form = NewGameForm()
return direct_to_template(request,
'open_or_new_games.html', {'form': form })
示例7: create_test_game
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def create_test_game(players):
game = Game()
for player in players:
game.players.append(player)
game.save()
game.create_map(1)
game.do_random_create_troops_and_resources()
return game
示例8: handlefbrequestcallback
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def handlefbrequestcallback(request):
fp = open( 'tmp.3.log', 'w' )
print >> fp, request
request_str = ( request.GET.get('request') )
request_to = ( request.GET.get('to') )
#request_from = ( request.GET.get('from') )
request_id = ( request.GET.get('id') )
request_from = request.session['fb_id']
#if request_to:
# request_to = int(request_to.encode('ascii'))
#if request_from:
# request_from = int(request_from.encode('ascii'))
#if request_id:
# request_id = int(request_id.encode('ascii'))
print >> fp, "request id", request_id
print >> fp, "request string", request_str
print >> fp, "request to", request_to
print >> fp, "request from", request_from
print >> fp, "request fb id", request.session['fb_id']
try:
m_to = Member.objects.get(fb_id=request_to)
except:
# need to make member
m_to = Member(fb_id=request_to)
try:
m_from = Member.objects.get(fb_id=request_from)
except:
# need to make member
m_from = Member(fb_id=request_from)
# name is set
gamename = "%s_%s_%s" % ( request_from, request_to, time.asctime(time.gmtime()) )
r = Room(name=gamename)
r.fb_request_id = request_id
r.save()
g = Game(room=r)
# game name and room name duplicated
g.name = gamename
g.save()
p = Player(game=g,turn=0)
p.member = m_from
p.save()
# TODO assumes two player game - generalize
p = Player(game=g,turn=1)
p.member = m_to
p.save()
#return HttpResponseRedirect('requestpage.html')
return HttpResponseRedirect( reverse('lupalo.views.lupalo') ) # Redirect after POST
示例9: testGameCreation
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def testGameCreation(self):
football = Game()
football.title = "The Big football game"
football.description = "Blah Blah Blah"
football.players = self.players[1]
football.save()
for player in self.players[1:]:
football.players = player
football.save()
示例10: new_game
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def new_game(request):
if request.method.lower() == 'get':
return render_to_response('asphodel/new_game_form.html',
{
'user': request.user,
'users': User.objects.all(),
'selected_users': [],
'STATIC_URL': '/static',
})
game_name = request.POST['name']
user_ids = set(map(int, request.POST.getlist('users')))
user_ids.add(request.user.id)
users = User.objects.filter(pk__in=user_ids)
errors = []
if not game_name:
errors.append("Please name your game")
if not users:
errors.append("You need more users to play a game")
if errors:
return render_to_response('asphodel/new_game_form.html', {
'errors': errors,
'user': request.user,
'users': User.objects.all(),
'selected_users': user_ids,
'STATIC_URL': '/static',
})
game = Game(name=game_name, start_date=datetime.now())
game.save()
players = []
my_player = None
for idx, user, color in zip(count(), users,
["red", "blue", "green", "yellow"]):
player = Player(game=game,
user=user,
color=color,
turn_index=idx,
resources=500)
player.save()
players.append(player)
if user == request.user:
my_player = player
game.current_turn = my_player if my_player else players[0]
game.save()
gen.map.new_game_map(game, players)
return redirect(game)
示例11: _add_and_return_level
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def _add_and_return_level(num, user):
game = Game(generator='Level'+num, name='Level '+num, public=False, main_user=user)
try:
game.save()
except ValidationError as e:
LOGGER.warn(e)
raise Http404
game.can_play = [user]
game.save()
level_attempt = LevelAttempt(game=game, user=user, level_number=num)
level_attempt.save()
return game
示例12: makerobogame
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def makerobogame(request):
fb_id = ROBOT1
request.session['fb_id'] = fb_id
# make robot player 1
try:
m_from = Member.objects.get( fb_id=ROBOT1 )
except Member.DoesNotExist:
m_from = Member(fb_id=ROBOT1)
m_from.user_name = 'Robo1'
m_from.fb_first_name = 'Robo1'
m_from.fb_last_name = 'Robo1'
m_from.save()
# make robot player 2
try:
m_to = Member.objects.get( fb_id=ROBOT2 )
except Member.DoesNotExist:
m_to = Member(fb_id=ROBOT2)
m_to.user_name = 'Robo2'
m_to.fb_first_name = 'Robo2'
m_to.fb_last_name = 'Robo2'
m_to.save()
# name is set
request_from = 'Robo1'
request_to = 'Robo2'
gamename = "%s_%s_%s" % ( request_from, request_to, time.asctime(time.gmtime()) )
r = Room(name=gamename)
r.fb_request_id = ROBOREQUEST
r.save()
g = Game(room=r)
# game name and room name duplicated
g.name = gamename
g.save()
p = Player(member=m_from) # player in game, link to member
p.game=g # game
p.turn=0 # static order of player in game
p.save()
request.session['player_id'] = p.turn
# TODO assumes two player game - generalize
p = Player( member = m_to )
p.game=g
p.turn=1
p.save()
return HttpResponse( simplejson.dumps( { "gamename": gamename } ), mimetype='application/javascript' )
示例13: new_game
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def new_game():
game = Game()
game.deck = ""
game.started = False
game.start_time = datetime.datetime.today() + datetime.timedelta(hours=1)#a timeout at some point?
game.save()
game_id = game.pk
#This actually belongs somewhere else, needs to be recreated with every server reboot.
gameEvent = threading.Event()
game_events[game_id] = gameEvent
gameLock = threading.Condition()
game_locks[game_id] = gameLock
print "creating a new game.", game.pk
return game
示例14: next_elimination_stage
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def next_elimination_stage(request, tournament_id, current_code=None):
tournament = Tournament.objects.get(pk=tournament_id)
if not current_code:
tg = TournamentGroup.objects.filter(tournament=tournament)[0]
tg.add_to_elimination()
return HttpResponseRedirect(reverse('view-tournament', args=[tournament_id]))
tes = list(TournamentElimination.objects.filter(game__tournament=tournament, status__code=current_code))
new_code = EliminationStatus.objects.get(code=(int(current_code) / 2))
while tes:
game = Game(home_team=tes.pop(0).game.winner(), away_team=tes.pop().game.winner(), tournament=tournament)
game.save()
te = TournamentElimination(game=game, status=new_code)
te.save()
return HttpResponseRedirect(reverse('view-tournament', args=[tournament_id]))
示例15: test_points_winner2
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import save [as 别名]
def test_points_winner2(self):
game = Game(
#tournament = self.tournament1,
time = self.time,
location = self.location,
teamA=self.team1,
teamB=self.team2,
score_teamA=1,
score_teamB=3,
)
game.save()
self.assertEquals(game.is_finished, True)
self.assertEquals(self.team1.points, 0)
self.assertEquals(self.team2.points, 3)
game.delete()