点击 和 输入 全部完成的初版
This commit is contained in:
parent
5c9ec8a98b
commit
8e8bf0d53c
|
|
@ -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); // 生成0到base之间的随机整数
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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(){
|
||||||
|
|
|
||||||
|
|
@ -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 ) {
|
||||||
|
|
|
||||||
|
|
@ -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('填入总金额')),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ class _WindowsPageState extends State<WindowsPage> {
|
||||||
|
|
||||||
SocketUtils.getInstance().connect();
|
SocketUtils.getInstance().connect();
|
||||||
SocketUtils.getInstance().heartbeat();
|
SocketUtils.getInstance().heartbeat();
|
||||||
|
|
||||||
controllerInit();
|
controllerInit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue