Added connections to the firo_runner_server. Fixed a couple introduced bugs. Added flag for non tournament version of the code for Google Play/ Apple.

This commit is contained in:
Marco Salazar 2021-10-08 21:50:47 -06:00
parent 504fff2a7e
commit cbca859226
9 changed files with 762 additions and 169 deletions

80
lib/deposit.dart Normal file
View File

@ -0,0 +1,80 @@
import 'package:flutter/material.dart';
import 'main.dart';
import 'package:qr_flutter/qr_flutter.dart';
class DepositOverlay extends StatelessWidget {
const DepositOverlay({
Key? key,
required this.game,
}) : super(key: key);
final Color textColor = Colors.cyan;
final Color cardColor = const Color(0xff262b3f);
final Color titleColor = const Color(0xff68d9cc);
final MyGame game;
List<Widget> getDepositAddress(double width) {
List<Widget> list = [];
if (game.address.length != 34) {}
list.add(QrImage(
data: game.address,
version: QrVersions.auto,
size: width / 5,
backgroundColor: Colors.white,
));
list.add(Padding(
padding: EdgeInsets.all(width * 0.01),
child: Card(
color: cardColor,
shape: RoundedRectangleBorder(
side: BorderSide(color: titleColor, width: 3),
borderRadius: BorderRadius.circular(10.0)),
child: Padding(
padding: EdgeInsets.all(width * 0.01),
child: SelectableText(
game.address,
style: TextStyle(
color: textColor,
fontSize: width * 0.03,
),
),
),
),
));
return list;
}
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return Material(
color: Colors.transparent,
child: InkWell(
child: Center(
child: Container(
height: game.viewport.canvasSize.y,
width: game.viewport.canvasSize.x,
decoration: const BoxDecoration(
image: DecorationImage(
image: lossImage,
fit: BoxFit.fill,
),
),
child: SizedBox(
width: width / 3,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: getDepositAddress(width),
),
),
),
),
onTap: () {
game.overlays.remove('deposit');
},
),
);
}
}

173
lib/leader_board.dart Normal file
View File

@ -0,0 +1,173 @@
import 'package:flutter/material.dart';
import 'main.dart';
class LeaderBoardOverlay extends StatelessWidget {
const LeaderBoardOverlay({
Key? key,
required this.game,
}) : super(key: key);
final Color textColor = Colors.cyan;
final Color cardColor = const Color(0xff262b3f);
final Color borderColor = const Color(0xdfd675e1);
final Color titleColor = const Color(0xff68d9cc);
// final Color textColor = Colors.black;
final MyGame game;
List<Card> getLeaderboard(double width) {
List<Card> leaders = [];
List<String> list = game.leaderboard.split("\n");
if (list.isEmpty || list.length % 2 != 1 || list.length == 1) {
leaders.add(
Card(
color: cardColor,
shape: RoundedRectangleBorder(
side: BorderSide(color: titleColor, width: 3),
borderRadius: BorderRadius.circular(10.0)),
child: FractionallySizedBox(
widthFactor: 0.5,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.all(width * 0.01),
child: Text(
"No Internet Connection.",
style: TextStyle(
color: textColor,
fontSize: width * 0.03,
),
),
)
],
),
),
),
);
return leaders;
}
leaders.add(
Card(
color: cardColor,
shape: RoundedRectangleBorder(
side: BorderSide(color: titleColor, width: 3),
borderRadius: BorderRadius.circular(10.0)),
child: FractionallySizedBox(
widthFactor: 0.4,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.all(width * 0.01),
child: Text(
"Username",
style: TextStyle(
color: textColor,
fontSize: width * 0.03,
),
),
),
SizedBox(
width: width / 11,
),
Padding(
padding: EdgeInsets.all(width * 0.01),
child: Text(
"Score",
style: TextStyle(
color: textColor,
fontSize: width * 0.03,
),
),
),
],
),
),
),
);
for (int i = 0; i < list.length - 2; i = i + 2) {
String name = list.elementAt(i);
String score = list.elementAt(i + 1);
leaders.add(
Card(
color: cardColor,
shape: RoundedRectangleBorder(
side: BorderSide(color: borderColor, width: 3),
borderRadius: BorderRadius.circular(10.0)),
child: FractionallySizedBox(
widthFactor: 0.4,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.all(width * 0.01),
child: Text(
name.substring(0, name.length < 10 ? name.length : 10),
style: TextStyle(
color: textColor,
fontSize: width * 0.03,
),
),
),
SizedBox(
width: width / 11,
),
Padding(
padding: EdgeInsets.all(width * 0.01),
child: Text(
score,
style: TextStyle(
color: textColor,
fontSize: width * 0.03,
),
),
),
],
),
),
),
);
}
return leaders;
}
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return Material(
color: Colors.transparent,
child: InkWell(
child: Center(
child: Container(
height: game.viewport.canvasSize.y,
width: game.viewport.canvasSize.x,
decoration: const BoxDecoration(
image: DecorationImage(
image: lossImage,
fit: BoxFit.fill,
),
),
child: SizedBox(
width: width / 3,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: getLeaderboard(width),
),
),
),
),
onTap: () {
game.overlays.remove('leaderboard');
},
),
);
}
}

