Skip to content

fix(market): sort active offers by price - #4001

Merged
dudantas merged 3 commits into
opentibiabr:mainfrom
zoelner:fix/market-offer-list-sort
Jun 10, 2026
Merged

fix(market): sort active offers by price#4001
dudantas merged 3 commits into
opentibiabr:mainfrom
zoelner:fix/market-offer-list-sort

Conversation

@zoelner

@zoelner zoelner commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Description

Market offers were returned in database insertion order, so new offers always appeared at the bottom of the list regardless of their price.

Add ORDER BY to the per-item active-offer query in IOMarket::getActiveOffers (itemId/tier overload, used for market browsing): buy offers highest price first, sell offers lowest price first, with the oldest offer breaking price ties. Also order own offers by creation time for consistency. The parameterless overload (used only by loadItemsPrice for max prices) is left unsorted to avoid unnecessary DB load.

No schema change or migration is required: this is a read-only ordering change over columns that already exist.

List buy/purchase items by highest price and sell/sale items by lowest price.

Behaviour

Actual

Market offers are displayed in database insertion order. New offers always appear at the bottom of the list, regardless of whether they have a better price than existing ones.

Expected

Buy offers are sorted by highest price first, and sell offers by lowest price first. Ties are broken by the oldest creation time.

Fixes #issuenumber

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested

Tested manually using the game client to verify the visual ordering of the market board:

  • Opened the market interface for a specific item with multiple active offers.
  • Verified that Buy Offers are correctly sorted from highest to lowest price.
  • Verified that Sell Offers are correctly sorted from lowest to highest price.
  • Confirmed that offers with identical prices are sorted by creation date (oldest first).
image

Test Configuration:

  • Server Version: 15.11 (main)
  • Client: OTClient (main)
  • Operating System: Linux - Ubuntu

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I checked the PR checks reports
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

Summary by CodeRabbit

  • Bug Fixes
    • Improved market offer sorting: Active offers now display by price with creation time as a secondary sort, and player's own offers sort by creation time for better organization.

Market offers were returned in database insertion order, so new offers
always appeared at the bottom of the list regardless of their price.

Add ORDER BY to the per-item active-offer query in IOMarket::getActiveOffers
(itemId/tier overload, used for market browsing): buy offers highest price
first, sell offers lowest price first, with the oldest offer breaking price
ties. Also order own offers by creation time for consistency. The
parameterless overload (used only by loadItemsPrice for max prices) is left
unsorted to avoid unnecessary DB load.

No schema change or migration is required: this is a read-only ordering
change over columns that already exist.
Copilot AI review requested due to automatic review settings June 9, 2026 15:28
@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Two SQL queries in the market I/O module now include ORDER BY clauses. getActiveOffers() sorts active market offers by price direction (descending for purchases, ascending for sales) with a secondary sort by creation time. getOwnOffers() sorts user offers by creation time ascending.

Changes

Market Query Result Ordering

Layer / File(s) Summary
Market query result ordering
src/io/iomarket.cpp
getActiveOffers() introduces dynamic price ordering (DESC for buys, ASC for sells) with secondary created timestamp sort; getOwnOffers() adds explicit created ASC ordering to query results.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

📜 The market bustles, offers dance and sway,
Now sorted crisp—by price and newest day.
Buy high, sell low, creation time aglow,
A tiny sort that makes the market flow. 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly matches the main change: adding ORDER BY sorting to active offers by price, which is the primary fix described in the PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
src/io/iomarket.cpp

src/io/iomarket.cpp:10:10: fatal error: 'io/iomarket.hpp' file not found
10 | #include "io/iomarket.hpp"
| ^~~~~~~~~~~~~~~~~
1 error generated.
Error: the following clang command did not run successfully:
/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/bin/clang-18
@/tmp/coderabbit-infer/52f5ea797f6403982739a1c5f8c73cb1fac74f98-6c4bb3446f33b2cd/tmp/clang_command_.tmp.6e845a.txt
++Contents of '/tmp/coderabbit-infer/52f5ea797f6403982739a1c5f8c73cb1fac74f98-6c4bb3446f33b2cd/tmp/clang_command_.tmp.6e845a.txt':
"-cc1" "-load"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../../facebook-clang-plugins/libtooling/build/FacebookClangPlugin.dylib"
"-add-plugin" "BiniouASTExporter" "-plugin-arg-BiniouASTExporter" "-"
"-plugin-arg-BiniouASTExporter" "PREPEND_CURRENT_DIR=1"
"-plugin-arg-BiniouASTExporter" "MAX_STRING_SIZE=65535" "-cc1" "-triple"
"x86_64-unknown-linux-gnu" "-emit-obj" "-mrelax-all" "-disable-free

