| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SmrTest\lib\DefaultGame; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use AbstractSmrPlayer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Smr\Bounty; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Smr\BountyType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Smr\Database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SmrTest\BaseIntegrationSpec; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @covers Smr\Bounty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class BountyTest extends BaseIntegrationSpec { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	protected function tablesToTruncate(): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		return ['bounty']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	public function test_update(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		// Calling update on a new bounty will add it to the database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		$bounty = new Bounty( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 			targetID: 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			bountyID: 4, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			gameID: 5, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			type: BountyType::UG, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			time: 6, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			claimerID: 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			credits: 8, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			smrCredits: 9, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		self::assertTrue($bounty->update()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		// Reading it out of the database gets back an equal object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$db = Database::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$dbRecord = $db->read('SELECT * FROM bounty')->record(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		self::assertEquals($bounty, Bounty::getFromRecord($dbRecord)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		// Calling update on an unchanged bounty should do nothing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		self::assertFalse($bounty->update()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		// Changing the credits should result in a database update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$bounty->increaseCredits(2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		self::assertSame(10, $bounty->getCredits()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		self::assertTrue($bounty->update()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		$bounty->increaseSmrCredits(2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		self::assertSame(11, $bounty->getSmrCredits()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		self::assertTrue($bounty->update()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		// Changing the claimer should result in a database update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		self::assertTrue($bounty->isActive()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$bounty->setClaimable(7); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		self::assertFalse($bounty->isActive()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		self::assertTrue($bounty->update()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		// All modified fields should be updated in the database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$dbRecord = $db->read('SELECT * FROM bounty')->record(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		self::assertEquals($bounty, Bounty::getFromRecord($dbRecord)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		// Updating a claimed bounty should delete it from the database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$bounty->setClaimed(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		self::assertSame(0, $bounty->getCredits()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		self::assertSame(0, $bounty->getSmrCredits()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		self::assertTrue($bounty->update()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$dbResult = $db->read('SELECT * FROM bounty'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		self::assertFalse($dbResult->hasRecord()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 	public function test_getPlacedOnPlayer(): void { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 		// Two bounties the same, except which player they're on | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 		$bounty1 = new Bounty( | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 			targetID: 1, | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 			bountyID: 7, | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 			gameID: 42, | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 			type: BountyType::HQ, | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 			time: 0, | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 			credits: 1, | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 		$bounty2 = new Bounty( | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 			targetID: 2, | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 			bountyID: 7, | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 			gameID: 42, | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 			type: BountyType::HQ, | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 			time: 0, | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 			credits: 1, | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 		// Add bounties to the database | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 		$bounty1->update(); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 		$bounty2->update(); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 		// We should only get $bounty1 if we get bounties on player 1 | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 		$player1 = $this->createStub(AbstractSmrPlayer::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 		$player1->method('getSQL')->willReturn('account_id=1 AND game_id=42'); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 		$bounties = Bounty::getPlacedOnPlayer($player1); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  | 		self::assertEquals([7 => $bounty1], $bounties); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	public function test_getClaimableByPlayer(): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$bounty1 = new Bounty( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			targetID: 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			bountyID: 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			gameID: 42, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			type: BountyType::HQ, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			time: 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			credits: 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		// Same as bounty1, except claimable by player 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		$bounty2 = new Bounty( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			targetID: 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			bountyID: 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			gameID: 42, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			type: BountyType::HQ, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			time: 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 			credits: 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			claimerID: 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		// Same as bounty1, except claimable by player 1 and for the UG | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		$bounty3 = new Bounty( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 			targetID: 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			bountyID: 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			gameID: 42, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			type: BountyType::UG, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			time: 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 			credits: 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 			claimerID: 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		// Add bounties to the database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		$bounty1->update(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		$bounty2->update(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$bounty3->update(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		$player1 = $this->createStub(AbstractSmrPlayer::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		$player1->method('getAccountID')->willReturn(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		$player1->method('getGameID')->willReturn(42); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		$bountiesHQ = Bounty::getClaimableByPlayer($player1, BountyType::HQ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		self::assertEquals([$bounty2], $bountiesHQ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$bountiesUG = Bounty::getClaimableByPlayer($player1, BountyType::UG); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		self::assertEquals([$bounty3], $bountiesUG); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		$bounties = Bounty::getClaimableByPlayer($player1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		self::assertEquals([$bounty2, $bounty3], $bounties); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  |