View File

@ -88,17 +88,22 @@ class LoseMenuOverlay extends StatelessWidget {
child: Padding( child: Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: Text( child: Text(
" REPLAY ", " REPLAY${game.competitive ? " ${game.tries}" : ""} ",
style: TextStyle( style: TextStyle(
color: Colors.cyan, color: game.competitive && game.tries <= 0
? Colors.grey
: Colors.cyan,
fontSize: width * 0.03, fontSize: width * 0.03,
), ),
), ),
), ),
), ),
// ), // ),
onPressed: () async { onPressed: game.competitive && game.tries <= 0
await FlameAudio.audioCache.play('sfx/button_click.mp3', ? null
: () async {
await FlameAudio.audioCache.play(
'sfx/button_click.mp3',
mode: PlayerMode.LOW_LATENCY); mode: PlayerMode.LOW_LATENCY);
game.runner.friend = await FlameAudio.audioCache game.runner.friend = await FlameAudio.audioCache
.loop('sfx/robot_friend_beep.mp3'); .loop('sfx/robot_friend_beep.mp3');

View File

@ -1,17 +1,18 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math'; import 'dart:math';
import 'package:firo_runner/bug_holder.dart'; import 'package:firo_runner/bug_holder.dart';
import 'package:firo_runner/circuit_background.dart'; import 'package:firo_runner/circuit_background.dart';
import 'package:firo_runner/coin_holder.dart'; import 'package:firo_runner/coin_holder.dart';
import 'package:firo_runner/debris_holder.dart'; import 'package:firo_runner/debris_holder.dart';
import 'package:firo_runner/deposit.dart';
import 'package:firo_runner/firework.dart'; import 'package:firo_runner/firework.dart';
import 'package:firo_runner/game_state.dart'; import 'package:firo_runner/game_state.dart';
import 'package:firo_runner/leader_board.dart';
import 'package:firo_runner/moving_object.dart'; import 'package:firo_runner/moving_object.dart';
import 'package:firo_runner/platform.dart'; import 'package:firo_runner/platform.dart';
import 'package:firo_runner/platform_holder.dart'; import 'package:firo_runner/platform_holder.dart';
import 'package:firo_runner/sign_in_overlay.dart';
import 'package:firo_runner/wall_holder.dart'; import 'package:firo_runner/wall_holder.dart';
import 'package:firo_runner/wire.dart'; import 'package:firo_runner/wire.dart';
import 'package:firo_runner/wire_holder.dart'; import 'package:firo_runner/wire_holder.dart';
@ -30,6 +31,14 @@ import 'package:http/http.dart' as http;
import 'package:firo_runner/lose_menu_overlay.dart'; import 'package:firo_runner/lose_menu_overlay.dart';
import 'package:firo_runner/main_menu_overlay.dart'; import 'package:firo_runner/main_menu_overlay.dart';
import 'package:shared_preferences/shared_preferences.dart';
// TODO Set NO_TOURNAMENT to false, and then set the SERVER and PORT for the
// firo runner server instance.
const NO_TOURNAMENT = false;
const SERVER = "http://10.0.0.224";
const PORT = "50067";
const COLOR = Color(0xFFDDC0A3); const COLOR = Color(0xFFDDC0A3);
const int LOADING_TIME = 2000000; const int LOADING_TIME = 2000000;
@ -88,6 +97,15 @@ void main() async {
), ),
); );
}, },
'leaderboard': (_, myGame) {
return LeaderBoardOverlay(game: myGame);
},
'deposit': (_, myGame) {
return DepositOverlay(game: myGame);
},
'signin': (_, myGame) {
return SignInOverlay(game: myGame);
},
'mainMenu': (_, myGame) { 'mainMenu': (_, myGame) {
return MainMenuOverlay(game: myGame); return MainMenuOverlay(game: myGame);
}, },
@ -118,6 +136,12 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
config: const TextPaintConfig(fontSize: 16.0, color: COLOR), config: const TextPaintConfig(fontSize: 16.0, color: COLOR),
); );
String leaderboard = "";
String address = "";
String username = "";
int tries = 0;
bool competitive = false;
late CircuitBackground circuitBackground; late CircuitBackground circuitBackground;
late PlatformHolder platformHolder; late PlatformHolder platformHolder;
late CoinHolder coinHolder; late CoinHolder coinHolder;
@ -146,7 +170,18 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
// debugMode = true; if (!NO_TOURNAMENT) {
final prefs = await SharedPreferences.getInstance();
username = prefs.getString('username') ?? "";
tries = prefs.getInt('tries') ?? 0;
String result = await connectServer("gettries", "user=$username");
try {
tries = int.parse(result);
prefs.setInt('tries', tries);
} catch (e) {
print(e);
}
}
FlameAudio.bgm.initialize(); FlameAudio.bgm.initialize();
await FlameAudio.audioCache.loadAll([ await FlameAudio.audioCache.loadAll([
@ -312,56 +347,30 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
bool shouldReset = false; bool shouldReset = false;
// late Socket socket; Future<String> connectServer(String command, String arguments) async {
// void dataHandler(data) { try {
// print(new String.fromCharCodes(data).trim()); final response = await http.post(
// } Uri.parse("$SERVER:$PORT/$command?$arguments"),
// headers: <String, String>{
// void errorHandler(error, StackTrace trace) { 'Content-Type': 'application/json; charset=UTF-8',
// print(error); },
// } );
// if (response.statusCode == 200) {
// void doneHandler() { // If the server did return a 200,
// socket.destroy(); // then parse the JSON.
// } return response.body;
} else {
// Future<void> connectServer() async { // If the server did not return a 201 CREATED response,
// try { // then throw an exception.
// Socket.connect('10.0.0.224', 50018).then((Socket sock) { throw Exception('Failed to connect to Firo Runner server.');
// socket = sock; }
// socket.listen(dataHandler, // var value = await channel.stream.first;
// onError: errorHandler, onDone: doneHandler, cancelOnError: false); // print(value);
// }); } catch (e) {
// } catch (e) { print(e);
// print(e); return "";
// } }
// // try { }
// // final response = await http.post(
// // Uri.parse('http://10.0.0.224:50017'),
// // headers: <String, String>{
// // 'Content-Type': 'application/json; charset=UTF-8',
// // },
// // body: jsonEncode(<String, String>{
// // 'title': "hi",
// // }),
// // );
// // if (response.statusCode == 201) {
// // // If the server did return a 201 CREATED response,
// // // then parse the JSON.
// // print("hello");
// // print(response);
// // print(response.body);
// // } else {
// // // If the server did not return a 201 CREATED response,
// // // then throw an exception.
// // throw Exception('Failed to create album.');
// // }
// // // var value = await channel.stream.first;
// // // print(value);
// // } catch (e) {
// // print(e);
// // }
// }
Future<void> displayLoss() async { Future<void> displayLoss() async {
if (!(runner.sprite.animation?.done() ?? false) && if (!(runner.sprite.animation?.done() ?? false) &&
@ -369,7 +378,6 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
firstDeath) { firstDeath) {
return; return;
} }
// await connectServer();
firstDeath = false; firstDeath = false;
overlays.add('gameOver'); overlays.add('gameOver');
} }
@ -390,8 +398,23 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
setUp(); setUp();
} }
void die() { Future<void> die() async {
gameState.setPaused(); gameState.setPaused();
if (!NO_TOURNAMENT) {
final prefs = await SharedPreferences.getInstance();
if (username != "" && competitive) {
await connectServer(
"newscore", "user=$username&score=${gameState.getPlayerScore()}");
}
tries = prefs.getInt('tries') ?? 0;
String result = await connectServer("gettries", "user=$username");
try {
tries = int.parse(result);
prefs.setInt('tries', tries);
} catch (e) {
print(e);
}
}
shouldReset = true; shouldReset = true;
} }
@ -425,12 +448,6 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
circuitBackground.render(canvas); circuitBackground.render(canvas);
fireworks.renderText(canvas); fireworks.renderText(canvas);
super.render(canvas); super.render(canvas);
// final fpsCount = fps(10000);
// fireworksPaint.render(
// canvas,
// fpsCount.toString(),
// Vector2(0, 0),
// );
coinHolder.renderCoinScore(canvas); coinHolder.renderCoinScore(canvas);
} }
} }

