Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
13205c6e80
|
||
|
|
2cd1d53616
|
||
|
|
5b504d10bd
|
||
|
|
5ea8c0df23 | ||
|
|
b6845ee0ea | ||
|
|
3ba3487f81 | ||
|
|
55399ab06a | ||
|
|
f7328d59eb | ||
|
|
b046a85806 | ||
|
|
a3da9af90d | ||
|
|
83832aaaa9 | ||
|
|
4cfab79611 | ||
|
|
00cb5ba966 | ||
|
|
ceba5178f3 | ||
|
|
0fb64ff30c | ||
|
|
e0ca8e51a6 | ||
|
|
cd25915d92 | ||
|
|
bcfa5c06f7 | ||
|
|
3dcd8df4c7 | ||
|
|
46e3f0eef6 | ||
|
|
7e6766c726 | ||
|
|
1088749386 | ||
|
|
52cb893bd0 | ||
|
|
59fea64df5 | ||
|
|
a09639abcf | ||
|
|
2d2d0149d8 | ||
|
|
894a38bb27 | ||
|
|
432d3c0b88 | ||
|
|
1a7916a836 | ||
|
|
030b00059e | ||
|
|
a804f41956 | ||
|
|
25d21f7cf8 | ||
|
|
c3f8c3fff0 | ||
|
|
23d0ea1343 | ||
|
|
1dc2d25a41 | ||
|
|
7c8fdd6ea2 | ||
|
|
5e90de812b | ||
|
|
ee28333da8 | ||
|
|
e89b60c844 | ||
|
|
b9df0a4d4a | ||
|
|
3a7b1b9794 | ||
|
|
5880960c42 | ||
|
|
ef277a3cfd | ||
|
|
e23ca3d1ff | ||
|
|
e84dd993af | ||
|
|
bcdf542606 | ||
|
|
16f8d3bdfc | ||
|
|
45d8aa0035 | ||
|
|
c0af76abd3 | ||
|
|
ff16a57a97 | ||
|
|
cf238d5451 | ||
|
|
50ae1b7f1f | ||
|
|
5975208877 | ||
|
|
f3e8ffc400 | ||
|
|
1b03558f93 | ||
|
|
881a60a388 | ||
|
|
cf3ca26382 | ||
|
|
58e6615876 | ||
|
|
9bfa06b1ce | ||
|
|
2a438dce26 | ||
|
|
f38f08923f | ||
|
|
5f845b2ba7 | ||
|
|
54b4fc7cb1
|
||
|
|
3d23627433 | ||
|
|
4f391e3638 | ||
|
|
c456c9482f | ||
|
|
754a227608 | ||
|
|
83f92e670d | ||
|
|
59df3e2d37 | ||
|
|
39f55a12c0 | ||
|
|
edecef3af8 | ||
|
|
51f745a3dd
|
||
|
|
4b311c74eb
|
||
|
|
0ecdc30bd4
|
||
|
|
25c819c6e4
|
||
|
|
f60b25f29b
|
||
|
|
52887fc2eb
|
||
|
|
c4078d9b82
|
||
|
|
d29e44c61c
|
||
|
|
8f7102b335
|
||
|
|
a34027889f
|
||
|
|
87e0dc2518
|
||
|
|
79be3544cb
|
||
|
|
eafb4a4d1c
|
||
|
|
08c20acdf4
|
||
|
|
ab231ee94f
|
||
|
|
ceb3d735ad
|
||
|
|
5924d08f73
|
||
|
|
3ec6f8971c
|
||
|
|
e68f1357cc
|
||
|
|
cfda949ea6
|
||
|
|
c68cda350e
|
||
|
|
b7daa4610b | ||
|
|
329bbcc1d3 | ||
|
|
b35cac3b2c | ||
|
|
b5c3c552da | ||
|
|
14dd4b3a4e | ||
|
|
0d0f7569a6 | ||
|
|
09c4732e19 | ||
|
|
d4361121d8
|
||
|
|
ef0bdab0d3 | ||
|
|
5295d0620f | ||
|
|
d8cdbbf2ad | ||
|
|
160489b080 | ||
|
|
a0bbd8a69d | ||
|
|
943a5dd898 | ||
|
|
8912029686 | ||
|
|
0bf96fc948 | ||
|
|
79310fa63e | ||
|
|
83adbcf2f1 | ||
|
|
00666a16b2 | ||
|
|
026e7af6a5
|
||
|
|
7cfe9d2746 | ||
|
|
7a6fbb4ba3 | ||
|
|
f3f6c6e564 | ||
|
|
d8e20dfc67 | ||
|
|
381f30a0f5 | ||
|
|
723d5270e4 | ||
|
|
f4aaf9a787 | ||
|
|
30705fd194 | ||
|
|
1939822e9b | ||
|
|
c0b77aebb1 | ||
|
|
ea15f5e28a | ||
|
|
44c0ac599e | ||
|
|
28466ecbbd | ||
|
|
e47a453588 | ||
|
|
0320230279 | ||
|
|
8505d207e6 | ||
|
|
c34d74db46 | ||
|
|
18cef8da4b | ||
|
|
b82b4d5b59 | ||
|
|
29fc78c9f5 | ||
|
|
d903f96d57 | ||
|
|
e54ec58f0c | ||
|
|
7d1b769bdb | ||
|
|
d0f9a03b93 | ||
|
|
10508cc0b1
|
||
|
|
c78c5d2784
|
||
|
|
e833e403f7 | ||
|
|
5c1fc11975 | ||
|
|
b59640c858 | ||
|
|
289e8df604 | ||
|
|
e8ccea4e42 | ||
|
|
8e179200a5 | ||
|
|
c03d49e7e4 | ||
|
|
2734e25cce | ||
|
|
a566387998 | ||
|
|
2b161e2099
|
||
|
|
d1dd2c00f6 | ||
|
|
8a6e0d0255 | ||
|
|
b485e19c9d | ||
|
|
94fdbd568c | ||
|
|
ae6a0b4706 | ||
|
|
5270e3c093 | ||
|
|
5473a4cc69 | ||
|
|
cf8f93d984 | ||
|
|
7686aff546 | ||
|
|
a0ad35bb08 | ||
|
|
af41990807 | ||
|
|
20f53197ec | ||
|
|
7c6d8ed446 | ||
|
|
1d5100fbdf | ||
|
|
511e645d7e | ||
|
|
daa72ce4c6 | ||
|
|
117f007b83 | ||
|
|
69705ce61c
|
||
|
|
8472263f91 | ||
|
|
d7a6862dcb | ||
|
|
a335e713ee | ||
|
|
1e5792567f | ||
|
|
2f47f69adb | ||
|
|
dc16a150e1 | ||
|
|
4ac9487605 | ||
|
|
98a2ca4611 | ||
|
|
3f1c69c15b | ||
|
|
7d91521d64
|
||
|
|
eb056ee995
|
||
|
|
d2e7a50ce5
|
||
|
|
dedcc73567
|
||
|
|
f1da93791d |
@@ -1,138 +1,109 @@
|
|||||||
# ---> Node
|
# -------------------------
|
||||||
# Logs
|
# Project-level .gitignore
|
||||||
logs
|
# Root: DADProject
|
||||||
*.log
|
# -------------------------
|
||||||
npm-debug.log*
|
|
||||||
yarn-debug.log*
|
|
||||||
yarn-error.log*
|
|
||||||
lerna-debug.log*
|
|
||||||
.pnpm-debug.log*
|
|
||||||
|
|
||||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
# macOS
|
||||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
.DS_Store
|
||||||
|
|
||||||
# Runtime data
|
# parcel / next / nuxt / vite / etc caches
|
||||||
pids
|
|
||||||
*.pid
|
|
||||||
*.seed
|
|
||||||
*.pid.lock
|
|
||||||
|
|
||||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
|
||||||
lib-cov
|
|
||||||
|
|
||||||
# Coverage directory used by tools like istanbul
|
|
||||||
coverage
|
|
||||||
*.lcov
|
|
||||||
|
|
||||||
# nyc test coverage
|
|
||||||
.nyc_output
|
|
||||||
|
|
||||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
|
||||||
.grunt
|
|
||||||
|
|
||||||
# Bower dependency directory (https://bower.io/)
|
|
||||||
bower_components
|
|
||||||
|
|
||||||
# node-waf configuration
|
|
||||||
.lock-wscript
|
|
||||||
|
|
||||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
|
||||||
build/Release
|
|
||||||
|
|
||||||
# Dependency directories
|
|
||||||
node_modules/
|
|
||||||
jspm_packages/
|
|
||||||
|
|
||||||
# Snowpack dependency directory (https://snowpack.dev/)
|
|
||||||
web_modules/
|
|
||||||
|
|
||||||
# TypeScript cache
|
|
||||||
*.tsbuildinfo
|
|
||||||
|
|
||||||
# Optional npm cache directory
|
|
||||||
.npm
|
|
||||||
|
|
||||||
# Optional eslint cache
|
|
||||||
.eslintcache
|
|
||||||
|
|
||||||
# Optional stylelint cache
|
|
||||||
.stylelintcache
|
|
||||||
|
|
||||||
# Microbundle cache
|
|
||||||
.rpt2_cache/
|
|
||||||
.rts2_cache_cjs/
|
|
||||||
.rts2_cache_es/
|
|
||||||
.rts2_cache_umd/
|
|
||||||
|
|
||||||
# Optional REPL history
|
|
||||||
.node_repl_history
|
|
||||||
|
|
||||||
# Output of 'npm pack'
|
|
||||||
*.tgz
|
|
||||||
|
|
||||||
# Yarn Integrity file
|
|
||||||
.yarn-integrity
|
|
||||||
|
|
||||||
# dotenv environment variable files
|
|
||||||
.env
|
|
||||||
.env.development.local
|
|
||||||
.env.test.local
|
|
||||||
.env.production.local
|
|
||||||
.env.local
|
|
||||||
|
|
||||||
# parcel-bundler cache (https://parceljs.org/)
|
|
||||||
.cache
|
.cache
|
||||||
.parcel-cache
|
.parcel-cache
|
||||||
|
|
||||||
# Next.js build output
|
|
||||||
.next
|
.next
|
||||||
out
|
|
||||||
|
|
||||||
# Nuxt.js build / generate output
|
|
||||||
.nuxt
|
.nuxt
|
||||||
dist
|
dist
|
||||||
|
|
||||||
# Gatsby files
|
|
||||||
.cache/
|
|
||||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
|
||||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
|
||||||
# public
|
|
||||||
|
|
||||||
# vuepress build output
|
|
||||||
.vuepress/dist
|
.vuepress/dist
|
||||||
|
|
||||||
# vuepress v2.x temp and cache directory
|
|
||||||
.temp
|
.temp
|
||||||
.cache
|
|
||||||
|
|
||||||
# vitepress build output
|
|
||||||
**/.vitepress/dist
|
**/.vitepress/dist
|
||||||
|
|
||||||
# vitepress cache directory
|
|
||||||
**/.vitepress/cache
|
**/.vitepress/cache
|
||||||
|
|
||||||
# Docusaurus cache and generated files
|
|
||||||
.docusaurus
|
.docusaurus
|
||||||
|
|
||||||
# Serverless directories
|
|
||||||
.serverless/
|
.serverless/
|
||||||
|
|
||||||
# FuseBox cache
|
|
||||||
.fusebox/
|
.fusebox/
|
||||||
|
|
||||||
# DynamoDB Local files
|
|
||||||
.dynamodb/
|
.dynamodb/
|
||||||
|
|
||||||
# TernJS port file
|
|
||||||
.tern-port
|
.tern-port
|
||||||
|
|
||||||
# Stores VSCode versions used for testing VSCode extensions
|
|
||||||
.vscode-test
|
.vscode-test
|
||||||
|
|
||||||
# yarn v2
|
|
||||||
.yarn/cache
|
.yarn/cache
|
||||||
.yarn/unplugged
|
.yarn/unplugged
|
||||||
.yarn/build-state.yml
|
.yarn/build-state.yml
|
||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Frontend: Vue (in frontend/)
|
||||||
|
# -------------------------
|
||||||
|
# Node deps and build output for the Vue app
|
||||||
|
/frontend/node_modules/
|
||||||
|
/frontend/dist/
|
||||||
|
/frontend/build/
|
||||||
|
/frontend/.cache/
|
||||||
|
/frontend/.vite/
|
||||||
|
/frontend/.serverless/
|
||||||
|
/frontend/.vscode/
|
||||||
|
|
||||||
|
# Vue / Vite / Vue CLI env files (local)
|
||||||
|
frontend/.env
|
||||||
|
frontend/.env.local
|
||||||
|
frontend/.env.*.local
|
||||||
|
frontend/.env.production
|
||||||
|
websockets/.env.production
|
||||||
|
# Misc frontend files that should be ignored
|
||||||
|
/frontend/public/hot
|
||||||
|
/frontend/public/storage
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Backend: PHP / Laravel (in api/)
|
||||||
|
# -------------------------
|
||||||
|
/api/vendor/
|
||||||
|
/api/node_modules/
|
||||||
|
/api/vscode/
|
||||||
|
/api/.env
|
||||||
|
/api/.env.*.local
|
||||||
|
|
||||||
|
# Laravel storage and compiled files
|
||||||
|
/api/storage/
|
||||||
|
/api/storage/*.key
|
||||||
|
/api/public/storage
|
||||||
|
/api/bootstrap/cache/*.php
|
||||||
|
|
||||||
|
# Homestead / vagrant
|
||||||
|
/api/.vagrant
|
||||||
|
/api/Homestead.yaml
|
||||||
|
/api/Homestead.json
|
||||||
|
|
||||||
|
# composer
|
||||||
|
/api/.composer/
|
||||||
|
/api/.composer-cache
|
||||||
|
|
||||||
|
# Laravel specific tooling
|
||||||
|
/api/.phpunit.result.cache
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Websockets: NodeJs (in websockets)
|
||||||
|
# -------------------------
|
||||||
|
/websockets/node_modules/
|
||||||
|
/websockets/.env
|
||||||
|
# -------------------------
|
||||||
|
# Other useful ignores
|
||||||
|
# -------------------------
|
||||||
|
# Generated environment files used by editors/OS
|
||||||
|
Thumbs.db
|
||||||
|
ehthumbs.db
|
||||||
|
Desktop.ini
|
||||||
|
|
||||||
|
# Logs and runtime files
|
||||||
|
*.log
|
||||||
|
*.tmp
|
||||||
|
*.temp
|
||||||
|
|
||||||
|
# Certificate/key files
|
||||||
|
*.pem
|
||||||
|
*.key
|
||||||
|
|
||||||
|
# If you have any local secrets or files not to track, add them here:
|
||||||
|
# /path/to/some/file
|
||||||
|
|
||||||
|
/frontend/package-lock.json
|
||||||
|
/websockets/package-lock.json
|
||||||
|
/api/package-lock.json
|
||||||
|
|
||||||
|
.vscode
|
||||||
@@ -1,2 +1,254 @@
|
|||||||
# DADProject
|
# DAD Project - Bisca Game Platform
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The DAD project aims to develop and deploy the **Bisca Game Platform** as a web application. The platform is a distributed system that provides users with the ability to play the traditional Portuguese card game Bisca in single-player and multiplayer modes, manage their coins, and track their gaming statistics.
|
||||||
|
|
||||||
|
The web client is implemented as a **Single-Page Application (SPA)** built with **Vue.js** and hosted on the designated internal school server. The backend system includes a database, a RESTful API server, and a WebSocket server for real-time multiplayer gameplay.
|
||||||
|
|
||||||
|
## Project Architecture
|
||||||
|
|
||||||
|
### Frontend
|
||||||
|
- **Technology**: Vue.js SPA
|
||||||
|
- **Purpose**: User interface for game play, user management, coin transactions, and statistics visualization
|
||||||
|
- **Features**: Real-time game updates via WebSockets, responsive design, player profiles, leaderboards
|
||||||
|
|
||||||
|
### Backend
|
||||||
|
- **RESTful API Server**: Handles user authentication, coin transactions, game history, leaderboards, and administrative operations
|
||||||
|
- **WebSocket Server**: Manages real-time multiplayer game communication, ensuring the server acts as the authoritative source for game state
|
||||||
|
- **Database**: Persists user data, game results, transactions, and platform statistics
|
||||||
|
|
||||||
|
## Constraints
|
||||||
|
|
||||||
|
The project must adhere to the following mandatory constraints:
|
||||||
|
|
||||||
|
1. **Single-Page Application (SPA)** - The application must be implemented exclusively as an SPA
|
||||||
|
2. **Vue.js Requirement** - The client application must be built with Vue.js
|
||||||
|
3. **Payment Gateway Integration** - All external coin purchases must be processed via the provided RESTful API (Payment Gateway Service)
|
||||||
|
4. **Deployment** - The application must be deployed on the designated internal school server and be accessible via a desktop web browser
|
||||||
|
5. **Backend Technologies** - Must be open-source or available as free external services
|
||||||
|
6. **Server Compatibility** - Backend technologies must be installable on or accessible from the designated server; if not supported, compliant alternatives must be selected
|
||||||
|
7. **Database Seeding** - The deployed application's database must be seeded with realistic data in both scale and content to simulate typical usage patterns
|
||||||
|
|
||||||
|
## Core Features
|
||||||
|
|
||||||
|
### 1. User Management
|
||||||
|
- **Registration**: Users register with email, nickname, name, password (minimum 3 characters), and optional photo/avatar
|
||||||
|
- **Authentication**: Email and password-based login
|
||||||
|
- **Profile Management**: View and update profile information, change password
|
||||||
|
- **Sign Out**: Users can end their session
|
||||||
|
- **Account Deletion**: Requires explicit confirmation; permanently forfeits all coins
|
||||||
|
- **Welcome Bonus**: New users receive 10 coins upon registration
|
||||||
|
- **Admin Accounts**: Created only by existing administrators; same profile capabilities as users but cannot delete their own accounts
|
||||||
|
|
||||||
|
### 2. Coins and Transactions
|
||||||
|
- **Coin Balance**: Users can view their current coin balance
|
||||||
|
- **Coin Purchases**: External purchases via Payment Gateway Service
|
||||||
|
- **Conversion Rate**: €1 = 10 coins (purchases in €1 increments)
|
||||||
|
- **Coin Earning**: Players earn coins through gameplay and bonuses
|
||||||
|
- **Transaction History**: All coin-related transactions (bonuses, purchases, spending, earnings) are logged
|
||||||
|
- **History Access**:
|
||||||
|
- Players view only their own transaction history
|
||||||
|
- Administrators have read-only access to all players' histories
|
||||||
|
- Administrators cannot create, modify, or delete transactions
|
||||||
|
|
||||||
|
### 3. Games and Matches
|
||||||
|
|
||||||
|
#### Game Variants
|
||||||
|
- **Bisca de 3**: Three-card hands
|
||||||
|
- **Bisca de 9**: Nine-card hands
|
||||||
|
|
||||||
|
#### Game Modes
|
||||||
|
- **Single-Player**: Against a bot (available to anonymous and registered users)
|
||||||
|
- **Multiplayer**: Against human opponents (available only to registered users)
|
||||||
|
- **Match**: Best-of series (up to 4 marks/wins) with two players
|
||||||
|
|
||||||
|
#### Game Rules
|
||||||
|
- Games and matches are limited to two players
|
||||||
|
- The bot follows game rules and attempts to win when playing second; otherwise plays its lowest-value card
|
||||||
|
- Official Bisca rules apply (as defined in the "Bisca Game" documentation)
|
||||||
|
|
||||||
|
#### Multiplayer Gameplay
|
||||||
|
- **Real-Time Communication**: WebSockets for live game updates
|
||||||
|
- **Server Authority**: The server is the source of truth for:
|
||||||
|
- Card dealing
|
||||||
|
- Turn order
|
||||||
|
- Valid moves
|
||||||
|
- Trick resolution
|
||||||
|
- Drawing from stock
|
||||||
|
- Scoring
|
||||||
|
- End-of-game/match conditions
|
||||||
|
- **Move Timer**: 20-second limit per move
|
||||||
|
- **Resignation**:
|
||||||
|
- Opponent awarded all remaining cards
|
||||||
|
- Game ends immediately
|
||||||
|
- If in a match, player forfeits all remaining games
|
||||||
|
- Scores computed accordingly
|
||||||
|
- **Persistent Results**: Game and match results with metadata are persisted for user history and administrative access
|
||||||
|
|
||||||
|
### 4. Game History and Leaderboards
|
||||||
|
|
||||||
|
#### Tracking
|
||||||
|
- **Scope**: Only multiplayer games and matches are tracked
|
||||||
|
- **Metadata**: Start/end timestamps, variant, outcome (win/loss/draw/forfeit), final points, marks (capote, bandeira), duration
|
||||||
|
|
||||||
|
#### History Access
|
||||||
|
- **Players**: View only their own multiplayer history
|
||||||
|
- **Administrators**: Read-only access to all players' histories and detailed records
|
||||||
|
- **Detailed Records**: Visible only to administrators and participating players
|
||||||
|
|
||||||
|
#### Leaderboards
|
||||||
|
- **Personal Leaderboards**: Visible only to the owner; summarize totals for game wins, match wins, capotes, and bandeiras (optionally segmented by variant)
|
||||||
|
- **Global Leaderboards**: Visible to all users including anonymous visitors; highlight top players by game and match wins, may include capotes and bandeiras
|
||||||
|
- **Tie-Breaking**: Earlier achievers rank higher in case of ties
|
||||||
|
|
||||||
|
### 5. Administration
|
||||||
|
|
||||||
|
#### Administrator Capabilities
|
||||||
|
- **Profile Management**: Same as regular users, except cannot delete their own accounts
|
||||||
|
- **Account Management**: Create new administrator accounts, remove any account except their own
|
||||||
|
- **Soft Deletion**: Player accounts with prior activity must be soft-deleted
|
||||||
|
- **User Monitoring**: View all users (players and administrators), block/unblock players
|
||||||
|
- **Read-Only Access**:
|
||||||
|
- All transactions
|
||||||
|
- All multiplayer games and matches
|
||||||
|
- Platform usage summaries and statistics
|
||||||
|
|
||||||
|
#### Administrator Access Control
|
||||||
|
- Cannot play games or hold coins
|
||||||
|
- New administrator accounts can only be created by existing administrators
|
||||||
|
|
||||||
|
### 6. Statistics and Reporting
|
||||||
|
|
||||||
|
#### Anonymous Users and Players
|
||||||
|
- Access only generic, anonymized aggregates:
|
||||||
|
- Total registered players
|
||||||
|
- Games and matches played
|
||||||
|
- Recent activity summaries
|
||||||
|
|
||||||
|
#### Administrators
|
||||||
|
- Full, non-anonymized access including:
|
||||||
|
- Purchases by period and player
|
||||||
|
- Game/match volumes
|
||||||
|
- User activity breakdowns
|
||||||
|
- Time-series data
|
||||||
|
|
||||||
|
#### Visualization
|
||||||
|
- Statistics available in both visual (charts) and textual/tabular forms
|
||||||
|
- Privacy for non-admin viewers is preserved
|
||||||
|
- Exact metrics and visualizations are team-defined
|
||||||
|
|
||||||
|
## Enhancement and Innovation Opportunities
|
||||||
|
|
||||||
|
To distinguish this project, consider implementing one or more of the following:
|
||||||
|
|
||||||
|
### Custom Features (Examples)
|
||||||
|
- Support for three or more players per game (optional extension while preserving two-player mode)
|
||||||
|
- Spectator mode for multiplayer games and matches
|
||||||
|
- Full playback of past games
|
||||||
|
- Custom card sets
|
||||||
|
- Extended bot capabilities (e.g., improved AI strategies)
|
||||||
|
- Integration of bots into multiplayer games
|
||||||
|
|
||||||
|
### Architecture and Performance Enhancements (Examples)
|
||||||
|
- Queue server or worker for asynchronous, long-running operations
|
||||||
|
- Horizontal scaling of the WebSocket server
|
||||||
|
- Session recovery after WebSocket or node failures
|
||||||
|
- Cache server implementation
|
||||||
|
- NoSQL database for specific use cases
|
||||||
|
- Search engine for game history or statistics
|
||||||
|
- Asynchronous messaging broker (e.g., Kafka, RabbitMQ)
|
||||||
|
- Distributed components for improved scalability and resilience
|
||||||
|
|
||||||
|
### Implementation Quality
|
||||||
|
- Use of technologies or techniques not covered in standard coursework
|
||||||
|
- Performance optimization with empirical benchmarks
|
||||||
|
- Improved architecture and code structure
|
||||||
|
- Enhanced implementation quality and documentation
|
||||||
|
|
||||||
|
**Note**: Credit is proportional to demonstrated impact with preference given to distributed capabilities that improve architecture, scalability, or resilience.
|
||||||
|
|
||||||
|
## Technology Stack
|
||||||
|
|
||||||
|
### Frontend
|
||||||
|
- Vue.js (mandatory)
|
||||||
|
- Vite (build tool)
|
||||||
|
- ESLint (code quality)
|
||||||
|
|
||||||
|
### Backend
|
||||||
|
- **API Server**: Open-source, student-selected technology
|
||||||
|
- **WebSocket Server**: Open-source, student-selected technology
|
||||||
|
- **Database**: Open-source, student-selected technology
|
||||||
|
- All must be compatible with the designated school server
|
||||||
|
|
||||||
|
### Supported Variants
|
||||||
|
- Laravel (PHP) for REST API
|
||||||
|
- Node.js for WebSocket server
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
DADProject/
|
||||||
|
├── api/ # Laravel REST API backend
|
||||||
|
├── frontend/ # Vue.js SPA
|
||||||
|
├── websockets/ # WebSocket server
|
||||||
|
└── README.md # This file
|
||||||
|
```
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
Refer to the individual `README.md` files in each directory for setup and deployment instructions:
|
||||||
|
|
||||||
|
- [API Documentation](./api/README.md)
|
||||||
|
- [Frontend Documentation](./frontend/README.md)
|
||||||
|
- [WebSocket Server Documentation](./websockets/README.md)
|
||||||
|
|
||||||
|
## Database Schema
|
||||||
|
|
||||||
|
The application's database includes the following primary entities:
|
||||||
|
|
||||||
|
- **Users**: Player and administrator accounts
|
||||||
|
- **Games**: Individual game records (both single-player and multiplayer)
|
||||||
|
- **Matches**: Match records (series of games)
|
||||||
|
- **Coin Transactions**: Purchase and spending history
|
||||||
|
- **Coin Transaction Types**: Transaction categorization
|
||||||
|
- **Coin Purchases**: External purchase records
|
||||||
|
- **Leaderboard Data**: Aggregated player statistics
|
||||||
|
|
||||||
|
For detailed schema information, refer to the database migrations in the `api/database/migrations/` directory.
|
||||||
|
|
||||||
|
## Key Business Rules
|
||||||
|
|
||||||
|
1. **Coin System**:
|
||||||
|
- New users receive 10 welcome coins
|
||||||
|
- Conversion rate: €1 = 10 coins
|
||||||
|
- Coin purchases are made in €1 increments
|
||||||
|
- All transactions are logged and immutable (except soft-deleted accounts)
|
||||||
|
|
||||||
|
2. **Game Rules**:
|
||||||
|
- Two-player games only (in standard mode)
|
||||||
|
- Bot cannot be played in multiplayer
|
||||||
|
- 20-second move timer in multiplayer
|
||||||
|
- Resignation forfeits remaining games in a match
|
||||||
|
|
||||||
|
3. **Account Management**:
|
||||||
|
- Administrators cannot be created via public registration
|
||||||
|
- Administrators cannot delete their own accounts
|
||||||
|
- Account deletion requires explicit confirmation
|
||||||
|
- Soft deletion preserves historical data for deleted player accounts
|
||||||
|
|
||||||
|
4. **Leaderboard Ranking**:
|
||||||
|
- Multiplayer games and matches only
|
||||||
|
- Ties resolved by earlier achievement date
|
||||||
|
|
||||||
|
## Notes for Developers
|
||||||
|
|
||||||
|
- The WebSocket server acts as the authoritative source for all game state in multiplayer games
|
||||||
|
- Ensure proper error handling and recovery for WebSocket disconnections
|
||||||
|
- All coin transactions must be atomic and immutable
|
||||||
|
- Database must be seeded with realistic, varied data
|
||||||
|
- Privacy and data protection must be maintained throughout
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is part of the EI – DAD 2025/26 course.
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
indent_size = 4
|
||||||
|
indent_style = space
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[*.md]
|
||||||
|
trim_trailing_whitespace = false
|
||||||
|
|
||||||
|
[*.{yml,yaml}]
|
||||||
|
indent_size = 2
|
||||||
|
|
||||||
|
[compose.yaml]
|
||||||
|
indent_size = 4
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
APP_NAME=Laravel
|
||||||
|
APP_ENV=local
|
||||||
|
APP_KEY=
|
||||||
|
APP_DEBUG=true
|
||||||
|
APP_URL=http://localhost
|
||||||
|
|
||||||
|
APP_LOCALE=en
|
||||||
|
APP_FALLBACK_LOCALE=en
|
||||||
|
APP_FAKER_LOCALE=en_US
|
||||||
|
|
||||||
|
APP_MAINTENANCE_DRIVER=file
|
||||||
|
# APP_MAINTENANCE_STORE=database
|
||||||
|
|
||||||
|
# PHP_CLI_SERVER_WORKERS=4
|
||||||
|
|
||||||
|
BCRYPT_ROUNDS=12
|
||||||
|
|
||||||
|
LOG_CHANNEL=stack
|
||||||
|
LOG_STACK=single
|
||||||
|
LOG_DEPRECATIONS_CHANNEL=null
|
||||||
|
LOG_LEVEL=debug
|
||||||
|
|
||||||
|
DB_CONNECTION=sqlite
|
||||||
|
# DB_HOST=127.0.0.1
|
||||||
|
# DB_PORT=3306
|
||||||
|
# DB_DATABASE=laravel
|
||||||
|
# DB_USERNAME=root
|
||||||
|
# DB_PASSWORD=
|
||||||
|
|
||||||
|
SESSION_DRIVER=database
|
||||||
|
SESSION_LIFETIME=120
|
||||||
|
SESSION_ENCRYPT=false
|
||||||
|
SESSION_PATH=/
|
||||||
|
SESSION_DOMAIN=null
|
||||||
|
|
||||||
|
BROADCAST_CONNECTION=log
|
||||||
|
FILESYSTEM_DISK=local
|
||||||
|
QUEUE_CONNECTION=database
|
||||||
|
|
||||||
|
CACHE_STORE=database
|
||||||
|
# CACHE_PREFIX=
|
||||||
|
|
||||||
|
MEMCACHED_HOST=127.0.0.1
|
||||||
|
|
||||||
|
REDIS_CLIENT=phpredis
|
||||||
|
REDIS_HOST=127.0.0.1
|
||||||
|
REDIS_PASSWORD=null
|
||||||
|
REDIS_PORT=6379
|
||||||
|
|
||||||
|
MAIL_MAILER=log
|
||||||
|
MAIL_SCHEME=null
|
||||||
|
MAIL_HOST=127.0.0.1
|
||||||
|
MAIL_PORT=2525
|
||||||
|
MAIL_USERNAME=null
|
||||||
|
MAIL_PASSWORD=null
|
||||||
|
MAIL_FROM_ADDRESS="[email protected]"
|
||||||
|
MAIL_FROM_NAME="${APP_NAME}"
|
||||||
|
|
||||||
|
AWS_ACCESS_KEY_ID=
|
||||||
|
AWS_SECRET_ACCESS_KEY=
|
||||||
|
AWS_DEFAULT_REGION=us-east-1
|
||||||
|
AWS_BUCKET=
|
||||||
|
AWS_USE_PATH_STYLE_ENDPOINT=false
|
||||||
|
|
||||||
|
VITE_APP_NAME="${APP_NAME}"
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
* text=auto eol=lf
|
||||||
|
|
||||||
|
*.blade.php diff=html
|
||||||
|
*.css diff=css
|
||||||
|
*.html diff=html
|
||||||
|
*.md diff=markdown
|
||||||
|
*.php diff=php
|
||||||
|
|
||||||
|
/.github export-ignore
|
||||||
|
CHANGELOG.md export-ignore
|
||||||
|
.styleci.yml export-ignore
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
*.log
|
||||||
|
.DS_Store
|
||||||
|
.env
|
||||||
|
.env.backup
|
||||||
|
.env.production
|
||||||
|
.phpactor.json
|
||||||
|
.phpunit.result.cache
|
||||||
|
/.fleet
|
||||||
|
/.idea
|
||||||
|
/.nova
|
||||||
|
/.phpunit.cache
|
||||||
|
/.vscode
|
||||||
|
/.zed
|
||||||
|
/auth.json
|
||||||
|
/node_modules
|
||||||
|
/public/build
|
||||||
|
/public/hot
|
||||||
|
/public/storage
|
||||||
|
/storage/*.key
|
||||||
|
/storage/pail
|
||||||
|
/vendor
|
||||||
|
Homestead.json
|
||||||
|
Homestead.yaml
|
||||||
|
Thumbs.db
|
||||||
|
|
||||||
|
composer.lock
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
<p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400" alt="Laravel Logo"></a></p>
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://github.com/laravel/framework/actions"><img src="https://github.com/laravel/framework/workflows/tests/badge.svg" alt="Build Status"></a>
|
||||||
|
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/dt/laravel/framework" alt="Total Downloads"></a>
|
||||||
|
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/v/laravel/framework" alt="Latest Stable Version"></a>
|
||||||
|
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/l/laravel/framework" alt="License"></a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
## About Laravel
|
||||||
|
|
||||||
|
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
|
||||||
|
|
||||||
|
- [Simple, fast routing engine](https://laravel.com/docs/routing).
|
||||||
|
- [Powerful dependency injection container](https://laravel.com/docs/container).
|
||||||
|
- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.
|
||||||
|
- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).
|
||||||
|
- Database agnostic [schema migrations](https://laravel.com/docs/migrations).
|
||||||
|
- [Robust background job processing](https://laravel.com/docs/queues).
|
||||||
|
- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).
|
||||||
|
|
||||||
|
Laravel is accessible, powerful, and provides tools required for large, robust applications.
|
||||||
|
|
||||||
|
## Learning Laravel
|
||||||
|
|
||||||
|
Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework. You can also check out [Laravel Learn](https://laravel.com/learn), where you will be guided through building a modern Laravel application.
|
||||||
|
|
||||||
|
If you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains thousands of video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.
|
||||||
|
|
||||||
|
## Laravel Sponsors
|
||||||
|
|
||||||
|
We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the [Laravel Partners program](https://partners.laravel.com).
|
||||||
|
|
||||||
|
### Premium Partners
|
||||||
|
|
||||||
|
- **[Vehikl](https://vehikl.com)**
|
||||||
|
- **[Tighten Co.](https://tighten.co)**
|
||||||
|
- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**
|
||||||
|
- **[64 Robots](https://64robots.com)**
|
||||||
|
- **[Curotec](https://www.curotec.com/services/technologies/laravel)**
|
||||||
|
- **[DevSquad](https://devsquad.com/hire-laravel-developers)**
|
||||||
|
- **[Redberry](https://redberry.international/laravel-development)**
|
||||||
|
- **[Active Logic](https://activelogic.com)**
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
|
||||||
|
|
||||||
|
## Code of Conduct
|
||||||
|
|
||||||
|
In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).
|
||||||
|
|
||||||
|
## Security Vulnerabilities
|
||||||
|
|
||||||
|
If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [[email protected]](mailto:[email protected]). All security vulnerabilities will be promptly addressed.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Requests\RegisterRequest;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
|
class AuthController extends Controller
|
||||||
|
{
|
||||||
|
public function login(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'email' => 'required|email',
|
||||||
|
'password' => 'required',
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (! Auth::attempt($request->only('email', 'password'))) {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'email' => ['The provided credentials are incorrect.'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = Auth::user();
|
||||||
|
|
||||||
|
if ($user->blocked) {
|
||||||
|
Auth::logout();
|
||||||
|
|
||||||
|
return response()->json(['message' => 'Your account is blocked. Please contact support.'], 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
$token = $user->createToken('auth-token')->plainTextToken;
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'user' => $user,
|
||||||
|
'token' => $token,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function logout(Request $request)
|
||||||
|
{
|
||||||
|
$request->user()->currentAccessToken()->delete();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Logged out successfully',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function register(RegisterRequest $request)
|
||||||
|
{
|
||||||
|
if (User::where('email', $request->email)->exists()) {
|
||||||
|
return response()->json(['message' => 'Email already registered'], 409);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (User::where('nickname', $request->nickname)->exists()) {
|
||||||
|
return response()->json(['message' => 'Nickname already registered'], 409);
|
||||||
|
}
|
||||||
|
|
||||||
|
$avatarFilename = null;
|
||||||
|
|
||||||
|
if ($request->hasFile('photo_avatar_filename')) {
|
||||||
|
$file = $request->file('photo_avatar_filename');
|
||||||
|
$path = $file->store('photos_avatars', 'public');
|
||||||
|
$avatarFilename = basename($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = User::create([
|
||||||
|
'email' => $request->email,
|
||||||
|
'nickname' => $request->nickname,
|
||||||
|
'name' => $request->name,
|
||||||
|
'password' => bcrypt($request->password),
|
||||||
|
'photo_avatar_filename' => $avatarFilename,
|
||||||
|
'type' => 'P',
|
||||||
|
'blocked' => false,
|
||||||
|
'coins_balance' => 10,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$token = $user->createToken('auth-token')->plainTextToken;
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'User registered successfully',
|
||||||
|
'user' => $user,
|
||||||
|
'token' => $token,
|
||||||
|
], 201);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\CoinPurchase;
|
||||||
|
use App\Models\CoinTransaction;
|
||||||
|
use App\Models\CoinTransactionType;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class CoinPurchaseController extends Controller
|
||||||
|
{
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'euros' => 'required|numeric|min:1|max:100',
|
||||||
|
'payment_type' => 'required|in:MBWAY,MB,VISA,PAYPAL',
|
||||||
|
'payment_reference' => [
|
||||||
|
'required',
|
||||||
|
function ($attribute, $value, $fail) use ($request) {
|
||||||
|
$type = $request->payment_type;
|
||||||
|
$regex = null;
|
||||||
|
|
||||||
|
switch ($type) {
|
||||||
|
case 'MBWAY': $regex = '/^9[0-9]{8}$/'; break;
|
||||||
|
case 'MB': $regex = '/^[0-9]{5}-[0-9]{9}$/'; break;
|
||||||
|
case 'VISA': $regex = '/^4[0-9]{15}$/'; break;
|
||||||
|
case 'PAYPAL':
|
||||||
|
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||||||
|
$fail("Invalid PayPal email.");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($regex && !preg_match($regex, $value)) {
|
||||||
|
$fail("Invalid format for $type.");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = $request->user();
|
||||||
|
$coinsAmount = $validated['euros'] * 10;
|
||||||
|
|
||||||
|
return DB::transaction(function () use ($user, $validated, $coinsAmount) {
|
||||||
|
|
||||||
|
$type = CoinTransactionType::firstOrCreate(['name' => 'Coin purchase'], ['type' => 'C']);
|
||||||
|
|
||||||
|
$transaction = CoinTransaction::create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'coin_transaction_type_id' => $type->id,
|
||||||
|
'transaction_datetime' => now(),
|
||||||
|
'coins' => $coinsAmount,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$purchase = CoinPurchase::create([
|
||||||
|
'purchase_datetime' => now(),
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'coin_transaction_id' => $transaction->id,
|
||||||
|
'euros' => $validated['euros'],
|
||||||
|
'payment_type' => $validated['payment_type'],
|
||||||
|
'payment_reference' => $validated['payment_reference'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user->increment('coins_balance', $coinsAmount);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Purchase successful',
|
||||||
|
'balance' => $user->coins_balance,
|
||||||
|
'transaction_id' => $transaction->id,
|
||||||
|
'purchase' => $purchase,
|
||||||
|
], 201);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||||
|
use Illuminate\Routing\Controller as BaseController;
|
||||||
|
|
||||||
|
abstract class Controller extends BaseController
|
||||||
|
{
|
||||||
|
use AuthorizesRequests;
|
||||||
|
}
|
||||||
@@ -0,0 +1,250 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Game;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
|
class GameController extends Controller
|
||||||
|
{
|
||||||
|
const GHOST_ID = 1;
|
||||||
|
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$this->authorize('viewAny', Game::class);
|
||||||
|
|
||||||
|
$user = Auth::user();
|
||||||
|
$query = Game::query()->with(["winner", "player1", "player2"]);
|
||||||
|
|
||||||
|
if ($user->type !== 'A') {
|
||||||
|
$query->where(function ($q) use ($user) {
|
||||||
|
$q->where('player1_user_id', $user->id)
|
||||||
|
->orWhere('player2_user_id', $user->id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("type") && in_array($request->type, ["3", "9"])) {
|
||||||
|
$query->where("type", $request->type);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
$request->has("status") &&
|
||||||
|
in_array($request->status, [
|
||||||
|
"Pending",
|
||||||
|
"Playing",
|
||||||
|
"Ended",
|
||||||
|
"Interrupted",
|
||||||
|
])
|
||||||
|
) {
|
||||||
|
$query->where("status", $request->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("sort_direction") && in_array($request->sort_direction, ["asc", "desc"])) {
|
||||||
|
$query->orderBy("began_at", $request->sort_direction);
|
||||||
|
} else {
|
||||||
|
$query->orderBy("began_at", "desc");
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($query->paginate(15));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function host(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'type' => 'required|in:3,9'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
$activeGame = Game::where(function ($q) use ($user) {
|
||||||
|
$q->where("player1_user_id", $user->id)
|
||||||
|
->orWhere("player2_user_id", $user->id);
|
||||||
|
})
|
||||||
|
->whereIn("status", ["Pending", "Playing"])
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if ($activeGame) {
|
||||||
|
return response()->json(["message" => "You already have an active game."], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$game = Game::create([
|
||||||
|
'type' => $request->type,
|
||||||
|
'status' => 'Pending',
|
||||||
|
'player1_user_id' => $user->id,
|
||||||
|
'player2_user_id' => self::GHOST_ID,
|
||||||
|
'began_at' => now(),
|
||||||
|
'player1_points' => 0,
|
||||||
|
'player2_points' => 0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json($game, 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function open(Request $request)
|
||||||
|
{
|
||||||
|
$type = $request->query('type', '9');
|
||||||
|
$GHOST_ID = 1;
|
||||||
|
|
||||||
|
$games = Game::where('status', 'Pending')
|
||||||
|
->where('player2_user_id', $GHOST_ID)
|
||||||
|
->where('type', $type)
|
||||||
|
->with('player1')
|
||||||
|
->orderBy('began_at', 'asc')
|
||||||
|
->paginate(10);
|
||||||
|
|
||||||
|
return response()->json($games);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$this->authorize('create', Game::class);
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
"type" => "required|in:3,9",
|
||||||
|
"player2_user_id" => "nullable|integer|exists:users,id",
|
||||||
|
"match_id" => "nullable|integer|exists:matches,id",
|
||||||
|
"status" => "nullable|in:Pending,Playing",
|
||||||
|
"began_at" => "nullable|date"
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = Auth::user();
|
||||||
|
|
||||||
|
$activeGame = Game::where(function ($q) use ($user) {
|
||||||
|
$q->where("player1_user_id", $user->id)
|
||||||
|
->orWhere("player2_user_id", $user->id);
|
||||||
|
})
|
||||||
|
->whereIn("status", ["Pending", "Playing"])
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if ($activeGame) {
|
||||||
|
return response()->json(["message" => "You already have an active game."], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$player2Id = $request->input('player2_user_id', self::GHOST_ID);
|
||||||
|
|
||||||
|
$initialStatus = $request->input('status', 'Pending');
|
||||||
|
|
||||||
|
if ($player2Id !== self::GHOST_ID && !$request->has('status')) {
|
||||||
|
$initialStatus = 'Playing';
|
||||||
|
}
|
||||||
|
|
||||||
|
$game = Game::create([
|
||||||
|
"type" => $validated["type"],
|
||||||
|
"status" => $initialStatus,
|
||||||
|
"player1_user_id" => $user->id,
|
||||||
|
"player2_user_id" => $player2Id,
|
||||||
|
"winner_user_id" => null,
|
||||||
|
"loser_user_id" => null,
|
||||||
|
"match_id" => $request->input('match_id', null),
|
||||||
|
"began_at" => $request->input('began_at', now()),
|
||||||
|
"player1_points" => 0,
|
||||||
|
"player2_points" => 0,
|
||||||
|
"custom" => null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
"message" => "Game created",
|
||||||
|
"game" => $game,
|
||||||
|
], 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function join(Request $request, Game $game)
|
||||||
|
{
|
||||||
|
if ($game->status !== 'Pending' || $game->player2_user_id !== self::GHOST_ID) {
|
||||||
|
return response()->json(['message' => 'Game is not available'], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($game->player1_user_id === $request->user()->id) {
|
||||||
|
return response()->json(['message' => 'Cannot join your own game'], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$activeGame = Game::where(function ($q) use ($request) {
|
||||||
|
$q->where("player1_user_id", $request->user()->id)
|
||||||
|
->orWhere("player2_user_id", $request->user()->id);
|
||||||
|
})
|
||||||
|
->whereIn("status", ["Pending", "Playing"])
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if ($activeGame) {
|
||||||
|
return response()->json(["message" => "You already have an active game."], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$game->update([
|
||||||
|
'player2_user_id' => $request->user()->id,
|
||||||
|
'status' => 'Playing',
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json($game);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(Game $game)
|
||||||
|
{
|
||||||
|
$this->authorize('view', $game);
|
||||||
|
|
||||||
|
$game->load(["player1", "player2", "winner"]);
|
||||||
|
return response()->json($game);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(Request $request, Game $game)
|
||||||
|
{
|
||||||
|
$this->authorize('update', $game);
|
||||||
|
|
||||||
|
if ($game->status == "Ended") {
|
||||||
|
return response()->json(["message" => "Game already ended"], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $request->validate([
|
||||||
|
"status" => "in:Playing,Ended,Interrupted",
|
||||||
|
"winner_user_id" => "exists:users,id",
|
||||||
|
"loser_user_id" => "exists:users,id",
|
||||||
|
"player1_points" => "integer",
|
||||||
|
"player2_points" => "integer",
|
||||||
|
"is_draw" => "boolean",
|
||||||
|
"total_time" => "numeric",
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (isset($data["status"]) && $data["status"] === "Ended") {
|
||||||
|
$data["ended_at"] = now();
|
||||||
|
}
|
||||||
|
|
||||||
|
$game->update($data);
|
||||||
|
|
||||||
|
return response()->json($game);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resign(Game $game)
|
||||||
|
{
|
||||||
|
$this->authorize('resign', $game);
|
||||||
|
|
||||||
|
$user = Auth::user();
|
||||||
|
|
||||||
|
$game->update([
|
||||||
|
'status' => 'Ended',
|
||||||
|
'winner_user_id' => $game->player1_user_id === $user->id
|
||||||
|
? $game->player2_user_id
|
||||||
|
: $game->player1_user_id,
|
||||||
|
'loser_user_id' => $user->id,
|
||||||
|
'ended_at' => now(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'You have resigned from the game',
|
||||||
|
'game' => $game->fresh(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(Game $game)
|
||||||
|
{
|
||||||
|
if ($game->player1_user_id !== auth()->id()) {
|
||||||
|
return response()->json(['message' => 'Unauthorized'], 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($game->status !== 'Pending') {
|
||||||
|
return response()->json(['message' => 'Cannot delete a game in progress'], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$game->delete();
|
||||||
|
return response()->noContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,367 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\MatchGame;
|
||||||
|
use App\Models\CoinTransaction;
|
||||||
|
use App\Models\CoinTransactionType;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Http\Requests\StoreMatchRequest;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class MatchController extends Controller
|
||||||
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$query = MatchGame::query()->with(["winner", "player1", "player2"]);
|
||||||
|
|
||||||
|
if ($request->has("type")) {
|
||||||
|
$query->where("type", $request->type);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("status")) {
|
||||||
|
$query->where("status", $request->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("pot") && in_array($request->pot, ["asc", "desc"])) {
|
||||||
|
$query->orderBy("stake", $request->pot);
|
||||||
|
} elseif (
|
||||||
|
$request->has("sort_direction") &&
|
||||||
|
in_array($request->sort_direction, ["asc", "desc"])
|
||||||
|
) {
|
||||||
|
$query->orderBy("began_at", $request->sort_direction);
|
||||||
|
} else {
|
||||||
|
$query->orderBy("began_at", "desc");
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($query->paginate(15));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(StoreMatchRequest $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validated();
|
||||||
|
$user = $request->user();
|
||||||
|
$stake = $validated['stake'];
|
||||||
|
|
||||||
|
if ($user->coins_balance < $stake) {
|
||||||
|
return response()->json(
|
||||||
|
["message" => "Insufficient coin balance"],
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$activeMatch = MatchGame::where(function ($q) use ($user) {
|
||||||
|
$q->where("player1_user_id", $user->id)->orWhere(
|
||||||
|
"player2_user_id",
|
||||||
|
$user->id,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
->whereIn("status", ["Pending", "Playing"])
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if ($activeMatch) {
|
||||||
|
return response()->json(['message' => 'You already have an active match.'], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
return DB::transaction(function () use ($validated, $user, $stake) {
|
||||||
|
$GHOST_ID = 1;
|
||||||
|
|
||||||
|
$user->decrement("coins_balance", $stake);
|
||||||
|
|
||||||
|
$stakeType = CoinTransactionType::firstOrCreate(
|
||||||
|
["name" => "Match stake"],
|
||||||
|
["type" => "D"],
|
||||||
|
);
|
||||||
|
|
||||||
|
$match = MatchGame::create([
|
||||||
|
"type" => $validated["type"],
|
||||||
|
"status" => "Pending",
|
||||||
|
"player1_user_id" => $user->id,
|
||||||
|
"player2_user_id" => $GHOST_ID,
|
||||||
|
"winner_user_id" => $GHOST_ID,
|
||||||
|
"loser_user_id" => $GHOST_ID,
|
||||||
|
"stake" => $stake,
|
||||||
|
"began_at" => now(),
|
||||||
|
"player1_marks" => 0,
|
||||||
|
"player2_marks" => 0,
|
||||||
|
"player1_points" => 0,
|
||||||
|
"player2_points" => 0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
CoinTransaction::create([
|
||||||
|
"user_id" => $user->id,
|
||||||
|
"match_id" => $match->id,
|
||||||
|
"coin_transaction_type_id" => $stakeType->id,
|
||||||
|
"transaction_datetime" => now(),
|
||||||
|
"coins" => -$stake,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json(
|
||||||
|
[
|
||||||
|
"message" => "Match created successfully",
|
||||||
|
"match" => $match,
|
||||||
|
"balance" => $user->coins_balance,
|
||||||
|
],
|
||||||
|
201,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(MatchGame $match)
|
||||||
|
{
|
||||||
|
$match->load(["player1", "player2", "winner"]);
|
||||||
|
return response()->json($match);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function join(MatchGame $match)
|
||||||
|
{
|
||||||
|
$user = request()->user();
|
||||||
|
|
||||||
|
if ($match->player1_user_id == $user->id) {
|
||||||
|
return response()->json(
|
||||||
|
["message" => "You cannot join your own match"],
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if ($match->status !== "Pending") {
|
||||||
|
return response()->json(
|
||||||
|
["message" => "Match is full or started"],
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($user->coins_balance < $match->stake) {
|
||||||
|
return response()->json(
|
||||||
|
["message" => "Insufficient coin balance to join this match"],
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$activeMatch = MatchGame::where(function ($q) use ($user) {
|
||||||
|
$q->where("player1_user_id", $user->id)->orWhere(
|
||||||
|
"player2_user_id",
|
||||||
|
$user->id,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
->whereIn("status", ["Pending", "Playing"])
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if ($activeMatch) {
|
||||||
|
return response()->json(
|
||||||
|
["message" => "You are already in another active match."],
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return DB::transaction(function () use ($match, $user) {
|
||||||
|
$user->decrement("coins_balance", $match->stake);
|
||||||
|
|
||||||
|
$stakeType = CoinTransactionType::firstOrCreate(
|
||||||
|
["name" => "Match stake"],
|
||||||
|
["type" => "D"],
|
||||||
|
);
|
||||||
|
|
||||||
|
CoinTransaction::create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'match_id' => $match->id,
|
||||||
|
'coin_transaction_type_id' => $stakeType->id,
|
||||||
|
'transaction_datetime' => now(),
|
||||||
|
'coins' => -$match->stake,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$match->update([
|
||||||
|
"status" => "Playing",
|
||||||
|
"player2_user_id" => $user->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
"message" => "Joined match successfully!",
|
||||||
|
"match" => $match,
|
||||||
|
"balance" => $user->coins_balance,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(Request $request, MatchGame $match)
|
||||||
|
{
|
||||||
|
if ($match->status == "Ended") {
|
||||||
|
return response()->json(["message" => "Match already ended"], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $request->validate([
|
||||||
|
"status" => "in:Playing,Ended,Interrupted",
|
||||||
|
"winner_user_id" => "exists:users,id",
|
||||||
|
"loser_user_id" => "exists:users,id",
|
||||||
|
"player1_marks" => "integer",
|
||||||
|
"player2_marks" => "integer",
|
||||||
|
"player1_points" => "integer",
|
||||||
|
"player2_points" => "integer",
|
||||||
|
"total_time" => "numeric",
|
||||||
|
]);
|
||||||
|
|
||||||
|
return DB::transaction(function () use ($match, $data) {
|
||||||
|
if (isset($data['status']) && $data['status'] === 'Ended') {
|
||||||
|
$data['ended_at'] = now();
|
||||||
|
}
|
||||||
|
|
||||||
|
$match->update($data);
|
||||||
|
|
||||||
|
if (
|
||||||
|
$match->status === "Ended" &&
|
||||||
|
$match->stake > 0 &&
|
||||||
|
$match->winner_user_id
|
||||||
|
) {
|
||||||
|
$payoutType = CoinTransactionType::firstOrCreate(
|
||||||
|
["name" => "Match payout"],
|
||||||
|
["type" => "C"],
|
||||||
|
);
|
||||||
|
|
||||||
|
$totalPrize = $match->stake * 2;
|
||||||
|
|
||||||
|
CoinTransaction::create([
|
||||||
|
"user_id" => $match->winner_user_id,
|
||||||
|
"match_id" => $match->id,
|
||||||
|
"coin_transaction_type_id" => $payoutType->id,
|
||||||
|
"transaction_datetime" => now(),
|
||||||
|
"coins" => $totalPrize,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$match->winner->increment("coins_balance", $totalPrize);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($match);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function host(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
"type" => "required|in:3,9",
|
||||||
|
"stake" => "required|integer|min:0",
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = $request->user();
|
||||||
|
$stake = $request->stake;
|
||||||
|
|
||||||
|
if ($user->coins_balance < $stake) {
|
||||||
|
return response()->json(
|
||||||
|
["message" => "Insufficient coin balance"],
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$activeMatch = MatchGame::where(function ($q) use ($user) {
|
||||||
|
$q->where("player1_user_id", $user->id)->orWhere(
|
||||||
|
"player2_user_id",
|
||||||
|
$user->id,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
->whereIn("status", ["Pending", "Playing"])
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if ($activeMatch) {
|
||||||
|
return response()->json(
|
||||||
|
["message" => "You already have an active match."],
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return DB::transaction(function () use ($request, $user, $stake) {
|
||||||
|
$GHOST_ID = 1;
|
||||||
|
|
||||||
|
$user->decrement("coins_balance", $stake);
|
||||||
|
|
||||||
|
$stakeType = CoinTransactionType::firstOrCreate(
|
||||||
|
["name" => "Match stake"],
|
||||||
|
["type" => "D"],
|
||||||
|
);
|
||||||
|
|
||||||
|
$match = MatchGame::create([
|
||||||
|
'type' => $request->type,
|
||||||
|
'status' => 'Pending',
|
||||||
|
'player1_user_id' => $user->id,
|
||||||
|
'player2_user_id' => $GHOST_ID,
|
||||||
|
'winner_user_id' => null,
|
||||||
|
'loser_user_id' => null,
|
||||||
|
'stake' => $stake,
|
||||||
|
'began_at' => now(),
|
||||||
|
'player1_marks' => 0, 'player2_marks' => 0,
|
||||||
|
'player1_points' => 0, 'player2_points' => 0,
|
||||||
|
|
||||||
|
]);
|
||||||
|
|
||||||
|
CoinTransaction::create([
|
||||||
|
"user_id" => $user->id,
|
||||||
|
"match_id" => $match->id,
|
||||||
|
"coin_transaction_type_id" => $stakeType->id,
|
||||||
|
"transaction_datetime" => now(),
|
||||||
|
"coins" => -$stake,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json($match, 201);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function open(Request $request)
|
||||||
|
{
|
||||||
|
$query = MatchGame::where('status', 'Pending')
|
||||||
|
->where(function($q) {
|
||||||
|
$q->whereNull('player2_user_id')
|
||||||
|
->orWhere('player2_user_id', 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
if ($request->has('type')) {
|
||||||
|
$query->where('type', $request->type);
|
||||||
|
}
|
||||||
|
|
||||||
|
$matches = $query->with('player1:id,nickname')
|
||||||
|
->orderBy('began_at', 'desc')
|
||||||
|
->paginate(10);
|
||||||
|
|
||||||
|
return response()->json($matches);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(MatchGame $match)
|
||||||
|
{
|
||||||
|
$user = request()->user();
|
||||||
|
|
||||||
|
if ($match->player1_user_id !== $user->id) {
|
||||||
|
return response()->json(['message' => 'Unauthorized'], 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($match->status !== 'Pending') {
|
||||||
|
return response()->json(['message' => 'Cannot cancel a match that has already started'], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
return DB::transaction(function () use ($match, $user) {
|
||||||
|
DB::table('games')->where('match_id', $match->id)->delete();
|
||||||
|
|
||||||
|
CoinTransaction::where('match_id', $match->id)->update(['match_id' => null]);
|
||||||
|
|
||||||
|
if ($match->stake > 0) {
|
||||||
|
$user->increment('coins_balance', $match->stake);
|
||||||
|
|
||||||
|
$refundType = CoinTransactionType::firstOrCreate(
|
||||||
|
['name' => 'Refund'],
|
||||||
|
['type' => 'C']
|
||||||
|
);
|
||||||
|
|
||||||
|
CoinTransaction::create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'match_id' => null,
|
||||||
|
'coin_transaction_type_id' => $refundType->id,
|
||||||
|
'transaction_datetime' => now(),
|
||||||
|
'coins' => $match->stake,
|
||||||
|
'custom' => "Refund for Match #{$match->id}"
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$match->delete();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Match canceled and refunded',
|
||||||
|
'balance' => $user->coins_balance
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
|
||||||
|
class ProfileController extends Controller
|
||||||
|
{
|
||||||
|
public function show(Request $request)
|
||||||
|
{
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
return response()->json($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
"name" => "required|string|max:255",
|
||||||
|
"nickname" =>
|
||||||
|
"required|string|max:255|unique:users,nickname," .
|
||||||
|
$request->user()->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = $request->user();
|
||||||
|
$user->name = $request->name;
|
||||||
|
$user->nickname = $request->nickname;
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
return response()->json($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(User $user)
|
||||||
|
{
|
||||||
|
$currentUser = Auth::user();
|
||||||
|
|
||||||
|
if ($currentUser->id !== $user->id && $currentUser->type !== "A") {
|
||||||
|
return response()->json(["message" => "Unauthorized"], 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->delete();
|
||||||
|
|
||||||
|
return response()->json(["message" => "User deleted successfully"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updatePassword(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
"current_password" => "required",
|
||||||
|
"new_password" => "required|min:8|confirmed",
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
if (!Hash::check($request->current_password, $user->password)) {
|
||||||
|
return response()->json(
|
||||||
|
[
|
||||||
|
"message" => "Current password is incorrect",
|
||||||
|
],
|
||||||
|
422,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->password = Hash::make($request->new_password);
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
"message" => "Password changed successfully",
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCoins(Request $request)
|
||||||
|
{
|
||||||
|
return response()->json([
|
||||||
|
'coins' => $request->user()->coins_balance
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function uploadAvatar(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
"avatar" => "required|image|mimes:jpeg,png,jpg,gif|max:2048",
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
if ($user->photo_avatar_filename) {
|
||||||
|
Storage::disk("public")->delete(
|
||||||
|
"photos_avatars/" . $user->photo_avatar_filename,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$file = $request->file("avatar");
|
||||||
|
$filename =
|
||||||
|
str_pad($user->id, 5, "0", STR_PAD_LEFT) .
|
||||||
|
"_" .
|
||||||
|
Str::random(10) .
|
||||||
|
"." .
|
||||||
|
$file->extension();
|
||||||
|
$file->storeAs("photos_avatars", $filename, "public");
|
||||||
|
|
||||||
|
$user->photo_avatar_filename = $filename;
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
return response()->json($user);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\Game;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use App\Models\CoinTransaction;
|
||||||
|
|
||||||
|
class StatisticsController extends Controller
|
||||||
|
{
|
||||||
|
public function getPublicStats()
|
||||||
|
{
|
||||||
|
$totalPlayers = User::where('type', 'P')->count();
|
||||||
|
|
||||||
|
$totalGames = Game::where('status', 'Ended')->count();
|
||||||
|
$activeGames = Game::where('status', 'Playing')->count();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'total_players' => $totalPlayers,
|
||||||
|
'total_games' => $totalGames,
|
||||||
|
'active_games' => $activeGames,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLeaderboard(Request $request)
|
||||||
|
{
|
||||||
|
$type = $request->query('type');
|
||||||
|
|
||||||
|
$leaderboard = User::where('type', 'P')
|
||||||
|
->withCount(['wonGames' => function ($query) use ($type) {
|
||||||
|
if ($type) {
|
||||||
|
$query->where('type', $type);
|
||||||
|
}
|
||||||
|
}])
|
||||||
|
->orderBy('won_games_count', 'desc')
|
||||||
|
->paginate(10);
|
||||||
|
|
||||||
|
$leaderboard->through(function ($user) {
|
||||||
|
return [
|
||||||
|
'id' => $user->id,
|
||||||
|
'nickname' => $user->nickname,
|
||||||
|
'photo_avatar_filename' => $user->photo_avatar_filename,
|
||||||
|
'won_games_count' => $user->won_games_count,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
return response()->json($leaderboard);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPersonalStats(Request $request)
|
||||||
|
{
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
$totalGames = Game::where('status', 'Ended')
|
||||||
|
->where(function ($q) use ($user) {
|
||||||
|
$q->where('player1_user_id', $user->id)
|
||||||
|
->orWhere('player2_user_id', $user->id);
|
||||||
|
})
|
||||||
|
->count();
|
||||||
|
|
||||||
|
$totalWins = Game::where('status', 'Ended')
|
||||||
|
->where('winner_user_id', $user->id)
|
||||||
|
->count();
|
||||||
|
|
||||||
|
$totalLosses = $totalGames - $totalWins;
|
||||||
|
|
||||||
|
$winRate = 0;
|
||||||
|
if ($totalGames > 0) {
|
||||||
|
$winRate = round(($totalWins / $totalGames) * 100, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'nickname' => $user->nickname,
|
||||||
|
'total_games' => $totalGames,
|
||||||
|
'total_wins' => $totalWins,
|
||||||
|
'total_losses' => $totalLosses,
|
||||||
|
'win_rate' => $winRate . '%',
|
||||||
|
'current_balance' => $user->coins_balance,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAdminStats()
|
||||||
|
{
|
||||||
|
$totalPlayers = User::where('type', 'P')->count();
|
||||||
|
$totalGames = Game::count();
|
||||||
|
$totalBlockedUsers = User::where('blocked', true)->count();
|
||||||
|
|
||||||
|
$gamesByType = Game::select('type', DB::raw('count(*) as total'))
|
||||||
|
->groupBy('type')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
|
||||||
|
$gamesPerMonth = Game::select(
|
||||||
|
DB::raw("strftime('%Y-%m', began_at) as month"),
|
||||||
|
DB::raw('count(*) as total')
|
||||||
|
)
|
||||||
|
->whereNotNull('began_at')
|
||||||
|
->groupBy('month')
|
||||||
|
->orderBy('month', 'desc')
|
||||||
|
->limit(12)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$totalCoinsPurchased = \App\Models\CoinTransaction::whereHas('type', function($q) {
|
||||||
|
$q->where('name', 'Coin purchase');
|
||||||
|
})
|
||||||
|
->sum('coins');
|
||||||
|
|
||||||
|
$purchasesPerMonth = \App\Models\CoinTransaction::join('coin_transaction_types', 'coin_transactions.coin_transaction_type_id', '=', 'coin_transaction_types.id')
|
||||||
|
->where('coin_transaction_types.name', 'Coin purchase')
|
||||||
|
->select(
|
||||||
|
DB::raw("strftime('%Y-%m', coin_transactions.transaction_datetime) as month"),
|
||||||
|
DB::raw('SUM(coin_transactions.coins) as total_coins')
|
||||||
|
)
|
||||||
|
->groupBy('month')
|
||||||
|
->orderBy('month', 'desc')
|
||||||
|
->limit(12)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'global_counters' => [
|
||||||
|
'total_players' => $totalPlayers,
|
||||||
|
'total_games' => $totalGames,
|
||||||
|
'blocked_users' => $totalBlockedUsers,
|
||||||
|
'total_coins_purchased' => (int) $totalCoinsPurchased,
|
||||||
|
],
|
||||||
|
'charts' => [
|
||||||
|
'games_by_type' => $gamesByType,
|
||||||
|
'games_per_month' => $gamesPerMonth,
|
||||||
|
'purchases_per_month' => $purchasesPerMonth,
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\CoinTransaction;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class TransactionController extends Controller
|
||||||
|
{
|
||||||
|
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->authorize('viewAny', CoinTransaction::class);
|
||||||
|
|
||||||
|
$query = CoinTransaction::with('user:id,nickname,email', 'type:id,name');
|
||||||
|
|
||||||
|
if ($request->has('sort_coins')) {
|
||||||
|
$direction = $request->get('sort_coins') === 'asc' ? 'asc' : 'desc';
|
||||||
|
$query->orderBy('coins', $direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
$dateDirection = $request->get('sort_datetime') === 'asc' ? 'asc' : 'desc';
|
||||||
|
$query->orderBy('transaction_datetime', $dateDirection);
|
||||||
|
|
||||||
|
|
||||||
|
$transactions = $query->paginate(15);
|
||||||
|
|
||||||
|
return response()->json($transactions);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,331 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Game;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\MatchGame;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class UserController extends Controller
|
||||||
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$this->authorize("viewAny", User::class);
|
||||||
|
|
||||||
|
$query = User::query();
|
||||||
|
|
||||||
|
if ($request->has("type")) {
|
||||||
|
$query->where("type", $request->type);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("blocked")) {
|
||||||
|
$query->where("blocked", $request->type);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("search")) {
|
||||||
|
$query->where(function ($q) use ($request) {
|
||||||
|
$q->where("name", "like", "%" . $request->search . "%")
|
||||||
|
->orWhere("email", "like", "%" . $request->search . "%")
|
||||||
|
->orWhere("nickname", "like", "%" . $request->search . "%");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($query->paginate(15));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$this->authorize("create", User::class);
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
"name" => "required|string|max:255",
|
||||||
|
"email" => "required|email|unique:users,email",
|
||||||
|
"password" => "required|string|min:3",
|
||||||
|
"type" => "required|in:A,U",
|
||||||
|
]);
|
||||||
|
|
||||||
|
$validated["password"] = Hash::make($validated["password"]);
|
||||||
|
|
||||||
|
$user = User::create($validated);
|
||||||
|
|
||||||
|
return response()->json($user, 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function me(Request $request)
|
||||||
|
{
|
||||||
|
return response()->json($request->user());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(User $user)
|
||||||
|
{
|
||||||
|
$currentUser = Auth::user();
|
||||||
|
|
||||||
|
if ($currentUser->type === "A" || $currentUser->id === $user->id) {
|
||||||
|
return response()->json($user);
|
||||||
|
} else {
|
||||||
|
return response()->json([
|
||||||
|
"id" => $user->id,
|
||||||
|
"name" => $user->name,
|
||||||
|
"nickname" => $user->nickname,
|
||||||
|
"photo_avatar_filename" => $user->photo_avatar_filename,
|
||||||
|
"type" => $user->type,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(Request $request, User $user)
|
||||||
|
{
|
||||||
|
$this->authorize("update", $user);
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
"name" => "sometimes|string|max:255",
|
||||||
|
"nickname" => [
|
||||||
|
"sometimes",
|
||||||
|
"string",
|
||||||
|
"max:20",
|
||||||
|
Rule::unique("users")->ignore($user->id),
|
||||||
|
],
|
||||||
|
"email" => [
|
||||||
|
"sometimes",
|
||||||
|
"email",
|
||||||
|
Rule::unique("users")->ignore($user->id),
|
||||||
|
],
|
||||||
|
"password" => "sometimes|string|min:3",
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user->update($validated);
|
||||||
|
|
||||||
|
return response()->json($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updatePassword(Request $request, User $user)
|
||||||
|
{
|
||||||
|
$this->authorize("update", $user);
|
||||||
|
|
||||||
|
$validated = $request->validate([
|
||||||
|
"current_password" => "required|string",
|
||||||
|
"new_password" => "required|string|min:3|confirmed",
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!Hash::check($validated["current_password"], $user->password)) {
|
||||||
|
return response()->json(
|
||||||
|
["message" => "Current password is incorrect"],
|
||||||
|
403,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->password = Hash::make($validated["new_password"]);
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
return response()->json(["message" => "Password updated successfully"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(User $user)
|
||||||
|
{
|
||||||
|
$this->authorize("delete", $user);
|
||||||
|
|
||||||
|
$hasTransactions = $user->transactions()->exists();
|
||||||
|
$hasGames = Game::where(function ($q) use ($user) {
|
||||||
|
$q->where("player1_user_id", $user->id)->orWhere(
|
||||||
|
"player2_user_id",
|
||||||
|
$user->id,
|
||||||
|
);
|
||||||
|
})->exists();
|
||||||
|
|
||||||
|
if ($hasTransactions || $hasGames) {
|
||||||
|
$user->delete();
|
||||||
|
$message =
|
||||||
|
"User account deactivated (soft-deleted due to transaction/game history)";
|
||||||
|
} else {
|
||||||
|
$user->forceDelete();
|
||||||
|
$message = "User permanently deleted";
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json(["message" => $message]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getGames(Request $request, User $user)
|
||||||
|
{
|
||||||
|
$this->authorize("view", $user);
|
||||||
|
|
||||||
|
$query = \App\Models\Game::query()
|
||||||
|
->where(function ($q) use ($user) {
|
||||||
|
$q->where("player1_user_id", $user->id)->orWhere(
|
||||||
|
"player2_user_id",
|
||||||
|
$user->id,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
->with(["winner", "player1", "player2"]);
|
||||||
|
|
||||||
|
if ($request->has("type")) {
|
||||||
|
$query->where("type", $request->type);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("status")) {
|
||||||
|
$query->where("status", $request->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("date_from")) {
|
||||||
|
$query->whereDate("began_at", ">=", $request->date_from);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("date_to")) {
|
||||||
|
$query->whereDate("began_at", "<=", $request->date_to);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sortColumn = $request->get("sort_by", "began_at");
|
||||||
|
$sortDirection = $request->get("sort_direction", "desc");
|
||||||
|
|
||||||
|
$allowedColumns = [
|
||||||
|
"began_at",
|
||||||
|
"ended_at",
|
||||||
|
"total_time",
|
||||||
|
"total_points",
|
||||||
|
];
|
||||||
|
if (in_array($sortColumn, $allowedColumns)) {
|
||||||
|
$query->orderBy($sortColumn, $sortDirection);
|
||||||
|
} else {
|
||||||
|
$query->orderBy("began_at", $sortDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
$games = $query->paginate(10);
|
||||||
|
|
||||||
|
$games->getCollection()->transform(function ($game) use ($user) {
|
||||||
|
if ($game->winner_user_id == $user->id) {
|
||||||
|
$game->outcome = "win";
|
||||||
|
} elseif ($game->is_draw) {
|
||||||
|
$game->outcome = "draw";
|
||||||
|
} else {
|
||||||
|
$game->outcome = "loss";
|
||||||
|
}
|
||||||
|
return $game;
|
||||||
|
});
|
||||||
|
|
||||||
|
return response()->json($games);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMatches(Request $request, User $user)
|
||||||
|
{
|
||||||
|
$this->authorize("view", $user);
|
||||||
|
|
||||||
|
$query = MatchGame::query()
|
||||||
|
->where(function ($q) use ($user) {
|
||||||
|
$q->where("player1_user_id", $user->id)->orWhere(
|
||||||
|
"player2_user_id",
|
||||||
|
$user->id,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
->with(["winner", "player1", "player2"]);
|
||||||
|
|
||||||
|
if ($request->has("type")) {
|
||||||
|
$query->where("type", $request->type);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("status")) {
|
||||||
|
$query->where("status", $request->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("date_from")) {
|
||||||
|
$query->whereDate("began_at", ">=", $request->date_from);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("date_to")) {
|
||||||
|
$query->whereDate("began_at", "<=", $request->date_to);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sortColumn = $request->get("sort_by", "began_at");
|
||||||
|
$sortDirection = $request->get("sort_direction", "desc");
|
||||||
|
|
||||||
|
$allowedColumns = ["began_at", "ended_at", "stake", "total_time"];
|
||||||
|
if (in_array($sortColumn, $allowedColumns)) {
|
||||||
|
$query->orderBy($sortColumn, $sortDirection);
|
||||||
|
} else {
|
||||||
|
$query->orderBy("began_at", $sortDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
$matches = $query->paginate(10);
|
||||||
|
|
||||||
|
$matches->getCollection()->transform(function ($match) use ($user) {
|
||||||
|
if ($match->winner_user_id == $user->id) {
|
||||||
|
$match->outcome = "win";
|
||||||
|
} elseif (
|
||||||
|
$match->winner_user_id &&
|
||||||
|
$match->winner_user_id != $user->id
|
||||||
|
) {
|
||||||
|
$match->outcome = "loss";
|
||||||
|
} else {
|
||||||
|
$match->outcome = "interrupted";
|
||||||
|
}
|
||||||
|
return $match;
|
||||||
|
});
|
||||||
|
|
||||||
|
return response()->json($matches);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function block(User $user)
|
||||||
|
{
|
||||||
|
$this->authorize("block", $user);
|
||||||
|
|
||||||
|
$user->blocked = true;
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
$user->tokens()->delete();
|
||||||
|
|
||||||
|
return response()->json(["message" => "User blocked successfully"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unblock(User $user)
|
||||||
|
{
|
||||||
|
$this->authorize("unblock", $user);
|
||||||
|
|
||||||
|
$user->blocked = false;
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
return response()->json(["message" => "User unblocked successfully"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTransactions(Request $request, User $user)
|
||||||
|
{
|
||||||
|
if (
|
||||||
|
$request->user()->id !== $user->id &&
|
||||||
|
$request->user()->type !== "A"
|
||||||
|
) {
|
||||||
|
return response()->json(["message" => "Forbidden"], 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
$query = $user->transactions()->with("type");
|
||||||
|
|
||||||
|
if ($request->has("type")) {
|
||||||
|
$query->whereHas("type", function ($q) use ($request) {
|
||||||
|
$q->where("type", $request->type);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("date_from")) {
|
||||||
|
$query->whereDate(
|
||||||
|
"transaction_datetime",
|
||||||
|
">=",
|
||||||
|
$request->date_from,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has("date_to")) {
|
||||||
|
$query->whereDate("transaction_datetime", "<=", $request->date_to);
|
||||||
|
}
|
||||||
|
|
||||||
|
$transactions = $query
|
||||||
|
->orderBy("transaction_datetime", "desc")
|
||||||
|
->paginate(10);
|
||||||
|
|
||||||
|
return response()->json($transactions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMyTransactions(Request $request)
|
||||||
|
{
|
||||||
|
return $this->getTransactions($request, $request->user());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
class CheckUserType
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Handle an incoming request.
|
||||||
|
*
|
||||||
|
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||||
|
*/
|
||||||
|
public function handle(
|
||||||
|
Request $request,
|
||||||
|
Closure $next,
|
||||||
|
string $type,
|
||||||
|
): Response {
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
if (!$user || $user->type !== $type) {
|
||||||
|
return response()->json(
|
||||||
|
["message" => "Unauthorized. User type {$type} required."],
|
||||||
|
403,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class RegisterRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'email' => 'required|email|unique:users,email',
|
||||||
|
'nickname' => 'required|string|max:255',
|
||||||
|
'name' => 'required|string|max:255',
|
||||||
|
'password' => 'required|string|min:3',
|
||||||
|
'photo' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
|
class StoreGameRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return Auth::check();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'type' => 'required|in:3,9',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function messages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'type.required' => 'You must choose a game type (Bisca de 3 or 9).',
|
||||||
|
'type.in' => 'Invalid game type. Choose 3 or 9.',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
|
class StoreMatchRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return Auth::check();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'type' => 'required|in:3,9',
|
||||||
|
'stake' => 'required|integer|min:1',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class CoinPurchase extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $table = 'coin_purchases';
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'purchase_datetime',
|
||||||
|
'user_id',
|
||||||
|
'coin_transaction_id',
|
||||||
|
'euros',
|
||||||
|
'payment_type',
|
||||||
|
'payment_reference',
|
||||||
|
'custom'
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'purchase_datetime' => 'datetime',
|
||||||
|
'custom' => 'array',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function transaction()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(CoinTransaction::class, 'coin_transaction_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class CoinTransaction extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $table = 'coin_transactions';
|
||||||
|
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'transaction_datetime',
|
||||||
|
'user_id',
|
||||||
|
'match_id',
|
||||||
|
'game_id',
|
||||||
|
'coin_transaction_type_id',
|
||||||
|
'coins',
|
||||||
|
'custom'
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'transaction_datetime' => 'datetime',
|
||||||
|
'custom' => 'array',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function type()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(CoinTransactionType::class, 'coin_transaction_type_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class CoinTransactionType extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'name',
|
||||||
|
'type',
|
||||||
|
'description'
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
|
||||||
|
|
||||||
|
class Game extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'type',
|
||||||
|
'status',
|
||||||
|
'player1_user_id',
|
||||||
|
'player2_user_id',
|
||||||
|
'winner_user_id',
|
||||||
|
'loser_user_id',
|
||||||
|
'began_at',
|
||||||
|
'ended_at',
|
||||||
|
'total_time',
|
||||||
|
'player1_points',
|
||||||
|
'player2_points',
|
||||||
|
'custom',
|
||||||
|
'is_draw',
|
||||||
|
'match_id'
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'began_at' => 'datetime',
|
||||||
|
'ended_at' => 'datetime',
|
||||||
|
'is_draw' => 'boolean',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function winner()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, "winner_user_id");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function loser()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, "loser_user_id");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function player1()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'player1_user_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function player2()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'player2_user_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class MatchGame extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $table = 'matches';
|
||||||
|
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'type',
|
||||||
|
'status',
|
||||||
|
'player1_user_id',
|
||||||
|
'player2_user_id',
|
||||||
|
'winner_user_id',
|
||||||
|
'loser_user_id',
|
||||||
|
'began_at',
|
||||||
|
'ended_at',
|
||||||
|
'total_time',
|
||||||
|
'player1_marks',
|
||||||
|
'player2_marks',
|
||||||
|
'player1_points',
|
||||||
|
'player2_points',
|
||||||
|
'stake',
|
||||||
|
'custom'
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'began_at' => 'datetime',
|
||||||
|
'ended_at' => 'datetime',
|
||||||
|
'player1_marks' => 'integer',
|
||||||
|
'player2_marks' => 'integer',
|
||||||
|
'player1_points' => 'integer',
|
||||||
|
'player2_points' => 'integer',
|
||||||
|
'stake' => 'integer',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function player1()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'player1_user_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function player2()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'player2_user_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function winner()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class, 'winner_user_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
|
use Illuminate\Notifications\Notifiable;
|
||||||
|
use Laravel\Sanctum\HasApiTokens;
|
||||||
|
|
||||||
|
class User extends Authenticatable
|
||||||
|
{
|
||||||
|
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array<int, string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'name',
|
||||||
|
'email',
|
||||||
|
'password',
|
||||||
|
'nickname',
|
||||||
|
'type',
|
||||||
|
'photo_avatar_filename',
|
||||||
|
'coins_balance',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that should be hidden for serialization.
|
||||||
|
*
|
||||||
|
* @var array<int, string>
|
||||||
|
*/
|
||||||
|
protected $hidden = [
|
||||||
|
'password',
|
||||||
|
'remember_token',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that should be cast.
|
||||||
|
*
|
||||||
|
* @var array<string, string>
|
||||||
|
*/
|
||||||
|
protected $casts = [
|
||||||
|
'email_verified_at' => 'datetime',
|
||||||
|
'password' => 'hashed',
|
||||||
|
'blocked' => 'boolean',
|
||||||
|
'deleted_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function wonGames()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Game::class, 'winner_user_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function games()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Game::class, 'player1_user_id')->orWhere('player2_user_id', $this->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function transactions()
|
||||||
|
{
|
||||||
|
return $this->hasMany(CoinTransaction::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Policies;
|
||||||
|
|
||||||
|
use App\Models\CoinTransaction;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Auth\Access\Response;
|
||||||
|
|
||||||
|
class CoinTransactionPolicy
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine whether the user can view any models.
|
||||||
|
*/
|
||||||
|
public function viewAny(User $user): bool
|
||||||
|
{
|
||||||
|
return $user->type === 'A' ? true : false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Policies;
|
||||||
|
|
||||||
|
use App\Models\Game;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
class GamePolicy
|
||||||
|
{
|
||||||
|
public function viewAny(User $user): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function view(User $user, Game $game): bool
|
||||||
|
{
|
||||||
|
if ($user->type === "A") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
$game->player1_user_id === $user->id ||
|
||||||
|
$game->player2_user_id === $user->id
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($game->status === "Pending") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(User $user): bool
|
||||||
|
{
|
||||||
|
if ($user->type === "A") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function join(User $user, Game $game): bool
|
||||||
|
{
|
||||||
|
if ($user->type === "A") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
$game->status !== "Pending" ||
|
||||||
|
$game->player1_user_id === $user->id
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($game->player2_user_id !== null && $game->player2_user_id !== 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resign(User $user, Game $game): bool
|
||||||
|
{
|
||||||
|
if ($user->type === "A") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
$game->status !== "Playing" ||
|
||||||
|
($game->player1_user_id !== $user->id &&
|
||||||
|
$game->player2_user_id !== $user->id)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(User $user, Game $game): bool
|
||||||
|
{
|
||||||
|
if ($user->type === "A") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!in_array($game->status, ['Playing', 'Pending'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($game->player1_user_id !== $user->id && $game->player2_user_id !== $user->id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(User $user, Game $game): bool
|
||||||
|
{
|
||||||
|
return $user->type === "A";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function restore(User $user, Game $game): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function forceDelete(User $user, Game $game): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Policies;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
class UserPolicy
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine whether the user can view any models.
|
||||||
|
*/
|
||||||
|
public function viewAny(User $user): bool
|
||||||
|
{
|
||||||
|
return $user->type === 'A';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can view the model.
|
||||||
|
*/
|
||||||
|
public function view(User $user, User $model): bool
|
||||||
|
{
|
||||||
|
return $user->id === $model->id || $user->type === 'A';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can create models.
|
||||||
|
*/
|
||||||
|
public function create(User $user): bool
|
||||||
|
{
|
||||||
|
return $user->type === 'A';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can update the model.
|
||||||
|
*/
|
||||||
|
public function update(User $user, User $model): bool
|
||||||
|
{
|
||||||
|
return $user->id === $model->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can delete the model.
|
||||||
|
*/
|
||||||
|
public function delete(User $user, User $model): bool
|
||||||
|
{
|
||||||
|
return ($user->type === 'A' && $user->id !== $model->id) || ($user->type === 'P' && $user->id === $model->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function block(User $user, User $model): bool
|
||||||
|
{
|
||||||
|
return $user->type === 'A' && $user->id !== $model->id && $model->type === 'P';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unblock(User $user, User $model): bool
|
||||||
|
{
|
||||||
|
return $user->type === 'A' && $user->id !== $model->id && $model->type === 'P';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can restore the model.
|
||||||
|
*/
|
||||||
|
public function restore(User $user, User $model): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether the user can permanently delete the model.
|
||||||
|
*/
|
||||||
|
public function forceDelete(User $user, User $model): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
|
class AppServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Register any application services.
|
||||||
|
*/
|
||||||
|
public function register(): void
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bootstrap any application services.
|
||||||
|
*/
|
||||||
|
public function boot(): void
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Application;
|
||||||
|
use Symfony\Component\Console\Input\ArgvInput;
|
||||||
|
|
||||||
|
define('LARAVEL_START', microtime(true));
|
||||||
|
|
||||||
|
// Register the Composer autoloader...
|
||||||
|
require __DIR__.'/vendor/autoload.php';
|
||||||
|
|
||||||
|
// Bootstrap Laravel and handle the command...
|
||||||
|
/** @var Application $app */
|
||||||
|
$app = require_once __DIR__.'/bootstrap/app.php';
|
||||||
|
|
||||||
|
$status = $app->handleCommand(new ArgvInput);
|
||||||
|
|
||||||
|
exit($status);
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Application;
|
||||||
|
use Illuminate\Foundation\Configuration\Exceptions;
|
||||||
|
use Illuminate\Foundation\Configuration\Middleware;
|
||||||
|
|
||||||
|
return Application::configure(basePath: dirname(__DIR__))
|
||||||
|
->withRouting(
|
||||||
|
web: __DIR__ . "/../routes/web.php",
|
||||||
|
api: __DIR__ . "/../routes/api.php",
|
||||||
|
commands: __DIR__ . "/../routes/console.php",
|
||||||
|
health: "/up",
|
||||||
|
)
|
||||||
|
->withMiddleware(function (Middleware $middleware): void {
|
||||||
|
$middleware->alias([
|
||||||
|
"user.type" => App\Http\Middleware\CheckUserType::class,
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
->withExceptions(function (Exceptions $exceptions): void {
|
||||||
|
})
|
||||||
|
->create();
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
*
|
||||||
|
!.gitignore
|
||||||
@@ -13,6 +13,13 @@
|
|||||||
0 => 'Laravel\\Sail\\SailServiceProvider',
|
0 => 'Laravel\\Sail\\SailServiceProvider',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
'laravel/sanctum' =>
|
||||||
|
array (
|
||||||
|
'providers' =>
|
||||||
|
array (
|
||||||
|
0 => 'Laravel\\Sanctum\\SanctumServiceProvider',
|
||||||
|
),
|
||||||
|
),
|
||||||
'laravel/tinker' =>
|
'laravel/tinker' =>
|
||||||
array (
|
array (
|
||||||
'providers' =>
|
'providers' =>
|
||||||
@@ -41,4 +48,11 @@
|
|||||||
0 => 'Termwind\\Laravel\\TermwindServiceProvider',
|
0 => 'Termwind\\Laravel\\TermwindServiceProvider',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
'pestphp/pest-plugin-laravel' =>
|
||||||
|
array (
|
||||||
|
'providers' =>
|
||||||
|
array (
|
||||||
|
0 => 'Pest\\Laravel\\PestServiceProvider',
|
||||||
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
@@ -26,11 +26,13 @@
|
|||||||
22 => 'Illuminate\\View\\ViewServiceProvider',
|
22 => 'Illuminate\\View\\ViewServiceProvider',
|
||||||
23 => 'Laravel\\Pail\\PailServiceProvider',
|
23 => 'Laravel\\Pail\\PailServiceProvider',
|
||||||
24 => 'Laravel\\Sail\\SailServiceProvider',
|
24 => 'Laravel\\Sail\\SailServiceProvider',
|
||||||
25 => 'Laravel\\Tinker\\TinkerServiceProvider',
|
25 => 'Laravel\\Sanctum\\SanctumServiceProvider',
|
||||||
26 => 'Carbon\\Laravel\\ServiceProvider',
|
26 => 'Laravel\\Tinker\\TinkerServiceProvider',
|
||||||
27 => 'NunoMaduro\\Collision\\Adapters\\Laravel\\CollisionServiceProvider',
|
27 => 'Carbon\\Laravel\\ServiceProvider',
|
||||||
28 => 'Termwind\\Laravel\\TermwindServiceProvider',
|
28 => 'NunoMaduro\\Collision\\Adapters\\Laravel\\CollisionServiceProvider',
|
||||||
29 => 'App\\Providers\\AppServiceProvider',
|
29 => 'Termwind\\Laravel\\TermwindServiceProvider',
|
||||||
|
30 => 'Pest\\Laravel\\PestServiceProvider',
|
||||||
|
31 => 'App\\Providers\\AppServiceProvider',
|
||||||
),
|
),
|
||||||
'eager' =>
|
'eager' =>
|
||||||
array (
|
array (
|
||||||
@@ -45,10 +47,12 @@
|
|||||||
8 => 'Illuminate\\Session\\SessionServiceProvider',
|
8 => 'Illuminate\\Session\\SessionServiceProvider',
|
||||||
9 => 'Illuminate\\View\\ViewServiceProvider',
|
9 => 'Illuminate\\View\\ViewServiceProvider',
|
||||||
10 => 'Laravel\\Pail\\PailServiceProvider',
|
10 => 'Laravel\\Pail\\PailServiceProvider',
|
||||||
11 => 'Carbon\\Laravel\\ServiceProvider',
|
11 => 'Laravel\\Sanctum\\SanctumServiceProvider',
|
||||||
12 => 'NunoMaduro\\Collision\\Adapters\\Laravel\\CollisionServiceProvider',
|
12 => 'Carbon\\Laravel\\ServiceProvider',
|
||||||
13 => 'Termwind\\Laravel\\TermwindServiceProvider',
|
13 => 'NunoMaduro\\Collision\\Adapters\\Laravel\\CollisionServiceProvider',
|
||||||
14 => 'App\\Providers\\AppServiceProvider',
|
14 => 'Termwind\\Laravel\\TermwindServiceProvider',
|
||||||
|
15 => 'Pest\\Laravel\\PestServiceProvider',
|
||||||
|
16 => 'App\\Providers\\AppServiceProvider',
|
||||||
),
|
),
|
||||||
'deferred' =>
|
'deferred' =>
|
||||||
array (
|
array (
|
||||||
@@ -98,12 +102,15 @@
|
|||||||
'Illuminate\\Queue\\Console\\ForgetFailedCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Queue\\Console\\ForgetFailedCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Queue\\Console\\ListenCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Queue\\Console\\ListenCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Queue\\Console\\MonitorCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Queue\\Console\\MonitorCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
|
'Illuminate\\Queue\\Console\\PauseCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Queue\\Console\\PruneBatchesCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Queue\\Console\\PruneBatchesCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Queue\\Console\\PruneFailedJobsCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Queue\\Console\\PruneFailedJobsCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Queue\\Console\\RestartCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Queue\\Console\\RestartCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
|
'Illuminate\\Queue\\Console\\ResumeCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Queue\\Console\\RetryCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Queue\\Console\\RetryCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Queue\\Console\\RetryBatchCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Queue\\Console\\RetryBatchCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Queue\\Console\\WorkCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Queue\\Console\\WorkCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
|
'Illuminate\\Foundation\\Console\\ReloadCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Foundation\\Console\\RouteCacheCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Foundation\\Console\\RouteCacheCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Foundation\\Console\\RouteClearCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Foundation\\Console\\RouteClearCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
'Illuminate\\Foundation\\Console\\RouteListCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
'Illuminate\\Foundation\\Console\\RouteListCommand' => 'Illuminate\\Foundation\\Providers\\ConsoleSupportServiceProvider',
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
App\Providers\AppServiceProvider::class,
|
||||||
|
];
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
services:
|
||||||
|
laravel.test:
|
||||||
|
build:
|
||||||
|
context: "./vendor/laravel/sail/runtimes/8.4"
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
args:
|
||||||
|
WWWGROUP: "${WWWGROUP}"
|
||||||
|
image: "sail-8.4/app"
|
||||||
|
extra_hosts:
|
||||||
|
- "host.docker.internal:host-gateway"
|
||||||
|
ports:
|
||||||
|
- "${APP_PORT:-80}:80"
|
||||||
|
environment:
|
||||||
|
WWWUSER: "${WWWUSER}"
|
||||||
|
LARAVEL_SAIL: 1
|
||||||
|
XDEBUG_MODE: "${SAIL_XDEBUG_MODE:-off}"
|
||||||
|
XDEBUG_CONFIG: "${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}"
|
||||||
|
IGNITION_LOCAL_SITES_PATH: "${PWD}"
|
||||||
|
volumes:
|
||||||
|
- ".:/var/www/html"
|
||||||
|
networks:
|
||||||
|
- sail
|
||||||
|
depends_on:
|
||||||
|
- mysql
|
||||||
|
mysql:
|
||||||
|
image: "mysql/mysql-server:8.0"
|
||||||
|
ports:
|
||||||
|
- "${FORWARD_DB_PORT:-3306}:3306"
|
||||||
|
environment:
|
||||||
|
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
|
||||||
|
MYSQL_ROOT_HOST: "%"
|
||||||
|
MYSQL_DATABASE: "${DB_DATABASE}"
|
||||||
|
MYSQL_USER: "${DB_USERNAME}"
|
||||||
|
MYSQL_PASSWORD: "${DB_PASSWORD}"
|
||||||
|
MYSQL_ALLOW_EMPTY_PASSWORD: 1
|
||||||
|
MYSQL_EXTRA_OPTIONS: "${MYSQL_EXTRA_OPTIONS:-}"
|
||||||
|
volumes:
|
||||||
|
- "sail-mysql:/var/lib/mysql"
|
||||||
|
- "./vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh"
|
||||||
|
networks:
|
||||||
|
- sail
|
||||||
|
healthcheck:
|
||||||
|
test:
|
||||||
|
- CMD
|
||||||
|
- mysqladmin
|
||||||
|
- ping
|
||||||
|
- "-p${DB_PASSWORD}"
|
||||||
|
retries: 3
|
||||||
|
timeout: 5s
|
||||||
|
networks:
|
||||||
|
sail:
|
||||||
|
driver: bridge
|
||||||
|
volumes:
|
||||||
|
sail-mysql:
|
||||||
|
driver: local
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://getcomposer.org/schema.json",
|
||||||
|
"name": "laravel/laravel",
|
||||||
|
"type": "project",
|
||||||
|
"description": "The skeleton application for the Laravel framework.",
|
||||||
|
"keywords": [
|
||||||
|
"laravel",
|
||||||
|
"framework"
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"require": {
|
||||||
|
"php": "^8.2",
|
||||||
|
"laravel/framework": "^12.0",
|
||||||
|
"laravel/sanctum": "^4.0",
|
||||||
|
"laravel/tinker": "^2.10.1"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"fakerphp/faker": "^1.23",
|
||||||
|
"laravel/pail": "^1.2.2",
|
||||||
|
"laravel/pint": "^1.26",
|
||||||
|
"laravel/sail": "^1.48",
|
||||||
|
"mockery/mockery": "^1.6",
|
||||||
|
"nunomaduro/collision": "^8.6",
|
||||||
|
"pestphp/pest": "^4.1",
|
||||||
|
"pestphp/pest-plugin-laravel": "^4.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"App\\": "app/",
|
||||||
|
"Database\\Factories\\": "database/factories/",
|
||||||
|
"Database\\Seeders\\": "database/seeders/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"Tests\\": "tests/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"setup": [
|
||||||
|
"composer install",
|
||||||
|
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
|
||||||
|
"@php artisan key:generate",
|
||||||
|
"@php artisan migrate --force",
|
||||||
|
"npm install",
|
||||||
|
"npm run build"
|
||||||
|
],
|
||||||
|
"dev": [
|
||||||
|
"Composer\\Config::disableProcessTimeout",
|
||||||
|
"npx concurrently -c \"#93c5fd,#c4b5fd,#fb7185,#fdba74\" \"php artisan serve\" \"php artisan queue:listen --tries=1\" \"php artisan pail --timeout=0\" \"npm run dev\" --names=server,queue,logs,vite --kill-others"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"@php artisan config:clear --ansi",
|
||||||
|
"@php artisan test"
|
||||||
|
],
|
||||||
|
"post-autoload-dump": [
|
||||||
|
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
|
||||||
|
"@php artisan package:discover --ansi"
|
||||||
|
],
|
||||||
|
"post-update-cmd": [
|
||||||
|
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
|
||||||
|
],
|
||||||
|
"post-root-package-install": [
|
||||||
|
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
|
||||||
|
],
|
||||||
|
"post-create-project-cmd": [
|
||||||
|
"@php artisan key:generate --ansi",
|
||||||
|
"@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"",
|
||||||
|
"@php artisan migrate --graceful --ansi"
|
||||||
|
],
|
||||||
|
"pre-package-uninstall": [
|
||||||
|
"Illuminate\\Foundation\\ComposerScripts::prePackageUninstall"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"laravel": {
|
||||||
|
"dont-discover": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"optimize-autoloader": true,
|
||||||
|
"preferred-install": "dist",
|
||||||
|
"sort-packages": true,
|
||||||
|
"allow-plugins": {
|
||||||
|
"pestphp/pest-plugin": true,
|
||||||
|
"php-http/discovery": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"minimum-stability": "stable",
|
||||||
|
"prefer-stable": true
|
||||||
|
}
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Application Name
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This value is the name of your application, which will be used when the
|
||||||
|
| framework needs to place the application's name in a notification or
|
||||||
|
| other UI elements where an application name needs to be displayed.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'name' => env('APP_NAME', 'Laravel'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Application Environment
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This value determines the "environment" your application is currently
|
||||||
|
| running in. This may determine how you prefer to configure various
|
||||||
|
| services the application utilizes. Set this in your ".env" file.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'env' => env('APP_ENV', 'production'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Application Debug Mode
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| When your application is in debug mode, detailed error messages with
|
||||||
|
| stack traces will be shown on every error that occurs within your
|
||||||
|
| application. If disabled, a simple generic error page is shown.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'debug' => (bool) env('APP_DEBUG', false),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Application URL
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This URL is used by the console to properly generate URLs when using
|
||||||
|
| the Artisan command line tool. You should set this to the root of
|
||||||
|
| the application so that it's available within Artisan commands.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'url' => env('APP_URL', 'http://localhost'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Application Timezone
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Here you may specify the default timezone for your application, which
|
||||||
|
| will be used by the PHP date and date-time functions. The timezone
|
||||||
|
| is set to "UTC" by default as it is suitable for most use cases.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'timezone' => 'UTC',
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Application Locale Configuration
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| The application locale determines the default locale that will be used
|
||||||
|
| by Laravel's translation / localization methods. This option can be
|
||||||
|
| set to any locale for which you plan to have translation strings.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'locale' => env('APP_LOCALE', 'en'),
|
||||||
|
|
||||||
|
'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'),
|
||||||
|
|
||||||
|
'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Encryption Key
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This key is utilized by Laravel's encryption services and should be set
|
||||||
|
| to a random, 32 character string to ensure that all encrypted values
|
||||||
|
| are secure. You should do this prior to deploying the application.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'cipher' => 'AES-256-CBC',
|
||||||
|
|
||||||
|
'key' => env('APP_KEY'),
|
||||||
|
|
||||||
|
'previous_keys' => [
|
||||||
|
...array_filter(
|
||||||
|
explode(',', (string) env('APP_PREVIOUS_KEYS', ''))
|
||||||
|
),
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Maintenance Mode Driver
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| These configuration options determine the driver used to determine and
|
||||||
|
| manage Laravel's "maintenance mode" status. The "cache" driver will
|
||||||
|
| allow maintenance mode to be controlled across multiple machines.
|
||||||
|
|
|
||||||
|
| Supported drivers: "file", "cache"
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'maintenance' => [
|
||||||
|
'driver' => env('APP_MAINTENANCE_DRIVER', 'file'),
|
||||||
|
'store' => env('APP_MAINTENANCE_STORE', 'database'),
|
||||||
|
],
|
||||||
|
|
||||||
|
];
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Authentication Defaults
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This option defines the default authentication "guard" and password
|
||||||
|
| reset "broker" for your application. You may change these values
|
||||||
|
| as required, but they're a perfect start for most applications.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'defaults' => [
|
||||||
|
'guard' => env('AUTH_GUARD', 'web'),
|
||||||
|
'passwords' => env('AUTH_PASSWORD_BROKER', 'users'),
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Authentication Guards
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Next, you may define every authentication guard for your application.
|
||||||
|
| Of course, a great default configuration has been defined for you
|
||||||
|
| which utilizes session storage plus the Eloquent user provider.
|
||||||
|
|
|
||||||
|
| All authentication guards have a user provider, which defines how the
|
||||||
|
| users are actually retrieved out of your database or other storage
|
||||||
|
| system used by the application. Typically, Eloquent is utilized.
|
||||||
|
|
|
||||||
|
| Supported: "session"
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'guards' => [
|
||||||
|
'web' => [
|
||||||
|
'driver' => 'session',
|
||||||
|
'provider' => 'users',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| User Providers
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| All authentication guards have a user provider, which defines how the
|
||||||
|
| users are actually retrieved out of your database or other storage
|
||||||
|
| system used by the application. Typically, Eloquent is utilized.
|
||||||
|
|
|
||||||
|
| If you have multiple user tables or models you may configure multiple
|
||||||
|
| providers to represent the model / table. These providers may then
|
||||||
|
| be assigned to any extra authentication guards you have defined.
|
||||||
|
|
|
||||||
|
| Supported: "database", "eloquent"
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'providers' => [
|
||||||
|
'users' => [
|
||||||
|
'driver' => 'eloquent',
|
||||||
|
'model' => env('AUTH_MODEL', App\Models\User::class),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Resetting Passwords
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| These configuration options specify the behavior of Laravel's password
|
||||||
|
| reset functionality, including the table utilized for token storage
|
||||||
|
| and the user provider that is invoked to actually retrieve users.
|
||||||
|
|
|
||||||
|
| The expiry time is the number of minutes that each reset token will be
|
||||||
|
| considered valid. This security feature keeps tokens short-lived so
|
||||||
|
| they have less time to be guessed. You may change this as needed.
|
||||||
|
|
|
||||||
|
| The throttle setting is the number of seconds a user must wait before
|
||||||
|
| generating more password reset tokens. This prevents the user from
|
||||||
|
| quickly generating a very large amount of password reset tokens.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'passwords' => [
|
||||||
|
'users' => [
|
||||||
|
'provider' => 'users',
|
||||||
|
'table' => env('AUTH_PASSWORD_RESET_TOKEN_TABLE', 'password_reset_tokens'),
|
||||||
|
'expire' => 60,
|
||||||
|
'throttle' => 60,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Password Confirmation Timeout
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Here you may define the number of seconds before a password confirmation
|
||||||
|
| window expires and users are asked to re-enter their password via the
|
||||||
|
| confirmation screen. By default, the timeout lasts for three hours.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),
|
||||||
|
|
||||||
|
];
|
||||||
@@ -39,11 +39,6 @@ return [
|
|||||||
'serialize' => false,
|
'serialize' => false,
|
||||||
],
|
],
|
||||||
|
|
||||||
'session' => [
|
|
||||||
'driver' => 'session',
|
|
||||||
'key' => env('SESSION_CACHE_KEY', '_cache'),
|
|
||||||
],
|
|
||||||
|
|
||||||
'database' => [
|
'database' => [
|
||||||
'driver' => 'database',
|
'driver' => 'database',
|
||||||
'connection' => env('DB_CACHE_CONNECTION'),
|
'connection' => env('DB_CACHE_CONNECTION'),
|
||||||
@@ -66,7 +61,6 @@ return [
|
|||||||
env('MEMCACHED_PASSWORD'),
|
env('MEMCACHED_PASSWORD'),
|
||||||
],
|
],
|
||||||
'options' => [
|
'options' => [
|
||||||
// Memcached::OPT_CONNECT_TIMEOUT => 2000,
|
|
||||||
],
|
],
|
||||||
'servers' => [
|
'servers' => [
|
||||||
[
|
[
|
||||||
@@ -117,6 +111,6 @@ return [
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'prefix' => env('CACHE_PREFIX', Str::slug((string) env('APP_NAME', 'laravel'), '_').'_cache_'),
|
'prefix' => env('CACHE_PREFIX', Str::slug((string) env('APP_NAME', 'laravel')).'-cache-'),
|
||||||
|
|
||||||
];
|
];
|
||||||
@@ -36,13 +36,11 @@ return [
|
|||||||
'url' => env('DB_URL'),
|
'url' => env('DB_URL'),
|
||||||
'database' => env('DB_DATABASE', database_path('database.sqlite')),
|
'database' => env('DB_DATABASE', database_path('database.sqlite')),
|
||||||
'prefix' => '',
|
'prefix' => '',
|
||||||
'prefix_indexes' => null,
|
|
||||||
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
|
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
|
||||||
'busy_timeout' => null,
|
'busy_timeout' => null,
|
||||||
'journal_mode' => null,
|
'journal_mode' => null,
|
||||||
'synchronous' => null,
|
'synchronous' => null,
|
||||||
'transaction_mode' => 'DEFERRED',
|
'transaction_mode' => 'DEFERRED',
|
||||||
'pragmas' => [],
|
|
||||||
],
|
],
|
||||||
|
|
||||||
'mysql' => [
|
'mysql' => [
|
||||||
@@ -150,7 +148,7 @@ return [
|
|||||||
|
|
||||||
'options' => [
|
'options' => [
|
||||||
'cluster' => env('REDIS_CLUSTER', 'redis'),
|
'cluster' => env('REDIS_CLUSTER', 'redis'),
|
||||||
'prefix' => env('REDIS_PREFIX', Str::slug((string) env('APP_NAME', 'laravel'), '_').'_database_'),
|
'prefix' => env('REDIS_PREFIX', Str::slug((string) env('APP_NAME', 'laravel')).'-database-'),
|
||||||
'persistent' => env('REDIS_PERSISTENT', false),
|
'persistent' => env('REDIS_PERSISTENT', false),
|
||||||
],
|
],
|
||||||
|
|
||||||
@@ -115,23 +115,4 @@ return [
|
|||||||
'name' => env('MAIL_FROM_NAME', 'Example'),
|
'name' => env('MAIL_FROM_NAME', 'Example'),
|
||||||
],
|
],
|
||||||
|
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Markdown Mail Settings
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| If you are using Markdown based email rendering, you may configure your
|
|
||||||
| theme and component paths here, allowing you to customize the design
|
|
||||||
| of the emails. Or, you may simply stick with the Laravel defaults!
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
'markdown' => [
|
|
||||||
'theme' => env('MAIL_MARKDOWN_THEME', 'default'),
|
|
||||||
|
|
||||||
'paths' => [
|
|
||||||
resource_path('views/vendor/mail'),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
|
|
||||||
];
|
];
|
||||||
@@ -25,7 +25,7 @@ return [
|
|||||||
| each backend supported by Laravel. You're also free to add more.
|
| each backend supported by Laravel. You're also free to add more.
|
||||||
|
|
|
|
||||||
| Drivers: "sync", "database", "beanstalkd", "sqs", "redis",
|
| Drivers: "sync", "database", "beanstalkd", "sqs", "redis",
|
||||||
| "deferred", "failover", "null"
|
| "deferred", "background", "failover", "null"
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -77,6 +77,10 @@ return [
|
|||||||
'driver' => 'deferred',
|
'driver' => 'deferred',
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'background' => [
|
||||||
|
'driver' => 'background',
|
||||||
|
],
|
||||||
|
|
||||||
'failover' => [
|
'failover' => [
|
||||||
'driver' => 'failover',
|
'driver' => 'failover',
|
||||||
'connections' => [
|
'connections' => [
|
||||||
@@ -15,11 +15,11 @@ return [
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
'postmark' => [
|
'postmark' => [
|
||||||
'token' => env('POSTMARK_TOKEN'),
|
'key' => env('POSTMARK_API_KEY'),
|
||||||
],
|
],
|
||||||
|
|
||||||
'resend' => [
|
'resend' => [
|
||||||
'key' => env('RESEND_KEY'),
|
'key' => env('RESEND_API_KEY'),
|
||||||
],
|
],
|
||||||
|
|
||||||
'ses' => [
|
'ses' => [
|
||||||
@@ -129,7 +129,7 @@ return [
|
|||||||
|
|
||||||
'cookie' => env(
|
'cookie' => env(
|
||||||
'SESSION_COOKIE',
|
'SESSION_COOKIE',
|
||||||
Str::slug((string) env('APP_NAME', 'laravel'), '_').'_session'
|
Str::slug((string) env('APP_NAME', 'laravel')).'-session'
|
||||||
),
|
),
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
*.sqlite*
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
|
||||||
|
*/
|
||||||
|
class UserFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The current password being used by the factory.
|
||||||
|
*/
|
||||||
|
protected static ?string $password;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => fake()->name(),
|
||||||
|
'email' => fake()->unique()->safeEmail(),
|
||||||
|
'email_verified_at' => now(),
|
||||||
|
'password' => static::$password ??= Hash::make('password'),
|
||||||
|
'remember_token' => Str::random(10),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicate that the model's email address should be unverified.
|
||||||
|
*/
|
||||||
|
public function unverified(): static
|
||||||
|
{
|
||||||
|
return $this->state(fn (array $attributes) => [
|
||||||
|
'email_verified_at' => null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('users', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('email')->unique();
|
||||||
|
$table->timestamp('email_verified_at')->nullable();
|
||||||
|
$table->string('password');
|
||||||
|
$table->rememberToken();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('password_reset_tokens', function (Blueprint $table) {
|
||||||
|
$table->string('email')->primary();
|
||||||
|
$table->string('token');
|
||||||
|
$table->timestamp('created_at')->nullable();
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('sessions', function (Blueprint $table) {
|
||||||
|
$table->string('id')->primary();
|
||||||
|
$table->foreignId('user_id')->nullable()->index();
|
||||||
|
$table->string('ip_address', 45)->nullable();
|
||||||
|
$table->text('user_agent')->nullable();
|
||||||
|
$table->longText('payload');
|
||||||
|
$table->integer('last_activity')->index();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('users');
|
||||||
|
Schema::dropIfExists('password_reset_tokens');
|
||||||
|
Schema::dropIfExists('sessions');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('jobs', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('queue')->index();
|
||||||
|
$table->longText('payload');
|
||||||
|
$table->unsignedTinyInteger('attempts');
|
||||||
|
$table->unsignedInteger('reserved_at')->nullable();
|
||||||
|
$table->unsignedInteger('available_at');
|
||||||
|
$table->unsignedInteger('created_at');
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('job_batches', function (Blueprint $table) {
|
||||||
|
$table->string('id')->primary();
|
||||||
|
$table->string('name');
|
||||||
|
$table->integer('total_jobs');
|
||||||
|
$table->integer('pending_jobs');
|
||||||
|
$table->integer('failed_jobs');
|
||||||
|
$table->longText('failed_job_ids');
|
||||||
|
$table->mediumText('options')->nullable();
|
||||||
|
$table->integer('cancelled_at')->nullable();
|
||||||
|
$table->integer('created_at');
|
||||||
|
$table->integer('finished_at')->nullable();
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('failed_jobs', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('uuid')->unique();
|
||||||
|
$table->text('connection');
|
||||||
|
$table->text('queue');
|
||||||
|
$table->longText('payload');
|
||||||
|
$table->longText('exception');
|
||||||
|
$table->timestamp('failed_at')->useCurrent();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('jobs');
|
||||||
|
Schema::dropIfExists('job_batches');
|
||||||
|
Schema::dropIfExists('failed_jobs');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('personal_access_tokens', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->morphs('tokenable');
|
||||||
|
$table->text('name');
|
||||||
|
$table->string('token', 64)->unique();
|
||||||
|
$table->text('abilities')->nullable();
|
||||||
|
$table->timestamp('last_used_at')->nullable();
|
||||||
|
$table->timestamp('expires_at')->nullable()->index();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('personal_access_tokens');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,258 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
// Administrator, Player
|
||||||
|
$table->enum('type', ['A', 'P'])->default('P');
|
||||||
|
|
||||||
|
// Nickname - must be unique
|
||||||
|
$table->string('nickname', 20)->nullable()->unique();
|
||||||
|
|
||||||
|
// User access is blocked
|
||||||
|
$table->boolean('blocked')->default(false);
|
||||||
|
|
||||||
|
// User Photo/Avatar
|
||||||
|
$table->string('photo_avatar_filename')->nullable();
|
||||||
|
|
||||||
|
// Brain Coin Balance
|
||||||
|
$table->integer('coins_balance')->default(0);
|
||||||
|
|
||||||
|
// Users can be deleted with "soft deletes"
|
||||||
|
$table->softDeletes();
|
||||||
|
|
||||||
|
// custom data
|
||||||
|
$table->json('custom')->nullable();
|
||||||
|
});
|
||||||
|
|
||||||
|
// NOTE: Only multiplayer matches are registered
|
||||||
|
Schema::create('matches', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
|
||||||
|
// Type of games in the match (Bisca dos 3; Bisca dos 9)
|
||||||
|
$table->enum('type', ['3', '9'])->default('3');
|
||||||
|
|
||||||
|
// Player 1 (usually, creates the match)
|
||||||
|
$table->unsignedBigInteger('player1_user_id');
|
||||||
|
$table->foreign('player1_user_id')->references('id')->on('users');
|
||||||
|
|
||||||
|
// Player 2 (usually, joins the match)
|
||||||
|
$table->unsignedBigInteger('player2_user_id');
|
||||||
|
$table->foreign('player2_user_id')->references('id')->on('users');
|
||||||
|
|
||||||
|
// Winner (null when not ended)
|
||||||
|
$table->unsignedBigInteger('winner_user_id')->nullable();
|
||||||
|
$table->foreign('winner_user_id')->references('id')->on('users');
|
||||||
|
|
||||||
|
// Loser (null when not ended)
|
||||||
|
$table->unsignedBigInteger('loser_user_id')->nullable();
|
||||||
|
$table->foreign('loser_user_id')->references('id')->on('users');
|
||||||
|
|
||||||
|
// Match status
|
||||||
|
// Pending - Match is waiting for players
|
||||||
|
// Playing - Match is in progress
|
||||||
|
// Ended - Match is over
|
||||||
|
// Interrupted - Match was interrupted due to technical issues (not finished; no winner)
|
||||||
|
// Note: depending of the project's implementation, some status may never be used
|
||||||
|
$table->enum('status', ['Pending', 'Playing', 'Ended', 'Interrupted']);
|
||||||
|
|
||||||
|
// The stake for this match
|
||||||
|
$table->integer('stake')->default(3);
|
||||||
|
|
||||||
|
// Moment when the match began
|
||||||
|
$table->dateTime('began_at')->nullable();
|
||||||
|
// Moment when the match ended
|
||||||
|
$table->dateTime('ended_at')->nullable();
|
||||||
|
// Match total time (in seconds) = ended_at - began_at
|
||||||
|
$table->decimal('total_time', 8, 2)->nullable();
|
||||||
|
|
||||||
|
// After the match ended, the total marks for player 1
|
||||||
|
$table->integer('player1_marks')->nullable();
|
||||||
|
|
||||||
|
// After the game ended, the total marks for player 2
|
||||||
|
$table->integer('player2_marks')->nullable();
|
||||||
|
|
||||||
|
// After the match ended, the sum of points for player 1 (all games)
|
||||||
|
$table->integer('player1_points')->nullable();
|
||||||
|
|
||||||
|
// After the match ended, the sum of points for player 2 (all games)
|
||||||
|
$table->integer('player2_points')->nullable();
|
||||||
|
|
||||||
|
// custom data
|
||||||
|
$table->json('custom')->nullable();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
// NOTE: Only multiplayer games are registered
|
||||||
|
Schema::create('games', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
|
||||||
|
// Type of game (Bisca dos 3; Bisca dos 9)
|
||||||
|
$table->enum('type', ['3', '9'])->default('3');
|
||||||
|
|
||||||
|
// Player 1 (usually, creates the game)
|
||||||
|
$table->unsignedBigInteger('player1_user_id');
|
||||||
|
$table->foreign('player1_user_id')->references('id')->on('users');
|
||||||
|
|
||||||
|
// Player 2 (usually, joins the game)
|
||||||
|
$table->unsignedBigInteger('player2_user_id');
|
||||||
|
$table->foreign('player2_user_id')->references('id')->on('users');
|
||||||
|
|
||||||
|
// True when the game has ended with a draw
|
||||||
|
$table->boolean('is_draw')->default(false);
|
||||||
|
|
||||||
|
// Winner (null when not ended, or when a draw)
|
||||||
|
$table->unsignedBigInteger('winner_user_id')->nullable();
|
||||||
|
$table->foreign('winner_user_id')->references('id')->on('users');
|
||||||
|
|
||||||
|
// Loser (null when not ended, or when a draw)
|
||||||
|
$table->unsignedBigInteger('loser_user_id')->nullable();
|
||||||
|
$table->foreign('loser_user_id')->references('id')->on('users');
|
||||||
|
|
||||||
|
// Match (null for standalone games)
|
||||||
|
$table->unsignedBigInteger('match_id')->nullable();
|
||||||
|
$table->foreign('match_id')->references('id')->on('matches');
|
||||||
|
|
||||||
|
// Game status: 'Pending', 'Playing', 'Ended', or 'Interrupted'.
|
||||||
|
// Pending: waiting for players
|
||||||
|
// Playing: in progress
|
||||||
|
// Ended: finished
|
||||||
|
// Interrupted: stopped due to technical issues (unfinished; no winner)
|
||||||
|
// Note: depending on the project’s implementation, some statuses may never be used.
|
||||||
|
$table->enum('status', ['Pending', 'Playing', 'Ended', 'Interrupted']);
|
||||||
|
|
||||||
|
// Moment when the game began
|
||||||
|
$table->dateTime('began_at')->nullable();
|
||||||
|
// Moment when the game ended
|
||||||
|
$table->dateTime('ended_at')->nullable();
|
||||||
|
// Game total time (in seconds) = ended_at - began_at
|
||||||
|
$table->decimal('total_time', 8, 2)->nullable();
|
||||||
|
|
||||||
|
|
||||||
|
// After the game ended, the total of points for player 1
|
||||||
|
$table->integer('player1_points')->nullable();
|
||||||
|
|
||||||
|
// After the game ended, the total of points for player 2
|
||||||
|
$table->integer('player2_points')->nullable();
|
||||||
|
|
||||||
|
// custom data
|
||||||
|
$table->json('custom')->nullable();
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('coin_transaction_types', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name', 40);
|
||||||
|
// Credit - increase coins
|
||||||
|
// Debit - decrease coins
|
||||||
|
$table->enum('type', ['C', 'D']);
|
||||||
|
// Transaction Types can be deleted with "soft deletes"
|
||||||
|
$table->softDeletes();
|
||||||
|
|
||||||
|
// custom data
|
||||||
|
$table->json('custom')->nullable();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add default coin_transaction_types
|
||||||
|
DB::table('coin_transaction_types')->insert([
|
||||||
|
['name' => 'Bonus', 'type' => 'C'], // id = 1
|
||||||
|
['name' => 'Coin purchase', 'type' => 'C'], // id = 2
|
||||||
|
['name' => 'Game fee', 'type' => 'D'], // id = 3
|
||||||
|
['name' => 'Match stake', 'type' => 'D'], // id = 4
|
||||||
|
['name' => 'Game payout', 'type' => 'C'], // id = 5
|
||||||
|
['name' => 'Match payout', 'type' => 'C'], // id = 6
|
||||||
|
]);
|
||||||
|
|
||||||
|
Schema::create('coin_transactions', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
|
||||||
|
// Datetime of the coin transaction
|
||||||
|
$table->dateTime('transaction_datetime');
|
||||||
|
|
||||||
|
// User associated with the transaction
|
||||||
|
$table->unsignedBigInteger('user_id');
|
||||||
|
$table->foreign('user_id')->references('id')->on('users');
|
||||||
|
|
||||||
|
// Match associated with the transaction (optional - it can be null)
|
||||||
|
$table->unsignedBigInteger('match_id')->nullable();
|
||||||
|
$table->foreign('match_id')->references('id')->on('matches');
|
||||||
|
|
||||||
|
// Game associated with the transaction (optional - it can be null)
|
||||||
|
$table->unsignedBigInteger('game_id')->nullable();
|
||||||
|
$table->foreign('game_id')->references('id')->on('games');
|
||||||
|
|
||||||
|
// Type of the transaction
|
||||||
|
$table->unsignedBigInteger('coin_transaction_type_id');
|
||||||
|
$table->foreign('coin_transaction_type_id')->references('id')->on('coin_transaction_types');
|
||||||
|
|
||||||
|
// Amount of the transaction (coins)
|
||||||
|
// Positive -> increments the total amount of brain coins (Credit)
|
||||||
|
// Negative -> decrements the total amount of brain coins (Debit)
|
||||||
|
$table->integer('coins');
|
||||||
|
|
||||||
|
// custom data
|
||||||
|
$table->json('custom')->nullable();
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('coin_purchases', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
|
||||||
|
// Datetime of the coin purchase
|
||||||
|
$table->dateTime('purchase_datetime');
|
||||||
|
|
||||||
|
// User associated with the purchase
|
||||||
|
$table->unsignedBigInteger('user_id');
|
||||||
|
$table->foreign('user_id')->references('id')->on('users');
|
||||||
|
|
||||||
|
// Coin Transaction associated with this purchase
|
||||||
|
// One to One relation (purchase must have a coin transaction)
|
||||||
|
$table->unsignedBigInteger('coin_transaction_id')->unique();
|
||||||
|
$table->foreign('coin_transaction_id')->references('id')->on('coin_transactions');
|
||||||
|
|
||||||
|
// Amount of the purchase (real money in euros)
|
||||||
|
$table->decimal('euros', 8, 2);
|
||||||
|
|
||||||
|
// Purchases will involve a payment with a type and a reference
|
||||||
|
// MBWAY - Phone number with 9 digits
|
||||||
|
// PAYPAL - eMail
|
||||||
|
// IBAN - bank transfer (2 letters + 23 digits)
|
||||||
|
// MB - Multibanco payment - entity number (5 digits) + Reference (9 digits))
|
||||||
|
// VISA - Visa card number (16 digits)
|
||||||
|
$table->enum('payment_type', ['MBWAY', 'PAYPAL', 'IBAN', 'MB', 'VISA']);
|
||||||
|
$table->string('payment_reference', 30);
|
||||||
|
|
||||||
|
// custom data
|
||||||
|
$table->json('custom')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::drop('coin_purchases');
|
||||||
|
Schema::drop('coin_transactions');
|
||||||
|
Schema::drop('coin_transaction_types');
|
||||||
|
Schema::drop('games');
|
||||||
|
Schema::drop('matches');
|
||||||
|
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('deleted_at');
|
||||||
|
$table->dropColumn('custom');
|
||||||
|
$table->dropColumn('coins_balance');
|
||||||
|
$table->dropColumn('photo_avatar_filename');
|
||||||
|
$table->dropColumn('blocked');
|
||||||
|
$table->dropColumn('nickname');
|
||||||
|
$table->dropColumn('type');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
|
class DatabaseSeeder extends Seeder
|
||||||
|
{
|
||||||
|
use WithoutModelEvents;
|
||||||
|
|
||||||
|
public static $startDate;
|
||||||
|
public static $dbInsertBlockSize = 500;
|
||||||
|
|
||||||
|
// public static $seedType = "small";
|
||||||
|
//public static $seedType = "full";
|
||||||
|
//public static $seedLanguage = "pt_PT";
|
||||||
|
public static $seedLanguage = "en_US";
|
||||||
|
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$this->command->info("-----------------------------------------------");
|
||||||
|
$this->command->info("START of database seeder");
|
||||||
|
$this->command->info("-----------------------------------------------");
|
||||||
|
|
||||||
|
self::$startDate = Carbon::now()->subMonths(14);
|
||||||
|
self::$seedLanguage = $this->command->choice('What is the language for users\' names?', ['pt_PT', 'en_US'], 0);
|
||||||
|
|
||||||
|
if (DB::getDriverName() === 'sqlite') {
|
||||||
|
DB::statement('PRAGMA foreign_keys = OFF');
|
||||||
|
} else {
|
||||||
|
DB::statement('SET foreign_key_checks=0');
|
||||||
|
// No permissions to change global setting. Change the session setting only
|
||||||
|
//DB::statement("SET @@global.time_zone = '+00:00'");
|
||||||
|
DB::statement("SET time_zone = '+00:00'");
|
||||||
|
}
|
||||||
|
|
||||||
|
DB::table('users')->delete();
|
||||||
|
DB::table('matches')->delete();
|
||||||
|
DB::table('games')->delete();
|
||||||
|
DB::table('coin_purchases')->delete();
|
||||||
|
DB::table('coin_transactions')->delete();
|
||||||
|
DB::table('coin_transaction_types')->delete();
|
||||||
|
|
||||||
|
if (DB::getDriverName() === 'sqlite') {
|
||||||
|
DB::statement("DELETE FROM sqlite_sequence WHERE name = 'users'");
|
||||||
|
DB::statement("DELETE FROM sqlite_sequence WHERE name = 'matches'");
|
||||||
|
DB::statement("DELETE FROM sqlite_sequence WHERE name = 'games'");
|
||||||
|
DB::statement("DELETE FROM sqlite_sequence WHERE name = 'coin_purchases'");
|
||||||
|
DB::statement("DELETE FROM sqlite_sequence WHERE name = 'coin_transactions'");
|
||||||
|
DB::statement("DELETE FROM sqlite_sequence WHERE name = 'coin_transaction_types'");
|
||||||
|
} else {
|
||||||
|
DB::statement('ALTER TABLE users AUTO_INCREMENT = 0');
|
||||||
|
DB::statement('ALTER TABLE matches AUTO_INCREMENT = 0');
|
||||||
|
DB::statement('ALTER TABLE games AUTO_INCREMENT = 0');
|
||||||
|
DB::statement('ALTER TABLE coin_purchases AUTO_INCREMENT = 0');
|
||||||
|
DB::statement('ALTER TABLE coin_transactions AUTO_INCREMENT = 0');
|
||||||
|
DB::statement('ALTER TABLE coin_transaction_types AUTO_INCREMENT = 0');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->command->info("-----------------------------------------------");
|
||||||
|
|
||||||
|
$this->call(TransactionTypesSeeder::class);
|
||||||
|
$this->call(UsersSeeder::class);
|
||||||
|
$this->call(InitialTransactionsSeeder::class);
|
||||||
|
$this->call(GamesSeeder::class);
|
||||||
|
$this->call(GamesTransactionsSeeder::class);
|
||||||
|
|
||||||
|
if (DB::getDriverName() === 'sqlite') {
|
||||||
|
DB::statement('PRAGMA foreign_keys = ON');
|
||||||
|
} else {
|
||||||
|
DB::statement('SET foreign_key_checks=1');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
$this->command->info("-----------------------------------------------");
|
||||||
|
$this->command->info("END of database seeder");
|
||||||
|
$this->command->info("-----------------------------------------------");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,214 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Container\Attributes\Database;
|
||||||
|
|
||||||
|
class GamesSeeder extends Seeder
|
||||||
|
{
|
||||||
|
private $ratioStandaloneToMatch = 15;
|
||||||
|
|
||||||
|
private function calculateRandomSeconds($filteredCollection)
|
||||||
|
{
|
||||||
|
$totalPlayers = $filteredCollection->count() + 1;
|
||||||
|
return (12 * 60 * 60) / $totalPlayers + rand(0, 2000);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function nextGameDateTime(&$d, $filteredPlayers, $withinSameMatch = false) {
|
||||||
|
if ($withinSameMatch) {
|
||||||
|
$deltaSegundos = rand(300, 900);
|
||||||
|
} else {
|
||||||
|
$deltaSegundos = $this->calculateRandomSeconds($filteredPlayers);
|
||||||
|
}
|
||||||
|
$d->addSeconds($deltaSegundos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$this->command->info("Games seeder - Start");
|
||||||
|
|
||||||
|
$start = DB::table('users')->where('type', 'P')->min('created_at');
|
||||||
|
|
||||||
|
$allPlayers = DB::table('users')->where('type', 'P')->get();
|
||||||
|
$sortedPlayers = $allPlayers->sortBy('created_at')->values();
|
||||||
|
|
||||||
|
$d = new \Carbon\Carbon($start);
|
||||||
|
$d = $d->addDay();
|
||||||
|
$now = \Carbon\Carbon::now();
|
||||||
|
$this->command->info("Starting to create games");
|
||||||
|
|
||||||
|
$games = [];
|
||||||
|
$matches = [];
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
|
||||||
|
$filteredPlayers = null;
|
||||||
|
$filteredPlayersIds = null;
|
||||||
|
$nextCreatedAt = null;
|
||||||
|
|
||||||
|
while ($d->lte($now)) {
|
||||||
|
$i++;
|
||||||
|
if (($filteredPlayers === null) || ($nextCreatedAt === null) ||
|
||||||
|
($d->gte($nextCreatedAt))
|
||||||
|
) {
|
||||||
|
$filteredPlayers = $allPlayers->filter(function ($value) use ($d) {
|
||||||
|
return $d->gt($value->created_at);
|
||||||
|
});
|
||||||
|
$nextCreatedAtPlayer = $sortedPlayers->first(function ($value) use ($d) {
|
||||||
|
return $d->lte($value->created_at);
|
||||||
|
});
|
||||||
|
$nextCreatedAt = $nextCreatedAtPlayer ? $nextCreatedAtPlayer->created_at : new \Carbon\Carbon('9999-12-31');
|
||||||
|
$filteredPlayersIds = $filteredPlayers->pluck('id')->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only creates games or matches when there are enough players
|
||||||
|
if ($filteredPlayersIds === null || empty($filteredPlayersIds) || count($filteredPlayersIds) <2) {
|
||||||
|
$this->nextGameDateTime($d, $filteredPlayers);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$userIdKeys = array_rand($filteredPlayersIds, 2);
|
||||||
|
$userIDPlayer1 = $filteredPlayersIds[$userIdKeys[0]];
|
||||||
|
$userIDPlayer2 = $filteredPlayersIds[$userIdKeys[1]];
|
||||||
|
|
||||||
|
if ($userIDPlayer1 == $userIDPlayer2) {
|
||||||
|
$this->nextGameDateTime($d, $filteredPlayers);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$match = null;
|
||||||
|
if (rand(1, $this->ratioStandaloneToMatch) === 1) {
|
||||||
|
$match = $this->newMatch($filteredPlayers, $userIDPlayer1, $userIDPlayer2, $d);
|
||||||
|
$playersMarks = [0, 0];
|
||||||
|
$playersPoints = [0, 0];
|
||||||
|
while($playersMarks[0] < 4 && $playersMarks[1] <4) {
|
||||||
|
$newGame = $this->newGame($filteredPlayers, $match, $userIDPlayer1, $userIDPlayer2, $d);
|
||||||
|
$games[] = $newGame;
|
||||||
|
$playersPoints[0] += $newGame['player1_points'];
|
||||||
|
$playersPoints[1] += $newGame['player2_points'];
|
||||||
|
if ($newGame['player1_points'] > $newGame['player2_points']) {
|
||||||
|
if ($newGame['player1_points'] >= 120) {
|
||||||
|
$playersMarks[0]+= 4;
|
||||||
|
} elseif ($newGame['player1_points'] >= 91) {
|
||||||
|
$playersMarks[0] += 2;
|
||||||
|
} else {
|
||||||
|
$playersMarks[0]++;
|
||||||
|
}
|
||||||
|
} elseif ($newGame['player2_points'] > $newGame['player1_points']) {
|
||||||
|
if ($newGame['player2_points'] >= 120) {
|
||||||
|
$playersMarks[1]+= 4;
|
||||||
|
} elseif ($newGame['player2_points'] >= 91) {
|
||||||
|
$playersMarks[1] += 2;
|
||||||
|
} else {
|
||||||
|
$playersMarks[1]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->updateMatchWinner($match, $playersMarks[0], $playersMarks[1], $playersPoints[0], $playersPoints[1], $d);
|
||||||
|
$matches[] = $match;
|
||||||
|
} else {
|
||||||
|
$newGame = $this->newGame($filteredPlayers, $match, $userIDPlayer1, $userIDPlayer2, $d);
|
||||||
|
$games[] = $newGame;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($i >= DatabaseSeeder::$dbInsertBlockSize) {
|
||||||
|
if (!empty($matches)) {
|
||||||
|
DB::table('matches')->insert($matches);
|
||||||
|
$this->command->info("Saved " . count($matches) . " matches at date " . $d->format('Y-m-d H:i:s'));
|
||||||
|
}
|
||||||
|
if (!empty($games)) {
|
||||||
|
DB::table('games')->insert($games);
|
||||||
|
$this->command->info("Saved " . count($games) . " games at date " . $d->format('Y-m-d H:i:s'));
|
||||||
|
}
|
||||||
|
$i = 0;
|
||||||
|
$games = [];
|
||||||
|
$matches = [];
|
||||||
|
}
|
||||||
|
//$this->nextGameDateTime($d, $filteredPlayers);
|
||||||
|
}
|
||||||
|
if (!empty($matches)) {
|
||||||
|
DB::table('matches')->insert($matches);
|
||||||
|
$this->command->info("Saved " . count($matches) . " matches at date " . $d->format('Y-m-d H:i:s'));
|
||||||
|
}
|
||||||
|
if (!empty($games)) {
|
||||||
|
DB::table('games')->insert($games);
|
||||||
|
$this->command->info("Saved " . count($games) . " games at date " . $d->format('Y-m-d H:i:s'));
|
||||||
|
}
|
||||||
|
$this->command->info("Games seeder - End");
|
||||||
|
}
|
||||||
|
|
||||||
|
private $matchID = 0;
|
||||||
|
private function newMatch($filteredPlayers,$user1, $user2, $d)
|
||||||
|
{
|
||||||
|
$this->matchID++;
|
||||||
|
$this->nextGameDateTime($d, $filteredPlayers);
|
||||||
|
return [
|
||||||
|
'id' => $this->matchID,
|
||||||
|
'type' => random_int(1,2) == 1 ? '3' : '9',
|
||||||
|
'player1_user_id' => $user1,
|
||||||
|
'player2_user_id' => $user2,
|
||||||
|
'winner_user_id' => null,
|
||||||
|
'loser_user_id' => null,
|
||||||
|
'status' => 'Ended',
|
||||||
|
'stake' => random_int(1,4) > 1 ? 3 : random_int(4,100),
|
||||||
|
'began_at' => $d->copy(),
|
||||||
|
'ended_at' => null,
|
||||||
|
'total_time' => null,
|
||||||
|
'player1_marks' => null,
|
||||||
|
'player2_marks' => null,
|
||||||
|
'player1_points' => null,
|
||||||
|
'player2_points' => null,
|
||||||
|
'custom' => null
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateMatchWinner(&$match, $player1Marks, $player2Marks, $totalPlayers1, $totalPlayers2, $d)
|
||||||
|
{
|
||||||
|
$match['player1_marks'] = $player1Marks;
|
||||||
|
$match['player2_marks'] = $player2Marks;
|
||||||
|
$match['player1_points'] = $totalPlayers1;
|
||||||
|
$match['player2_points'] = $totalPlayers2;
|
||||||
|
$match['ended_at'] = $d->copy();
|
||||||
|
$match['total_time'] = $match['began_at']->diffInSeconds($match['ended_at']);
|
||||||
|
$match['winner_user_id'] = $player1Marks > $player2Marks ? $match['player1_user_id'] : ($player2Marks > $player1Marks ? $match['player2_user_id'] : null);
|
||||||
|
$match['loser_user_id'] = $player1Marks > $player2Marks ? $match['player2_user_id'] : ($player2Marks > $player1Marks ? $match['player1_user_id'] : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private $gameID = 0;
|
||||||
|
private function newGame($filteredPlayers, $match, $user1, $user2, $d)
|
||||||
|
{
|
||||||
|
$this->gameID++;
|
||||||
|
$this->nextGameDateTime($d, $filteredPlayers, $match != null);
|
||||||
|
$begin_d = $d->copy();
|
||||||
|
$pointsUser1 = 60;
|
||||||
|
$pointsUser2 = 60;
|
||||||
|
// if random == 1 it is a draw
|
||||||
|
if (random_int(1,30) > 1) {
|
||||||
|
$pointsUser1 = rand(0, 120);
|
||||||
|
$pointsUser2 = 120 - $pointsUser1;
|
||||||
|
}
|
||||||
|
$duration = random_int(200, 900);
|
||||||
|
$d->addSeconds($duration);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'id' => $this->gameID,
|
||||||
|
'type' => $match ? $match['type'] : (random_int(1, 2) == 1 ? '3' : '9'),
|
||||||
|
'match_id' => $match ? $match['id'] : null,
|
||||||
|
'player1_user_id' => $user1,
|
||||||
|
'player2_user_id' => $user2,
|
||||||
|
'is_draw' => $pointsUser1 == $pointsUser2,
|
||||||
|
'winner_user_id' => $pointsUser1 > $pointsUser2 ? $user1 : ($pointsUser2 > $pointsUser1 ? $user2 : null),
|
||||||
|
'loser_user_id' => $pointsUser1 < $pointsUser2 ? $user1 : ($pointsUser2 < $pointsUser1 ? $user2 : null),
|
||||||
|
'status' => 'Ended',
|
||||||
|
'began_at' => $begin_d,
|
||||||
|
'ended_at' => $d->copy(),
|
||||||
|
'total_time' => $duration,
|
||||||
|
'player1_points' => $pointsUser1,
|
||||||
|
'player2_points' => $pointsUser2,
|
||||||
|
'custom' => null
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,265 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class GamesTransactionsSeeder extends Seeder
|
||||||
|
{
|
||||||
|
private $faker = null;
|
||||||
|
private $users = null;
|
||||||
|
|
||||||
|
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$this->command->info("Create game related Transactions");
|
||||||
|
$this->faker = \Faker\Factory::create(DatabaseSeeder::$seedLanguage);
|
||||||
|
|
||||||
|
$this->users = DB::table('users')->orderBy('id', 'asc')->get();
|
||||||
|
|
||||||
|
$games = DB::table('games')->whereNull('match_id')->orderBy('began_at', 'asc')->get();
|
||||||
|
$matches = DB::table('matches')->orderBy('began_at', 'asc')->get();
|
||||||
|
|
||||||
|
$coin_purchases = [];
|
||||||
|
$coin_transactions = [];
|
||||||
|
$i = 0;
|
||||||
|
while($games->count() > 0 || $matches->count() > 0) {
|
||||||
|
$firstGame = $games->first();
|
||||||
|
$firstMatch = $matches->first();
|
||||||
|
$isGameFirst = false;
|
||||||
|
if ($firstGame && $firstMatch) {
|
||||||
|
$isGameFirst = $firstGame->began_at < $firstMatch->began_at;
|
||||||
|
} elseif ($firstGame && !$firstMatch) {
|
||||||
|
$isGameFirst = true;
|
||||||
|
} elseif (!$firstGame && $firstMatch) {
|
||||||
|
$isGameFirst = false;
|
||||||
|
}
|
||||||
|
if ($isGameFirst) {
|
||||||
|
$this->handleGame($coin_purchases, $coin_transactions, $firstGame);
|
||||||
|
$games->shift();
|
||||||
|
} else {
|
||||||
|
$this->handleMatch($coin_purchases, $coin_transactions, $firstMatch);
|
||||||
|
$matches->shift();
|
||||||
|
}
|
||||||
|
|
||||||
|
$i++;
|
||||||
|
if ($i >= DatabaseSeeder::$dbInsertBlockSize) {
|
||||||
|
$this->command->info("Block of coin purchases and transactions");
|
||||||
|
if (!empty($coin_transactions)) {
|
||||||
|
DB::table('coin_transactions')->insert($coin_transactions);
|
||||||
|
$this->command->info("Saved " . count($coin_transactions) . " coin transactions");
|
||||||
|
}
|
||||||
|
if (!empty($coin_purchases)) {
|
||||||
|
DB::table('coin_purchases')->insert($coin_purchases);
|
||||||
|
$this->command->info("Saved " . count($coin_purchases) . " coin purchases");
|
||||||
|
}
|
||||||
|
$i = 0;
|
||||||
|
$coin_purchases = [];
|
||||||
|
$coin_transactions = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->command->info("Last block of coin purchases and transactions");
|
||||||
|
if (!empty($coin_transactions)) {
|
||||||
|
DB::table('coin_transactions')->insert($coin_transactions);
|
||||||
|
$this->command->info("Saved " . count($coin_transactions) . " coin transactions");
|
||||||
|
}
|
||||||
|
if (!empty($coin_purchases)) {
|
||||||
|
DB::table('coin_purchases')->insert($coin_purchases);
|
||||||
|
$this->command->info("Saved " . count($coin_purchases) . " coin purchases");
|
||||||
|
}
|
||||||
|
$this->updateAllUsersBalance();
|
||||||
|
$this->command->info("Game related Transactions Created Successfully.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTimeStampFromDB($strDateFromDB, $minusOneSecond = false)
|
||||||
|
{
|
||||||
|
if ($minusOneSecond) {
|
||||||
|
return date("Y-m-d H:i:s", strtotime($strDateFromDB . " - 1 second"));
|
||||||
|
}
|
||||||
|
return date("Y-m-d H:i:s", strtotime($strDateFromDB));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function getRandomPayment($user, &$type, &$ref): void
|
||||||
|
{
|
||||||
|
$type = $this->faker->randomElement(['MBWAY', 'PAYPAL', 'IBAN', 'MB', 'VISA']);
|
||||||
|
switch ($type) {
|
||||||
|
case 'MBWAY':
|
||||||
|
$ref = rand(910000000, 999999999);
|
||||||
|
break;
|
||||||
|
case 'PAYPAL':
|
||||||
|
$ref = $user->email;
|
||||||
|
break;
|
||||||
|
case 'IBAN':
|
||||||
|
$ref = 'PT' . rand(5000000000000, 9999999999999) . rand(1000000000, 9999999999);
|
||||||
|
break;
|
||||||
|
case 'MB':
|
||||||
|
$ref = rand(10000, 99999) . '-' . rand(100000000, 999999999);
|
||||||
|
break;
|
||||||
|
case 'VISA':
|
||||||
|
$ref = rand(40000000, 49999999) . rand(10000000, 99999999);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private $purchaseID = null;
|
||||||
|
private function addPurchase(&$coin_purchases, &$coin_transactions, &$user, $time, $eurosAmount)
|
||||||
|
{
|
||||||
|
$this->purchaseID++;
|
||||||
|
$coins = $eurosAmount * 10;
|
||||||
|
|
||||||
|
$newCoinTransaction = $this->addCoinTransaction($coin_transactions, $user, $coins, 2, $time, null, null);
|
||||||
|
|
||||||
|
$this->getRandomPayment($user, $paymentType, $paymentRef);
|
||||||
|
|
||||||
|
$coin_purchases[] = [
|
||||||
|
'id' => $this->purchaseID,
|
||||||
|
'purchase_datetime' => $time,
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'coin_transaction_id' => $newCoinTransaction['id'],
|
||||||
|
'euros' => $eurosAmount,
|
||||||
|
'payment_type' => $paymentType,
|
||||||
|
'payment_reference' => $paymentRef,
|
||||||
|
'custom' => null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private $transactionID = 0;
|
||||||
|
private function addCoinTransaction(&$coin_transactions, &$user, $coins, $type, $time, $game_id = null, $match_id = null)
|
||||||
|
{
|
||||||
|
if ($this->transactionID == null) {
|
||||||
|
$this->transactionID = DB::table('coin_transactions')->max('id');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Types of coin transactions:
|
||||||
|
// 1 - Bonus
|
||||||
|
// 2 - Coin purchase
|
||||||
|
// 3 - Game fee - requires game_id
|
||||||
|
// 4 - Match stake - requires match_id
|
||||||
|
// 5 - Game payout - requires game_id
|
||||||
|
// 6 - Match payout - requires match_id
|
||||||
|
if(($type == 3 || $type == 5) && $game_id === null) {
|
||||||
|
throw new \Exception("Game ID is required for coin transaction type $type");
|
||||||
|
}
|
||||||
|
if(($type == 4 || $type == 6) && $match_id === null) {
|
||||||
|
throw new \Exception("Match ID is required for coin transaction type $type");
|
||||||
|
}
|
||||||
|
$this->transactionID++;
|
||||||
|
|
||||||
|
$coins = abs($coins);
|
||||||
|
if (in_array($type, [3, 4])) {
|
||||||
|
$coins *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
$newCoinTransaction = [
|
||||||
|
'id' => $this->transactionID,
|
||||||
|
'transaction_datetime' => $time,
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'match_id' => $match_id, // nullable
|
||||||
|
'game_id' => $game_id, // nullable
|
||||||
|
'coin_transaction_type_id' => $type,
|
||||||
|
'coins' => $coins,
|
||||||
|
'custom' => null,
|
||||||
|
];
|
||||||
|
$coin_transactions[] = $newCoinTransaction;
|
||||||
|
$user->coins_balance += $coins;
|
||||||
|
return $newCoinTransaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function calculateGamePayouts($game)
|
||||||
|
{
|
||||||
|
$payouts = [0, 0];
|
||||||
|
if ($game->player1_points == $game->player2_points) {
|
||||||
|
$payouts = [1, 1];
|
||||||
|
} elseif ($game->player1_points == 120) {
|
||||||
|
$payouts = [6, 0];
|
||||||
|
} elseif ($game->player2_points == 120) {
|
||||||
|
$payouts = [0, 6];
|
||||||
|
} elseif ($game->player1_points >= 91) {
|
||||||
|
$payouts = [4, 0];
|
||||||
|
} elseif ($game->player2_points >= 91) {
|
||||||
|
$payouts = [0, 4];
|
||||||
|
} elseif ($game->player1_points >= 61) {
|
||||||
|
$payouts = [3, 0];
|
||||||
|
} elseif ($game->player2_points >= 61) {
|
||||||
|
$payouts = [0, 3];
|
||||||
|
} else { // this condition should never be reached
|
||||||
|
$payouts = [0, 0];
|
||||||
|
}
|
||||||
|
return $payouts;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function calculateMatchPayouts($match)
|
||||||
|
{
|
||||||
|
$payouts = [0, 0];
|
||||||
|
if ($match->winner_user_id == $match->player1_user_id) {
|
||||||
|
$payouts = [$match->stake * 2 - 1, 0];
|
||||||
|
} elseif ($match->winner_user_id == $match->player2_user_id) {
|
||||||
|
$payouts = [0, $match->stake * 2 - 1];
|
||||||
|
}
|
||||||
|
return $payouts;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function handleGame(&$coin_purchases, &$coin_transactions, $game)
|
||||||
|
{
|
||||||
|
$player1 = $this->users->where('id', $game->player1_user_id)->first();
|
||||||
|
$player2 = $this->users->where('id', $game->player2_user_id)->first();
|
||||||
|
// not enough coins to play - must purchase before the game
|
||||||
|
$timeOneSecondBefore = $this->getTimeStampFromDB($game->began_at, true);
|
||||||
|
if ($player1->coins_balance < 2) {
|
||||||
|
$this->addPurchase($coin_purchases, $coin_transactions, $player1, $timeOneSecondBefore, random_int(1, 10));
|
||||||
|
}
|
||||||
|
// not enough coins to play - must purchase before the game
|
||||||
|
if ($player2->coins_balance < 2) {
|
||||||
|
$this->addPurchase($coin_purchases, $coin_transactions, $player2, $timeOneSecondBefore, random_int(1, 10));
|
||||||
|
}
|
||||||
|
$this->addCoinTransaction($coin_transactions, $player1, -2, 3, $this->getTimeStampFromDB($game->began_at, false), $game->id);
|
||||||
|
$this->addCoinTransaction($coin_transactions, $player2, -2, 3, $this->getTimeStampFromDB($game->began_at, false), $game->id);
|
||||||
|
$payouts = $this->calculateGamePayouts($game);
|
||||||
|
if ($payouts[0] > 0) {
|
||||||
|
$this->addCoinTransaction($coin_transactions, $player1, $payouts[0], 5, $this->getTimeStampFromDB($game->ended_at, false), $game->id);
|
||||||
|
}
|
||||||
|
if ($payouts[1] > 0) {
|
||||||
|
$this->addCoinTransaction($coin_transactions, $player2, $payouts[1], 5, $this->getTimeStampFromDB($game->ended_at, false), $game->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function handleMatch(&$coin_purchases, &$coin_transactions, $match)
|
||||||
|
{
|
||||||
|
$player1 = $this->users->where('id', $match->player1_user_id)->first();
|
||||||
|
$player2 = $this->users->where('id', $match->player2_user_id)->first();
|
||||||
|
// not enough coins to play - must purchase before the match
|
||||||
|
$timeOneSecondBefore = $this->getTimeStampFromDB($match->began_at, true);
|
||||||
|
if ($player1->coins_balance < $match->stake) {
|
||||||
|
$requiredCoins = $match->stake - $player1->coins_balance;
|
||||||
|
$euros = ceil(($requiredCoins + 3) / 10);
|
||||||
|
$this->addPurchase($coin_purchases, $coin_transactions, $player1, $timeOneSecondBefore, $euros);
|
||||||
|
}
|
||||||
|
// not enough coins to play - must purchase before the match
|
||||||
|
if ($player2->coins_balance < $match->stake) {
|
||||||
|
$requiredCoins = $match->stake - $player2->coins_balance;
|
||||||
|
$euros = ceil(($requiredCoins + 3) / 10);
|
||||||
|
$this->addPurchase($coin_purchases, $coin_transactions, $player2, $timeOneSecondBefore, $euros);
|
||||||
|
}
|
||||||
|
$this->addCoinTransaction($coin_transactions, $player1, $match->stake * -1, 4, $this->getTimeStampFromDB($match->began_at, false), null, $match->id);
|
||||||
|
$this->addCoinTransaction($coin_transactions, $player2, $match->stake * -1, 4, $this->getTimeStampFromDB($match->began_at, false), null, $match->id);
|
||||||
|
$payouts = $this->calculateMatchPayouts($match);
|
||||||
|
if ($payouts[0] > 0) {
|
||||||
|
$this->addCoinTransaction($coin_transactions, $player1, $payouts[0], 6, $this->getTimeStampFromDB($match->ended_at, false), null, $match->id);
|
||||||
|
}
|
||||||
|
if ($payouts[1] > 0) {
|
||||||
|
$this->addCoinTransaction($coin_transactions, $player2, $payouts[1], 6, $this->getTimeStampFromDB($match->ended_at, false), null, $match->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateAllUsersBalance()
|
||||||
|
{
|
||||||
|
$this->command->info("Update users balances...");
|
||||||
|
foreach ($this->users as $user) {
|
||||||
|
DB::table('users')->where('id', $user->id)->update(['coins_balance' => $user->coins_balance]);
|
||||||
|
}
|
||||||
|
$this->command->info("All users balances updated.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
|
class InitialTransactionsSeeder extends Seeder
|
||||||
|
{
|
||||||
|
private $allPlayers;
|
||||||
|
private $sizeOfSaveBlock = 200;
|
||||||
|
private $valueOfInitialBonus = 10;
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$this->allPlayers = DB::table('users')->where('type', 'P')->get();
|
||||||
|
$this->command->info("Create initial Bonuses transactions");
|
||||||
|
|
||||||
|
$i = $this->sizeOfSaveBlock;
|
||||||
|
$dataBlock = [];
|
||||||
|
foreach ($this->allPlayers as $player) {
|
||||||
|
if ($i <= 0) {
|
||||||
|
$this->saveDataBlock($dataBlock);
|
||||||
|
$i = $this->sizeOfSaveBlock;
|
||||||
|
$dataBlock = [];
|
||||||
|
}
|
||||||
|
$dataBlock[] = $this->createBonusTransaction($player);
|
||||||
|
$i--;
|
||||||
|
}
|
||||||
|
if (!empty($dataBlock)) {
|
||||||
|
$this->saveDataBlock($dataBlock);
|
||||||
|
}
|
||||||
|
$this->updateAllPlayerBalances();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function saveDataBlock($dataBlock): void
|
||||||
|
{
|
||||||
|
$size = count($dataBlock);
|
||||||
|
DB::table('coin_transactions')->insert($dataBlock);
|
||||||
|
$this->command->info("Saved $size initial bonuses transactions");
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createBonusTransaction($player): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'transaction_datetime' => $player->created_at,
|
||||||
|
'user_id' => $player->id,
|
||||||
|
'match_id' => null,
|
||||||
|
'game_id' => null,
|
||||||
|
'coin_transaction_type_id' => 1,
|
||||||
|
'coins' => $this->valueOfInitialBonus,
|
||||||
|
'custom' => null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateAllPlayerBalances(): void
|
||||||
|
{
|
||||||
|
DB::table('users')->where('type', 'P')->update(['coins_balance' => $this->valueOfInitialBonus]);
|
||||||
|
$this->command->info("Updated all players initial Balance");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,149 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\File;
|
||||||
|
|
||||||
|
trait SeederUtils
|
||||||
|
{
|
||||||
|
private static $used_emails = [];
|
||||||
|
|
||||||
|
public function randomString(int $length = 10): string
|
||||||
|
{
|
||||||
|
return substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, $length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function integerAsStringWithPadding(int $num, int $length = 10): string
|
||||||
|
{
|
||||||
|
return str_pad((string) $num, $length, '0', STR_PAD_LEFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function randomIntegerAsStringWithPadding(int $min = 0, int $max = 100, int $length = 10): string
|
||||||
|
{
|
||||||
|
return $this->integerAsStringWithPadding($this->mt_rand($min, $max), $length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function randomDecimal(float $min, float $max, int $decimals = 2): float
|
||||||
|
{
|
||||||
|
$factor = pow(10, $decimals);
|
||||||
|
return mt_rand($min * $factor, $max * $factor) / $factor;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function getFileNameFromString($str, $extension)
|
||||||
|
{
|
||||||
|
return str_replace(' ', '_', strtolower($this->stripAccents($str))) . ".$extension";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stripAccents($stripAccents)
|
||||||
|
{
|
||||||
|
$from = 'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ';
|
||||||
|
$to = 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY';
|
||||||
|
$keys = array();
|
||||||
|
$values = array();
|
||||||
|
preg_match_all('/./u', $from, $keys);
|
||||||
|
preg_match_all('/./u', $to, $values);
|
||||||
|
$mapping = array_combine($keys[0], $values[0]);
|
||||||
|
return strtr($stripAccents, $mapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function strtr_utf8($str, $from, $to)
|
||||||
|
{
|
||||||
|
$keys = array();
|
||||||
|
$values = array();
|
||||||
|
preg_match_all('/./u', $from, $keys);
|
||||||
|
preg_match_all('/./u', $to, $values);
|
||||||
|
$mapping = array_combine($keys[0], $values[0]);
|
||||||
|
return strtr($str, $mapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function randomName($faker, &$gender, &$fullname, &$email, $allowRepeated = false)
|
||||||
|
{
|
||||||
|
$gender = $gender ?? $faker->randomElement(['male', 'female']);
|
||||||
|
$firstname = $faker->firstName($gender);
|
||||||
|
$lastname = $faker->lastName();
|
||||||
|
$secondname = mt_rand(1, 3) == 2 ? "" : " " . $faker->firstName($gender);
|
||||||
|
$number_middlenames = $faker->randomElement([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3]);
|
||||||
|
$middlenames = "";
|
||||||
|
for ($i = 0; $i < $number_middlenames; $i++) {
|
||||||
|
$middlenames .= " " . $faker->lastName();
|
||||||
|
}
|
||||||
|
$fullname = $firstname . $secondname . $middlenames . " " . $lastname;
|
||||||
|
$email = strtolower($this->stripAccents($firstname) . "." . $this->stripAccents($lastname) . "@mail.pt");
|
||||||
|
if (!$allowRepeated) {
|
||||||
|
$i = 2;
|
||||||
|
while (in_array($email, self::$used_emails)) {
|
||||||
|
$email = strtolower($this->stripAccents($firstname) . "." . $this->stripAccents($lastname) . "." . $i ."@mail.pt");
|
||||||
|
$i++;
|
||||||
|
if ($i > 10) {
|
||||||
|
$this->command->error("Repeated email - not possible to create a unique email for $fullname!");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self::$used_emails[] = $email;
|
||||||
|
$gender = $gender == 'male' ? 'M' : 'F';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function ramdomPaymentMethod($email, &$paymentType, &$paymentReference)
|
||||||
|
{
|
||||||
|
$paymentType = $this->faker->randomElement(['Visa', 'PayPal', 'MB WAY']);
|
||||||
|
$paymentReference = match ($paymentType) {
|
||||||
|
'Visa' => mt_rand(4, 6) .
|
||||||
|
$this->faker->randomNumber($nbDigits = 8, $strict = true) .
|
||||||
|
$this->faker->randomNumber($nbDigits = 7, $strict = true),
|
||||||
|
'PayPal' => $email,
|
||||||
|
'MB WAY' => '9' . $this->faker->randomNumber($nbDigits = 8, $strict = true)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cleanStorageFolder($folder, $public = true)
|
||||||
|
{
|
||||||
|
$storagePath = $public ? storage_path("app/public/$folder") : storage_path("app/private/$folder");
|
||||||
|
if (File::exists($storagePath)) {
|
||||||
|
File::deleteDirectory($storagePath);
|
||||||
|
}
|
||||||
|
if (!File::exists(storage_path("app"))) {
|
||||||
|
File::makeDirectory(storage_path("app"));
|
||||||
|
}
|
||||||
|
if ($public) {
|
||||||
|
if (!File::exists(storage_path("app/public"))) {
|
||||||
|
File::makeDirectory(storage_path("app/public"));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!File::exists(storage_path("app/private"))) {
|
||||||
|
File::makeDirectory(storage_path("app/private"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
File::makeDirectory($storagePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function copyFileToStorage($originalFolder, $originalFileName, $folder, $id = null, $public = true)
|
||||||
|
{
|
||||||
|
$originalFullName = database_path("seeders/$originalFolder") . '/' . $originalFileName;
|
||||||
|
$storagePath = $public ? storage_path("app/public/$folder") : storage_path("app/private/$folder");
|
||||||
|
$prefix = $id ? $this->integerAsStringWithPadding($id, 5) . '_' : '';
|
||||||
|
$ext = pathinfo($originalFullName, PATHINFO_EXTENSION);
|
||||||
|
$newFileName = $prefix . $this->randomString(10) . '.' . $ext;
|
||||||
|
if (!File::exists($storagePath)) {
|
||||||
|
File::makeDirectory($storagePath);
|
||||||
|
}
|
||||||
|
if (File::exists($originalFullName)) {
|
||||||
|
File::copy($originalFullName, "$storagePath/$newFileName");
|
||||||
|
return $newFileName;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function directCopyFileToStorage($originalFolder, $originalFileName, $folder, $public = true)
|
||||||
|
{
|
||||||
|
$originalFullName = database_path("seeders/$originalFolder") . '/' . $originalFileName;
|
||||||
|
$storagePath = $public ? storage_path("app/public/$folder") : storage_path("app/private/$folder");
|
||||||
|
if (!File::exists($storagePath)) {
|
||||||
|
File::makeDirectory($storagePath);
|
||||||
|
}
|
||||||
|
File::copy($originalFullName, "$storagePath/$originalFileName");
|
||||||
|
return $originalFileName;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class TransactionTypesSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$this->command->line("Creating Coin Transaction Types.");
|
||||||
|
|
||||||
|
// Add default coin_transaction_types
|
||||||
|
DB::table('coin_transaction_types')->insert([
|
||||||
|
['id' => 1, 'name' => 'Bonus', 'type' => 'C'], // id = 1
|
||||||
|
['id' => 2, 'name' => 'Coin purchase', 'type' => 'C'], // id = 2
|
||||||
|
['id' => 3, 'name' => 'Game fee', 'type' => 'D'], // id = 3
|
||||||
|
['id' => 4, 'name' => 'Match stake', 'type' => 'D'], // id = 4
|
||||||
|
['id' => 5, 'name' => 'Game payout', 'type' => 'C'], // id = 5
|
||||||
|
['id' => 6, 'name' => 'Match payout', 'type' => 'C'], // id = 6
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->command->line("Coin Transaction Types Created Successfully.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Faker\Factory;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\File;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
|
class UsersSeeder extends Seeder
|
||||||
|
{
|
||||||
|
use SeederUtils;
|
||||||
|
|
||||||
|
private $faker = null;
|
||||||
|
private $files_M = [];
|
||||||
|
private $files_F = [];
|
||||||
|
public static $hashPasword = "";
|
||||||
|
public static $dbUsers = null;
|
||||||
|
|
||||||
|
|
||||||
|
public static $fixedUsers = [
|
||||||
|
['type' => 'A', 'name' => 'First Administrator', 'email' => '[email protected]', 'gender' => 'M', 'softdelete' => false],
|
||||||
|
['type' => 'A', 'name' => 'Second Administrator', 'email' => '[email protected]', 'gender' => 'F', 'softdelete' => false],
|
||||||
|
['type' => 'A', 'name' => 'Third Administrator', 'email' => '[email protected]', 'gender' => 'M', 'softdelete' => false],
|
||||||
|
['type' => 'A', 'name' => 'Forth Administrator', 'email' => '[email protected]', 'gender' => 'M', 'softdelete' => true],
|
||||||
|
|
||||||
|
['type' => 'P', 'name' => 'Player A', 'email' => '[email protected]', 'gender' => 'M', 'softdelete' => false],
|
||||||
|
['type' => 'P', 'name' => 'Player B', 'email' => '[email protected]', 'gender' => 'M', 'softdelete' => false],
|
||||||
|
['type' => 'P', 'name' => 'Player C', 'email' => '[email protected]', 'gender' => 'F', 'softdelete' => false],
|
||||||
|
['type' => 'P', 'name' => 'Player D', 'email' => '[email protected]', 'gender' => 'M', 'softdelete' => true],
|
||||||
|
['type' => 'P', 'name' => 'Player E', 'email' => '[email protected]', 'gender' => 'F', 'softdelete' => false],
|
||||||
|
['type' => 'P', 'name' => 'Player F', 'email' => '[email protected]', 'gender' => 'M', 'softdelete' => true],
|
||||||
|
];
|
||||||
|
|
||||||
|
public static $userTypes = [
|
||||||
|
'A' => 10,
|
||||||
|
'P' => 500];
|
||||||
|
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$this->command->line("Creating Users.");
|
||||||
|
self::$hashPasword = bcrypt('123');
|
||||||
|
|
||||||
|
//$this->faker = Factory::create('pt_PT');
|
||||||
|
$this->faker = Factory::create(DatabaseSeeder::$seedLanguage);
|
||||||
|
|
||||||
|
$this->cleanStorageFolder('photos_avatars');
|
||||||
|
|
||||||
|
$this->addUsersToDatabase();
|
||||||
|
$this->addPhotoFiles();
|
||||||
|
DB::table('users')->where('type', 'A')->update(['nickname' => null]);
|
||||||
|
self::$dbUsers = DB::table('users')->orderBy('id')->get();
|
||||||
|
$this->command->line("Users Created Successfully.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addUsersToDatabase()
|
||||||
|
{
|
||||||
|
$this->command->line("Adding users to the database");
|
||||||
|
$usersAdded = Self::$fixedUsers;
|
||||||
|
foreach (self::$userTypes as $userType => $totalUsers) {
|
||||||
|
for ($i = 0; $i < $totalUsers; $i++) {
|
||||||
|
$gender = null;
|
||||||
|
$name = null;
|
||||||
|
$email = null;
|
||||||
|
$this->randomName($this->faker, $gender, $name, $email, false);
|
||||||
|
$usersAdded[] = [
|
||||||
|
'type' => $userType,
|
||||||
|
'name' => $name,
|
||||||
|
'email' => $email,
|
||||||
|
'gender' => $gender,
|
||||||
|
'softdelete' => random_int(1, 20) == 1,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$createdDate = DatabaseSeeder::$startDate->copy()->addMinutes(mt_rand(20000, 100000));
|
||||||
|
foreach ($usersAdded as $key => $user) {
|
||||||
|
$usersAdded[$key]['password'] = self::$hashPasword;
|
||||||
|
$createdDate = $createdDate->copy()->addMinutes(mt_rand(10, 60));
|
||||||
|
$usersAdded[$key]['created_at'] = $createdDate;
|
||||||
|
$usersAdded[$key]['email_verified_at'] = $createdDate->copy()->addMinutes(mt_rand(1, 9));
|
||||||
|
if (random_int(1, 7) > 1) {
|
||||||
|
$usersAdded[$key]['updated_at'] = $createdDate->copy()->addMinutes(mt_rand(10, 10000));
|
||||||
|
} else {
|
||||||
|
$usersAdded[$key]['updated_at'] = $createdDate;
|
||||||
|
}
|
||||||
|
$usersAdded[$key]['nickname'] = ($user['gender'] == 'M' ? 'Mickey' : 'Minnie') . ($key + 1) ;
|
||||||
|
$usersAdded[$key]['blocked'] = false;
|
||||||
|
$usersAdded[$key]['photo_avatar_filename'] = null;
|
||||||
|
$usersAdded[$key]['coins_balance'] = 0;
|
||||||
|
$usersAdded[$key]['deleted_at'] = null;
|
||||||
|
if ($usersAdded[$key]['softdelete']) {
|
||||||
|
$usersAdded[$key]['deleted_at'] = $createdDate->copy()->addMinutes(mt_rand(10001, 100000));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$arrayToStore = [];
|
||||||
|
foreach ($usersAdded as $user) {
|
||||||
|
unset($user['gender']);
|
||||||
|
unset($user['softdelete']);
|
||||||
|
$arrayToStore[] = $user;
|
||||||
|
|
||||||
|
if (count($arrayToStore) >= DatabaseSeeder::$dbInsertBlockSize) {
|
||||||
|
DB::table('users')->insert($arrayToStore);
|
||||||
|
$this->command->line("Created " . count($arrayToStore) . " users");
|
||||||
|
$arrayToStore = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (count($arrayToStore) >= 1) {
|
||||||
|
DB::table('users')->insert($arrayToStore);
|
||||||
|
$this->command->line("Created " . count($arrayToStore) . " users");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->command->line("Total users created: " . DB::table('users')->count());
|
||||||
|
self::$dbUsers = DB::table('users')->get();
|
||||||
|
|
||||||
|
print_r(self::$dbUsers);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addPhotoFiles()
|
||||||
|
{
|
||||||
|
$this->command->line("Copying users' photos");
|
||||||
|
$this->fillPhotoFilesNames();
|
||||||
|
$total = count($this->files_M) + count($this->files_F);
|
||||||
|
$sortedUsers = self::$dbUsers->sortBy(function (stdClass $user) {
|
||||||
|
if ($user->id <= 10 ) {
|
||||||
|
return $user->id;
|
||||||
|
}
|
||||||
|
return match($user->type) {
|
||||||
|
'A' => 20 + $user->id,
|
||||||
|
'P' => 1000 + $user->id,
|
||||||
|
default => ($user->id < 50) ? 2000 + $user->id : 3000 + mt_rand(0, 100000),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
$i = 0;
|
||||||
|
foreach($sortedUsers as $user) {
|
||||||
|
$originalFilename = str_starts_with($user->nickname, 'Mickey') ? array_shift($this->files_M) : array_shift($this->files_F);
|
||||||
|
if (!$originalFilename) {
|
||||||
|
if ((count($this->files_M) == 0) && (count($this->files_F) == 0))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if ($originalFilename) {
|
||||||
|
$originalFilename = basename($originalFilename);
|
||||||
|
$newFileName = $this->copyFileToStorage('photos', $originalFilename, 'photos_avatars', $user->id);
|
||||||
|
$user->photo_avatar_filename = $newFileName;
|
||||||
|
DB::table('users')->where('id', $user->id)->update(['photo_avatar_filename' => $user->photo_avatar_filename]);
|
||||||
|
$i++;
|
||||||
|
if ($i % 10 == 0) {
|
||||||
|
$this->command->line("User photo $i/$total copied");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->command->line("Total of $total user's photos were copied!");
|
||||||
|
$this->directCopyFileToStorage('photos', 'anonymous.png', 'photos_avatars');
|
||||||
|
$this->command->line("Image for user with no associated photo was copied!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private function fillPhotoFilesNames()
|
||||||
|
{
|
||||||
|
$allFiles = collect(File::files(database_path('seeders/photos')));
|
||||||
|
foreach ($allFiles as $f) {
|
||||||
|
if (strpos($f->getPathname(), 'm_')) {
|
||||||
|
$this->files_M[] = $f->getPathname();
|
||||||
|
} else if (strpos($f->getPathname(), 'w_')) {
|
||||||
|
$this->files_F[] = $f->getPathname();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
shuffle($this->files_M);
|
||||||
|
shuffle($this->files_F);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 574 KiB |
|
After Width: | Height: | Size: 564 KiB |
|
After Width: | Height: | Size: 549 KiB |
|
After Width: | Height: | Size: 535 KiB |
|
After Width: | Height: | Size: 601 KiB |
|
After Width: | Height: | Size: 543 KiB |
|
After Width: | Height: | Size: 594 KiB |
|
After Width: | Height: | Size: 536 KiB |
|
After Width: | Height: | Size: 557 KiB |
|
After Width: | Height: | Size: 550 KiB |
|
After Width: | Height: | Size: 482 KiB |
|
After Width: | Height: | Size: 543 KiB |
|
After Width: | Height: | Size: 480 KiB |
|
After Width: | Height: | Size: 558 KiB |
|
After Width: | Height: | Size: 587 KiB |
|
After Width: | Height: | Size: 536 KiB |
|
After Width: | Height: | Size: 512 KiB |
|
After Width: | Height: | Size: 530 KiB |
|
After Width: | Height: | Size: 507 KiB |
|
After Width: | Height: | Size: 535 KiB |
|
After Width: | Height: | Size: 497 KiB |
|
After Width: | Height: | Size: 558 KiB |
|
After Width: | Height: | Size: 524 KiB |
|
After Width: | Height: | Size: 509 KiB |
|
After Width: | Height: | Size: 560 KiB |
|
After Width: | Height: | Size: 581 KiB |
|
After Width: | Height: | Size: 593 KiB |
|
After Width: | Height: | Size: 588 KiB |
|
After Width: | Height: | Size: 571 KiB |
|
After Width: | Height: | Size: 542 KiB |
|
After Width: | Height: | Size: 591 KiB |
|
After Width: | Height: | Size: 521 KiB |
|
After Width: | Height: | Size: 529 KiB |
|
After Width: | Height: | Size: 584 KiB |
|
After Width: | Height: | Size: 509 KiB |
|
After Width: | Height: | Size: 568 KiB |