Skip to content

Integrate API events, add market hours scheduling, and unify trading events#132

Merged
MaksyKun merged 7 commits intodevfrom
copilot/integrate-api-events
Nov 17, 2025
Merged

Integrate API events, add market hours scheduling, and unify trading events#132
MaksyKun merged 7 commits intodevfrom
copilot/integrate-api-events

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 17, 2025

API events existed but were never fired. Trading had separate InstrumentBuyEvent and ShareBuyEvent/ShareSellEvent causing fragmentation.

Changes

Event Architecture

  • Created TransactionType enum - Distinguishes INSTRUMENT, SHARE, CRYPTO transactions
  • Unified buy/sell events - ShareBuyEvent/ShareSellEvent now handle all asset types via TransactionType
  • Removed InstrumentBuyEvent - Deprecated in favor of unified approach
  • Added getHandlerList() - All 14 events now properly implement Bukkit static method requirement

Service Integration

Integrated events across 7+ services with proper timing:

Cancellable (pre-action):

  • TradingService - ShareBuyEvent, ShareSellEvent before wallet/holdings modification
  • WatchlistService - Add/remove before database write
  • CompanyService - Create before cost charge, IPO before market enable
  • CryptoService - Create before balance checks

Non-cancellable (post-action):

  • WalletService - Balance change after modification (with reason enum)
  • InvitationService - Employee join after database insert
  • CompanyService - Employee leave after removal (with wasKicked flag)
  • StockMarketService - Price updates fire InstrumentPriceUpdateEvent
  • MarketScheduler - Market open/close events with scheduling

Market Hours System

New MarketScheduler service manages trading hours:

  • Configuration in market.yml:

    • market.hours.enabled - Enable/disable market hours restriction
    • market.hours.open-at - Market opening time (HH:mm:ss format, default: "06:00:00")
    • market.hours.close-at - Market closing time (HH:mm:ss format, default: "22:00:00")
    • market.hours.timezone - Timezone for market hours (default: "UTC", supports IANA timezone IDs)
  • Features:

    • Automatically opens/closes market based on configured schedule
    • Fires MarketOpenEvent when market opens (with broadcast)
    • Fires MarketCloseEvent when market closes (with broadcast)
    • Checks market hours every minute
    • Supports overnight markets (e.g., 22:00 - 06:00)
    • Added translations: MarketClosed, MarketOpens, MarketCloses

InstrumentPriceUpdateEvent Integration

  • Integrated in StockMarketService.updateAllStockPrices()
  • Fires for each stock with meaningful price change (>0.0001)
  • Includes old price, new price, symbol, and timestamp
  • Non-blocking (exceptions don't break price updates)

Code Quality Improvements

  • Fixed import style - Replaced all inline fully qualified class names with proper imports at the top
  • Affected 7 service files with cleaner, more maintainable code

API Changes

Before:

InstrumentBuyEvent event = new InstrumentBuyEvent(player, instrumentId, symbol, qty, price, total);

After:

ShareBuyEvent event = new ShareBuyEvent(
    player, 
    TransactionType.INSTRUMENT,  // or SHARE, CRYPTO
    assetId,
    assetSymbol,
    qty,
    pricePerUnit,
    totalCost
);

Legacy constructors maintained for backward compatibility but marked @Deprecated.

All Events Now Integrated

  • ✅ ShareBuyEvent / ShareSellEvent
  • ✅ WalletBalanceChangeEvent
  • ✅ WatchlistAddEvent / WatchlistRemoveEvent
  • ✅ CompanyCreateEvent
  • ✅ CompanyEmployeeJoinEvent / CompanyEmployeeLeaveEvent
  • ✅ CompanyIPOEvent
  • ✅ CryptoCreateEvent
  • MarketOpenEvent / MarketCloseEvent (NEW - with scheduling)
  • InstrumentPriceUpdateEvent (NEW - on price updates)

Remaining event: CircuitBreakerTriggeredEvent - Circuit breaker exists but not actively triggered

Breaking Changes

Original prompt

This section details on the original issue you should resolve

<issue_title>API-Event integration</issue_title>
<issue_description>There already are lots of events under api/events however, I dont see them being integrated in the code anywhere. Please observe all events and integrate them into their respective spots.

Furthermore, if more potential is found, feel free to implement more events to listen too.

Last important thing:

  • InstrumentBuyEvent shall behave same for instruments, company shares and cryptos. I want ShareBuyEvent and ShareSellEvent to be the leading events for all of those being seperated through an enum. The InstrumentBuyEvent should be removed therefore.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits November 17, 2025 12:03
…Event

Co-authored-by: MaksyKun <77341370+MaksyKun@users.noreply.github.com>
Co-authored-by: MaksyKun <77341370+MaksyKun@users.noreply.github.com>
Co-authored-by: MaksyKun <77341370+MaksyKun@users.noreply.github.com>
Copilot AI changed the title [WIP] Integrate existing events from api/events into code Integrate API events and unify trading events with TransactionType enum Nov 17, 2025
Copilot AI requested a review from MaksyKun November 17, 2025 12:14
@MaksyKun
Copy link
Copy Markdown
Contributor


InstrumentPriceUpdateEvent - No active price simulation
CircuitBreakerTriggeredEvent - Circuit breaker exists but not triggered
MarketOpenEvent/MarketCloseEvent - No market scheduler```

@copilot They seem not to be integrated at the moment, so:
- implement in market.yml the options `open-at: 06:00:00` and `close-at: 22:00:00` and `timezone: <default utc>` to define from when to when the market is allowed for trades etc. This also includes adding the translations, the events not listed yet etc.
- Please implement logic for the InstrumentPriceUpdateEvent 
- I had to remove some import namespace from the objects in my last commit. Please add into your copilot instruction that doing such long classes is not ok. I one them to be imported at the top from now on instead of being embedded in the code.

Co-authored-by: MaksyKun <77341370+MaksyKun@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 17, 2025


InstrumentPriceUpdateEvent - No active price simulation

...

Implemented all requested changes in commit 9f5c90b:

1. Market Hours Configuration
Added to market.yml:

market:
  hours:
    enabled: true
    open-at: "06:00:00"
    close-at: "22:00:00"
    timezone: "UTC"  # Supports IANA timezone IDs

2. MarketScheduler Service

  • Automatically opens/closes market based on configured hours
  • Fires MarketOpenEvent and MarketCloseEvent with broadcasts
  • Checks every minute, supports overnight markets
  • Added translations: MarketClosed, MarketOpens, MarketCloses

3. InstrumentPriceUpdateEvent Integration

  • Fires in StockMarketService.updateAllStockPrices() for each price change
  • Includes old/new price, symbol, and timestamp
  • Non-blocking (exceptions don't break updates)

4. Import Style Fixed
Replaced all fully qualified class names with proper imports in 7 service files:

  • org.bukkit.BukkitBukkit
  • org.bukkit.entity.PlayerPlayer
  • java.util.UUIDUUID

All events now properly integrated with clean import statements.

Copilot AI changed the title Integrate API events and unify trading events with TransactionType enum Integrate API events, add market hours scheduling, and unify trading events Nov 17, 2025
@MaksyKun MaksyKun marked this pull request as ready for review November 17, 2025 13:32
@MaksyKun MaksyKun merged commit 28aabba into dev Nov 17, 2025
@MaksyKun MaksyKun deleted the copilot/integrate-api-events branch November 17, 2025 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API-Event integration

2 participants