本文整理汇总了Python中models.Move.save方法的典型用法代码示例。如果您正苦于以下问题:Python Move.save方法的具体用法?Python Move.save怎么用?Python Move.save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Move
的用法示例。
在下文中一共展示了Move.save方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_move
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def make_move(request, game_id):
if not request.user.is_authenticated():
return HttpResponse('Unauthorized', status=401)
if request.method == 'POST':
# make sure authed & joined
try:
json_data = json.loads(request.body)
#so check that the pile is valid
game = Game.objects.get(id = game_id)
usernames = [player.username for player in list(game.players.all())]
if (request.user.username not in usernames):
return HttpResponseServerError("Error, you havent joined this game")
#check that it's this users turn to go
moves = game.move_set.all().order_by('date')
if (len(moves) > 0 and moves.last().user.username == request.user.username):
#then this user went last, error pls
return HttpResponseServerError("Error, wait for the other play to move")
#expecting a "move"
# {
# pile : 0
# taken : 5
# }
# By default, this Manager is named FOO_set, where FOO is the source model name, lowercased.
piles = game.pile_set.all() # Returns all pile objects related to game.
pile_pos = json_data['pile']
if not (pile_pos >=0 and pile_pos < len(piles)):
#invalid pile id
return HttpResponseServerError("invalid pile id")
pile_pull = game.pile_set.get(position=pile_pos)
taken = int(json_data['taken'])
if (taken > pile_pull.amount):
return HttpResponseServerError("invalid, trying to take more than %s available" % (str(pile_pull.amount)))
if (taken <= 0):
return HttpResponseServerError("invalid, trying to take 0 or less")
#we should be good
#update the pile
pile_pull.amount = pile_pull.amount - taken
pile_pull.save()
#create a move
new_move = Move(
#start at 0th move
order = len(moves),
game = game,
pile = pile_pull,
taken = taken,
user = request.user
)
new_move.save()
return get_json(Game.objects.get(id = game_id))
except Game.DoesNotExist:
return HttpResponseNotFound("Game doesnt exist")
except Exception, e:
return HttpResponseServerError("Malformed data!")
示例2: test_save_move_normal
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def test_save_move_normal(self):
''' Tests a simple move to valid location.
Expected: no exceptions.
'''
game = Game.objects.get(id=1)
player1 = Player.objects.get(id=1)
move = Move(game=game, player=player1, position_x=1, position_y=1)
move.save()
from_db = Move.objects.get(id=1)
self.assertEqual(from_db.position_x, 1)
示例3: test_simple_game
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def test_simple_game(self):
game = Game.objects.get()
#create 3 piles - 2,3,4
for i, p in enumerate([2,3,4]):
new_pile = Pile(
position = i,
amount = p,
game = game
)
new_pile.save()
self.assertEqual(len(game.pile_set.all()), 3)
#create a couple users
user_one = User.objects.create_user("one", password="one")
user_one.save()
user_two = User.objects.create_user("two", password="two")
user_two.save()
#add them to the game
game.players.add(user_one)
game.players.add(user_two)
game.save()
#reget the game
game = Game.objects.get()
self.assertEqual(len(game.players.all()), 2)
#test a basic set of three moves, each player taking the max on each
for i, (user, take) in enumerate([(user_one, 2), (user_two, 3), (user_one, 4)]):
pile = game.pile_set.get(position = i)
self.assertEqual(pile.amount, take)
pile.amount = 0
pile.save()
#create a corresponding move
new_move = Move(
#start at 0th move
order = i,
game = game,
pile = pile,
taken = take,
user = user
)
new_move.save()
#check it went user 1, user 2, user 1
moves = game.move_set.all().order_by('date')
self.assertEqual(len(moves), 3)
# print moves[0]
# print moves[1]
# print moves[2]
self.assertEqual(moves[0].user, user_one)
self.assertEqual(moves[1].user, user_two)
self.assertEqual(moves[2].user, user_one)
示例4: test_save_same_player_immediate_succession
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def test_save_same_player_immediate_succession(self):
''' Tests same player making otherwise valid moves back to back.
Expected: An exception is raised with message for consecutive moves.
'''
game = Game.objects.get(id=1)
player1 = Player.objects.get(id=1)
player2 = Player.objects.get(id=2)
move = Move(game=game, player=player1, position_x=1, position_y=1)
move2 = Move(game=game, player=player1, position_x=1, position_y=2)
move.save()
from_db = Move.objects.get(id=1)
self.assertEqual(from_db.position_x, 1)
with self.assertRaises(IntegrityError) as context:
move2.save()
self.assertEqual(context.exception.message, 'Same player cannot make consecutive moves in the same game')
示例5: test_save_move_duplicate_positions
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def test_save_move_duplicate_positions(self):
''' Tests second move in same x/y position from different player.
Expected: An exception is raised with message for unique together (game, x, y).
'''
game = Game.objects.get(id=1)
player1 = Player.objects.get(id=1)
player2 = Player.objects.get(id=2)
move = Move(game=game, player=player1, position_x=1, position_y=1)
move2 = Move(game=game, player=player2, position_x=1, position_y=1)
move.save()
from_db = Move.objects.get(id=1)
self.assertEqual(from_db.position_x, 1)
with self.assertRaises(IntegrityError) as context:
move2.save()
self.assertEqual(context.exception.message, 'columns game_id, position_x, position_y are not unique')
示例6: test_invalid_positions
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def test_invalid_positions(self):
''' Tests for positions outside of possible values.
Expected: Exception raised
'''
game = Game.objects.get(id=1)
player1 = Player.objects.get(id=1)
invalid_position = 7
move = Move(game=game, player=player1, position_x=invalid_position, position_y=1)
with self.assertRaises(IntegrityError) as context:
move.save()
self.assertEqual(context.exception.message, 'position_x, %s is outside of valid range,0-2' % invalid_position)
move.position_x = 0
move.position_y = invalid_position
with self.assertRaises(IntegrityError) as context:
move.save()
self.assertEqual(context.exception.message, 'position_y, %s is outside of valid range,0-2' % invalid_position)
示例7: send
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def send(request):
'''
Updates latest move in database.
Responds with id of last player and move id
Expects the following POST parameters:
member_id
message
'''
# TODO modify for muti ip. Works from different IP addresses but not from same one
fb_id = request.session['fb_id']
player_id = request.GET.get('player_id')
game_id = request.GET.get('game_id')
try:
moveid = int( request.GET.get('moveid') )
cellid = int( request.GET.get('cellid') )
tileid = int( request.GET.get('tileid') )
rot = int( request.GET.get('rot') )
turnsInHand = int( request.GET.get('turnsInHand') )
loops = int( request.GET.get('loops') )
except:
cellid = EMPTYBOARD
fp = open( 'send_data.txt', 'w' )
g = Game.objects.get(name=game_id)
m = Move( game=g, player=player_id, cell=cellid, tile=tileid, rotation=rot, turnsInHand=turnsInHand, loops=loops )
#m = Move( game=g, player=101, cell=cellid, tile=101, rotation=101 )
m.save()
print >> fp, "=================="
print >> fp, "cell", m.cell
print >> fp, "tile", m.tile
print >> fp, "rot", m.rotation
print >> fp, "turns", m.turnsInHand
print >> fp, "loops", m.loops
#r.say(request.user, p['message'])
#return HttpResponse(p)
print >> fp, "Send complete:", fb_id, player_id, game_id
fp.close()
return HttpResponse( simplejson.dumps( { "playerids": player_id, "moveid": m.id } ), mimetype='application/javascript' )
示例8: test_pile_move_added
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def test_pile_move_added(self):
game = Game.objects.get()
new_pile = Pile(
position = 0,
amount = 3,
game = game
)
new_pile.save()
new_move = Move(
#start at 0th move
order = 0,
game = game,
pile = new_pile,
taken = 2
)
new_move.save()
self.assertEqual(len(game.pile_set.all()), 1)
self.assertEqual(len(game.move_set.all()), 1)
示例9: negotiate_first
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def negotiate_first(game, resolved_board, allempty, clientSecretHashed):
previousMove = game.lastMove
if previousMove.clientSecret == "":
raise UnfinishedMove("I was expecting client secret now.")
previousNumber = previousMove.moveNumber
serverSecret = rand256hex()
serverSecretHashed = sha256(serverSecret)
move = Move(
belongs_to = game,
moveNumber = previousNumber + 1,
board = move_logic.serialize_board(resolved_board),
allempty = move_logic.serialize_board(allempty),
serverSecret = serverSecret,
serverSecretHashed = serverSecretHashed,
clientSecret = "",
clientSecretHashed = clientSecretHashed
)
move.save()
game.lastMove = move
game.save()
return move
示例10: gameover
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def gameover(self):
board = [[128, 64, 32, 16], [64, 32, 16, 8], [32, 16, 8, 4], [16, 8, 1, 0]]
boardString = move_logic.serialize_board(board)
allempty = [[3, 3]]
allemptyString = move_logic.serialize_board(allempty)
move = Move(
belongs_to=None,
moveNumber=100,
board=boardString,
allempty=allemptyString,
serverSecret="a04aa4256d3fb3847c9a594975fd26efbdebac31bd17b88b0b39be592567230b",
serverSecretHashed="aa1e27d29a4e17d308534ceb6c2774d9ec4f2b9ef1022a49d66a3770ca424a13",
clientSecret="",
clientSecretHashed="fee9aa280f017fd716c496afe03a6291bf9a0fe80f07a9026efc4257b71fe848",
)
move.save()
game = Game(belongs_to=None, lastMove=move, gameover=False, result=None, gameid="jakasgra")
game.save()
move.belongs_to = game
move.save()
returned = negotiate_second(game, "b7d64a5f92b663560a3f000a947fae7cad549a5c2e396a4828c5151fd5034ce4")
self.assertEqual(returned["valid"], True)
self.assertEqual(returned["gameover"], False)
示例11: newGame
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def newGame(user):
board = move_logic.create_board(move_logic.size)
board[1][1] = 1
move = Move(
belongs_to = None,
moveNumber = 1,
board = move_logic.serialize_board(board),
serverSecret = "0",
serverSecretHashed = "0",
clientSecret = "0",
clientSecretHashed = "0"
)
move.save()
game = Game(
gameid = rand256hex(),
belongs_to = user,
gameover = False,
lastMove = move
)
game.save()
move.belongs_to = game
move.save()
return game
示例12: send
# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import save [as 别名]
def send(request):
'''
Expects the following POST parameters:
member_id
message
'''
# TODO modify for muti ip. Works from different IP addresses but not from same one
single_ip_test = True
if ( single_ip_test == False ):
user_id = request.session['member_id']
player_id = request.session['player_id']
game_id = request.session['game_id']
try:
moveid = int( request.GET.get('moveid') )
cellid = int( request.GET.get('cellid') )
tileid = int( request.GET.get('tileid') )
rot = int( request.GET.get('rot') )
turnsInHand = int( request.GET.get('turnsInHand') )
loops = int( request.GET.get('loops') )
if ( single_ip_test == True ):
user_id = ( request.GET.get('member_id') )
player_id = int( request.GET.get('player_id') )
game_id = int( request.GET.get('game_id') )
#p = simplejson.loads(request.raw_post_data)
#print p
#moveid = int( p['moveid'] )
#cellid = int( p['cellid'] )
#tileid = int( p['tileid'] )
#rot = int( p['rot'] )
#turnsInHand = int( p['turnsInHand'] )
#loops = int( p['loops'] )
#if ( single_ip_test == True ):
# user_id = int( p['member_id'] )
# player_id = int( p['player_id'] )
# game_id = int( p['game_id'] )
except:
cellid = EMPTYBOARD
print "=================="
print moveid
print cellid
print tileid
print rot
print turnsInHand
print loops
print "Send complete:", user_id, player_id, game_id
g = Game.objects.get(id=game_id)
m = Move( game=g, player=player_id, cell=cellid, tile=tileid, rotation=rot, turnsInHand=turnsInHand, loops=loops )
#m = Move( game=g, player=101, cell=cellid, tile=101, rotation=101 )
m.save()
#r.say(request.user, p['message'])
#return HttpResponse(p)
return HttpResponse( simplejson.dumps( { "playerids": player_id } ), mimetype='application/javascript' )