View File

@ -2,7 +2,9 @@ import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:audioplayers/src/api/player_mode.dart'; import 'package:audioplayers/src/api/player_mode.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'main.dart'; import 'main.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
class MainMenuOverlay extends StatelessWidget { class MainMenuOverlay extends StatelessWidget {
const MainMenuOverlay({ const MainMenuOverlay({
@ -27,22 +29,26 @@ class MainMenuOverlay extends StatelessWidget {
), ),
child: Row( child: Row(
mainAxisSize: MainAxisSize.max, mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: NO_TOURNAMENT
? MainAxisAlignment.spaceEvenly
: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: [ children: [
Column( Column(
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ children: [
// const SizedBox( const SizedBox(
// height: 32.0, height: 16.0,
// ), ),
// const SizedBox( const SizedBox(
// height: 32.0, height: 16.0,
// ), ),
// const SizedBox( const SizedBox(
// height: 32.0, height: 16.0,
// ), ),
Row(
children: [
MaterialButton( MaterialButton(
padding: const EdgeInsets.fromLTRB(8, 8, 8, 8), padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
textColor: Colors.white, textColor: Colors.white,
@ -50,8 +56,8 @@ class MainMenuOverlay extends StatelessWidget {
elevation: 8.0, elevation: 8.0,
child: Container( child: Container(
decoration: const BoxDecoration( decoration: const BoxDecoration(
image: image: DecorationImage(
DecorationImage(image: buttonImage, fit: BoxFit.fill), image: buttonImage, fit: BoxFit.fill),
), ),
child: Padding( child: Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
@ -59,17 +65,21 @@ class MainMenuOverlay extends StatelessWidget {
" START ", " START ",
style: TextStyle( style: TextStyle(
color: Colors.cyan, color: Colors.cyan,
fontSize: width * 0.03, fontSize: width * 0.025,
), ),
), ),
), ),
), ),
// ),
onPressed: () async { onPressed: () async {
// Go to the Main Menu // Go to the Main Menu
FlameAudio.audioCache.play('sfx/menu_button.mp3', FlameAudio.audioCache.play('sfx/menu_button.mp3',
mode: PlayerMode.LOW_LATENCY); mode: PlayerMode.LOW_LATENCY);
game.competitive = false;
game.reset(); game.reset();
game.runner.boost = FlameAudio.audioCache.play(
'sfx/laser.mp3',
volume: 0.0,
mode: PlayerMode.LOW_LATENCY);
FlameAudio.bgm.stop(); FlameAudio.bgm.stop();
FlameAudio.bgm.play('Infinite_Spankage_M.mp3'); FlameAudio.bgm.play('Infinite_Spankage_M.mp3');
game.runner.friend = await FlameAudio.audioCache.loop( game.runner.friend = await FlameAudio.audioCache.loop(
@ -78,60 +88,161 @@ class MainMenuOverlay extends StatelessWidget {
mode: PlayerMode.LOW_LATENCY); mode: PlayerMode.LOW_LATENCY);
}, },
), ),
// MaterialButton( ],
// padding: const EdgeInsets.fromLTRB(8, 8, 8, 8), ),
// textColor: Colors.white, NO_TOURNAMENT
// splashColor: Colors.greenAccent, ? const SizedBox(
// elevation: 8.0, height: 16.0,
// child: Container( )
// decoration: const BoxDecoration( : Row(
// image: children: [
// DecorationImage(image: buttonImage, fit: BoxFit.fill), MaterialButton(
// ), padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
// child: Padding( textColor: Colors.white,
// padding: EdgeInsets.all(8.0), splashColor: Colors.greenAccent,
// child: Text( elevation: 8.0,
// " DEPOSIT ", child: Container(
// style: TextStyle( decoration: const BoxDecoration(
// color: Colors.cyan, image: DecorationImage(
// fontSize: width * 0.03, image: buttonImage, fit: BoxFit.fill),
// ), ),
// ), child: Padding(
// ), padding: EdgeInsets.all(8.0),
// ), child: Text(
// // ), " DEPOSIT ",
// onPressed: () { style: TextStyle(
// // Show QR code color: game.username == ""
// await FlameAudio.audioCache.play('sfx/button_click.mp3', mode: PlayerMode.LOW_LATENCY); ? Colors.grey
// }, : Colors.cyan,
// ), fontSize: width * 0.025,
// MaterialButton( ),
// padding: const EdgeInsets.fromLTRB(8, 8, 8, 8), ),
// textColor: Colors.white, ),
// splashColor: Colors.greenAccent, ),
// elevation: 8.0, onPressed: game.username == ""
// child: Container( ? null
// decoration: const BoxDecoration( : () async {
// image: game.address = await game.connectServer(
// DecorationImage(image: buttonImage, fit: BoxFit.fill), "deposit", "user=${game.username}");
// ), FlameAudio.audioCache.play(
// child: Padding( 'sfx/button_click.mp3',
// padding: EdgeInsets.all(8.0), mode: PlayerMode.LOW_LATENCY);
// child: Text( game.overlays.add("deposit");
// " LEADER BOARD ", },
// style: TextStyle( ),
// color: Colors.cyan, MaterialButton(
// fontSize: width * 0.03, padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
// ), textColor: Colors.white,
// ), splashColor: Colors.greenAccent,
// ), elevation: 8.0,
// ), child: Container(
// // ), decoration: const BoxDecoration(
// onPressed: () { image: DecorationImage(
// // Show QR code image: buttonImage, fit: BoxFit.fill),
// await FlameAudio.audioCache.play('sfx/button_click.mp3', mode: PlayerMode.LOW_LATENCY); ),
// }, child: Padding(
// ), padding: const EdgeInsets.all(8.0),
child: Text(
" TOURNAMENT ${game.tries} ",
style: TextStyle(
color:
game.username == "" || game.tries == 0
? Colors.grey
: Colors.cyan,
fontSize: width * 0.025,
),
),
),
),
onPressed: game.username == "" || game.tries == 0
? null
: () async {
// Go to the Main Menu
FlameAudio.audioCache.play(
'sfx/menu_button.mp3',
mode: PlayerMode.LOW_LATENCY);
game.runner.boost = FlameAudio.audioCache
.play('sfx/laser.mp3',
volume: 0.0,
mode: PlayerMode.LOW_LATENCY);
game.competitive = true;
game.reset();
FlameAudio.bgm.stop();
FlameAudio.bgm
.play('Infinite_Spankage_M.mp3');
game.runner.friend = await FlameAudio
.audioCache
.loop('sfx/robot_friend_beep.mp3',
volume: 0.25,
mode: PlayerMode.LOW_LATENCY);
},
),
],
),
NO_TOURNAMENT
? const SizedBox(
height: 16.0,
)
: Row(
children: [
MaterialButton(
padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
textColor: Colors.white,
splashColor: Colors.greenAccent,
elevation: 8.0,
child: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: buttonImage, fit: BoxFit.fill),
),
child: Padding(
padding: EdgeInsets.all(8.0),
child: Text(
" ${game.username == "" ? "SIGN IN" : game.username} ",
style: TextStyle(
color: Colors.cyan,
fontSize: width * 0.025,
),
),
),
),
onPressed: () async {
FlameAudio.audioCache.play('sfx/button_click.mp3',
mode: PlayerMode.LOW_LATENCY);
game.overlays.add("signin");
},
),
MaterialButton(
padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
textColor: Colors.white,
splashColor: Colors.greenAccent,
elevation: 8.0,
child: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: buttonImage, fit: BoxFit.fill),
),
child: Padding(
padding: EdgeInsets.all(8.0),
child: Text(
" LEADER BOARD ",
style: TextStyle(
color: Colors.cyan,
fontSize: width * 0.025,
),
),
),
),
onPressed: () async {
game.leaderboard = await game.connectServer(
"leaderboard", "user=value");
FlameAudio.audioCache.play('sfx/button_click.mp3',
mode: PlayerMode.LOW_LATENCY);
game.overlays.add("leaderboard");
},
),
],
),
], ],
), ),
const SizedBox( const SizedBox(

View File

@ -557,8 +557,6 @@ class Runner extends Component with HasGameRef<MyGame> {
} }
Future load() async { Future load() async {
boost = FlameAudio.audioCache
.play('sfx/laser.mp3', volume: 0.0, mode: PlayerMode.LOW_LATENCY);
List<Image> satellites = []; List<Image> satellites = [];
for (int i = 1; i <= 38; i++) { for (int i = 1; i <= 38; i++) {
satellites.add(await Flame.images.load( satellites.add(await Flame.images.load(

151
lib/sign_in_overlay.dart Normal file
View File

@ -0,0 +1,151 @@
import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'main.dart';
import 'package:audioplayers/src/api/player_mode.dart';
/// This is the stateful widget that the main application instantiates.
class SignInOverlay extends StatefulWidget {
const SignInOverlay({
Key? key,
required this.game,
}) : super(key: key);
final Color cardColor = Colors.white;
final Color textColor = Colors.black;
final MyGame game;
@override
State<SignInOverlay> createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<SignInOverlay> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final accountController = TextEditingController();
final keyController = TextEditingController();
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return Material(
color: Colors.transparent,
child: InkWell(
child: Center(
child: Container(
height: widget.game.viewport.canvasSize.y,
width: widget.game.viewport.canvasSize.x,
decoration: const BoxDecoration(
image: DecorationImage(
image: lossImage,
fit: BoxFit.fill,
),
),
child: SingleChildScrollView(
child: Center(
child: SizedBox(
width: 2 * width / 3,
child: Container(
padding: const EdgeInsets.all(30),
color: Colors.white,
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
hintText: 'Enter your account name',
),
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Please enter your account name';
} else if (value.contains(' ')) {
return 'Please input a valid account name without any spaces';
}
return null;
},
controller: accountController,
),
TextFormField(
decoration: const InputDecoration(
hintText: 'Enter your receiving Firo address.',
),
validator: (String? value) {
// if (value == null || value.isEmpty) {
// return 'Please enter your receiving Firo address.';
// } else
if (value == null || value.isEmpty) {
print("logging in instead of signing up.");
return null;
} else if (value.length != 34) {
return 'Not a valid receiving Firo address.';
}
return null;
},
controller: keyController,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: () async {
// // Validate will return true if the form is valid, or false if
// // the form is invalid.
FlameAudio.audioCache.play(
'sfx/button_click.mp3',
mode: PlayerMode.LOW_LATENCY);
if (!_formKey.currentState!.validate()) {
return;
}
// Process data.
String account = accountController.text;
String key = keyController.text;
String username = await widget.game.connectServer(
"newuser",
"user=$account&receive=${key == "" ? "dud" : key}");
if (username.toLowerCase().contains("error")) {
print("There was an error");
} else {
final prefs =
await SharedPreferences.getInstance();
prefs.setString('username', username);
widget.game.username =
prefs.getString('username') ?? "";
try {
String result = await widget.game
.connectServer(
"gettries", "user=$username");
widget.game.tries = int.parse(result);
prefs.setInt('tries', widget.game.tries);
} catch (e) {
print(e);
}
}
widget.game.overlays.remove('signin');
},
child: const Text('Sign In'),
),
),
],
),
),
),
),
),
),
),
),
onTap: () {
widget.game.overlays.remove('signin');
},
),
);
}
}

View File

@ -275,6 +275,62 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "4.2.3" version: "4.2.3"
qr:
dependency: transitive
description:
name: qr
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
qr_flutter:
dependency: "direct main"
description:
name: qr_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@ -380,4 +436,4 @@ packages:
version: "3.1.0" version: "3.1.0"
sdks: sdks:
dart: ">=2.14.0 <3.0.0" dart: ">=2.14.0 <3.0.0"
flutter: ">=2.0.0" flutter: ">=2.5.0"

View File

@ -26,6 +26,8 @@ dependencies:
flame: "^1.0.0-releasecandidate.11" flame: "^1.0.0-releasecandidate.11"
flame_audio: "^1.0.0-rc.1" flame_audio: "^1.0.0-rc.1"
http: ^0.13.3 http: ^0.13.3
qr_flutter: ^4.0.0
shared_preferences: ^2.0.8
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.