Skip to content

Commit

Permalink
Merge pull request #1 from BZMplan/test
Browse files Browse the repository at this point in the history
Test
  • Loading branch information
BZMplan authored Jun 16, 2023
2 parents b3757f9 + f44bc12 commit 7607171
Show file tree
Hide file tree
Showing 2 changed files with 298 additions and 27 deletions.
218 changes: 218 additions & 0 deletions data/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
[
{
"id": 1,
"count": 0
},
{
"id": 2,
"count": 0
},
{
"id": 3,
"count": 0
},
{
"id": 4,
"count": 0
},
{
"id": 5,
"count": 0
},
{
"id": 6,
"count": 0
},
{
"id": 7,
"count": 0
},
{
"id": 8,
"count": 0
},
{
"id": 9,
"count": 0
},
{
"id": 10,
"count": 0
},
{
"id": 11,
"count": 0
},
{
"id": 12,
"count": 0
},
{
"id": 13,
"count": 0
},
{
"id": 14,
"count": 0
},
{
"id": 15,
"count": 0
},
{
"id": 16,
"count": 0
},
{
"id": 17,
"count": 0
},
{
"id": 18,
"count": 0
},
{
"id": 19,
"count": 0
},
{
"id": 20,
"count": 0
},
{
"id": 21,
"count": 0
},
{
"id": 22,
"count": 0
},
{
"id": 23,
"count": 0
},
{
"id": 24,
"count": 0
},
{
"id": 25,
"count": 0
},
{
"id": 26,
"count": 0
},
{
"id": 27,
"count": 0
},
{
"id": 28,
"count": 0
},
{
"id": 29,
"count": 0
},
{
"id": 30,
"count": 0
},
{
"id": 31,
"count": 0
},
{
"id": 32,
"count": 0
},
{
"id": 33,
"count": 0
},
{
"id": 34,
"count": 0
},
{
"id": 35,
"count": 0
},
{
"id": 36,
"count": 0
},
{
"id": 37,
"count": 0
},
{
"id": 38,
"count": 0
},
{
"id": 39,
"count": 0
},
{
"id": 40,
"count": 0
},
{
"id": 41,
"count": 0
},
{
"id": 42,
"count": 0
},
{
"id": 43,
"count": 0
},
{
"id": 44,
"count": 0
},
{
"id": 45,
"count": 0
},
{
"id": 46,
"count": 0
},
{
"id": 47,
"count": 0
},
{
"id": 48,
"count": 0
},
{
"id": 49,
"count": 0
},
{
"id": 50,
"count": 0
},
{
"id": 51,
"count": 0
},
{
"id": 52,
"count": 0
},
{
"id": 53,
"count": 0
},
{
"id": 54,
"count": 0
}
]
107 changes: 80 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern crate native_windows_gui as nwg;
use nwd::NwgUi;
use nwg::NativeUi;
use rand::Rng;
use serde_json::Value;

#[derive(Default, NwgUi)]
pub struct BasicApp {
Expand Down Expand Up @@ -93,33 +94,66 @@ impl BasicApp {
let student = rng.gen_range(0..stduents.len());
let id = usize_to_i32(student);

if check_file(id) {

let count = read_file(id);
let _ = write_file(id, count.to_string());
nwg::modal_info_message(
&self.window,
"恭喜",
&format!(
"恭喜 {} !\n目前你已经被抽到 {} 次",
stduents[student],
string_to_i32(count) + 1
),
);
} else {
let _ = create_file(id);
let count = read_file(id);
let _ = write_file(id, count.to_string());
nwg::modal_info_message(
&self.window,
"恭喜",
&format!(
"恭喜 {} !\n目前你已经被抽到 {} 次",
stduents[student],
string_to_i32(count) + 1
),
);
//first version 用文本文件存贮数据
// if check_file(id) {

// let count = read_file(id);
// let _ = write_file(id, count.to_string());
// nwg::modal_info_message(
// &self.window,
// "恭喜",
// &format!(
// "恭喜 {} !\n目前你已经被抽到 {} 次",
// stduents[student],
// string_to_i32(count) + 1
// ),
// );
// } else {
// let _ = create_file(id);
// let count = read_file(id);
// let _ = write_file(id, count.to_string());
// nwg::modal_info_message(
// &self.window,
// "恭喜",
// &format!(
// "恭喜 {} !\n目前你已经被抽到 {} 次",
// stduents[student],
// string_to_i32(count) + 1
// ),
// );
// }

//second version 用json文件存储数据

let mut file = File::open("data/data.json").expect("Unable to open file");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Unable to read data from file");

// 将JSON字符串解析为serde_json::Value
let mut data: Value = serde_json::from_str(&contents).unwrap();

// 修改JSON数组中某个对象的某个值
if let Some(people) = data.as_array_mut() {
if let Some(person) = people.get_mut(student) {
if let Some(count) = person["count"].as_u64() {
person["count"] = Value::from(count + 1);
}
}
}

let count = get_info(data.to_owned(), student);

// 将修改后的JSON对象序列化为字符串并写回文件
let json_string = serde_json::to_string_pretty(&data).unwrap();
let mut file = File::create("data/data.json").expect("Unable to create file");
file.write_all(json_string.as_bytes())
.expect("Unable to write data to file");
nwg::modal_info_message(
&self.window,
"恭喜",
&format!("恭喜 {} !\n目前你已经被抽到 {} 次", stduents[student],count),
);
}
//当软件关闭的时候
fn when_app_close(&self) {
Expand All @@ -138,7 +172,7 @@ fn main() {

use std::{
fs::{self, File},
io::{self, Write},
io::{self, Read, Write},
};

fn read_file(id: i32) -> String {
Expand Down Expand Up @@ -182,6 +216,25 @@ fn check_file(id: i32) -> bool {
result
}

fn get_info(mut data:Value,student:usize) ->u64{
//读取数据
if let Some(people) = data.as_array_mut() {
if let Some(person) = people.get_mut(student) {
if let Some(count) = person["count"].as_u64() {
count
}
else {
todo!();
}
}else {
todo!();
}
}
else {
todo!();
}
}

fn usize_to_i32(usize: usize) -> i32 {
usize as i32
}
Expand Down

0 comments on commit 7607171

Please sign in to comment.