1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
Copyright 2024 Eric Stokes.

This file is part of dcso3.

dcso3 is free software: you can redistribute it and/or modify it under
the terms of the MIT License.

dcso3 is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.
*/

use super::{as_tbl, coalition::Side, cvt_err, String};
use crate::{
    env::miz::UnitId, lua_err, simple_enum, wrapped_prim, wrapped_table, LuaEnv, Sequence,
};
use anyhow::Result;
use compact_str::format_compact;
use core::fmt;
use mlua::{prelude::*, Value};
use serde_derive::{Deserialize, Serialize};
use std::{ops::Deref, str::FromStr};

simple_enum!(PlayerStat, u8, [
    Car => 2,
    Crash => 1,
    Eject => 7,
    ExtraAllyAAA => 17,
    ExtraAllyFighters => 14,
    ExtraAllySAM => 16,
    ExtraAllyTransports => 15,
    ExtraAllyTroops => 18,
    ExtraAllyCoalition => 19,
    ExtraEnemyAAA => 12,
    ExtraEnemyFighters => 9,
    ExtraEnemySAM => 11,
    ExtraEnemyTransports => 10,
    ExtraEnemyTroops => 13,
    Land => 6,
    Num => 20,
    OldScore => 8,
    Ping => 0,
    Plane => 3,
    Score => 5,
    Ship => 4
]);

wrapped_prim!(PlayerId, i64, Copy, Hash);

impl FromStr for PlayerId {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        Ok(Self(s.parse::<i64>()?))
    }
}

impl fmt::Display for PlayerId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(into = "crate::String")]
#[serde(try_from = "crate::String")]
pub enum SlotId {
    Unit(i64),
    MultiCrew(i64, u8),
    Spectator,
    ArtilleryCommander(Side, u8),
    ForwardObserver(Side, u8),
    Observer(Side, u8),
    Instructor(Side, u8),
}

impl<'lua> FromLua<'lua> for SlotId {
    fn from_lua(value: Value<'lua>, _: &'lua Lua) -> LuaResult<Self> {
        match value {
            Value::Integer(i) => {
                if i == 0 {
                    Ok(Self::Spectator)
                } else {
                    Ok(Self::Unit(i))
                }
            }
            Value::Number(i) => {
                let i = i as i64;
                if i == 0 {
                    Ok(Self::Spectator)
                } else {
                    Ok(Self::Unit(i))
                }
            }
            Value::String(s) => Self::parse_string_slot(s.to_str().map_err(lua_err)?.trim()),
            v => Err(lua_err(format!("invalid slot type {:?}", v))),
        }
    }
}

impl<'lua> IntoLua<'lua> for SlotId {
    fn into_lua(self, lua: &'lua Lua) -> LuaResult<Value<'lua>> {
        match self {
            Self::Unit(i) => {
                if i < 1 {
                    return Err(lua_err("invalid unit number"));
                }
                Ok(Value::Integer(i))
            }
            Self::Spectator => Ok(Value::Integer(0)),
            Self::ArtilleryCommander(s, n) => {
                if n < 1 {
                    return Err(lua_err("invalid ca slot number"));
                }
                String(format_compact!("artillery_commander_{s}_{n}")).into_lua(lua)
            }
            Self::ForwardObserver(s, n) => {
                if n < 1 {
                    return Err(lua_err("invalid ca slot number"));
                }
                String(format_compact!("forward_observer_{s}_{n}")).into_lua(lua)
            }
            Self::Instructor(s, n) => {
                if n < 1 {
                    return Err(lua_err("invalid ca slot number"));
                }
                String(format_compact!("instructor_{s}_{n}")).into_lua(lua)
            }
            Self::Observer(s, n) => {
                if n < 1 {
                    return Err(lua_err("invalid ca slot number"));
                }
                String(format_compact!("observer_{s}_{n}")).into_lua(lua)
            }
            Self::MultiCrew(i, n) => {
                if n < 1 {
                    return Err(lua_err("invalid multi crew slot number"));
                }
                String(format_compact!("{i}_{n}")).into_lua(lua)
            }
        }
    }
}

impl TryFrom<String> for SlotId {
    type Error = anyhow::Error;

