点击 和 输入 全部完成的初版

This commit is contained in:
WoNiu 2024-04-03 21:53:27 +08:00
parent 5c9ec8a98b
commit 8e8bf0d53c
5 changed files with 144 additions and 44 deletions

View File

@ -7,18 +7,42 @@ class NumberTool {
/// @allNum: /// @allNum:
/// @num: /// @num:
List<int> randomNum(int allNum, {int num = 10}) { List<int> randomNum(int allNum, {int num = 10}) {
List<int> randomNumbers = []; Random random = Random();
int remainingNum = allNum;
//
int base = allNum ~/ 1.5 ~/ num;
List<int> numbers = List.generate(num, (index) => base);
for (int i = 0; i < num - 1; i++) { for (int i = 0; i < num - 1; i++) {
int randomNumber = Random().nextInt(remainingNum) + 1; int randomNumber = random.nextInt(base); // 0base之间的随机整数
randomNumbers.add(randomNumber); numbers[i] += randomNumber;
remainingNum -= randomNumber;
} }
randomNumbers.add(allNum - randomNumbers.fold(0, (prev, element) => prev + element)); int sum = numbers.reduce((a, b) => a + b) ;
int lastNumber = allNum - sum;
return randomNumbers; // /2 /
base = (lastNumber / 2) ~/ (num);
for (int i = 0; i < num - 1; i++) {
numbers[i] += base;
}
sum = numbers.reduce((a, b) => a + b);
lastNumber = allNum - sum;
//
base = lastNumber ~/ 2;
numbers[ random.nextInt(num - 1)] = base;
sum = numbers.reduce((a, b) => a + b);
lastNumber = allNum - sum;
numbers.add(lastNumber);
//
numbers.shuffle();
return numbers;
} }
} }

View File

