Skip to content

Commit 37c02f3

Browse files
committed
分割した
1 parent ed790ce commit 37c02f3

File tree

3 files changed

+225
-223
lines changed

3 files changed

+225
-223
lines changed

src/bin/app.rs

Lines changed: 1 addition & 223 deletions
Original file line numberDiff line numberDiff line change
@@ -1,226 +1,4 @@
1-
use reqwest::blocking as reqwest;
2-
use parser::*;
3-
use app::*;
4-
use itertools::*;
5-
6-
#[derive(Debug, Clone)]
7-
pub struct Response {
8-
pub stage: i32,
9-
pub info: Info,
10-
pub state: State
11-
}
12-
13-
#[derive(Debug, Clone)]
14-
pub struct Info {
15-
pub x0: E,
16-
pub role: i32,
17-
pub x2: E,
18-
pub x3: E,
19-
pub x4: E,
20-
}
21-
22-
#[derive(Debug, Clone)]
23-
pub struct State {
24-
pub tick: i32,
25-
pub x1: E,
26-
pub ships: Vec<Ship>
27-
}
28-
29-
#[derive(Debug, Clone)]
30-
pub struct Ship {
31-
pub role: i32,
32-
pub id: i32,
33-
pub pos: (i32, i32),
34-
pub v: (i32, i32),
35-
pub x4: E,
36-
pub x5: E,
37-
pub x6: E,
38-
pub x7: E,
39-
pub commands: Vec<Command>,
40-
}
41-
42-
#[derive(Debug, Clone)]
43-
pub enum Command {
44-
Accelerate(i32, (i32, i32)),
45-
Detonate(i32),
46-
Shoot(i32, (i32, i32), i32),
47-
Unknown,
48-
}
49-
50-
impl std::fmt::Display for Command {
51-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52-
match self {
53-
Command::Accelerate(id, v) => {
54-
write!(f, "[0, {}, <{}, {}>]", id, v.0, v.1)?
55-
},
56-
Command::Detonate(id) => {
57-
write!(f, "[1, {}]", id)?
58-
},
59-
Command::Shoot(id, t, x3) => {
60-
write!(f, "[2, {}, <{}, {}>, {}]", id, t.0, t.1, x3)?
61-
},
62-
_ => {
63-
panic!("unreachable");
64-
}
65-
}
66-
Ok(())
67-
}
68-
}
69-
70-
impl From<&E> for Command {
71-
fn from(e: &E) -> Command {
72-
let e = get_list(e).unwrap();
73-
match get_num(&e[0]) {
74-
0 => {
75-
Command::Accelerate(-1, get_pair(&e[1]))
76-
},
77-
1 => {
78-
Command::Detonate(-1)
79-
},
80-
2 => {
81-
Command::Shoot(-1, get_pair(&e[1]), get_num(&e[2]))
82-
},
83-
_ => {
84-
Command::Unknown
85-
}
86-
}
87-
}
88-
}
89-
90-
struct Client {
91-
server_url: String,
92-
player_key: String,
93-
client: reqwest::Client
94-
}
95-
96-
impl Client {
97-
pub fn new(server_url: String) -> Self {
98-
Self {
99-
server_url,
100-
player_key: String::new(),
101-
client: reqwest::Client::new()
102-
}
103-
}
104-
pub fn send(&self, msg: &str) -> E {
105-
eprintln!("send: {}", msg);
106-
let msg = to_text(&parse_lisp(msg).0);
107-
let ss = msg.split_whitespace().collect::<Vec<_>>();
108-
let (exp, n) = parser::parse(&ss, 0);
109-
assert_eq!(n, ss.len());
110-
let e = parser::eval(&exp, true);
111-
let msg = modulation::modulate(&e);
112-
eprintln!("send: {}", msg);
113-
let resp = self.client.post(&self.server_url).body(msg).send().unwrap().text().unwrap();
114-
eprintln!("resp: {}", resp);
115-
let resp = modulation::demodulate(&resp);
116-
eprintln!("resp: {}", resp);
117-
resp
118-
}
119-
pub fn join(&mut self, player_key: &str) -> Response {
120-
self.player_key = player_key.to_owned();
121-
let resp = self.send(&format!("[2, {}, [192496425430, 103652820]]", player_key));
122-
parse(resp)
123-
}
124-
pub fn start(&self, x0: i32, x1: i32, x2: i32, x3: i32) -> Response {
125-
let resp = self.send(&format!("[3, {}, [{}, {}, {}, {}]]", self.player_key, x0, x1, x2, x3));
126-
parse(resp)
127-
}
128-
pub fn command(&self, cs: &[Command]) -> Response {
129-
let resp = self.send(&format!("[4, {}, [{}]]", self.player_key, cs.iter().join(", ")));
130-
parse(resp)
131-
}
132-
}
133-
134-
fn get_num(a: &E) -> i32 {
135-
if let E::Num(a) = a {
136-
*a as i32
137-
} else {
138-
panic!("not number");
139-
}
140-
}
141-
142-
fn get_pair(a: &E) -> (i32, i32) {
143-
if let E::Pair(a, b) = a {
144-
(get_num(a), get_num(b))
145-
} else {
146-
panic!("not pair");
147-
}
148-
}
149-
150-
pub fn parse(e: E) -> Response {
151-
let a = get_list(&e).unwrap();
152-
assert_eq!(a.len(), 4);
153-
assert_eq!(get_num(&a[0]), 1);
154-
let stage = get_num(&a[1]);
155-
let info = get_list(&a[2]).unwrap();
156-
let x0 = info[0].as_ref().clone();
157-
let role = get_num(&info[1]);
158-
let x2 = info[2].as_ref().clone();
159-
let x3 = info[3].as_ref().clone();
160-
let x4 = info[4].as_ref().clone();
161-
let state = get_list(&a[3]).unwrap();
162-
let (tick, x1, ships) = if state.len() > 0 {
163-
let tick = get_num(&state[0]);
164-
let x1 = state[1].as_ref().clone();
165-
let ships = get_list(&state[2]).unwrap().into_iter().map(|a| {
166-
let tmp = get_list(&a).unwrap();
167-
let s = get_list(&tmp[0]).unwrap();
168-
let commands = get_list(&tmp[1]).unwrap();
169-
let role = get_num(&s[0]);
170-
let id = get_num(&s[1]); // shipId
171-
let pos = get_pair(&s[2]);
172-
let v = get_pair(&s[3]);
173-
let x4 = s[4].as_ref().clone();
174-
let x5 = s[5].as_ref().clone();
175-
let x6 = s[6].as_ref().clone();
176-
let x7 = s[7].as_ref().clone();
177-
// [1, 1, [256, 1, [448, 2, 128], [16, 128], []], [1, [16, 128], [[[1, 0, <34, -46>, <0, 2>, [445, 0, 0, 1], 8, 128, 2], [[0, <0, -1>]]], [[0, 1, <-34, 48>, <0, 0>, [445, 0, 0, 1], 8, 128, 2], [[0, <0, -1>]]]]]]
178-
// [src/bin/app.rs:177] &commands = [
179-
// Pair(
180-
// Num(
181-
// 0,
182-
// ),
183-
// Pair(
184-
// Pair(
185-
// Num(
186-
// 0,
187-
// ),
188-
// Num(
189-
// -1,
190-
// ),
191-
// ),
192-
// Nil,
193-
// ),
194-
// ),
195-
// ]
196-
197-
let commands = commands.into_iter().map(|e| e.as_ref().into()).collect();
198-
Ship {
199-
role,
200-
id,
201-
pos,
202-
v,
203-
x4,
204-
x5,
205-
x6,
206-
x7,
207-
commands
208-
}
209-
}).collect();
210-
(tick, x1, ships)
211-
} else {
212-
(0, E::Nil, vec![])
213-
};
214-
Response {
215-
stage,
216-
info: Info {
217-
x0, role, x2, x3, x4
218-
},
219-
state: State {
220-
tick, x1, ships
221-
}
222-
}
223-
}
1+
use app::client::*;
2242

2253
fn run() {
2264
let server_url = std::env::args().nth(1).unwrap();

0 commit comments

Comments
 (0)