    fn try_from(s: String) -> Result<Self> {
        match s.parse::<i64>() {
            Ok(i) => {
                if i == 0 {
                    Ok(Self::Spectator)
                } else {
                    Ok(Self::Unit(i))
                }
            }
            Err(_) => Ok(Self::parse_string_slot(s.as_str())?),
        }
    }
}

impl Into<String> for SlotId {
    fn into(self) -> String {
        String::from(match self {
            Self::Unit(i) => format_compact!("{i}"),
            Self::Spectator => format_compact!("0"),
            Self::ArtilleryCommander(s, n) => format_compact!("artillery_commander_{s}_{n}"),
            Self::ForwardObserver(s, n) => format_compact!("forward_observer_{s}_{n}"),
            Self::Instructor(s, n) => format_compact!("instructor_{s}_{n}"),
            Self::Observer(s, n) => format_compact!("observer_{s}_{n}"),
            Self::MultiCrew(i, n) => format_compact!("{i}_{n}"),
        })
    }
}

impl From<UnitId> for SlotId {
    fn from(value: UnitId) -> Self {
        Self::Unit(value.inner())
    }
}

impl SlotId {
    fn parse_string_slot(s: &str) -> LuaResult<SlotId> {
        fn side_and_num(s: &str) -> LuaResult<(Side, u8)> {
            match s.split_once("_") {
                None => Err(lua_err(format_compact!("side number {s}"))),
                Some((s, n)) => {
                    let side = match s {
                        "red" => Ok(Side::Red),
                        "blue" => Ok(Side::Blue),
                        "neutrals" => Ok(Side::Neutral),
                        s => Err(lua_err(format_compact!("slot side {s}"))),
                    }?;
                    let n = n.parse::<u8>().map_err(lua_err)?;
                    Ok((side, n))
                }
            }
        }
        if s == "" {
            Ok(Self::Spectator)
        } else if let Ok(i) = s.parse::<i64>() {
            if i == 0 {
                Ok(Self::Spectator)
            } else {
                Ok(Self::Unit(i))
            }
        } else if let Some(s) = s.strip_prefix("artillery_commander_") {
            let (side, n) = side_and_num(s)?;
            Ok(Self::ArtilleryCommander(side, n))
        } else if let Some(s) = s.strip_prefix("observer_") {
            let (side, n) = side_and_num(s)?;
            Ok(Self::Observer(side, n))
        } else if let Some(s) = s.strip_prefix("forward_observer_") {
            let (side, n) = side_and_num(s)?;
            Ok(Self::ForwardObserver(side, n))
        } else if let Some(s) = s.strip_prefix("instructor_") {
            let (side, n) = side_and_num(s)?;
            Ok(Self::Instructor(side, n))
        } else {
            match s.split_once("_") {
                None => Err(lua_err(format!("invalid string slot {s}"))),
                Some((i, n)) => {
                    let i = i.parse::<i64>().map_err(lua_err)?;
                    let n = n.parse::<u8>().map_err(lua_err)?;
                    Ok(Self::MultiCrew(i, n))
                }
            }
        }
    }

    pub fn is_artillery_commander(&self) -> bool {
        match self {
            Self::ArtilleryCommander(_, _) => true,
            _ => false,
        }
    }

    pub fn is_observer(&self) -> bool {
        match self {
            Self::Observer(_, _) => true,
            _ => false,
        }
    }

    pub fn is_forward_observer(&self) -> bool {
        match self {
            Self::ForwardObserver(_, _) => true,
            _ => false,
        }
    }

    pub fn is_instructor(&self) -> bool {
        match self {
            Self::Instructor(_, _) => true,
            _ => false,
        }
    }

    pub fn is_spectator(&self) -> bool {
        match self {
            Self::Spectator => true,
            _ => false,
        }
    }

    pub fn as_unit_id(&self) -> Option<UnitId> {
        match self {
            Self::Unit(i) => Some(UnitId::from(*i)),
            Self::MultiCrew(i, _) => Some(UnitId::from(*i)),
            Self::ArtilleryCommander(_, _)
            | Self::ForwardObserver(_, _)
            | Self::Instructor(_, _)
            | Self::Observer(_, _)
            | Self::Spectator => None,
        }
    }
}

wrapped_prim!(Ucid, String, Hash);

impl From<&str> for Ucid {
    fn from(value: &str) -> Self {
        Self(String::from(value))
    }
}

impl fmt::Display for Ucid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", &self.0)
    }
}

