1#![expect(clippy::wildcard_imports)]
5
6mod battle;
7mod chat;
8mod cheat;
9mod city;
10mod continent;
11mod infrastructure;
12mod military;
13mod npc;
14mod player;
15mod ranking;
16mod report;
17mod round;
18mod user;
19mod world;
20
21use mlua::{LuaSerdeExt, UserData, UserDataMethods};
22use nil_client::Client;
23use serde::Serialize;
24use std::sync::Arc;
25use tap::Pipe;
26use tokio::sync::RwLock;
27
28pub struct ClientUserData {
29 client: Arc<RwLock<Client>>,
30}
31
32impl ClientUserData {
33 pub fn new(client: Arc<RwLock<Client>>) -> Self {
34 Self { client }
35 }
36
37 async fn client<F, T>(&self, f: F) -> T
38 where
39 F: AsyncFnOnce(&Client) -> T,
40 T: Serialize,
41 {
42 f(&*self.client.read().await).await
43 }
44}
45
46impl UserData for ClientUserData {
47 fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
48 battle::add_methods(methods);
49 chat::add_methods(methods);
50 cheat::behavior::add_methods(methods);
51 cheat::city::add_methods(methods);
52 cheat::infrastructure::add_methods(methods);
53 cheat::military::add_methods(methods);
54 cheat::npc::add_methods(methods);
55 cheat::player::add_methods(methods);
56 cheat::resources::add_methods(methods);
57 cheat::round::add_methods(methods);
58 city::add_methods(methods);
59 continent::add_methods(methods);
60 infrastructure::add_methods(methods);
61 infrastructure::academy::add_methods(methods);
62 infrastructure::prefecture::add_methods(methods);
63 infrastructure::stable::add_methods(methods);
64 infrastructure::workshop::add_methods(methods);
65 military::add_methods(methods);
66 npc::bot::add_methods(methods);
67 npc::precursor::add_methods(methods);
68 player::add_methods(methods);
69 ranking::add_methods(methods);
70 report::add_methods(methods);
71 round::add_methods(methods);
72 user::add_methods(methods);
73 world::add_methods(methods);
74
75 methods.add_async_method("getServerKind", async |lua, this, ()| {
76 this
77 .client(async |it| it.get_server_kind().await)
78 .await
79 .map(|it| lua.to_value(&it))?
80 });
81
82 methods.add_async_method("getServerVersion", async |lua, this, ()| {
83 this
84 .client(async |it| it.get_server_version().await)
85 .await
86 .map(|it| lua.to_value(&it))?
87 });
88
89 methods.add_async_method("isServerLocal", async |lua, this, ()| {
90 this
91 .client(async |it| it.is_local())
92 .await
93 .pipe(|it| lua.to_value(&it))
94 });
95
96 methods.add_async_method("isServerReady", async |lua, this, ()| {
97 this
98 .client(async |it| it.is_ready().await)
99 .await
100 .pipe(|it| lua.to_value(&it))
101 });
102
103 methods.add_async_method("isServerRemote", async |lua, this, ()| {
104 this
105 .client(async |it| it.is_remote())
106 .await
107 .pipe(|it| lua.to_value(&it))
108 });
109
110 methods.add_async_method("server", async |lua, this, ()| {
111 this
112 .client(async |it| it.server())
113 .await
114 .pipe(|it| lua.to_value(&it))
115 });
116
117 methods.add_async_method("userAgent", async |lua, this, ()| {
118 this
119 .client(async |it| it.user_agent().to_owned())
120 .await
121 .pipe(|it| lua.to_value(&it))
122 });
123
124 methods.add_method("version", |lua, _, ()| lua.to_value(nil_client::VERSION));
125
126 methods.add_async_method("world", async |lua, this, ()| {
127 this
128 .client(async |it| it.world())
129 .await
130 .pipe(|it| lua.to_value(&it))
131 });
132 }
133}