@ -6,6 +6,7 @@ import 'package:web_synchronization_tool/windows/synchronization_web_tool.dart';
import 'little_extension.dart'; import 'little_extension.dart';
import 'code.dart'; import 'code.dart';
import 'number_tool.dart';
class SocketUtils extends Socket{ class SocketUtils extends Socket{
// //
@ -23,7 +24,7 @@ class SocketUtils extends Socket{
class Socket { class Socket {
static String url = '127.0.0.1'; String url = '127.0.0.1';
static int port = 12345; static int port = 12345;
RawDatagramSocket? socket; RawDatagramSocket? socket;
@ -31,8 +32,11 @@ class Socket {
// IC 0 1 // IC 0 1
int ic = 1; int ic = 1;
/// ip
List<String> childrenIp = [];
connect() async { connect() async {
socket = await RawDatagramSocket.bind('127.0.0.1', 25501); socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0);
// //
Map data = { Map data = {
@ -53,31 +57,68 @@ class Socket {
print(data); print(data);
Map click = data['value']['click']; //
int x = click['x'] as int; if (data['click'] != null){
int y = click['y'] as int; Map click = data['click'];
int x = click['x'] as int;
int y = click['y'] as int;
SynchronizationWebTool.getInstance().clickSynchronization(x, y); SynchronizationWebTool.getInstance().clickSynchronization(x, y);
}
//
if (data['input'] != null){
int input = data['input'];
List<int> nums = NumberTool().randomNum(input);
SynchronizationWebTool.getInstance().input(nums);
}
} }
// //
if (data['op'] == 11){ if (data['op'] == 11){
} }
// ip
if (data['Arr'] != null){
List arr = data['Arr'];
List<String> ips = [];
arr.forEach((clientMap) {
String ip = clientMap['Client'];
ips.add(ip);
});
childrenIp = ips;
}
}); });
} }
/// ///
sendMessage(Map data){ sendClickMessage(Map data){
Map messageMap = { Map messageMap = {
"op": 2, "op": 2,
"value": data "click": data
}; };
socket?.send(dataMake(messageMap), InternetAddress(url), port); socket?.send(dataMake(messageMap), InternetAddress(url), port);
} }
///
sendGetChilds(){
Map messageMap = {
"op": 3,
};
socket?.send(dataMake(messageMap), InternetAddress(url), port);
}
///
sendInpuMessage(int num){
Map messageMap = {
"op": 2,
"input": num
};
socket?.send(dataMake(messageMap), InternetAddress(url), port);
}
/// ///
heartbeat(){ heartbeat(){

View File

@ -54,7 +54,7 @@ class SynchronizationWebTool{
int y = click['y'] as int; int y = click['y'] as int;
clickSynchronization(x, y); clickSynchronization(x, y);
SocketUtils.getInstance().sendMessage(event); SocketUtils.getInstance().sendClickMessage(click);
} }
if (event['scroll'] != null ) { if (event['scroll'] != null ) {

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:web_synchronization_tool/windows/number_tool.dart'; import 'package:web_synchronization_tool/windows/number_tool.dart';
import 'package:web_synchronization_tool/windows/socket_tool.dart';
import 'package:web_synchronization_tool/windows/synchronization_web_tool.dart'; import 'package:web_synchronization_tool/windows/synchronization_web_tool.dart';
import 'package:web_synchronization_tool/windows/windowsJs.dart'; import 'package:web_synchronization_tool/windows/windowsJs.dart';
import 'package:webview_windows/webview_windows.dart'; import 'package:webview_windows/webview_windows.dart';
@ -115,8 +116,12 @@ class ShowWebWidget extends StatefulWidget {
class _ShowWebWidgetState extends State<ShowWebWidget> { class _ShowWebWidgetState extends State<ShowWebWidget> {
TextEditingController textController = TextEditingController(); ///
TextEditingController urlController = TextEditingController();
///
TextEditingController numController = TextEditingController(); TextEditingController numController = TextEditingController();
/// ip
TextEditingController ipController = TextEditingController();
@override @override
void dispose() { void dispose() {
@ -152,29 +157,7 @@ class _ShowWebWidgetState extends State<ShowWebWidget> {
widget.controller.openDevTools(); widget.controller.openDevTools();
}, },
child: const Text('开发者')), child: const Text('开发者')),
Row( inputNumber(),
children: [
SizedBox(width: 100, child: TextField(style: const TextStyle(fontSize: 14),controller: numController,)),
TextButton(
onPressed: () {
try{
int num = int.parse(numController.text);
if (num <= 10){
numController.text = '金额不能小于10';
return;
}
List<int> nums = NumberTool().randomNum(num,num: SynchronizationWebTool.getInstance().childController.length);
widget.controller.executeScript(WindowsJs.inputJs(nums.first));
nums.removeAt(0);
SynchronizationWebTool.getInstance().input(nums);
}catch(e){
numController.text = '请输入数字';
}
},
child: const Text('填入总金额')),
],
),
Row( Row(
children: [ children: [
const Text('同步'), const Text('同步'),
@ -183,21 +166,26 @@ class _ShowWebWidgetState extends State<ShowWebWidget> {
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
SynchronizationWebTool.getInstance().webSync = value; SynchronizationWebTool.getInstance().webSync = value;
if (value){ //
SocketUtils.getInstance().sendGetChilds();
}
}); });
}) })
], ],
), ),
input() input(),
ipSet()
], ],
); );
} }
///
input(){ input(){
return Row( return Row(
children: [ children: [
SizedBox(width: 400, child: TextField(style: const TextStyle(fontSize: 14),controller: textController,)), SizedBox(width: 400, child: TextField(style: const TextStyle(fontSize: 14),controller: urlController,)),
TextButton(onPressed: (){ TextButton(onPressed: (){
String url = textController.text; String url = urlController.text;
// if (!url.startsWith("https://") ) { // if (!url.startsWith("https://") ) {
// url = "https://$url"; // url = "https://$url";
// } // }
@ -207,6 +195,53 @@ class _ShowWebWidgetState extends State<ShowWebWidget> {
); );
} }
ipSet(){
ipController.text = SocketUtils.getInstance().url;
return Row(
children: [
SizedBox(width: 100, child: TextField(style: const TextStyle(fontSize: 14),controller: ipController,)),
TextButton(onPressed: (){
SocketUtils.getInstance().url = ipController.text;
SocketUtils.getInstance().connect();
}, child: const Text('保存服务器ip',style: TextStyle(fontSize: 14),))
],
);
}
///
inputNumber(){
return Row(
children: [
SizedBox(width: 100, child: TextField(style: const TextStyle(fontSize: 14),controller: numController,)),
TextButton(
onPressed: () {
try{
int num = int.parse(numController.text);
int chidNum = SocketUtils.getInstance().childrenIp.length + 1;
if (num <= chidNum * 10){
numController.text = '金额不能小于${chidNum*10}';
return;
}
int maxNum = num ~/ chidNum;
SocketUtils.getInstance().sendInpuMessage(maxNum);
//
List<int> nums = NumberTool().randomNum(maxNum);
widget.controller.executeScript(WindowsJs.inputJs(nums.first));
nums.removeAt(0);
SynchronizationWebTool.getInstance().input(nums);
}catch(e){
numController.text = '请输入数字';
}
},
child: const Text('填入总金额')),
],
);
}
} }

View File

@ -26,7 +26,7 @@ class _WindowsPageState extends State<WindowsPage> {
SocketUtils.getInstance().connect(); SocketUtils.getInstance().connect();
SocketUtils.getInstance().heartbeat(); SocketUtils.getInstance().heartbeat();
controllerInit(); controllerInit();
} }