diff --git a/lib/windows/number_tool.dart b/lib/windows/number_tool.dart index 06f110a..7d4694f 100644 --- a/lib/windows/number_tool.dart +++ b/lib/windows/number_tool.dart @@ -7,18 +7,42 @@ class NumberTool { /// @allNum: 随机数的和 /// @num: 随机数的数量 List randomNum(int allNum, {int num = 10}) { - List randomNumbers = []; - int remainingNum = allNum; + Random random = Random(); + + // 基础数字 最小值 + int base = allNum ~/ 1.5 ~/ num; + List numbers = List.generate(num, (index) => base); + for (int i = 0; i < num - 1; i++) { - int randomNumber = Random().nextInt(remainingNum) + 1; - randomNumbers.add(randomNumber); - remainingNum -= randomNumber; + int randomNumber = random.nextInt(base); // 生成0到base之间的随机整数 + numbers[i] += 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; } } diff --git a/lib/windows/socket_tool.dart b/lib/windows/socket_tool.dart index 2217bd0..84165fb 100644 --- a/lib/windows/socket_tool.dart +++ b/lib/windows/socket_tool.dart @@ -6,6 +6,7 @@ import 'package:web_synchronization_tool/windows/synchronization_web_tool.dart'; import 'little_extension.dart'; import 'code.dart'; +import 'number_tool.dart'; class SocketUtils extends Socket{ // 私有构造函数 @@ -23,7 +24,7 @@ class SocketUtils extends Socket{ class Socket { - static String url = '127.0.0.1'; + String url = '127.0.0.1'; static int port = 12345; RawDatagramSocket? socket; @@ -31,8 +32,11 @@ class Socket { // IC 0主机 1 从机 int ic = 1; + /// 从机ip + List childrenIp = []; + connect() async { - socket = await RawDatagramSocket.bind('127.0.0.1', 25501); + socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0); // 发送身份包 Map data = { @@ -53,31 +57,68 @@ class Socket { print(data); - Map click = data['value']['click']; - int x = click['x'] as int; - int y = click['y'] as int; + //点击 + if (data['click'] != null){ + 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 nums = NumberTool().randomNum(input); + SynchronizationWebTool.getInstance().input(nums); + } } + // 心跳 if (data['op'] == 11){ } + // 服务器 已连接的ip + if (data['Arr'] != null){ + List arr = data['Arr']; + List ips = []; + arr.forEach((clientMap) { + String ip = clientMap['Client']; + ips.add(ip); + }); + childrenIp = ips; + } }); } /// 发送消息 - sendMessage(Map data){ + sendClickMessage(Map data){ Map messageMap = { "op": 2, - "value": data + "click": data }; 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(){ diff --git a/lib/windows/synchronization_web_tool.dart b/lib/windows/synchronization_web_tool.dart index fa79eef..5c3aeea 100644 --- a/lib/windows/synchronization_web_tool.dart +++ b/lib/windows/synchronization_web_tool.dart @@ -54,7 +54,7 @@ class SynchronizationWebTool{ int y = click['y'] as int; clickSynchronization(x, y); - SocketUtils.getInstance().sendMessage(event); + SocketUtils.getInstance().sendClickMessage(click); } if (event['scroll'] != null ) { diff --git a/lib/windows/web_grid_view.dart b/lib/windows/web_grid_view.dart index 2e7bc67..0e3a44e 100644 --- a/lib/windows/web_grid_view.dart +++ b/lib/windows/web_grid_view.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.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/windowsJs.dart'; import 'package:webview_windows/webview_windows.dart'; @@ -115,8 +116,12 @@ class ShowWebWidget extends StatefulWidget { class _ShowWebWidgetState extends State { - TextEditingController textController = TextEditingController(); + /// 网址 + TextEditingController urlController = TextEditingController(); + /// 总金额 TextEditingController numController = TextEditingController(); + /// 服务器ip + TextEditingController ipController = TextEditingController(); @override void dispose() { @@ -152,29 +157,7 @@ class _ShowWebWidgetState extends State { widget.controller.openDevTools(); }, child: const Text('开发者')), - Row( - 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 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('填入总金额')), - ], - ), + inputNumber(), Row( children: [ const Text('同步'), @@ -183,21 +166,26 @@ class _ShowWebWidgetState extends State { onChanged: (value) { setState(() { SynchronizationWebTool.getInstance().webSync = value; + if (value){ // 打开同步获取 从机 + SocketUtils.getInstance().sendGetChilds(); + } }); }) ], ), - input() + input(), + ipSet() ], ); } + /// 跳转网址 input(){ return Row( 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: (){ - String url = textController.text; + String url = urlController.text; // if (!url.startsWith("https://") ) { // url = "https://$url"; // } @@ -207,6 +195,53 @@ class _ShowWebWidgetState extends State { ); } + 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 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('填入总金额')), + ], + ); + } + } diff --git a/lib/windows/windows_main_page.dart b/lib/windows/windows_main_page.dart index b64c20d..2228668 100644 --- a/lib/windows/windows_main_page.dart +++ b/lib/windows/windows_main_page.dart @@ -26,7 +26,7 @@ class _WindowsPageState extends State { SocketUtils.getInstance().connect(); SocketUtils.getInstance().heartbeat(); - + controllerInit(); }