screeps_rust_api/
types.rs

1use std::{collections::HashMap, str};
2
3use serde::{Deserialize, Serialize};
4
5/// 头像数据
6#[derive(Serialize, Deserialize, Debug)]
7pub struct Badge {
8    pub r#type: BadgeType,
9    pub color1: String,
10    pub color2: String,
11    pub color3: String,
12    pub param: i32,
13    pub flip: bool,
14    pub decoration: Option<String>,
15}
16
17/// 头像类型
18#[derive(Serialize, Deserialize, Debug)]
19#[serde(untagged)]
20pub enum BadgeType {
21    /// 种类
22    Kind(i32),
23    /// 路径
24    Path(BadgePath),
25}
26
27/// 头像路径
28#[derive(Serialize, Deserialize, Debug)]
29pub struct BadgePath {
30    pub path1: String,
31    pub path2: String,
32}
33
34/// 用户对象
35#[derive(Serialize, Deserialize, Debug)]
36pub struct User {
37    pub username: String,
38    pub badge: Option<Badge>,
39}
40
41/// 带有 _id 的用户对象
42#[derive(Serialize, Deserialize, Debug)]
43pub struct UserWithId {
44    pub _id: String,
45    pub username: String,
46    pub badge: Option<Badge>,
47}
48
49/// 我的信息
50#[derive(Serialize, Deserialize, Debug)]
51pub struct MyInfo {
52    pub _id: String,
53    pub email: String,
54    pub username: String,
55    pub cpu: i32,
56    pub badge: Option<Badge>,
57    pub password: Option<bool>,
58    #[serde(rename = "notifyPrefs")]
59    pub notify_prefs: Option<NotifyPrefs>,
60    pub gcl: u64,
61    pub credits: f64,
62    pub power: u64,
63    pub money: f64,
64    #[serde(rename = "subscriptionTokens")]
65    pub subscription_tokens: i32,
66    #[serde(rename = "cpuShard")]
67    pub cpu_shard: HashMap<String, f64>,
68    #[serde(rename = "cpuShardUpdatedTime")]
69    pub cpu_shard_updated_time: Option<u64>,
70    #[serde(rename = "powerExperimentations")]
71    pub power_experimentations: Option<u64>,
72    #[serde(rename = "powerExperimentationTime")]
73    pub power_experimentation_time: Option<u64>,
74    pub resources: GlobalResources,
75    pub steam: Option<SteamAccount>,
76}
77
78/// 用户信息
79#[derive(Serialize, Deserialize, Debug)]
80pub struct UserInfo {
81    pub _id: String,
82    /// 最多只能拿到 id
83    pub steam: Option<HashMap<String, String>>,
84    pub username: String,
85    pub gcl: u64,
86    pub power: u64,
87    pub badge: Option<Badge>,
88}
89
90/// 用户通知设置
91#[derive(Serialize, Deserialize, Debug)]
92pub struct NotifyPrefs {
93    #[serde(rename = "sendOnline")]
94    pub send_online: bool,
95    #[serde(rename = "disabledOnMessages")]
96    pub disabled_on_messages: bool,
97}
98
99/// 全局资源数
100#[derive(Serialize, Deserialize, Debug)]
101pub struct GlobalResources {
102    #[serde(rename = "accessKey")]
103    pub access_key: i32,
104    pub pixel: i32,
105    #[serde(rename = "cpuUnlock")]
106    pub cpu_unlock: i32,
107}
108
109/// steam 账号信息
110#[derive(Serialize, Deserialize, Debug)]
111pub struct SteamAccount {
112    pub id: String,
113    #[serde(rename = "displayName")]
114    pub display_name: String,
115    pub ownership: Vec<i32>,
116}
117
118/// 房间地形
119#[derive(Serialize, Deserialize, Debug)]
120pub struct RoomTerrain {
121    pub room: String,
122    pub x: i32,
123    pub y: i32,
124    /// 地形类型, "swamp" | "wall"
125    pub r#type: String,
126}
127
128/// 编码后的房间地形
129#[derive(Serialize, Deserialize, Debug)]
130pub struct EncodedRoomTerrain {
131    pub _id: String,
132    pub room: String,
133    /**
134     * terrain is a string of digits, giving the terrain left-to-right and top-to-bottom.
135     * 0: plain, 1: wall, 2: swamp, 3: also wall
136     */
137    pub terrain: String,
138    /// type 固定为 terrain
139    pub r#type: String,
140}
141
142/// 房间状态
143#[derive(Serialize, Deserialize, Debug)]
144pub struct RoomStatus {
145    _id: String,
146    /// "normal" | "out of borders"
147    pub status: String,
148    #[serde(rename = "respawnArea")]
149    pub respawn_area: Option<i32>,
150    pub novice: Option<i32>,
151}
152
153/// shard 信息
154#[derive(Serialize, Deserialize, Debug)]
155pub struct ShardInfo {
156    name: String,
157    #[serde(rename = "lastTicks")]
158    last_ticks: Vec<u64>,
159    #[serde(rename = "cpuLimit")]
160    cpu_limit: i32,
161    rooms: i32,
162    users: i32,
163    tick: f64,
164}
165
166/// 基本对象数据,每个对象都继承该结构
167#[derive(Serialize, Deserialize, Debug)]
168pub struct BaseObject {
169    /// 对象 id
170    pub _id: String,
171    /// x 坐标
172    pub x: i32,
173    /// y 坐标
174    pub y: i32,
175    /// 所在房间名
176    pub room: String,
177}
178
179// 首先,我们需要定义一个枚举来表示所有可能的对象类型
180#[derive(Debug, Serialize, Deserialize)]
181#[serde(tag = "type")]
182pub enum RoomObject {
183    #[serde(rename = "source")]
184    Source(Source),
185
186    #[serde(rename = "mineral")]
187    Mineral(Mineral),
188
189    #[serde(rename = "constructedWall")]
190    ConstructedWall(ConstructedWall),
191
192    #[serde(rename = "road")]
193    Road(Road),
194
195    #[serde(rename = "controller")]
196    Controller(Controller),
197
198    #[serde(rename = "spawn")]
199    Spawn(Spawn),
200
201    #[serde(rename = "extension")]
202    Extension(Extension),
203
204    #[serde(rename = "storage")]
205    Storage(Storage),
206
207    #[serde(rename = "tower")]
208    Tower(Tower),
209
210    #[serde(rename = "rampart")]
211    Rampart(Rampart),
212
213    #[serde(rename = "extractor")]
214    Extractor(Extractor),
215
216    #[serde(rename = "terminal")]
217    Terminal(Terminal),
218
219    #[serde(rename = "observer")]
220    Observer(Observer),
221
222    #[serde(rename = "powerSpawn")]
223    PowerSpawn(PowerSpawn),
224
225    #[serde(rename = "nuker")]
226    Nuker(Nuker),
227
228    #[serde(rename = "factory")]
229    Factory(Factory),
230
231    #[serde(rename = "lab")]
232    Lab(Lab),
233
234    #[serde(rename = "creep")]
235    Creep(Creep),
236
237    #[serde(rename = "powerCreep")]
238    PowerCreep(PowerCreep),
239
240    // 对于未知的对象类型,我们可以使用未匹配的变体
241    #[serde(other)]
242    Unknown,
243}
244
245/// Source 对象
246#[derive(Serialize, Deserialize, Debug)]
247pub struct Source {
248    #[serde(flatten)]
249    pub base_object: BaseObject,
250    pub energy: i32,
251    #[serde(rename = "energyCapacity")]
252    pub energy_capacity: i32,
253    #[serde(rename = "ticksToRegeneration")]
254    pub ticks_to_regeneration: i32,
255    #[serde(rename = "invaderHarvested")]
256    pub invader_harvested: i32,
257    #[serde(rename = "nextRegenerationTime")]
258    pub next_regeneration_time: Option<u64>,
259}
260
261/// Mineral 对象
262#[derive(Serialize, Deserialize, Debug)]
263pub struct Mineral {
264    #[serde(flatten)]
265    pub base_object: BaseObject,
266    #[serde(rename = "mineralType")]
267    pub mineral_type: String,
268    #[serde(rename = "mineralAmount")]
269    pub mineral_amount: i32,
270    #[serde(rename = "nextRegenerationTime")]
271    pub next_regeneration_time: Option<u64>,
272}
273
274/// ConstructedWall 对象
275#[derive(Serialize, Deserialize, Debug)]
276pub struct ConstructedWall {
277    #[serde(flatten)]
278    pub base_object: BaseObject,
279    pub hits: Option<i32>,
280    #[serde(rename = "hitsMax")]
281    pub hits_max: Option<i32>,
282    #[serde(rename = "notifyWhenAttacked")]
283    pub notify_when_attacked: Option<bool>,
284    #[serde(rename = "decayTime")]
285    pub decay_time: Option<DecayTime>,
286}
287
288/// 消失时间
289#[derive(Serialize, Deserialize, Debug)]
290pub struct DecayTime {
291    timestamp: u64,
292}
293
294/// Road 对象
295#[derive(Serialize, Deserialize, Debug)]
296pub struct Road {
297    #[serde(flatten)]
298    pub base_object: BaseObject,
299    pub hits: i32,
300    #[serde(rename = "hitsMax")]
301    pub hits_max: i32,
302    #[serde(rename = "notifyWhenAttacked")]
303    pub notify_when_attacked: bool,
304    #[serde(rename = "nextDecayTime")]
305    pub next_decay_time: Option<u64>,
306}
307
308/// Controller 对象的 Reservation 字段
309#[derive(Serialize, Deserialize, Debug)]
310pub struct Reservation {
311    pub user: String,
312    pub ticks_to_end: i32,
313}
314
315/// Controller 对象的 Sign 字段
316#[derive(Serialize, Deserialize, Debug)]
317pub struct Sign {
318    pub user: String,
319    pub time: i32,
320    pub text: String,
321    #[serde(rename = "datetime")]
322    pub date_time: u64,
323}
324
325/// Controller 对象的 Effect 字段
326#[derive(Serialize, Deserialize, Debug)]
327pub struct Effect {
328    pub effect: Option<i32>,
329    pub power: Option<i32>,
330    #[serde(rename = "endTime")]
331    pub end_time: Option<u64>,
332    #[serde(rename = "duration")]
333    pub duration: Option<u64>,
334}
335
336/// Controller 对象
337#[derive(Serialize, Deserialize, Debug)]
338pub struct Controller {
339    #[serde(flatten)]
340    pub base_object: BaseObject,
341    pub level: i32,
342    pub progress: Option<i32>,
343    #[serde(rename = "progressTotal")]
344    pub progress_total: Option<i32>,
345    pub user: Option<String>,
346    #[serde(rename = "downgradeTime")]
347    pub downgrade_time: Option<u64>,
348    #[serde(rename = "safeMode")]
349    pub safe_mode: Option<i32>,
350    #[serde(rename = "safeModeAvailable")]
351    pub safe_mode_available: Option<i32>,
352    #[serde(rename = "safeModeCooldown")]
353    pub safe_mode_cooldown: Option<i64>,
354    #[serde(rename = "upgradeBlocked")]
355    pub upgrade_blocked: Option<i32>,
356    #[serde(rename = "downgradeBlocked")]
357    pub downgrade_blocked: Option<i32>,
358    pub reservation: Option<Reservation>,
359    pub sign: Option<Sign>,
360    #[serde(rename = "isPowerEnabled")]
361    pub is_power_enabled: Option<bool>,
362    /// key 为 effect id
363    pub effects: Option<HashMap<String, Effect>>,
364}
365
366/// Spawn 对象的 Spawning 字段
367#[derive(Serialize, Deserialize, Debug)]
368pub struct Spawning {
369    pub name: String,
370    #[serde(rename = "needTime")]
371    pub need_time: u64,
372    #[serde(rename = "spawnTime")]
373    pub remaining_time: u64,
374}
375
376/// Spawn 对象的 Store 字段
377#[derive(Serialize, Deserialize, Debug)]
378pub struct SpawnStore {
379    pub energy: Option<i32>,
380}
381
382/// Spawn 对象
383#[derive(Serialize, Deserialize, Debug)]
384pub struct Spawn {
385    #[serde(flatten)]
386    pub base_object: BaseObject,
387    pub name: String,
388    pub hits: i32,
389    #[serde(rename = "hitsMax")]
390    pub hits_max: i32,
391    #[serde(rename = "notifyWhenAttacked")]
392    pub notify_when_attacked: bool,
393    pub spawning: Option<Spawning>,
394    pub off: bool,
395    pub store: SpawnStore,
396    #[serde(rename = "storeCapacityResource")]
397    pub store_capacity_resource: SpawnStore,
398}
399
400/// Extension 对象
401#[derive(Serialize, Deserialize, Debug)]
402pub struct Extension {
403    #[serde(flatten)]
404    pub base_object: BaseObject,
405    pub hits: i32,
406    #[serde(rename = "hitsMax")]
407    pub hits_max: i32,
408    pub user: String,
409    #[serde(rename = "notifyWhenAttacked")]
410    pub notify_when_attacked: bool,
411    pub store: SpawnStore,
412    #[serde(rename = "storeCapacityResource")]
413    pub store_capacity_resource: SpawnStore,
414    pub off: bool,
415}
416
417/// Storage 对象的 Store 字段
418pub type Store = HashMap<String, Option<i32>>;
419
420/// Storage 对象
421#[derive(Serialize, Deserialize, Debug)]
422pub struct Storage {
423    #[serde(flatten)]
424    pub base_object: BaseObject,
425    pub hits: i32,
426    #[serde(rename = "hitsMax")]
427    pub hits_max: i32,
428    #[serde(rename = "notifyWhenAttacked")]
429    pub notify_when_attacked: bool,
430    pub user: String,
431    pub store: Store,
432    #[serde(rename = "storeCapacity")]
433    pub store_capacity: Option<i32>,
434    pub effects: Option<HashMap<String, Effect>>,
435}
436
437/// Tower 对象的 ActionLog 字段
438#[derive(Serialize, Deserialize, Debug)]
439pub struct ActionLog {
440    pub attack: Option<Point>,
441    pub heal: Option<Point>,
442    pub repair: Option<Point>,
443}
444
445/// Point 对象
446#[derive(Serialize, Deserialize, Debug)]
447pub struct Point {
448    pub x: i32,
449    pub y: i32,
450}
451
452/// Tower 对象
453#[derive(Serialize, Deserialize, Debug)]
454pub struct Tower {
455    #[serde(flatten)]
456    pub base_object: BaseObject,
457    pub hits: i32,
458    #[serde(rename = "hitsMax")]
459    pub hits_max: i32,
460    #[serde(rename = "notifyWhenAttacked")]
461    pub notify_when_attacked: bool,
462    pub user: String,
463    pub store: SpawnStore,
464    #[serde(rename = "storeCapacityResource")]
465    pub store_capacity_resource: SpawnStore,
466    #[serde(rename = "actionLog")]
467    pub action_log: ActionLog,
468}
469
470/// Rampart 对象
471#[derive(Serialize, Deserialize, Debug)]
472pub struct Rampart {
473    #[serde(flatten)]
474    pub base_object: BaseObject,
475    pub hits: i32,
476    #[serde(rename = "hitsMax")]
477    pub hits_max: i32,
478    pub user: String,
479    #[serde(rename = "notifyWhenAttacked")]
480    pub notify_when_attacked: bool,
481    #[serde(rename = "nextDecayTime")]
482    pub next_decay_time: Option<u64>,
483}
484
485/// Extractor 对象
486#[derive(Serialize, Deserialize, Debug)]
487pub struct Extractor {
488    #[serde(flatten)]
489    pub base_object: BaseObject,
490    pub hits: i32,
491    #[serde(rename = "hitsMax")]
492    pub hits_max: i32,
493    pub user: Option<String>,
494    #[serde(rename = "notifyWhenAttacked")]
495    pub notify_when_attacked: bool,
496    pub cooldown: Option<i32>,
497}
498
499/// Terminal 对象
500#[derive(Serialize, Deserialize, Debug)]
501pub struct Terminal {
502    #[serde(flatten)]
503    pub base_object: BaseObject,
504    pub hits: i32,
505    #[serde(rename = "hitsMax")]
506    pub hits_max: i32,
507    pub user: String,
508    #[serde(rename = "notifyWhenAttacked")]
509    pub notify_when_attacked: bool,
510    pub store: Store,
511    #[serde(rename = "storeCapacity")]
512    pub store_capacity: Option<i32>,
513    #[serde(rename = "cooldownTime")]
514    pub cooldown_time: Option<u64>,
515    pub send: Option<String>,
516}
517
518/// Observer 对象
519#[derive(Serialize, Deserialize, Debug)]
520pub struct Observer {
521    #[serde(flatten)]
522    pub base_object: BaseObject,
523    pub hits: i32,
524    #[serde(rename = "hitsMax")]
525    pub hits_max: i32,
526    pub user: String,
527    #[serde(rename = "notifyWhenAttacked")]
528    pub notify_when_attacked: bool,
529    #[serde(rename = "observeRoom")]
530    pub observe_room: Option<String>,
531}
532
533/// PowerSpawn 对象
534#[derive(Serialize, Deserialize, Debug)]
535pub struct PowerSpawn {
536    #[serde(flatten)]
537    pub base_object: BaseObject,
538    pub hits: i32,
539    #[serde(rename = "hitsMax")]
540    pub hits_max: i32,
541    pub user: String,
542    #[serde(rename = "notifyWhenAttacked")]
543    pub notify_when_attacked: bool,
544    pub store: HashMap<String, Option<i32>>, // 包含 energy 和 power
545    #[serde(rename = "storeCapacityResource")]
546    pub store_capacity_resource: HashMap<String, Option<i32>>, // 包含 energy 和 power
547}
548
549/// Nuker 对象
550#[derive(Serialize, Deserialize, Debug)]
551pub struct Nuker {
552    #[serde(flatten)]
553    pub base_object: BaseObject,
554    pub hits: i32,
555    #[serde(rename = "hitsMax")]
556    pub hits_max: i32,
557    pub user: String,
558    #[serde(rename = "notifyWhenAttacked")]
559    pub notify_when_attacked: bool,
560    pub store: HashMap<String, Option<i32>>, // 包含 energy 和 G
561    #[serde(rename = "storeCapacityResource")]
562    pub store_capacity_resource: HashMap<String, Option<i32>>, // 包含 energy 和 G
563    #[serde(rename = "cooldownTime")]
564    pub cooldown_time: Option<u64>,
565}
566
567/// Factory 对象
568#[derive(Serialize, Deserialize, Debug)]
569pub struct Factory {
570    #[serde(flatten)]
571    pub base_object: BaseObject,
572    pub hits: i32,
573    #[serde(rename = "hitsMax")]
574    pub hits_max: i32,
575    pub user: String,
576    #[serde(rename = "notifyWhenAttacked")]
577    pub notify_when_attacked: bool,
578    pub store: Store,
579    #[serde(rename = "storeCapacity")]
580    pub store_capacity: Option<i32>,
581    pub cooldown: Option<i32>,
582    #[serde(rename = "cooldownTime")]
583    pub cooldown_time: Option<u64>,
584    pub effects: Option<HashMap<String, Effect>>,
585    pub level: Option<i32>,
586}
587
588/// Lab 对象
589#[derive(Serialize, Deserialize, Debug)]
590pub struct Lab {
591    #[serde(flatten)]
592    pub base_object: BaseObject,
593    pub hits: i32,
594    #[serde(rename = "hitsMax")]
595    pub hits_max: i32,
596    pub user: String,
597    #[serde(rename = "notifyWhenAttacked")]
598    pub notify_when_attacked: bool,
599    pub cooldown: Option<i32>,
600    #[serde(rename = "cooldownTime")]
601    pub cooldown_time: Option<u64>,
602    pub store: Store,
603    #[serde(rename = "storeCapacity")]
604    pub store_capacity: Option<i32>,
605    #[serde(rename = "storeCapacityResource")]
606    pub store_capacity_resource: SpawnStore,
607    #[serde(rename = "mineralAmount")]
608    pub mineral_amount: Option<i32>,
609    pub effects: Option<HashMap<String, Effect>>,
610}
611
612/// Creep 对象的 Body 字段
613#[derive(Serialize, Deserialize, Debug)]
614pub struct CreepBodyPart {
615    #[serde(rename = "type")]
616    pub part_type: String,
617    pub hits: Option<i32>,
618    pub boost: Option<String>,
619}
620
621/// Creep 对象
622#[derive(Serialize, Deserialize, Debug)]
623pub struct Creep {
624    #[serde(flatten)]
625    pub base_object: BaseObject,
626    pub name: String,
627    pub hits: i32,
628    #[serde(rename = "hitsMax")]
629    pub hits_max: i32,
630    pub user: String,
631    pub spawning: Option<bool>,
632    pub fatigue: Option<i32>,
633    pub body: Option<Vec<CreepBodyPart>>,
634    pub store: Store,
635    #[serde(rename = "storeCapacity")]
636    pub store_capacity: Option<i32>,
637    #[serde(rename = "notifyWhenAttacked")]
638    pub notify_when_attacked: Option<bool>,
639}
640
641/// PowerCreep 对象
642#[derive(Serialize, Deserialize, Debug)]
643pub struct PowerCreep {
644    #[serde(flatten)]
645    pub base_object: BaseObject,
646    pub name: String,
647    pub hits: i32,
648    #[serde(rename = "hitsMax")]
649    pub hits_max: i32,
650    pub user: String,
651    pub spawning: Option<bool>,
652    pub fatigue: Option<i32>,
653    pub body: Option<Vec<CreepBodyPart>>,
654    pub store: Store,
655    #[serde(rename = "storeCapacity")]
656    pub store_capacity: Option<i32>,
657    #[serde(rename = "notifyWhenAttacked")]
658    pub notify_when_attacked: bool,
659    #[serde(rename = "className")]
660    pub class_name: String,
661    pub power: Option<HashMap<i32, PowerInfo>>,
662}
663
664/// PowerCreep 对象的 Power 字段
665#[derive(Serialize, Deserialize, Debug)]
666pub struct PowerInfo {
667    pub level: i32,
668    #[serde(rename = "cooldownTime")]
669    pub cooldown_time: Option<u64>,
670}