From b6f8a4b32086c09d9763fdc0a52bf063b9d0c963 Mon Sep 17 00:00:00 2001 From: Mercurio <47455213+NotLugozzi@users.noreply.github.com> Date: Mon, 23 Dec 2024 21:48:34 +0100 Subject: [PATCH] Add global page for api url --- lib/globals.dart | 3 + lib/main.dart | 2 +- lib/pages/login.dart | 24 ++-- lib/pages/views/creatematch.dart | 10 +- lib/pages/views/friendlist.dart | 27 ++-- lib/pages/views/joinmatch.dart | 156 ++++++++++++----------- lib/pages/views/leaderboard.dart | 47 +++---- lib/pages/views/myprofile.dart | 209 ++++++++++++++++--------------- pubspec.yaml | 4 +- 9 files changed, 245 insertions(+), 237 deletions(-) create mode 100644 lib/globals.dart diff --git a/lib/globals.dart b/lib/globals.dart new file mode 100644 index 0000000..e7c099b --- /dev/null +++ b/lib/globals.dart @@ -0,0 +1,3 @@ +// lib/globals.dart +//const String apiurl = "https://api.dthpp.mercurio.moe"; +const String apiurl = "http://10.0.0.10:9134"; diff --git a/lib/main.dart b/lib/main.dart index 68b0a17..b7ecb71 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -17,7 +17,7 @@ class MyApp extends StatelessWidget { title: 'Ping Pong Tracker', theme: ThemeData.dark(useMaterial3: true).copyWith( colorScheme: ColorScheme.dark( - primary: Colors.blue.shade900, + primary: Colors.blue.shade800, ), ), home: EntryPoint(), diff --git a/lib/pages/login.dart b/lib/pages/login.dart index 8e29c70..a2cf1fa 100644 --- a/lib/pages/login.dart +++ b/lib/pages/login.dart @@ -3,7 +3,7 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import 'home.dart'; - +import '../globals.dart'; class LoginPage extends StatefulWidget { @override @@ -18,8 +18,6 @@ class _LoginPageState extends State { bool _isLogin = true; bool _isLoading = false; - final String baseUrl = 'http://api.dthpp.mercurio.moe'; - Future _handleAuth() async { final email = _emailController.text.trim(); final password = _passwordController.text.trim(); @@ -38,13 +36,14 @@ class _LoginPageState extends State { context, MaterialPageRoute(builder: (context) => HomePage())); } } else { - final uid = await _register(email, password, _displayNameController.text.trim()); + final uid = await _register( + email, password, _displayNameController.text.trim()); if (uid != null) { setState(() { _isLogin = true; }); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Registration successful! Please login.'))); + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text('Registration successful! Please login.'))); } } } catch (e) { @@ -58,7 +57,7 @@ class _LoginPageState extends State { } Future _login(String email, String password) async { - final url = Uri.parse('$baseUrl/login'); + final url = Uri.parse('$apiurl/login'); final response = await http.post( url, headers: {'Content-Type': 'application/json'}, @@ -73,8 +72,9 @@ class _LoginPageState extends State { } } - Future _register(String email, String password, String displayName) async { - final url = Uri.parse('$baseUrl/register'); + Future _register( + String email, String password, String displayName) async { + final url = Uri.parse('$apiurl/register'); final response = await http.post( url, headers: {'Content-Type': 'application/json'}, @@ -121,9 +121,9 @@ class _LoginPageState extends State { _isLoading ? CircularProgressIndicator() : ElevatedButton( - onPressed: _handleAuth, - child: Text(_isLogin ? 'Login' : 'Register'), - ), + onPressed: _handleAuth, + child: Text(_isLogin ? 'Login' : 'Register'), + ), TextButton( onPressed: () { setState(() { diff --git a/lib/pages/views/creatematch.dart b/lib/pages/views/creatematch.dart index c26ebd8..0ec1afb 100644 --- a/lib/pages/views/creatematch.dart +++ b/lib/pages/views/creatematch.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; +import '../../globals.dart'; class CreateMatchPage extends StatefulWidget { @override @@ -12,7 +13,8 @@ class _CreateMatchPageState extends State { String? _matchId; bool _isLoading = false; - final String _createMatchApiUrl = 'http://api.dthpp.mercurio.moe/creatematch'; // Replace with your API endpoint + final String _createMatchApiUrl = + '$apiurl/creatematch'; // Replace with your API endpoint // Method to create a match Future _createMatch() async { @@ -91,9 +93,9 @@ class _CreateMatchPageState extends State { _isLoading ? CircularProgressIndicator() // Show loading spinner : ElevatedButton( - onPressed: _createMatch, - child: Text('Create Match'), - ), + onPressed: _createMatch, + child: Text('Create Match'), + ), SizedBox(height: 16), if (_matchId != null) Text( diff --git a/lib/pages/views/friendlist.dart b/lib/pages/views/friendlist.dart index a8f7d44..af37ae4 100644 --- a/lib/pages/views/friendlist.dart +++ b/lib/pages/views/friendlist.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; +import '../../globals.dart'; class AddFriendPage extends StatefulWidget { @override @@ -13,8 +14,8 @@ class _AddFriendPageState extends State { List _friends = []; bool _isLoading = false; - final String _addFriendApiUrl = 'http://api.dthpp.mercurio.moe/add_friend'; - final String _getFriendsApiUrl = 'http://api.dthpp.mercurio.moe/get_friends'; + final String _addFriendApiUrl = '$apiurl/add_friend'; + final String _getFriendsApiUrl = '$apiurl/get_friends'; // Method to add a friend Future _addFriend(String friendUid) async { @@ -124,17 +125,17 @@ class _AddFriendPageState extends State { _isLoading ? Center(child: CircularProgressIndicator()) : Expanded( - child: ListView.builder( - itemCount: _friends.length, - itemBuilder: (context, index) { - final friend = _friends[index]; - return ListTile( - title: Text(friend['name']), - subtitle: Text('UID: ${friend['uid']}'), - ); - }, - ), - ), + child: ListView.builder( + itemCount: _friends.length, + itemBuilder: (context, index) { + final friend = _friends[index]; + return ListTile( + title: Text(friend['name']), + subtitle: Text('UID: ${friend['uid']}'), + ); + }, + ), + ), ], ), ), diff --git a/lib/pages/views/joinmatch.dart b/lib/pages/views/joinmatch.dart index f68bce5..58b506f 100644 --- a/lib/pages/views/joinmatch.dart +++ b/lib/pages/views/joinmatch.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; +import '../../globals.dart'; class JoinMatchPage extends StatefulWidget { @override @@ -16,10 +17,9 @@ class _JoinMatchPageState extends State { int _player2Score = 0; String? _matchId; - final String _joinMatchApiUrl = 'http://api.dthpp.mercurio.moe/joinmatch'; // Replace with your API endpoint - final String _endMatchApiUrl = 'http://api.dthpp.mercurio.moe/endmatch'; // Replace with your API endpoint + final String _joinMatchApiUrl = '$apiurl/joinmatch'; + final String _endMatchApiUrl = '$apiurl/endmatch'; - // Join Match Function Future _joinMatch() async { setState(() { _isLoading = true; @@ -64,7 +64,6 @@ class _JoinMatchPageState extends State { } } - // Increment/Decrement Player Scores void _updateScore(int player, int delta) { setState(() { if (player == 1) { @@ -75,7 +74,6 @@ class _JoinMatchPageState extends State { }); } - // End Match Function Future _endMatch() async { setState(() { _isLoading = true; @@ -135,80 +133,80 @@ class _JoinMatchPageState extends State { child: _isLoading ? Center(child: CircularProgressIndicator()) : _isJoined - ? Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - IconButton( - icon: Icon(Icons.remove), - onPressed: () => _updateScore(1, -1), - ), - Text( - 'Player 1 Score: $_player1Score', - style: TextStyle(fontSize: 20), - ), - IconButton( - icon: Icon(Icons.add), - onPressed: () => _updateScore(1, 1), - ), - ], - ), - SizedBox(height: 16), - // Player 2 Score Controls - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - IconButton( - icon: Icon(Icons.remove), - onPressed: () => _updateScore(2, -1), - ), - Text( - 'Player 2 Score: $_player2Score', - style: TextStyle(fontSize: 20), - ), - IconButton( - icon: Icon(Icons.add), - onPressed: () => _updateScore(2, 1), - ), - ], - ), - SizedBox(height: 32), - // End Match Button - ElevatedButton( - onPressed: _endMatch, - child: Text('End Match'), - ), - ], - ) - : Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - // Match ID Text Field - TextField( - controller: _matchIdController, - decoration: InputDecoration( - labelText: 'Enter Match ID', - border: OutlineInputBorder(), - ), - ), - SizedBox(height: 16), - // Join Match Button - ElevatedButton( - onPressed: () { - if (_matchIdController.text.isNotEmpty) { - _joinMatch(); - } else { - _showToast('Please enter a Match ID.'); - } - }, - child: Text('Join Match'), - ), - ], - ), + ? Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + IconButton( + icon: Icon(Icons.remove), + onPressed: () => _updateScore(1, -1), + ), + Text( + 'Player 1 Score: $_player1Score', + style: TextStyle(fontSize: 20), + ), + IconButton( + icon: Icon(Icons.add), + onPressed: () => _updateScore(1, 1), + ), + ], + ), + SizedBox(height: 16), + // Player 2 Score Controls + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + IconButton( + icon: Icon(Icons.remove), + onPressed: () => _updateScore(2, -1), + ), + Text( + 'Player 2 Score: $_player2Score', + style: TextStyle(fontSize: 20), + ), + IconButton( + icon: Icon(Icons.add), + onPressed: () => _updateScore(2, 1), + ), + ], + ), + SizedBox(height: 32), + // End Match Button + ElevatedButton( + onPressed: _endMatch, + child: Text('End Match'), + ), + ], + ) + : Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + // Match ID Text Field + TextField( + controller: _matchIdController, + decoration: InputDecoration( + labelText: 'Enter Match ID', + border: OutlineInputBorder(), + ), + ), + SizedBox(height: 16), + // Join Match Button + ElevatedButton( + onPressed: () { + if (_matchIdController.text.isNotEmpty) { + _joinMatch(); + } else { + _showToast('Please enter a Match ID.'); + } + }, + child: Text('Join Match'), + ), + ], + ), ), ); } diff --git a/lib/pages/views/leaderboard.dart b/lib/pages/views/leaderboard.dart index 52984b1..129e939 100644 --- a/lib/pages/views/leaderboard.dart +++ b/lib/pages/views/leaderboard.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; +import '../../globals.dart'; class LeaderboardPage extends StatefulWidget { @override @@ -11,7 +12,7 @@ class _LeaderboardPageState extends State { List _leaderboard = []; bool _isLoading = true; - final String _apiUrl = 'http://api.dthpp.mercurio.moe/leaderboards'; + final String _leaderboardApi = '$apiurl/leaderboards'; Future _fetchLeaderboard() async { setState(() { @@ -19,7 +20,7 @@ class _LeaderboardPageState extends State { }); try { - final response = await http.get(Uri.parse(_apiUrl)); + final response = await http.get(Uri.parse(_leaderboardApi)); if (response.statusCode == 200) { List data = json.decode(response.body); @@ -62,35 +63,35 @@ class _LeaderboardPageState extends State { @override void initState() { super.initState(); - _fetchLeaderboard(); + _fetchLeaderboard(); } @override Widget build(BuildContext context) { return Scaffold( body: _isLoading - ? Center(child: CircularProgressIndicator()) + ? Center(child: CircularProgressIndicator()) : RefreshIndicator( - onRefresh: _fetchLeaderboard, - child: ListView.builder( - itemCount: _leaderboard.length, - itemBuilder: (context, index) { - var player = _leaderboard[index]; - return Card( - margin: EdgeInsets.all(8), - child: ListTile( - contentPadding: EdgeInsets.all(10), - leading: CircleAvatar( - child: Text(player['player_name'][0].toUpperCase()), - ), - title: Text(player['player_name']), - subtitle: Text('Elo Rating: ${player['elo_rating']}'), - trailing: Text('Friend Code: ${player['friend_code']}'), + onRefresh: _fetchLeaderboard, + child: ListView.builder( + itemCount: _leaderboard.length, + itemBuilder: (context, index) { + var player = _leaderboard[index]; + return Card( + margin: EdgeInsets.all(8), + child: ListTile( + contentPadding: EdgeInsets.all(10), + leading: CircleAvatar( + child: Text(player['player_name'][0].toUpperCase()), + ), + title: Text(player['player_name']), + subtitle: Text('Elo Rating: ${player['elo_rating']}'), + trailing: Text('Friend Code: ${player['friend_code']}'), + ), + ); + }, ), - ); - }, - ), - ), + ), ); } } diff --git a/lib/pages/views/myprofile.dart b/lib/pages/views/myprofile.dart index 3e429df..3a8cfb7 100644 --- a/lib/pages/views/myprofile.dart +++ b/lib/pages/views/myprofile.dart @@ -3,6 +3,7 @@ import 'dart:math'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; +import '../../globals.dart'; class ProfilePage extends StatefulWidget { @override @@ -16,7 +17,7 @@ class _ProfilePageState extends State { int? _elo; List _matches = []; - final String _getProfileApiUrl = 'http://api.dthpp.mercurio.moe/getprofile'; + final String _getProfileApiUrl = '$apiurl/getprofile'; @override void initState() { @@ -79,114 +80,116 @@ class _ProfilePageState extends State { body: _isLoading ? Center(child: CircularProgressIndicator()) : RefreshIndicator( - onRefresh: _fetchProfileData, - child: Column( - children: [ - // Profile Details - Container( - padding: EdgeInsets.all(16.0), - child: Row( + onRefresh: _fetchProfileData, + child: Column( children: [ - // Profile Icon - CircleAvatar( - backgroundColor: _generateRandomColor(), - child: Text( - _name != null && _name!.isNotEmpty - ? _name![0].toUpperCase() - : '?', - style: TextStyle(fontSize: 24, color: Colors.white), - ), - radius: 40, - ), - SizedBox(width: 16), - // Profile Info - Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - _name ?? 'Name not available', - style: TextStyle( - fontSize: 24, - fontWeight: FontWeight.bold, + // Profile Details + Container( + padding: EdgeInsets.all(16.0), + child: Row( + children: [ + // Profile Icon + CircleAvatar( + backgroundColor: _generateRandomColor(), + child: Text( + _name != null && _name!.isNotEmpty + ? _name![0].toUpperCase() + : '?', + style: TextStyle(fontSize: 24, color: Colors.white), + ), + radius: 40, ), - ), - SizedBox(height: 8), - Text('UID: ${_uid ?? 'N/A'}'), - Text('ELO: ${_elo ?? 'N/A'}'), - ], + SizedBox(width: 16), + // Profile Info + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + _name ?? 'Name not available', + style: TextStyle( + fontSize: 24, + fontWeight: FontWeight.bold, + ), + ), + SizedBox(height: 8), + Text('UID: ${_uid ?? 'N/A'}'), + Text('ELO: ${_elo ?? 'N/A'}'), + ], + ), + ], + ), + ), + SizedBox(height: 16), + // Recent Matches + Expanded( + child: _matches.isEmpty + ? Center( + child: Text( + "You haven't played any matches yet", + style: TextStyle( + fontSize: 16, + color: Colors.grey, + ), + ), + ) + : ListView.builder( + itemCount: _matches.length, + itemBuilder: (context, index) { + final match = _matches[index]; + final result = match['result']; + final eloChange = match['elo_change']; + + return Card( + margin: EdgeInsets.symmetric( + horizontal: 16.0, vertical: 8.0), + child: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + crossAxisAlignment: + CrossAxisAlignment.start, + children: [ + Text( + 'Match ID: ${match['match_id']}', + style: TextStyle( + fontSize: 16, + fontWeight: FontWeight.bold, + ), + ), + Text( + 'Opponent: ${match['opponent_name']}'), + Row( + children: [ + Text( + '$result', + style: TextStyle( + fontWeight: FontWeight.bold, + color: result == 'Win' + ? Colors.green + : Colors.red, + ), + ), + SizedBox(width: 16), + if (eloChange != null) + Text( + 'ELO Change: ${eloChange > 0 ? '+' : ''}$eloChange', + style: TextStyle( + color: eloChange > 0 + ? Colors.green + : Colors.red, + ), + ), + ], + ), + ], + ), + ), + ); + }, + ), ), ], ), ), - SizedBox(height: 16), - // Recent Matches - Expanded( - child: _matches.isEmpty - ? Center( - child: Text( - "You haven't played any matches yet", - style: TextStyle( - fontSize: 16, - color: Colors.grey, - ), - ), - ) - : ListView.builder( - itemCount: _matches.length, - itemBuilder: (context, index) { - final match = _matches[index]; - final result = match['result']; - final eloChange = match['elo_change']; - - return Card( - margin: EdgeInsets.symmetric( - horizontal: 16.0, vertical: 8.0), - child: Padding( - padding: const EdgeInsets.all(16.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - 'Match ID: ${match['match_id']}', - style: TextStyle( - fontSize: 16, - fontWeight: FontWeight.bold, - ), - ), - Text('Opponent: ${match['opponent_name']}'), - Row( - children: [ - Text( - '$result', - style: TextStyle( - fontWeight: FontWeight.bold, - color: result == 'Win' - ? Colors.green - : Colors.red, - ), - ), - SizedBox(width: 16), - if (eloChange != null) - Text( - 'ELO Change: ${eloChange > 0 ? '+' : ''}$eloChange', - style: TextStyle( - color: eloChange > 0 - ? Colors.green - : Colors.red, - ), - ), - ], - ), - ], - ), - ), - ); - }, - ), - ), - ], - ), - ), ); } } diff --git a/pubspec.yaml b/pubspec.yaml index 0e113e6..67df21a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,8 @@ name: pingpongapp -description: "A new Flutter project." +description: "DTH Ping Pong Score tracking app" publish_to: 'none' -version: 0.0.32+1 +version: 0.0.34+1 environment: sdk: '>=3.4.3 <4.0.0'