wrapped_table!(PlayerInfo, None);

impl<'lua> PlayerInfo<'lua> {
    pub fn id(&self) -> Result<PlayerId> {
        Ok(self.t.raw_get("id")?)
    }

    pub fn name(&self) -> Result<String> {
        Ok(self.t.raw_get("name")?)
    }

    pub fn side(&self) -> Result<Side> {
        Ok(self.t.raw_get("side")?)
    }

    pub fn slot(&self) -> Result<SlotId> {
        Ok(self.t.raw_get("slot")?)
    }

    pub fn ping(&self) -> Result<f32> {
        Ok(self.t.raw_get("ping")?)
    }

    pub fn ip(&self) -> Result<Option<String>> {
        Ok(self.t.raw_get("ipaddr")?)
    }

    pub fn ucid(&self) -> Result<Option<Ucid>> {
        Ok(self.t.raw_get("ucid")?)
    }
}

#[derive(Debug, Clone, Copy)]
pub enum DcsLuaEnvironment {
    /// aka hooks
    Server,
    Mission,
    Config,
    Export,
}

impl<'lua> IntoLua<'lua> for DcsLuaEnvironment {
    fn into_lua(self, lua: &'lua Lua) -> LuaResult<Value<'lua>> {
        Ok(Value::String(match self {
            Self::Server => lua.create_string("server"),
            Self::Mission => lua.create_string("mission"),
            Self::Config => lua.create_string("config"),
            Self::Export => lua.create_string("export"),
        }?))
    }
}

wrapped_table!(Net, None);

impl<'lua> Net<'lua> {
    pub fn singleton<L: LuaEnv<'lua>>(lua: L) -> Result<Self> {
        Ok(lua.inner().globals().raw_get("net")?)
    }

    pub fn send_chat(&self, message: String, all: bool) -> Result<()> {
        Ok(self.t.call_function("send_chat", (message, all))?)
    }

    pub fn send_chat_to(
        &self,
        message: String,
        player: PlayerId,
        from_id: Option<PlayerId>,
    ) -> Result<()> {
        Ok(self
            .t
            .call_function("send_chat_to", (message, player, from_id))?)
    }

    pub fn get_player_list(&self) -> Result<Sequence<PlayerId>> {
        Ok(self.t.call_function("get_player_list", ())?)
    }

    pub fn get_my_player_id(&self) -> Result<PlayerId> {
        Ok(self.t.call_function("get_my_player_id", ())?)
    }

    pub fn get_server_id(&self) -> Result<PlayerId> {
        Ok(self.t.call_function("get_server_id", ())?)
    }

    pub fn get_player_info(&self, id: PlayerId) -> Result<PlayerInfo> {
        Ok(self.t.call_function("get_player_info", id)?)
    }

    pub fn kick(&self, id: PlayerId, message: String) -> Result<()> {
        Ok(self.t.call_function("kick", (id, message))?)
    }

    pub fn get_stat(&self, id: PlayerId, stat: PlayerStat) -> Result<i64> {
        Ok(self.t.call_function("get_stat", (id, stat))?)
    }

    pub fn get_name(&self, id: PlayerId) -> Result<String> {
        Ok(self.t.call_function("get_name", id)?)
    }

    pub fn get_slot(&self, id: PlayerId) -> Result<(Side, SlotId)> {
        Ok(self.t.call_function("get_slot", id)?)
    }

    pub fn force_player_slot(&self, id: PlayerId, side: Side, slot: SlotId) -> Result<()> {
        Ok(self
            .t
            .call_function("force_player_slot", (id, side, slot))?)
    }

    pub fn lua2json<T: IntoLua<'lua>>(&self, v: T) -> Result<String> {
        Ok(self.t.call_function("lua2json", v)?)
    }

    pub fn json2lua<T: FromLua<'lua>>(&self, v: String) -> Result<T> {
        Ok(self.t.call_function("json2lua", v)?)
    }

    pub fn dostring_in(&self, state: DcsLuaEnvironment, dostring: String) -> Result<String> {
        Ok(self.t.call_function("dostring_in", (state, dostring))?)
    }

    pub fn log(&self, message: String) -> Result<()> {
        Ok(self.t.call_function("log", message)?)
    }
}