Page MenuHomec4science

Ac6.dart
No OneTemporary

File Metadata

Created
Tue, Oct 8, 04:29

Ac6.dart

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:student/widgets/celluloMap.Dart';
import 'dart:async';
import 'dart:convert';
import 'package:student/Database.dart';
import 'package:student/model/Group.dart';
import 'package:student/model/Cellulo.dart';
import 'package:student/widgets/showAlertDialog.Dart';
import 'package:student/widgets/membersBar.Dart';
import 'package:student/widgets/inactivityDetector.Dart';
import 'package:flutter_appavailability/flutter_appavailability.dart';
import 'dart:math' as math;
class Ac6 extends StatefulWidget {
Ac6({Key key}) : super(key: key);
@override
_Ac6State createState() => _Ac6State();
}
class _Ac6State extends State<Ac6>
with SingleTickerProviderStateMixin, WidgetsBindingObserver {
// Timers
Timer timerCelluloPosition;
Timer timerslopedetection;
Timer timercleartracking;
Timer timerCelluloPositionPaint;
Timer timerInactivityDetector;
Timer timerCelluloPositionPainttoserver;
final acID = 6;
final maxLiitCellulo = 800;
// UI parameters
bool celluloxSwitch1 = true;
bool celluloySwitch1 = false;
bool celluloxSwitch2 = true;
bool celluloySwitch2 = false;
var onStop = false;
bool mode = false;
int wrongcounts = 0;
double _progress = 0.0;
final celluloSize = 60;
final ScrollController _scrollController = ScrollController();
final functionFormulas = ['X ⟼ Y', 'X+1 ⟼ Y', 'X+2 ⟼ Y', '2*X ⟼ Y', '-X ⟼ Y'];
Offset beginpath = Offset(0, 0);
Offset endpath = Offset(10, 100);
AnimationController controllerTrial;
AnimationController controllerTurn;
Animation<double> animation;
Animation<double> animationNextTurn;
TextEditingController controllerinitialX = TextEditingController();
TextEditingController controllerinitialY = TextEditingController();
TextEditingController controllerXslope = TextEditingController();
TextEditingController controllerYslope = TextEditingController();
var counterfunctionImage = 0;
//Activity Dependent
var linesPath = [
'assets/images/Ac6_function1.svg',
'assets/images/Ac6_function2.svg',
'assets/images/Ac6_function3.svg',
'assets/images/Grid_Ac6_Screen.svg',
];
var lineallow = [false, false, false];
var linesSlope = [1, 2, -1];
var initialPoint = [0, 0, 6];
// map-related
final double mapSizeWidth = 860;
final double mapSizeHeight = 860;
final double screenSizeWidth = 500;
final double screenSizeHeight = 500;
final String mapPath = 'assets/images/Grid_Ac6_Screen.svg';
final String celluloxpath = 'assets/images/celluloRed.svg';
final String celluloypath = 'assets/images/celluloBlue.svg';
var onTap = [false, false, false, false, false];
// var onTap=[false,false,false,false,false];
final names = [group.member1name, group.member2name, group.member3name];
var currentTurn = 1;
var currentTap = 0;
var tapCounter = 0;
int simulationVelCoeff = 1;
// Learning
List<int> progress = [0, -2, -2];
// zero: undergoing, -1: not accomplished, 1: accomplished
bool waitforanimation = false;
List<int> mistakesSlope = [0, 0, 0];
List<int> mistakesIntrepet = [0, 0, 0];
List<int> mistakesInitialPosition = [0, 0, 0];
List<int> progressElpasedTime = [0, 0, 0];
var inactivity = 0;
bool checkend = true;
var prevcelluloxPositionSlope = 0.0;
var prevcelluloyPositionSlope = 0.0;
// robot related
var elapseTimer = new Stopwatch();
bool allowPaint = false;
var celluloxPosition = [0.0, 0.0];
var celluloyPosition = [0.0, 0.0];
var xHaptic;
var yHaptic;
bool runHaptic = false;
var xcelluloy = 820;
var ycellulox = 45;
var celluloTargetPosition = [0.0, 0.0];
List<List<double>> celluloxPositiontopaint =
new List.generate(3, (int index) => [0.0, 0.0], growable: true);
List<List<double>> celluloyPositiontopaint =
new List.generate(3, (int index) => [0.0, 0.0], growable: true);
var xlabelposition = [1.0, 1.0];
var ylabelposition = [-0.6, -1.0];
var prevcelluloxPosition = [0.0, 0.0];
var prevcelluloyPosition = [0.0, 0.0];
// screen-related
var originCoordinates = [123.5, 736.5];
var correctAnswer = [
{"initialPointX": 1, "initialPointY": 1, "slope": 1},
{"initialPointX": 0, "initialPointY": 0, "slope": 2},
{"initialPointX": 0, "initialPointY": 6, "slope": -1}
];
void calcHaptic() {
var slopeHaptic = -1 / linesSlope[currentTurn - 1];
var initialHaptic = (originCoordinates[1] - celluloyPosition[1]) / 100 -
slopeHaptic * (celluloxPosition[0] - originCoordinates[0]) / 100;
// print('initialhaptic' + initialHaptic.toString());
setState(() {
xHaptic = (initialHaptic - initialPoint[currentTurn - 1]) /
(linesSlope[currentTurn - 1] - slopeHaptic);
yHaptic = ((linesSlope[currentTurn - 1]) * xHaptic +
initialPoint[currentTurn - 1]);
});
// print('xcelluloy' + xcelluloy.toDouble().toString());
// print('yhaptic' + yHaptic.toString());
// print('xhaptic' + xHaptic.toString());
cellulox.setGoalPosition(
xHaptic * 100 + originCoordinates[0], xcelluloy.toDouble(), 150);
celluloy.setGoalPosition(
ycellulox.toDouble(), originCoordinates[1] - (100 * yHaptic), 150);
}
@override
void initState() {
cellulox.setColor(0, 255, 0, 0, 0);
celluloy.setColor(0, 0, 255, 0, 0);
cellulox.clearrobot();
celluloy.clearrobot();
WidgetsBinding.instance.addObserver(this);
dbRef.child('groups').child(group.id).child('tabletStatus').set("YES");
dbRef.child('groups').child(group.id).child('currentActivity').set("Ac6");
elapseTimer.start();
onDataSend();
controllerTrial = AnimationController(
duration: Duration(milliseconds: 30000), vsync: this);
animation = Tween(begin: 0.0, end: 1.0).animate(controllerTrial)
..addListener(() {
_progress = animation.value;
if (_progress < 0.2) {
//print('uuh');
cellulox.setGoalPosition(originCoordinates[0], 700, 150);
celluloy.setGoalPosition(80, originCoordinates[1], 150);
}
if (_progress > 0.22) {
setState(() {
allowPaint = true;
});
// }
//if (celluloxPosition[0] > 700 ||
// celluloyPosition[1] < 100) {
// cellulox.resetrobot();
// celluloy.resetrobot();
// calcHaptic();
// cellulox.setGoalPosition(xHaptic, 0);
// celluloy.setGoalPosition(0, yHaptic);
}
if (_progress == 1.0) {
// cellulox.setGoalPosition(770, 600);
// celluloy.setGoalPosition(100, 80);
setState(() {
allowPaint = false;
});
// cellulox.setVelocity(0, 0);
// celluloy.setVelocity(0, 0);
}
if (celluloxPosition[0] > 800 ||
celluloyPosition[1] < 50 ||
celluloyPosition[1] > 800) {
cellulox.setVelocity(0, 0);
celluloy.setVelocity(0, 0);
}
});
super.initState();
// cellulox.resetrobot();
// celluloy.resetrobot();
timerCelluloPosition =
new Timer.periodic(new Duration(milliseconds: 100), (time) {
if (totalRobots() > 1) {
cellulox.getrobotx().then((val) => setState(() {
celluloxPosition[0] = val;
// print(celluloxPosition[0]);
// if (allowPaint == true)
// celluloxPositiontopaint.add(val * 500 / mapSizeWidth);
// print(allowPaint.toString());
}));
cellulox.getroboty().then((val) => setState(() {
celluloxPosition[1] = val;
//if (allowPaint == true) prevcelluloxPosition[1] = celluloxPosition[1];
}));
celluloy.getrobotx().then((val) => setState(() {
celluloyPosition[0] = val;
// if (allowPaint == true) prevcelluloyPosition[0] = celluloyPosition[0];
}));
celluloy.getroboty().then((val) => setState(() {
celluloyPosition[1] = val;
// if (allowPaint == true)
// celluloyPositiontopaint.add(val * 500 / mapSizeHeight);
}));
if (checkend == true) {
if (celluloxPosition[0] > 800 ||
celluloyPosition[1] > 800 ||
celluloxPosition[0] < 50 ||
celluloyPosition[1] < 50) {
cellulox.clearrobot();
celluloy.clearrobot();
runHaptic = false;
showAlertDialog(context, '', 'Make sure robots are on the map');
checkend = false;
}
}
if (checkend == false) {
if (celluloxPosition[0] < 800 &&
celluloyPosition[1] < 800 &&
celluloxPosition[0] > 50 &&
celluloyPosition[1] > 50) {
// cellulox.clearrobot();
// celluloy.clearrobot();
// runHaptic = false;
// showAlertDialog(context, '', 'Make sure robots are on the map');
checkend = true;
runHaptic = true;
}
}
}
if (true) {
var distancePointStartX = math.sqrt(
math.pow((celluloxPosition[0] - originCoordinates[0]), 2) +
math.pow((celluloxPosition[1] - xcelluloy), 2));
var distancePointStartY = math.sqrt(
math.pow((celluloyPosition[0] - ycellulox), 2) +
math.pow((celluloyPosition[1] - originCoordinates[1]), 2));
if (distancePointStartY < 10 && distancePointStartX < 10) {
celluloy.clearrobot();
cellulox.clearrobot();
setState(() {
allowPaint = true;
runHaptic = true;
});
}
}
if (runHaptic == true) {
calcHaptic();
}
});
timerCelluloPositionPainttoserver =
new Timer.periodic(new Duration(milliseconds: 1800), (time) {
if (allowPaint == true) {
dbRef.child("celluloPosition").push().set(json.encode({
"x": celluloxPosition[0],
"y": celluloyPosition[1],
"acID": "Ac6",
"turn": currentTurn,
"groupID": group.id,
}));
}
});
timerCelluloPositionPaint =
new Timer.periodic(new Duration(seconds: 1), (time) {
setState(() {
if (allowPaint == true) {
celluloxPositiontopaint[tapCounter - 1]
.add(celluloxPosition[0] * screenSizeWidth / mapSizeWidth);
celluloyPositiontopaint[tapCounter - 1]
.add(celluloyPosition[1] * screenSizeHeight / mapSizeHeight);
}
});
});
timerInactivityDetector =
new Timer.periodic(new Duration(seconds: 60), (time) {
onInactivityTimer();
});
///
/// Ask to be notified when messages related to the game
/// are sent by the server
///
}
void onDataSend() {
dbRef.child('attempts').push().set(json.encode({
"numAttempts": tapCounter,
"groupID": group.id,
"acID": "Ac6",
"elpasedTime": elapseTimer.elapsedMilliseconds -
progressElpasedTime[currentTurn - 1],
"progress": {
"turn1": progress[0],
"turn2": progress[1],
"turn3": progress[2]
},
"progressElpasedTime": {
"turn1": progressElpasedTime[0],
"turn2": progressElpasedTime[1],
"turn3": progressElpasedTime[2],
},
"currentTurn": currentTurn,
"inactivity": inactivity,
"mistakes": {
"turn1": {
"slope": mistakesSlope[0],
"initialPoint": mistakesInitialPosition[0]
},
"turn2": {
"slope": mistakesSlope[1],
"initialPoint": mistakesInitialPosition[1]
},
"turn3": {
"slope": mistakesSlope[2],
"initialPoint": mistakesInitialPosition[2]
},
}
}));
}
void slopeDtectionTimer() {
timerslopedetection = new Timer.periodic(new Duration(seconds: 1), (time) {
print("prev" +
(((celluloxPosition[0] - prevcelluloxPositionSlope).roundToDouble() >
0)
.toString()));
// print("currwnt" + celluloxPosition[0].toString());
controllerXslope.text = 1.0.toString();
if ((celluloxPosition[0] - prevcelluloxPositionSlope).abs() < 10) {
controllerYslope.text = "You sould move the RED robot faster";
} else {
controllerYslope.text =
(((prevcelluloyPositionSlope - celluloyPosition[1])) /
(celluloxPosition[0] - prevcelluloxPositionSlope))
.roundToDouble()
.toString();
}
prevcelluloxPositionSlope = celluloxPosition[0];
prevcelluloyPositionSlope = celluloyPosition[1];
// print(int.parse(controllerXslope.text) > 0);
print("cur" +
(((celluloxPosition[0] - prevcelluloxPositionSlope).roundToDouble() >
0)
.toString()));
if ((celluloxPosition[0] - prevcelluloxPositionSlope).roundToDouble() >
1) {
setState(() {
celluloxSwitch2 = true;
print(celluloxSwitch2);
});
}
if (((celluloxPosition[0] - prevcelluloxPositionSlope) / 100)
.roundToDouble() <
-1) {
setState(() {
celluloxSwitch2 = false;
print(celluloxSwitch2);
});
}
if (celluloyPosition[1] - prevcelluloyPositionSlope > 0) {
setState(() {
celluloySwitch2 = true;
print(celluloySwitch2);
});
}
if (celluloyPosition[1] - prevcelluloyPositionSlope < 1.0) {
setState(() {
celluloySwitch2 = false;
print(celluloySwitch2);
});
}
});
}
void onInactivityTimer() {
group.inactivity = group.inactivity + 1;
}
@override
void dispose() {
cellulox.clearrobot();
celluloy.clearrobot();
timer.cancel();
timerCelluloPosition.cancel();
timerCelluloPositionPaint.cancel();
timerCelluloPositionPainttoserver.cancel();
controllerTrial.dispose();
super.dispose();
}
AppLifecycleState _notification;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
dbRef.child('groups').child(group.id).child('tabletStatus').set("YES");
} else {
dbRef.child('groups').child(group.id).child('tabletStatus').set("NO");
}
setState(() {
_notification = state;
});
}
void nextPLayer() {
setState(() {
currentTurn = currentTurn + 1;
// celluloyPositiontopaint = [0.0, 0.0];
// celluloxPositiontopaint = [0.0, 0.0];
});
}
void animationRunner(progress) {}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Move your robots'),
),
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Column(children: <Widget>[
Container(
// margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(5.0),
decoration: BoxDecoration(),
height: 290.0,
child: Column(children: <Widget>[
Card(
color: Colors.blue,
child: ListTile(
title: Text(
' Each of you take one robot in each hand and try to follow the orange line',
style: new TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
),
SizedBox(
height: 10,
),
Row(
//mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 470,
child: Card(
color: Colors.blue,
child: ListTile(
title: Text(
(tapCounter == 0)
? names[0] + 'Start the game'
: 'When you tried, start again with a next member',
style: new TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
)),
SizedBox(width: 40),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(100)),
color: Colors.green),
child: FlatButton(
child: Text(
(tapCounter == 0) ? "Start" : "Start Again",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w700,
fontSize: 18),
),
// tooltip: 'Run the Robots',
onPressed: () => {
// avgActivation=
if (checkend == true)
{
if (tapCounter < 3)
{
setState(() {
checkend = true;
allowPaint = false;
runHaptic = false;
lineallow[tapCounter] = true;
tapCounter = tapCounter + 1;
}),
cellulox.setGoalPosition(
originCoordinates[0],
xcelluloy.toDouble(),
150),
celluloy.setGoalPosition(
ycellulox.toDouble(),
originCoordinates[1],
150),
onDataSend(),
// onDataSend(),
}
else
{
showAlertDialog(
context,
'Lets go to next turn',
'Lets try a new line'),
},
}
// controllerTrial.reset(),
// controllerTrial.forward(),
},
)),
SizedBox(width: 15),
]),
Row(
//mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 470,
child: Card(
color: Colors.blue,
child: ListTile(
title: Text(
' When you all tried, go to next turn.',
style: new TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
)),
SizedBox(width: 25),
SizedBox(width: 15),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(100)),
color: Colors.blue),
child: FlatButton(
child: Text(
"Next Line",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w700,
fontSize: 18),
),
onPressed: () => {
if (currentTurn < 3)
{
setState(() {
currentTurn = currentTurn + 1;
lineallow = [false, false, false];
runHaptic = false;
if (currentTurn == 2) {
progressElpasedTime[0] =
elapseTimer.elapsedMilliseconds;
} else {
progressElpasedTime[currentTurn - 2] =
elapseTimer.elapsedMilliseconds -
progressElpasedTime[
currentTurn - 3];
}
celluloxPositiontopaint[0] = [0, 0];
celluloyPositiontopaint[0] = [0, 0];
celluloxPositiontopaint[1] = [0, 0];
celluloyPositiontopaint[1] = [0, 0];
celluloxPositiontopaint[2] = [0, 0];
celluloyPositiontopaint[2] = [0, 0];
}),
// if (progress[currentTurn - 2] == 0)
progress[currentTurn - 2] = 1,
progress[currentTurn - 1] = 0,
onDataSend(),
setState(() {
allowPaint = false;
tapCounter = 0;
}),
}
else
{
progress[currentTurn - 1] = 1,
onDataSend(),
showAlertDialog(context, 'Wait for teacher',
'Game has finished! '),
},
},
)),
]),
])),
SizedBox(
height: 70,
),
celluloMap(
context,
celluloxPositiontopaint,
celluloyPositiontopaint,
celluloxPosition,
celluloyPosition,
mapPath,
linesPath[currentTurn - 1],
celluloxpath,
celluloypath,
mapSizeWidth,
mapSizeHeight,
'Right Hand',
'Left Hand',
xlabelposition,
ylabelposition,
lineallow),
SizedBox(
height: 125,
),
MembersBar(
curTurn: currentTurn,
),
])));
}
}

Event Timeline