From 48259307627f8318159d6778efa876924a031387 Mon Sep 17 00:00:00 2001 From: mathisDlmr <140653974+mathisDlmr@users.noreply.github.com> Date: Mon, 19 May 2025 18:10:05 +0200 Subject: [PATCH 01/13] =?UTF-8?q?Pipeline=20de=20d=C3=A9ploiement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..81aa901 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,39 @@ +name: Build ocktopus + +on: + push: + branches: [ main ] + pull_request: + branches: [ "main" ] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install PHP dependencies + run: composer install --no-dev --optimize-autoloader + + - name: Prepare deploy folder + run: | + mkdir deploy + rsync -av --progress ./ ./deploy \ + --exclude='.env' \ + --exclude='storage' \ + --exclude='node_modules' \ + --exclude='tests' \ + --exclude='vendor/bin' \ + --exclude='deploy' \ + --exclude='.git' + + cd deploy + tar -czf ../deploy.tar.gz . + + - name: Upload artifact + uses: actions/upload-artifact@v3 + with: + name: laravel-deploy-package + path: deploy.tar.gz From 1f155471d722113a641bdf7f1d3f5721683f8d74 Mon Sep 17 00:00:00 2001 From: mathisDlmr <140653974+mathisDlmr@users.noreply.github.com> Date: Tue, 20 May 2025 20:12:36 +0200 Subject: [PATCH 02/13] =?UTF-8?q?Changement=20de=20l'action=20(voir=20si?= =?UTF-8?q?=20=C3=A7a=20enl=C3=A8ve=20le=20fail=20du=20build)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 81aa901..8bb6899 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,7 +12,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v2 - name: Install PHP dependencies run: composer install --no-dev --optimize-autoloader From 79a4acbbbfdec8b4aec4a285588963a9bbfea372 Mon Sep 17 00:00:00 2001 From: mathisDlmr <140653974+mathisDlmr@users.noreply.github.com> Date: Tue, 20 May 2025 20:19:05 +0200 Subject: [PATCH 03/13] Tout passer en v2 --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8bb6899..da41f8f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,7 +33,7 @@ jobs: tar -czf ../deploy.tar.gz . - name: Upload artifact - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v2 with: name: laravel-deploy-package path: deploy.tar.gz From e794ac5f61ebb92cedb8f849a91347d99982883a Mon Sep 17 00:00:00 2001 From: mathisDlmr <140653974+mathisDlmr@users.noreply.github.com> Date: Tue, 20 May 2025 20:39:11 +0200 Subject: [PATCH 04/13] =?UTF-8?q?Voir=20o=C3=B9=20=C3=A7a=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index da41f8f..6d7b163 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,5 +1,9 @@ name: Build ocktopus +env: + ACTIONS_STEP_DEBUG: true + ACTIONS_RUNNER_DEBUG: true + on: push: branches: [ main ] From 068be37d9c3aacee56c39dbba8fe35152a3dbce8 Mon Sep 17 00:00:00 2001 From: mathisDlmr Date: Tue, 20 May 2025 20:44:25 +0200 Subject: [PATCH 05/13] Je pense que j'avais pas les droits pour modifier les policy de l'orga donc je met directement l'action dans le repo --- .github/actions/upload-artifact | 1 + .github/workflows/build.yml | 8 ++------ 2 files changed, 3 insertions(+), 6 deletions(-) create mode 160000 .github/actions/upload-artifact diff --git a/.github/actions/upload-artifact b/.github/actions/upload-artifact new file mode 160000 index 0000000..6027e3d --- /dev/null +++ b/.github/actions/upload-artifact @@ -0,0 +1 @@ +Subproject commit 6027e3dd177782cd8ab9af838c04fd81a07f1d47 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6d7b163..bbabb30 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,9 +1,5 @@ name: Build ocktopus -env: - ACTIONS_STEP_DEBUG: true - ACTIONS_RUNNER_DEBUG: true - on: push: branches: [ main ] @@ -35,9 +31,9 @@ jobs: cd deploy tar -czf ../deploy.tar.gz . - + - name: Upload artifact - uses: actions/upload-artifact@v2 + uses: ./.github/actions/upload-artifact with: name: laravel-deploy-package path: deploy.tar.gz From c6a9b14edf6166fd634b885918a55cbd360592cc Mon Sep 17 00:00:00 2001 From: mathisDlmr Date: Wed, 4 Jun 2025 15:48:15 +0200 Subject: [PATCH 06/13] Premier brouillon du code pour handle les transactions pendant l'event de bourse --- .../Controllers/TransactionController.php | 92 +++++++++++++++++++ app/Models/Articles.php | 13 +++ app/Models/MarketPrices.php | 13 +++ database/migrations/create_articles_table.php | 28 ++++++ .../migrations/create_market_prices_table.php | 28 ++++++ routes/api.php | 3 + 6 files changed, 177 insertions(+) create mode 100644 app/Http/Controllers/TransactionController.php create mode 100644 app/Models/Articles.php create mode 100644 app/Models/MarketPrices.php create mode 100644 database/migrations/create_articles_table.php create mode 100644 database/migrations/create_market_prices_table.php diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php new file mode 100644 index 0000000..609d6b9 --- /dev/null +++ b/app/Http/Controllers/TransactionController.php @@ -0,0 +1,92 @@ +input('badge_id'); + $items = $request->input('items'); // [[article_id, quantity], ...] + $sessionId = $request->header('session_id'); + + $systemId = env('WEEZEVENT_SYSTEM_ID'); + $appKey = env('WEEZEVENT_APP_KEY'); + $fundId = env('WEEZEVENT_FUND_ID'); + + $mappedItems = []; + foreach ($items as [$articleId, $quantity]) { + $categoryId = $this->getCategoryFromArticle($articleId); + if (in_array($categoryId, [10, 11])) { // Si c'est une bière, on remplace par le prix du marché + $currentPrice = $this->getCurrentMarketPrice($articleId); + $replacementArticleId = $this->mapPriceToBeerArticle($currentPrice); + $mappedItems[] = [$replacementArticleId, $quantity]; + $this->updateMarket($articleId, $quantity); + } else { + $mappedItems[] = [$articleId, $quantity]; + } + } + + $response = Http::withHeaders([ + 'accept-language' => 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7', + ])->post('https://api.nemopay.net/services/POSS3/transaction?system_id='.$systemId.'&app_key='.$appKey.'&sessionid='.$sessionId, [ + 'badge_id' => $badgeId, + 'obj_ids' => $mappedItems, + 'fun_id' => $fundId, + ])->throw(); + + return response()->json($response->json()); + } + + private function getCategoryFromArticle($articleId) + { + return Articles::where('article_id', $articleId)->first()->category_id; + } + + private function getCurrentMarketPrice($articleId) + { + return MarketPrices::where('article_id', $articleId) + ->first() + ->price ?? 1.0; + } + + private function mapPriceToBeerArticle($price) + { + $rounded = round($price, 2); + $priceToId = [ + 0.10 => 20445, + 0.20 => 20446, + 0.30 => 21003, + 0.40 => 20682, + 0.50 => 23596, + 0.60 => 23597, + 0.70 => 23598, + 0.80 => 23599, + 0.90 => 23600, + 1.00 => 23601, + 1.10 => 23602, + 1.20 => 23603, + 1.30 => 23604, + 1.40 => 23605, + ]; + return $priceToId[$rounded] ?? 23601; + } + + private function updateMarket($articleId, $quantity) + { + $lastPrice = $this->getCurrentMarketPrice($articleId); + $newPrice = min(1.40, $lastPrice + 0.10 * $quantity); + + MarketPrices::firstOrCreate([ + 'article_id' => $articleId, + 'price' => $newPrice, + 'updated_at' => now(), + ]); + } +} diff --git a/app/Models/Articles.php b/app/Models/Articles.php new file mode 100644 index 0000000..e68a114 --- /dev/null +++ b/app/Models/Articles.php @@ -0,0 +1,13 @@ +integer('article_id')->primary(); + $table->integer('category_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('articles'); + } +}; diff --git a/database/migrations/create_market_prices_table.php b/database/migrations/create_market_prices_table.php new file mode 100644 index 0000000..1e3e663 --- /dev/null +++ b/database/migrations/create_market_prices_table.php @@ -0,0 +1,28 @@ +integer('article_id')->primary(); + $table->float('price')->default(1.0); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('market_prices'); + } +}; diff --git a/routes/api.php b/routes/api.php index acdebed..e1d553d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -3,6 +3,7 @@ use App\Http\Controllers\BachController; use App\Http\Controllers\ExonerationController; use App\Http\Controllers\TodayConsumptionController; +use App\Http\Controllers\TransactionController; use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; @@ -36,3 +37,5 @@ // Exoneration d'un achat Route::post('/exoneration', [ExonerationController::class, 'storeExonerations']); + +Route::post('/transaction', [TransactionController::class, 'handle']); \ No newline at end of file From 6e063ae649095e4e0a0bdd5423a0e65c542951b2 Mon Sep 17 00:00:00 2001 From: mathisDlmr Date: Wed, 4 Jun 2025 18:29:08 +0200 Subject: [PATCH 07/13] =?UTF-8?q?Am=C3=A9liore=20la=20logique=20de=20fluct?= =?UTF-8?q?uation=20mais=20=C3=A7a=20reste=20sur=20du=201.0=20et=201.1=20m?= =?UTF-8?q?ajoritairement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TransactionController.php | 75 ++++++++++++------- app/Models/Articles.php | 2 +- database/migrations/create_articles_table.php | 1 + 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 609d6b9..63e7ac7 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -6,7 +6,6 @@ use App\Models\MarketPrices; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; -use Illuminate\Support\Facades\Log; class TransactionController extends Controller { @@ -14,7 +13,7 @@ public function handle(Request $request) { $badgeId = $request->input('badge_id'); $items = $request->input('items'); // [[article_id, quantity], ...] - $sessionId = $request->header('session_id'); + $sessionId = $request->input('session_id'); $systemId = env('WEEZEVENT_SYSTEM_ID'); $appKey = env('WEEZEVENT_APP_KEY'); @@ -58,35 +57,61 @@ private function getCurrentMarketPrice($articleId) private function mapPriceToBeerArticle($price) { - $rounded = round($price, 2); + $rounded = number_format(round($price, 2), 1, '.', ''); $priceToId = [ - 0.10 => 20445, - 0.20 => 20446, - 0.30 => 21003, - 0.40 => 20682, - 0.50 => 23596, - 0.60 => 23597, - 0.70 => 23598, - 0.80 => 23599, - 0.90 => 23600, - 1.00 => 23601, - 1.10 => 23602, - 1.20 => 23603, - 1.30 => 23604, - 1.40 => 23605, + '0.1' => 20445, + '0.2' => 20446, + '0.3' => 21003, + '0.4' => 20682, + '0.5' => 23596, + '0.6' => 23597, + '0.7' => 23598, + '0.8' => 23599, + '0.9' => 23600, + '1.0' => 23601, + '1.1' => 23602, + '1.2' => 23603, + '1.3' => 23604, + '1.4' => 23605, ]; return $priceToId[$rounded] ?? 23601; } private function updateMarket($articleId, $quantity) { - $lastPrice = $this->getCurrentMarketPrice($articleId); - $newPrice = min(1.40, $lastPrice + 0.10 * $quantity); - - MarketPrices::firstOrCreate([ - 'article_id' => $articleId, - 'price' => $newPrice, - 'updated_at' => now(), - ]); + $maxPrice = 1.4; + $minPrice = 0.1; + $priceStep = 0.05; + $fluctuationRange = 0.02; + + $article = Articles::where('article_id', $articleId)->first(); + if (!$article) return; + + $categoryId = $article->category_id; + + $allArticles = Articles::where('category_id', $categoryId)->get(); + + foreach ($allArticles as $otherArticle) { + $currentPrice = MarketPrices::firstOrCreate( + ['article_id' => $otherArticle->article_id], + ); + + $newPrice = $currentPrice->price; + + if ($otherArticle->article_id == $articleId) { + $newPrice += $priceStep * $quantity; + } else { + $newPrice -= $priceStep * 0.5; + } + + $fluctuation = rand(-100, 100) / 100 * $fluctuationRange; + $newPrice += $fluctuation; + + $newPrice = max($minPrice, min($maxPrice, $newPrice)); + + $currentPrice->price = $newPrice; + $currentPrice->updated_at = now(); + $currentPrice->save(); + } } } diff --git a/app/Models/Articles.php b/app/Models/Articles.php index e68a114..702a044 100644 --- a/app/Models/Articles.php +++ b/app/Models/Articles.php @@ -9,5 +9,5 @@ class Articles extends Model { use HasFactory; - protected $fillable = ['article_id', 'category_id']; + protected $fillable = ['article_id', 'article_name', 'category_id']; } diff --git a/database/migrations/create_articles_table.php b/database/migrations/create_articles_table.php index 3ed92ca..35a95eb 100644 --- a/database/migrations/create_articles_table.php +++ b/database/migrations/create_articles_table.php @@ -13,6 +13,7 @@ public function up(): void { Schema::create('articles', function (Blueprint $table) { $table->integer('article_id')->primary(); + $table->string('article_name'); $table->integer('category_id'); $table->timestamps(); }); From 80be830836ba87fdf7d1de77a3cdfee9e6a15ca7 Mon Sep 17 00:00:00 2001 From: mathisDlmr Date: Tue, 10 Jun 2025 12:18:28 +0200 Subject: [PATCH 08/13] =?UTF-8?q?D=C3=A9bug=20et=20equilibrage=20du=20marc?= =?UTF-8?q?h=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TransactionController.php | 67 ++++++++++++------- app/Models/MarketPrices.php | 6 ++ .../migrations/create_market_prices_table.php | 2 +- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 63e7ac7..4f46ea0 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -6,6 +6,7 @@ use App\Models\MarketPrices; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; class TransactionController extends Controller { @@ -19,10 +20,18 @@ public function handle(Request $request) $appKey = env('WEEZEVENT_APP_KEY'); $fundId = env('WEEZEVENT_FUND_ID'); + $firstTransaction = Http::withHeaders([ + 'accept-language' => 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7', + ])->post('https://api.nemopay.net/services/POSS3/transaction?system_id='.$systemId.'&app_key='.$appKey.'&sessionid='.$sessionId, [ + 'badge_id' => $badgeId, + 'obj_ids' => $items, + 'fun_id' => $fundId, + ])->throw(); + $mappedItems = []; foreach ($items as [$articleId, $quantity]) { $categoryId = $this->getCategoryFromArticle($articleId); - if (in_array($categoryId, [10, 11])) { // Si c'est une bière, on remplace par le prix du marché + if ($categoryId === 11) { // Si c'est une bière pression, on remplace par le prix du marché $currentPrice = $this->getCurrentMarketPrice($articleId); $replacementArticleId = $this->mapPriceToBeerArticle($currentPrice); $mappedItems[] = [$replacementArticleId, $quantity]; @@ -57,22 +66,25 @@ private function getCurrentMarketPrice($articleId) private function mapPriceToBeerArticle($price) { - $rounded = number_format(round($price, 2), 1, '.', ''); + $rounded = number_format(round($price / 0.05) * 0.05, 2, '.', ''); $priceToId = [ - '0.1' => 20445, - '0.2' => 20446, - '0.3' => 21003, - '0.4' => 20682, - '0.5' => 23596, - '0.6' => 23597, - '0.7' => 23598, - '0.8' => 23599, - '0.9' => 23600, - '1.0' => 23601, - '1.1' => 23602, - '1.2' => 23603, - '1.3' => 23604, - '1.4' => 23605, + '0.60' => 23597, + '0.65' => 20445, + '0.70' => 23598, + '0.75' => 20446, + '0.80' => 23599, + '0.85' => 21003, + '0.90' => 23600, + '0.95' => 20682, + '1.00' => 23601, + '1.05' => 23596, + '1.10' => 23602, + '1.15' => 23648, + '1.20' => 23603, + '1.25' => 23649, + '1.30' => 23604, + '1.35' => 23650, + '1.40' => 23605, ]; return $priceToId[$rounded] ?? 23601; } @@ -80,9 +92,9 @@ private function mapPriceToBeerArticle($price) private function updateMarket($articleId, $quantity) { $maxPrice = 1.4; - $minPrice = 0.1; - $priceStep = 0.05; - $fluctuationRange = 0.02; + $minPrice = 0.6; + $priceStep = 0.01; + // $fluctuationRange = 0.05; $article = Articles::where('article_id', $articleId)->first(); if (!$article) return; @@ -90,28 +102,31 @@ private function updateMarket($articleId, $quantity) $categoryId = $article->category_id; $allArticles = Articles::where('category_id', $categoryId)->get(); + $nbArticles = $allArticles->count(); foreach ($allArticles as $otherArticle) { - $currentPrice = MarketPrices::firstOrCreate( - ['article_id' => $otherArticle->article_id], + $currentPrice = MarketPrices::firstOrNew( + ['article_id' => $otherArticle->article_id] ); + if (!$currentPrice->exists) { + $currentPrice->price = 1.00; + } + $newPrice = $currentPrice->price; if ($otherArticle->article_id == $articleId) { $newPrice += $priceStep * $quantity; } else { - $newPrice -= $priceStep * 0.5; + $newPrice -= ($priceStep * $quantity) / ($nbArticles - 1); } - $fluctuation = rand(-100, 100) / 100 * $fluctuationRange; - $newPrice += $fluctuation; - $newPrice = max($minPrice, min($maxPrice, $newPrice)); - $currentPrice->price = $newPrice; $currentPrice->updated_at = now(); $currentPrice->save(); + + Log::info('Article '.$otherArticle->article_id.' => '.$newPrice); } } } diff --git a/app/Models/MarketPrices.php b/app/Models/MarketPrices.php index 284e054..fa0bbdf 100644 --- a/app/Models/MarketPrices.php +++ b/app/Models/MarketPrices.php @@ -9,5 +9,11 @@ class MarketPrices extends Model { use HasFactory; + protected $table = 'market_prices'; + protected $primaryKey = 'article_id'; + protected $keyType = 'int'; + public $incrementing = false; + public $timestamps = true; + protected $fillable = ['article_id', 'price']; } diff --git a/database/migrations/create_market_prices_table.php b/database/migrations/create_market_prices_table.php index 1e3e663..fab4133 100644 --- a/database/migrations/create_market_prices_table.php +++ b/database/migrations/create_market_prices_table.php @@ -13,7 +13,7 @@ public function up(): void { Schema::create('market_prices', function (Blueprint $table) { $table->integer('article_id')->primary(); - $table->float('price')->default(1.0); + $table->decimal('price', 3, 2)->default(1.00); $table->timestamps(); }); } From 9f9c9d6e0c328959802fa972b9d7f056fdf167ea Mon Sep 17 00:00:00 2001 From: mathisDlmr Date: Fri, 13 Jun 2025 18:11:45 +0200 Subject: [PATCH 09/13] Dernier patch de mise en prod --- .../Controllers/TransactionController.php | 35 ++++++++++++------- routes/web.php | 2 ++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 4f46ea0..d73e214 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -20,18 +20,18 @@ public function handle(Request $request) $appKey = env('WEEZEVENT_APP_KEY'); $fundId = env('WEEZEVENT_FUND_ID'); - $firstTransaction = Http::withHeaders([ - 'accept-language' => 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7', - ])->post('https://api.nemopay.net/services/POSS3/transaction?system_id='.$systemId.'&app_key='.$appKey.'&sessionid='.$sessionId, [ - 'badge_id' => $badgeId, - 'obj_ids' => $items, - 'fun_id' => $fundId, - ])->throw(); - $mappedItems = []; foreach ($items as [$articleId, $quantity]) { $categoryId = $this->getCategoryFromArticle($articleId); - if ($categoryId === 11) { // Si c'est une bière pression, on remplace par le prix du marché + if ($categoryId == 11) { // Si c'est une bière pression, on remplace par le prix du marché + $firstTransaction = Http::withHeaders([ // Première transaction de l'article (qui aura été mis à 0e) pour les stats + 'accept-language' => 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7', + ])->post('https://api.nemopay.net/services/POSS3/transaction?system_id='.$systemId.'&app_key='.$appKey.'&sessionid='.$sessionId, [ + 'badge_id' => $badgeId, + 'obj_ids' => [[$articleId, $quantity]], + 'fun_id' => $fundId, + ])->throw(); + $currentPrice = $this->getCurrentMarketPrice($articleId); $replacementArticleId = $this->mapPriceToBeerArticle($currentPrice); $mappedItems[] = [$replacementArticleId, $quantity]; @@ -49,12 +49,13 @@ public function handle(Request $request) 'fun_id' => $fundId, ])->throw(); - return response()->json($response->json()); + return response()->json($response->json()); } private function getCategoryFromArticle($articleId) { - return Articles::where('article_id', $articleId)->first()->category_id; + $article = Articles::where('article_id', $articleId)->first() ?? null; + return $article ? $article->category_id : null; } private function getCurrentMarketPrice($articleId) @@ -125,8 +126,18 @@ private function updateMarket($articleId, $quantity) $currentPrice->price = $newPrice; $currentPrice->updated_at = now(); $currentPrice->save(); + } + } - Log::info('Article '.$otherArticle->article_id.' => '.$newPrice); + public function getPrices() + { + $articles = MarketPrices::all(); + + foreach ($articles as $article) { + $articleModel = Articles::where('article_id', $article->article_id)->first(); + $article->article_name = $articleModel ? $articleModel->article_name : null; } + + return $articles; } } diff --git a/routes/web.php b/routes/web.php index 990be13..adc44c3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -58,3 +58,5 @@ return response()->json(['message' => 'Image not found'], 404); } })->name('download'); + +Route::get('/bourse',[\App\Http\Controllers\TransactionController::class,'getPrices']); From 80524abaa98b516094ebd744b91884604cde0264 Mon Sep 17 00:00:00 2001 From: mathisDlmr Date: Sat, 14 Jun 2025 11:10:08 +0200 Subject: [PATCH 10/13] =?UTF-8?q?J'ai=20p=C3=A9t=C3=A9=20mon=20crane,=20?= =?UTF-8?q?=C3=A7a=20va=20etre=20maxi=20pr=C3=A9cis=20en=20fait?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TransactionController.php | 35 +++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index d73e214..6b2c300 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -67,34 +67,25 @@ private function getCurrentMarketPrice($articleId) private function mapPriceToBeerArticle($price) { - $rounded = number_format(round($price / 0.05) * 0.05, 2, '.', ''); - $priceToId = [ - '0.60' => 23597, - '0.65' => 20445, - '0.70' => 23598, - '0.75' => 20446, - '0.80' => 23599, - '0.85' => 21003, - '0.90' => 23600, - '0.95' => 20682, - '1.00' => 23601, - '1.05' => 23596, - '1.10' => 23602, - '1.15' => 23648, - '1.20' => 23603, - '1.25' => 23649, - '1.30' => 23604, - '1.35' => 23650, - '1.40' => 23605, - ]; - return $priceToId[$rounded] ?? 23601; + $rounded = number_format($price, 2, '.', ''); + $basePrice = 0.60; + $baseId = 23658; + + $diff = ($rounded - $basePrice) * 100; // nb centimes d'écart + $id = $baseId + (int)round($diff); // l'ID part de baseId + centimes d'écart + + if ($id < $baseId || $id > 23658 + 80) { + return 23698; // Prix inconnu ? -> 1e + } + + return $id; } private function updateMarket($articleId, $quantity) { $maxPrice = 1.4; $minPrice = 0.6; - $priceStep = 0.01; + $priceStep = 0.03; // $fluctuationRange = 0.05; $article = Articles::where('article_id', $articleId)->first(); From 65a9ab916e93b8299d81aa45a8e66adba2f0923c Mon Sep 17 00:00:00 2001 From: mathisDlmr Date: Mon, 16 Jun 2025 09:48:54 +0200 Subject: [PATCH 11/13] Versio finale --- .../Controllers/TransactionController.php | 60 ++++++++++++++++--- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 6b2c300..a3a368d 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -6,7 +6,6 @@ use App\Models\MarketPrices; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; -use Illuminate\Support\Facades\Log; class TransactionController extends Controller { @@ -34,8 +33,13 @@ public function handle(Request $request) $currentPrice = $this->getCurrentMarketPrice($articleId); $replacementArticleId = $this->mapPriceToBeerArticle($currentPrice); - $mappedItems[] = [$replacementArticleId, $quantity]; - $this->updateMarket($articleId, $quantity); + for ($i = 0; $i < $quantity; $i++) { + $currentPrice = $this->getCurrentMarketPrice($articleId); + $replacementArticleId = $this->mapPriceToBeerArticle($currentPrice); + + $mappedItems[] = [$replacementArticleId, 1]; // On passe les articles 1 par 1 au cas où le prix augmente + $this->updateMarket($articleId, 1); + } } else { $mappedItems[] = [$articleId, $quantity]; } @@ -86,16 +90,26 @@ private function updateMarket($articleId, $quantity) $maxPrice = 1.4; $minPrice = 0.6; $priceStep = 0.03; - // $fluctuationRange = 0.05; $article = Articles::where('article_id', $articleId)->first(); if (!$article) return; $categoryId = $article->category_id; - $allArticles = Articles::where('category_id', $categoryId)->get(); $nbArticles = $allArticles->count(); + if ($nbArticles <= 1) return; + + $weights = []; + $totalWeight = 0; + foreach ($allArticles as $otherArticle) { + if ($otherArticle->article_id == $articleId) continue; + + $w = mt_rand(50, 150); + $weights[$otherArticle->article_id] = $w; + $totalWeight += $w; + } + foreach ($allArticles as $otherArticle) { $currentPrice = MarketPrices::firstOrNew( ['article_id' => $otherArticle->article_id] @@ -110,14 +124,36 @@ private function updateMarket($articleId, $quantity) if ($otherArticle->article_id == $articleId) { $newPrice += $priceStep * $quantity; } else { - $newPrice -= ($priceStep * $quantity) / ($nbArticles - 1); + $weight = $weights[$otherArticle->article_id]; + $share = ($priceStep * $quantity) * ($weight / $totalWeight); + $newPrice -= $share; } - $newPrice = max($minPrice, min($maxPrice, $newPrice)); + $newPrice = max($minPrice, min($maxPrice, round($newPrice, 2))); + $currentPrice->price = $newPrice; $currentPrice->updated_at = now(); $currentPrice->save(); } + + $sum = 0; + foreach ($allArticles as $article) { + $marketPrice = MarketPrices::where('article_id', $article->article_id)->first(); + $sum += $marketPrice ? $marketPrice->price : 1.00; + } + + $mean = $sum / $nbArticles; + $delta = 1.00 - $mean; + $correction = round($delta, 4); + + foreach ($allArticles as $article) { + $marketPrice = MarketPrices::where('article_id', $article->article_id)->first(); + if (!$marketPrice) continue; + + $adjusted = max($minPrice, min($maxPrice, round($marketPrice->price + $correction, 2))); + $marketPrice->price = $adjusted; + $marketPrice->save(); + } } public function getPrices() @@ -129,6 +165,14 @@ public function getPrices() $article->article_name = $articleModel ? $articleModel->article_name : null; } - return $articles; + $response = [ + 'refresh' => 5000, + 'data' => $articles, + ]; + + return response()->json($response) + ->header('Access-Control-Allow-Origin', '*') + ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') + ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); } } From 17a832d60dd3b3c23cbbdb1949ee0f7e3d6179b2 Mon Sep 17 00:00:00 2001 From: mathisDlmr Date: Mon, 16 Jun 2025 10:09:55 +0200 Subject: [PATCH 12/13] version poru tous les jours --- app/Http/Controllers/TransactionController.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index a3a368d..de8cb73 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -78,8 +78,8 @@ private function mapPriceToBeerArticle($price) $diff = ($rounded - $basePrice) * 100; // nb centimes d'écart $id = $baseId + (int)round($diff); // l'ID part de baseId + centimes d'écart - if ($id < $baseId || $id > 23658 + 80) { - return 23698; // Prix inconnu ? -> 1e + if ($id < $baseId || $id > 23658 + 180) { // Si le prix est inférieur à 0.60 ou spérieur à 2.40 + return 23778; // Prix inconnu ? -> 1e80 } return $id; @@ -87,9 +87,10 @@ private function mapPriceToBeerArticle($price) private function updateMarket($articleId, $quantity) { - $maxPrice = 1.4; + $maxPrice = 2.4; $minPrice = 0.6; - $priceStep = 0.03; + $priceStep = 0.07; + $balanceMarket = 1.80; // Le marché se balance autour de cette valeur $article = Articles::where('article_id', $articleId)->first(); if (!$article) return; @@ -143,7 +144,7 @@ private function updateMarket($articleId, $quantity) } $mean = $sum / $nbArticles; - $delta = 1.00 - $mean; + $delta = $balanceMarket - $mean; $correction = round($delta, 4); foreach ($allArticles as $article) { From 4e9d3738baf8adfab205fdffba7b6ffa2e943f91 Mon Sep 17 00:00:00 2001 From: mathisDlmr Date: Sun, 6 Jul 2025 18:52:50 +0200 Subject: [PATCH 13/13] Bouteilles aussi --- app/Http/Controllers/TransactionController.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index de8cb73..b7de874 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -22,7 +22,7 @@ public function handle(Request $request) $mappedItems = []; foreach ($items as [$articleId, $quantity]) { $categoryId = $this->getCategoryFromArticle($articleId); - if ($categoryId == 11) { // Si c'est une bière pression, on remplace par le prix du marché + if ($categoryId == 11 || $categoryId == 10) { // Si c'est une bière pression ou bouteille, on remplace par le prix du marché $firstTransaction = Http::withHeaders([ // Première transaction de l'article (qui aura été mis à 0e) pour les stats 'accept-language' => 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7', ])->post('https://api.nemopay.net/services/POSS3/transaction?system_id='.$systemId.'&app_key='.$appKey.'&sessionid='.$sessionId, [ @@ -66,10 +66,10 @@ private function getCurrentMarketPrice($articleId) { return MarketPrices::where('article_id', $articleId) ->first() - ->price ?? 1.0; + ->price ?? 1.80; } - private function mapPriceToBeerArticle($price) + private function mapPriceToBeerArticle($price) // Calcule l'id de la bière sachant que 60 centimes c'est l'id 23658 et qu'on augmente l'id de 1 par centime { $rounded = number_format($price, 2, '.', ''); $basePrice = 0.60; @@ -89,7 +89,7 @@ private function updateMarket($articleId, $quantity) { $maxPrice = 2.4; $minPrice = 0.6; - $priceStep = 0.07; + $priceStep = 0.07; // Plus ou moins de fluctuation sur le prix du marché $balanceMarket = 1.80; // Le marché se balance autour de cette valeur $article = Articles::where('article_id', $articleId)->first(); @@ -164,6 +164,7 @@ public function getPrices() foreach ($articles as $article) { $articleModel = Articles::where('article_id', $article->article_id)->first(); $article->article_name = $articleModel ? $articleModel->article_name : null; + $article->category_id = $articleModel ? $articleModel->category_id : null; } $response = [