... [truncated 1058 characters] ...

s/clang/install/lib/clang/18/include"
"-internal-isystem" "/usr/local/include" "-internal-isystem"
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
"-internal-externc-isystem" "/usr/include/x86_64-linux-gnu"
"-internal-externc-isystem" "/include" "-internal-externc-isystem"
"/usr/include" "-Wno-ignored-optimization-argument" "-Wno-everything"
"-fdeprecated-macro" "-ferror-limit" "19" "-fgnuc-version=4.2.1"
"-fskip-odr-check-in-gmf" "-fcxx-exceptions" "-fexceptions"
"-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o"
"/tmp/coderabbit-infer/6c4bb3446f33b2cd/file.o" "-x" "c++"
"src/io/iomarket.cpp" "-O0" "-fno-builtin" "-include"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../lib/clang_wrappers/global_defines.h"
"-Wno-everything"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds deterministic ordering to market offer queries so results are returned sorted (by price/created for active offers, by created for own offers).

Changes:

  • Sort active offers by price (direction depends on buy vs sell) with created as a tie-breaker.
  • Sort “own offers” by created ascending.

Comment thread src/io/iomarket.cpp
Comment thread src/io/iomarket.cpp
Comment thread src/io/iomarket.cpp
Comment thread src/io/iomarket.cpp

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces ordering to the market offer queries in src/io/iomarket.cpp to ensure active and own offers are returned in a sorted order. The feedback suggests refactoring the query construction to use fmt::format and the global g_database() helper instead of std::ostringstream and Database::getInstance() for improved performance, consistency, and readability, as well as using std::string_view for the price order variable.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/io/iomarket.cpp
Comment thread src/io/iomarket.cpp

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/io/iomarket.cpp (1)

69-71: ⚡ Quick win

Add a focused regression check for market ordering semantics.

The SQL ordering changes look correct, but this C++ behavior change should be covered by a focused test (or the smallest practical query-level check) asserting: buy = highest price first, sell = lowest price first, tie-break = oldest created first, and own offers sorted by created ASC.

As per coding guidelines, “For C++ changes, prefer focused tests or the smallest practical build/check that validates the touched code.”

Also applies to: 104-104

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/io/iomarket.cpp` around lines 69 - 71, Add a focused test that validates
the market ordering semantics implemented around priceOrder and the built SQL in
query: create test data in a transient/test DB with multiple offers for the same
item/tier including different prices, same prices with different created
timestamps, and offers owned by the same player; then assert for
MARKETACTION_BUY the results return highest price first (price DESC) with ties
resolved by created ASC, and for sell (non-BUY) lowest price first (price ASC)
with ties resolved by created ASC and that a player's own offers are sorted by
created ASC; the test should either execute the actual SQL string built by the
code path that uses priceOrder and query or invoke the function that runs it so
you verify real query ordering and not only string contents.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/io/iomarket.cpp`:
- Around line 69-71: Add a focused test that validates the market ordering
semantics implemented around priceOrder and the built SQL in query: create test
data in a transient/test DB with multiple offers for the same item/tier
including different prices, same prices with different created timestamps, and
offers owned by the same player; then assert for MARKETACTION_BUY the results
return highest price first (price DESC) with ties resolved by created ASC, and
for sell (non-BUY) lowest price first (price ASC) with ties resolved by created
ASC and that a player's own offers are sorted by created ASC; the test should
either execute the actual SQL string built by the code path that uses priceOrder
and query or invoke the function that runs it so you verify real query ordering
and not only string contents.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 122f3ae4-27d7-43a4-8147-1662239c2319

📥 Commits

Reviewing files that changed from the base of the PR and between 77a911d and 52f5ea7.

📒 Files selected for processing (1)
  • src/io/iomarket.cpp

@sonarqubecloud

sonarqubecloud Bot commented Jun 9, 2026

Copy link
Copy Markdown

@dudantas
dudantas merged commit e6d575b into opentibiabr:main Jun 10, 2026
15 checks passed
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.

3 participants