-
Couldn't load subscription status.
- Fork 12
Badge trading #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mogiiii
wants to merge
21
commits into
TwitchPlaysPokemon:badge-trading
Choose a base branch
from
Mogiiii:BadgeTrading
base: badge-trading
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Badge trading #231
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
1f56dd2
Add badge forms
Mogiiii 570514c
Add badge forms
Mogiiii c28319f
format code
Mogiiii 047a245
Expand !giftbadge args to allow specified form and source
Mogiiii 637b048
add BadgeSourceParser
Mogiiii c7e3d71
Convert badgeform to int
Mogiiii f706dbf
Add shiny field to badges
Mogiiii 3d867e1
fix issues found in code review
Mogiiii 4123a3e
Implement badge selling
Mogiiii be38334
Add test for null shiny field on badges
Mogiiii ff11dc9
Extend shiny test, fix bug
Mogiiii ce5c836
Add BadgeBuyOfferRepo
Mogiiii 63cfecd
simplify forms, general fixes, format
Mogiiii 15afff9
add indexes
Mogiiii 51c6d32
Extend BadgeBuyOfferRepo
Mogiiii 641bc4c
Rename BadgeBuyOfferRepo -> BadgeMarketRepo, fixes
Mogiiii a3f0c52
Rename BadgeBuyOfferRepo -> BadgeMarketRepo, fixes
Mogiiii 3d0038e
try to fill buy offers into the highest sell offer
Mogiiii 56cbc36
BadgeMarketRepo.ResolveBuyOffers returns sales
Mogiiii eb1ef3f
Merge branch 'master' of https://github.com/TwitchPlaysPokemon/tpp-co…
Mogiiii 7073e32
WIP expand commands to use metadata
Mogiiii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
TPP.Persistence.MongoDB.Tests/Repos/BadgeBuyOfferRepoTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| using Moq; | ||
| using NodaTime; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using TPP.Persistence.Models; | ||
| using TPP.Persistence.MongoDB.Repos; | ||
| using MongoDB.Driver; | ||
| using NUnit.Framework; | ||
| using TPP.Common; | ||
| using TPP.Persistence.Repos; | ||
|
|
||
| namespace TPP.Persistence.MongoDB.Tests.Repos | ||
| { | ||
| class BadgeBuyOfferRepoTest : MongoTestBase | ||
| { | ||
| public BadgeBuyOfferRepo CreateBadgeBuyOfferRepo() | ||
| { | ||
| IMongoDatabase db = CreateTemporaryDatabase(); | ||
| BadgeRepo mockBadgeRepo = new BadgeRepo(db, Mock.Of<IMongoBadgeLogRepo>(), Mock.Of<IClock>()); | ||
| return new BadgeBuyOfferRepo(db, mockBadgeRepo, Mock.Of<IClock>()); | ||
| } | ||
|
|
||
| internal class MockClock : IClock | ||
| { | ||
| public Instant FixedCurrentInstant = Instant.FromUnixTimeSeconds(1234567890); | ||
| public Instant GetCurrentInstant() => FixedCurrentInstant; | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task write_then_read_are_equal() | ||
| { | ||
| string userId = "m4"; | ||
| PkmnSpecies species = PkmnSpecies.OfId("1"); | ||
| int form = 0; | ||
| Badge.BadgeSource source = Badge.BadgeSource.ManualCreation; | ||
| bool shiny = true; | ||
| int price = 999; | ||
| int amount = 1; | ||
|
|
||
| IBadgeBuyOfferRepo badgeBuyOfferRepo = CreateBadgeBuyOfferRepo(); | ||
|
|
||
| BadgeBuyOffer offer = await badgeBuyOfferRepo.CreateBuyOffer(userId, species, form, source, shiny, price, amount, Instant.MaxValue); | ||
|
|
||
| Assert.AreEqual(userId, offer.UserId); | ||
| Assert.AreEqual(species, offer.Species); | ||
| Assert.AreEqual(form, offer.Form); | ||
| Assert.AreEqual(source, offer.Source); | ||
| Assert.AreEqual(shiny, offer.Shiny); | ||
| Assert.AreEqual(price, offer.Price); | ||
| Assert.AreEqual(amount, offer.Amount); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using MongoDB.Bson.Serialization; | ||
| using MongoDB.Bson.Serialization.IdGenerators; | ||
| using MongoDB.Driver; | ||
| using NodaTime; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using TPP.Common; | ||
| using TPP.Persistence.Models; | ||
| using TPP.Persistence.MongoDB.Serializers; | ||
| using TPP.Persistence.Repos; | ||
|
|
||
| namespace TPP.Persistence.MongoDB.Repos | ||
| { | ||
| public class BadgeBuyOfferRepo : IBadgeBuyOfferRepo | ||
| { | ||
| private const string CollectionName = "badgebuyoffers"; | ||
|
|
||
| public readonly IMongoCollection<BadgeBuyOffer> Collection; | ||
| private readonly IBadgeRepo _badgeRepo; | ||
| private readonly IClock _clock; | ||
|
|
||
| static BadgeBuyOfferRepo() | ||
| { | ||
| BsonClassMap.RegisterClassMap<BadgeBuyOffer>(cm => | ||
| { | ||
| cm.MapIdProperty(o => o.Id) | ||
| .SetIdGenerator(StringObjectIdGenerator.Instance) | ||
| .SetSerializer(ObjectIdAsStringSerializer.Instance); | ||
| cm.MapProperty(o => o.UserId).SetElementName("user"); | ||
| cm.MapProperty(o => o.Species).SetElementName("species"); | ||
| cm.MapProperty(o => o.Source).SetElementName("source"); | ||
| cm.MapProperty(o => o.CreatedAt).SetElementName("created_at"); | ||
| cm.MapProperty(o => o.Form).SetElementName("form") | ||
| .SetDefaultValue(0); | ||
| cm.MapProperty(o => o.Shiny).SetElementName("shiny") | ||
| .SetDefaultValue(false) | ||
| .SetIgnoreIfDefault(true); | ||
| }); | ||
| } | ||
|
|
||
| public BadgeBuyOfferRepo(IMongoDatabase database, BadgeRepo badgeRepo, IClock clock) | ||
| { | ||
| _badgeRepo = badgeRepo; | ||
|
|
||
| database.CreateCollectionIfNotExists(CollectionName).Wait(); | ||
| Collection = database.GetCollection<BadgeBuyOffer>(CollectionName); | ||
| _clock = clock; | ||
| } | ||
|
|
||
| public async Task<BadgeBuyOffer> CreateBuyOffer(string userId, PkmnSpecies species, int? form, Badge.BadgeSource? source, bool? shiny, int price, int amount, Instant? createdAt=null) | ||
| { | ||
| BadgeBuyOffer buyOffer = new BadgeBuyOffer( | ||
| id: string.Empty, | ||
| userId: userId, | ||
| species: species, | ||
| form: form, | ||
| source: source, | ||
| shiny: shiny, | ||
| price: price, | ||
| amount: amount, | ||
| createdAt: createdAt ?? _clock.GetCurrentInstant() | ||
| ); | ||
|
|
||
| await Collection.InsertOneAsync(buyOffer); | ||
| Debug.Assert(buyOffer.Id.Length > 0, "The MongoDB driver injected a generated ID"); | ||
| return buyOffer; | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| using NodaTime; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using TPP.Common; | ||
|
|
||
| namespace TPP.Persistence.Models | ||
| { | ||
| public class BadgeBuyOffer | ||
| { | ||
| /// <summary> | ||
| /// Unique Id. | ||
| /// </summary> | ||
| public string Id { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// The ID of the user that created the buy offer. | ||
| /// </summary> | ||
| public string UserId { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// The species of pokemon to buy. | ||
| /// </summary> | ||
| public PkmnSpecies Species { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// The form of pokemon to buy. | ||
| /// </summary> | ||
| public int? Form { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// The source of the badge to buy. | ||
| /// </summary> | ||
| public Badge.BadgeSource? Source { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Is the offer seeking shiny badges. | ||
| /// </summary> | ||
| public bool? Shiny { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// How much to pay for each badge. | ||
| /// </summary> | ||
| public int Price { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// The number of badges to buy. | ||
| /// </summary> | ||
| public int Amount { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// When the buy offer was created. | ||
| /// </summary> | ||
| public Instant CreatedAt { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// When this offer was last updated. | ||
| /// </summary> | ||
| public Instant WaitingSince { get; private set; } | ||
|
|
||
| //duration and expires_at depricated from old core | ||
|
|
||
| public BadgeBuyOffer(string id, string userId, PkmnSpecies species, int? form, Badge.BadgeSource? source, bool? shiny, int price, int amount, Instant createdAt) | ||
| { | ||
| Id = id; | ||
| UserId = userId; | ||
| Species = species; | ||
| Form = form; | ||
| Source = source; | ||
| Shiny = shiny; | ||
| Price = price; | ||
| Amount = amount; | ||
| CreatedAt = createdAt; | ||
| WaitingSince = createdAt; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Decrement the amount to buy. | ||
| /// </summary> | ||
| public void decrement(Instant decrementedAt) | ||
| { | ||
| if (Amount <= 0) | ||
| throw new InvalidOperationException("The buy offer has no badges remaining, and cannot be decremented further."); | ||
| Amount--; | ||
| WaitingSince = decrementedAt; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using NodaTime; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using TPP.Common; | ||
| using TPP.Persistence.Models; | ||
|
|
||
| namespace TPP.Persistence.Repos | ||
| { | ||
| public interface IBadgeBuyOfferRepo | ||
| { | ||
| public Task<BadgeBuyOffer> CreateBuyOffer(string userId, PkmnSpecies species, int? form, Badge.BadgeSource? source, bool? shiny, int price, int amount, Instant? createdAt); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.