def test_territory_exchange_with_resources_canceled(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') aglax = Territory.objects.get(name='Aglax') efea = Territory.objects.get(name='Efea') offeror_r = Resources(currency=499) offeror_r.save() exchange = Exchange(offeror=arthur, offeror_territory=aglax, offeror_resources=offeror_r, offeree=brian, offeree_territory=efea) self.assertEqual(aglax.owner, arthur) self.assertEqual(efea.owner, brian) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(brian.player.resources.currency, 0) exchange.offer(user=arthur) self.assertEqual(arthur.player.resources.currency, 501) self.assertEqual(brian.player.resources.currency, 0) exchange.reject(user=brian) self.assertEqual(exchange.state, exchange.REJECTED) self.assertEqual(aglax.owner, arthur) self.assertEqual(efea.owner, brian) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(brian.player.resources.currency, 0)
def test_exchange_invalid_user(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') dickens = User.objects.get(username='******') offeror_r = Resources(currency=100) offeror_r.save() offeree_r = Resources(agricultural=10) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=brian, offeree_resources=offeree_r) with self.assertRaisesRegexp(ValidationError, "“brian” is not the offeror of this exchange."): exchange.offer(user=brian) exchange.offer(user=arthur) with self.assertRaisesRegexp(ValidationError, "“arthur” is not the offeree of this exchange."): exchange.accept(user=arthur) with self.assertRaisesRegexp(ValidationError, "“arthur” is not the offeree of this exchange."): exchange.reject(user=arthur) with self.assertRaisesRegexp(ValidationError, "“brian” is not the offeror of this exchange."): exchange.cancel(user=brian) self.assertEqual(exchange.state, exchange.WAITING)
def test_update_game_calls_send_actions(self): cells = [ Cell(0, 5, 10, Vec2(0, 0), Vec2(1, 1)), Cell(1, 5, 10, Vec2(0, 0), Vec2(1, 1)), Cell(2, 5, 10, Vec2(0, 0), Vec2(1, 1)) ] player = Player(0, "", 10, True, cells) game = Game(0, 0, 0, [player], Resources([], [], []), Map(0, 0), []) cells[0].target = Vec2(2, 2) cells[0].burst() cells[1].split() cells[1].trade(3) class MockApi: def send_actions(self, game_id, actions): self.actions = actions api = MockApi() update_game(api, game, lambda x: None) self.assertEqual(len(api.actions), 2) self.assertTrue(api.actions[0].target.almost_equals(Vec2(2, 2))) self.assertTrue(api.actions[0].burst) self.assertTrue(api.actions[1].split) self.assertEqual(3, api.actions[1].trade)
def test_parse(self): obj = { "regular": [{ "x": 1, "y": 2 }], "silver": [{ "x": 3, "y": 4 }], "gold": [{ "x": 5, "y": 6 }] } resources = Resources.parse(obj) self.assertEqual(1, len(resources.regular)) self.assertTrue(resources.regular[0].almost_equals(Vec2(1, 2))) self.assertEqual(1, len(resources.silver)) self.assertTrue(resources.silver[0].almost_equals(Vec2(3, 4))) self.assertEqual(1, len(resources.gold)) self.assertTrue(resources.gold[0].almost_equals(Vec2(5, 6)))
def test_simple_ask_for_donation_cancel(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeree_r = Resources(agricultural=1) offeree_r.save() exchange = Exchange(offeror=arthur, offeree=brian, offeree_resources=offeree_r) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11) exchange.offer(user=arthur) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11) exchange.cancel(user=arthur) self.assertEqual(exchange.state, exchange.CANCELED) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11)
def test_simple_donation_cancel(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeror_r = Resources(currency=1) offeror_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=brian) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(brian.player.resources.currency, 0) exchange.offer(user=arthur) self.assertEqual(arthur.player.resources.currency, 999) self.assertEqual(brian.player.resources.currency, 0) exchange.cancel(user=arthur) self.assertEqual(exchange.state, exchange.CANCELED) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(brian.player.resources.currency, 0)
def test_simple_exchange_accept(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') dickens = User.objects.get(username='******') offeror_r = Resources(currency=100) offeror_r.save() offeree_r = Resources(agricultural=10) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=brian, offeree_resources=offeree_r) arthur.player.resources.currency = 5 arthur.player.resources.save() with self.assertRaisesRegexp(ValidationError, "Offeror “arthur” lack resources to offer this exchange."): exchange.offer(user=arthur) arthur.player.resources.currency = 1000 arthur.player.resources.save() exchange.offer(user=arthur) self.assertEqual(arthur.player.resources.currency, 900) brian.player.resources.agricultural = 3 brian.player.resources.save() with self.assertRaisesRegexp(ValidationError, "Offeree “brian” lack resources to accept this exchange."): exchange.accept(user=brian) brian.player.resources.agricultural = 11 brian.player.resources.save() self.assertEqual(exchange.state, exchange.WAITING) self.assertEqual(arthur.player.resources.currency, 900) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(brian.player.resources.agricultural, 11) self.assertTrue(exchange.accept(user=brian)) self.assertEqual(exchange.state, exchange.ACCEPTED) self.assertEqual(arthur.player.resources.currency, 900) self.assertEqual(arthur.player.resources.agricultural, 10) self.assertEqual(brian.player.resources.currency, 100) self.assertEqual(brian.player.resources.agricultural, 1)
def test_public_exchange_offering_accept_reject(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') patsy = User.objects.get(username='******') offeror_r = Resources(currency=499) offeror_r.save() offeree_r = Resources(agricultural=1) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree_resources=offeree_r)
def mission_request( request, group_id, path, mission_type='move', target_id=None, split_group=None, resources=None, ): path = simplejson.loads(path) origin_group = Group.objects.get(id=group_id) if split_group: split_group = simplejson.loads(split_group) split_dict = {int(k): int(split_group[k]) for k in split_group} try: group = origin_group.split(split_dict) or origin_group except origin_group.CannotSplit: return simplejson.dumps( {'message': 'You cannot split this way' } ) else: group = origin_group if resources: resources = Resources.dict_to_resources(simplejson.loads(resources)) if resources: try: group.take_resources(origin_group.inhabitants_of, resources) except Village.DoesNotExist: pass if split_group: group.take_resources(origin_group, resources) if group.position_id == path[0]\ and group.get_tribe() == request.session['tribe']\ and Carte.is_a_path(path): #try: Mission.start_mission( group, path, mission_type, target_id, ) #TODO more precise handling of exceptions return simplejson.dumps( { 'message': 'le groupe part en expedition' } ) return simplejson.dumps({'message':'id du groupe:' + str(group_id)})
def test_simple_exchange_cancel(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') dickens = User.objects.get(username='******') offeror_r = Resources(currency=100) offeror_r.save() offeree_r = Resources(agricultural=10) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=brian, offeree_resources=offeree_r) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(brian.player.resources.agricultural, 11) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.agricultural, 0) exchange.offer(user=arthur) self.assertEqual(exchange.state, exchange.WAITING) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(brian.player.resources.agricultural, 11) self.assertEqual(arthur.player.resources.currency, 900) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertTrue(exchange.cancel(user=arthur)) self.assertEqual(exchange.state, exchange.CANCELED) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(brian.player.resources.agricultural, 11) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.agricultural, 0) with self.assertRaisesRegexp(ValidationError, "This exchange is not waiting for response."): exchange.accept(user=brian) with self.assertRaisesRegexp(ValidationError, "This exchange is not waiting for response."): exchange.reject(user=brian) with self.assertRaisesRegexp(ValidationError, "This exchange is not waiting for response."): exchange.cancel(user=arthur)
def test_bond_lending(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeror_r = Resources(currency=2000) offeror_r.save() offeree_r = Resources(currency=1000) offeree_r.save() # Arthur offers: $2000 as Bond in up to 2 turns in exchange of Brian # sending $1000. exchange = Exchange(offeror=brian, offeror_resources=offeror_r, offeror_as_bond=True, offeror_as_bond_maturity=2, offeree=arthur, offeree_resources=offeree_r) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(arthur.player.resources.currency, 1000) exchange.offer(user=brian) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(arthur.player.resources.currency, 1000) exchange.accept(user=arthur) self.assertEqual(exchange.state, exchange.ACCEPTED) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 0) bond = Bond.objects.filter(borrower=brian)[0] self.assertEqual(bond.state, bond.PENDING) with self.assertRaisesRegexp(ValidationError, "“arthur” is not the borrower of this bond."): bond.pay(user=arthur) with self.assertRaisesRegexp(ValidationError, "Offeror “brian” lack resources to offer this exchange."): bond.pay(user=brian) brian.player.resources.currency = 2000 brian.player.resources.save() # Need to request Bond again, to get updated Brian information bond = Bond.objects.filter(borrower=brian)[0] self.assertEqual(bond.state, bond.PENDING) bond.pay(user=brian) self.assertEqual(bond.state, bond.PAID) with self.assertRaisesRegexp(ValidationError, "This bond is not pending."): bond.pay(user=brian)
def test_empty_exchange(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeror_r = Resources(currency=0) offeror_r.save() offeree_r = Resources(agricultural=0) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=brian, offeree_resources=offeree_r) with self.assertRaisesRegexp(ValidationError, "Empty exchange."): exchange.offer(user=arthur)
def test_bond_payment(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') brian.player.resources.currency = 1000 brian.player.resources.save() offeror_r = Resources(currency=450) offeror_r.save() offeree_r = Resources(currency=311) offeree_r.save() # Arthur offers: $2000 as Bond in up to 2 turns in exchange of Brian # sending $1000. exchange = Exchange(offeror=brian, offeror_resources=offeror_r, offeror_as_bond=True, offeror_as_bond_maturity=2, offeree=arthur, offeree_resources=offeree_r, offeree_as_bond=True, offeree_as_bond_maturity=2) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) exchange.offer(user=brian) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) exchange.accept(user=arthur) self.assertEqual(exchange.state, exchange.ACCEPTED) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) brian_bond = Bond.objects.filter(borrower=brian)[0] self.assertEqual(brian_bond.state, brian_bond.PENDING) arthur_bond = Bond.objects.filter(borrower=arthur)[0] self.assertEqual(arthur_bond.state, arthur_bond.PENDING) brian_bond.pay(user=brian) arthur_bond.pay(user=arthur) self.assertEqual(brian_bond.state, brian_bond.PAID) self.assertEqual(arthur_bond.state, arthur_bond.PAID) arthur = User.objects.get(username='******') brian = User.objects.get(username='******') self.assertEqual(brian.player.resources.currency, 1000 - 450 + 311) self.assertEqual(arthur.player.resources.currency, 1000 - 311 + 450)
def test_fail_bond_of_bond(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeror_r = Resources(currency=499) offeror_r.save() bond = Bond(holder=arthur, borrower=brian) bond.save() exchange = Exchange(offeror=arthur, offeror_bond=bond, offeror_as_bond=True, offeree=brian) with self.assertRaisesRegexp(ValidationError, "Cannot build a Bond of Bond."): exchange.offer(user=arthur) exchange = Exchange(offeror=arthur, offeree=brian, offeree_bond=bond, offeree_as_bond=True) with self.assertRaisesRegexp(ValidationError, "Cannot build a Bond of Bond."): exchange.offer(user=arthur)
def test_exchange_between_the_same_player(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeror_r = Resources(currency=1) offeror_r.save() offeree_r = Resources(agricultural=1) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=arthur, offeree_resources=offeree_r) with self.assertRaisesRegexp(ValidationError, "Offeror and offeree cannot be the same."): exchange.offer(user=arthur)
def test_territory_buy_reject(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') aglax = Territory.objects.get(name='Aglax') efea = Territory.objects.get(name='Efea') offeree_r = Resources(agricultural=10) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_territory=aglax, offeree=brian, offeree_resources=offeree_r) self.assertEqual(aglax.owner, arthur) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11) exchange.offer(user=arthur) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11) exchange.reject(user=brian) self.assertEqual(exchange.state, exchange.REJECTED) self.assertEqual(aglax.owner, arthur) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11)
def test_bond_payment(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') gumby = User.objects.get(username='******') brian.player.resources.currency = 1000 brian.player.resources.save() offeror_r = Resources(currency=450) offeror_r.save() offeree_r = Resources(currency=311) offeree_r.save() exchange = Exchange(offeror=brian, offeror_resources=offeror_r, offeror_as_bond=True, offeror_as_bond_maturity=2, offeree=arthur, offeree_resources=offeree_r, offeree_as_bond=True, offeree_as_bond_maturity=2) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) exchange.offer(user=brian) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) exchange.accept(user=arthur) self.assertEqual(exchange.state, exchange.ACCEPTED) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) brian_bond = Bond.objects.filter(borrower=brian)[0] self.assertEqual(brian_bond.state, brian_bond.PENDING) arthur_bond = Bond.objects.filter(borrower=arthur)[0] self.assertEqual(arthur_bond.state, arthur_bond.PENDING) exchange = Exchange(offeror=brian, offeror_bond=brian_bond, offeree=arthur) with self.assertRaisesRegexp(ValidationError, ("Cannot exchange a Bond with its holder. " "To pay the bond, use the payment section.")): exchange.offer(user=brian) exchange = Exchange(offeror=brian, offeror_bond=arthur_bond, offeree=arthur) with self.assertRaisesRegexp(ValidationError, ("“brian” is not the borrower of this Bond.")): exchange.offer(user=brian) exchange = Exchange(offeror=brian, offeree=arthur, offeree_bond=brian_bond) with self.assertRaisesRegexp(ValidationError, "“arthur” is not the holder of this Bond."): exchange.offer(user=brian) exchange = Exchange(offeror=brian, offeree=arthur, offeree_bond=arthur_bond) with self.assertRaisesRegexp(ValidationError, "Cannot exchange a Bond with its holder."): exchange.offer(user=brian) brian_bond.pay(user=brian) arthur_bond.pay(user=arthur) exchange = Exchange(offeror=gumby, offeree=arthur, offeree_bond=arthur_bond) with self.assertRaisesRegexp(ValidationError, "This Bond is not pending."): exchange.offer(user=gumby) exchange = Exchange(offeror=arthur, offeror_bond=arthur_bond, offeree=gumby) with self.assertRaisesRegexp(ValidationError, "This Bond is not pending."): exchange.offer(user=arthur) self.assertEqual(brian_bond.state, brian_bond.PAID) self.assertEqual(arthur_bond.state, arthur_bond.PAID) arthur = User.objects.get(username='******') brian = User.objects.get(username='******') self.assertEqual(brian.player.resources.currency, 1000 - 450 + 311) self.assertEqual(arthur.player.resources.currency, 1000 - 311 + 450)
def test_bond_payment_while_offering(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') gumby = User.objects.get(username='******') brian.player.resources.currency = 1000 brian.player.resources.save() gumby.player.resources.currency = 455 gumby.player.resources.save() offeror_r = Resources(currency=450) offeror_r.save() offeree_r = Resources(currency=311) offeree_r.save() exchange = Exchange(offeror=brian, offeror_resources=offeror_r, offeror_as_bond=True, offeror_as_bond_maturity=2, offeree=arthur, offeree_resources=offeree_r, offeree_as_bond=True, offeree_as_bond_maturity=2) exchange.offer(user=brian) exchange.accept(user=arthur) brian_bond = Bond.objects.filter(borrower=brian)[0] self.assertEqual(brian_bond.holder, arthur) self.assertEqual(brian_bond.borrower, brian) self.assertEqual(brian_bond.state, brian_bond.PENDING) arthur_bond = Bond.objects.filter(borrower=arthur)[0] self.assertEqual(arthur_bond.holder, brian) self.assertEqual(arthur_bond.borrower, arthur) self.assertEqual(arthur_bond.state, arthur_bond.PENDING) exchange = Exchange(offeror=gumby, offeree=arthur, offeree_bond=arthur_bond) exchange.offer(user=gumby) self.assertEqual(exchange.state, exchange.WAITING) self.assertEqual(arthur_bond.holder, brian) self.assertEqual(arthur_bond.borrower, arthur) arthur_bond.pay(user=arthur) brian = User.objects.get(username='******') arthur = User.objects.get(username='******') gumby = User.objects.get(username='******') self.assertEqual(brian.player.resources.currency, 1000 + 311) self.assertEqual(arthur.player.resources.currency, 1000 - 311) self.assertEqual(gumby.player.resources.currency, 455) with self.assertRaisesRegexp(ValidationError, "This Bond is not pending."): exchange.accept(user=arthur) exchange = Exchange(offeror=gumby, offeree=brian, offeree_bond=brian_bond) exchange.offer(user=gumby) exchange.accept(user=brian) # Now brian's Bond became Gumby's Bond gumby_bond = Bond.objects.filter(borrower=gumby)[0] self.assertEqual(gumby_bond.holder, arthur) self.assertEqual(gumby_bond.borrower, gumby) gumby_bond.pay(user=gumby) arthur = User.objects.get(username='******') brian = User.objects.get(username='******') gumby = User.objects.get(username='******') self.assertEqual(brian.player.resources.currency, 1000 + 311) self.assertEqual(arthur.player.resources.currency, 1000 - 311 + 450) self.assertEqual(gumby.player.resources.currency, 5)