1use log::*;
2use crate::photon_decode;
3use crate::photon_messages::Items;
4use crate::photon_decode::Parameters;
5use crate::photon_decode::Value;
6
7macro_rules! decode_number {
8 ($val:expr, $index:expr, $name:expr) => {
9 if let Some(p) = $val.get(&$index) {
10 match p {
11 Value::Short(v) => Some(*v as usize),
12 Value::Integer(v) => Some(*v as usize),
13 Value::Byte(v) => Some(*v as usize),
14 _ => {
15 error!("Failed to decode {}", $name);
16 None
17 }
18 }
19 } else {
20 error!("Index {} not found in {}", $index, $name);
21 None
22 }
23 };
24}
25
26macro_rules! decode_string {
27 ($val:expr, $index:expr, $name:expr) => {
28 if let Some(p) = $val.get(&$index) {
29 match p {
30 Value::String(v) => Some(v.clone()),
31 _ => {
32 error!("Failed to decode {}", $name);
33 None
34 }
35 }
36 } else {
37 None
38 }
39 };
40}
41
42#[allow(unused_macros)]
43macro_rules! decode_string_vec {
44 ($val:expr, $index:expr, $name:expr) => {
45 if let Some(p) = $val.get(&$index) {
46 match p {
47 Value::Array(arr) => {
48 let mut ret = vec![];
49 for v in arr {
50 if let Value::String(s) = v {
51 ret.push(s.clone());
52 }
53 }
54
55 Some(ret)
56 }
57 _ => {
58 error!("Failed to decode {}", $name);
59 None
60 }
61 }
62 } else {
63 None
64 }
65 };
66}
67
68macro_rules! decode_number_vec {
69 ($val:expr, $index:expr, $name:expr) => {
70 if let Some(p) = $val.get(&$index) {
71 match p {
72 Value::Array(arr) => {
73 let mut ret = vec![];
74 for v in arr {
75 match v {
76 Value::Short(v) => {
77 ret.push(*v as u32);
78 },
79 Value::Byte(v) => {
80 ret.push(*v as u32);
81 },
82 _ => {}
83 }
84 }
85
86 Some(ret)
87 },
88 Value::ByteArray(v) => {
89 Some(v.iter().map(|b| *b as u32).collect::<Vec<u32>>())
90 },
91 _ => {
92 error!("Failed to decode {}", $name);
93 None
94 }
95 }
96 } else {
97 None
98 }
99 };
100}
101
102macro_rules! decode_float {
103 ($val:expr, $index:expr, $name:expr) => {
104 if let Some(p) = $val.get(&$index) {
105 match p {
106 Value::Float(v) => Some(*v as f32),
107 _ => {
108 error!("Failed to decode {}", $name);
109 None
110 }
111 }
112 } else {
113 None
114 }
115 };
116}
117
118
119macro_rules! decode_vec_of_number_vec {
120 ($val:expr, $index:expr, $name:expr) => {
121 if let Some(p) = $val.get(&$index) {
122 match p {
123 Value::Array(v) => {
124 let mut ret = vec![];
125 for i in v {
126 let mut params = Parameters::new();
127 params.insert(0, i.clone());
128 let item = decode_number_vec!(params, 0, $name)?;
129 ret.push(item);
130 }
131
132 Some(ret)
133 },
134 _ => {
135 error!("Failed to decode {}", $name);
136 None
137 }
138 }
139 } else {
140 None
141 }
142 };
143}
144
145
146#[derive(Debug, Clone, PartialEq, Default)]
147pub struct Leave {
148 pub source: usize,
149
150}
151
152impl Leave {
153 pub fn parse(val: Parameters) -> Option<Message> {
154 info!("Leave parameters: {:?}", val);
155 let source = decode_number!(val, 0, "Leave::source")?;
156
157
158 Some(Message::Leave(Leave { source, }))
159 }
160}
161
162#[derive(Debug, Clone, PartialEq, Default)]
163pub struct HealthUpdate {
164 pub source: usize,
165 pub target: usize,
166 pub value: f32,
167
168}
169
170impl HealthUpdate {
171 pub fn parse(val: Parameters) -> Option<Message> {
172 info!("HealthUpdate parameters: {:?}", val);
173 let source = decode_number!(val, 0, "HealthUpdate::source")?;
174 let target = decode_number!(val, 6, "HealthUpdate::target")?;
175 let value = decode_float!(val, 2, "HealthUpdate::value")?;
176
177
178 Some(Message::HealthUpdate(HealthUpdate { source, target, value, }))
179 }
180}
181
182#[derive(Debug, Clone, PartialEq, Default)]
183pub struct RegenerationHealthChanged {
184 pub source: usize,
185 pub health: f32,
186 pub max_health: f32,
187 pub regeneration_rate: Option<f32>,
188
189}
190
191impl RegenerationHealthChanged {
192 pub fn parse(val: Parameters) -> Option<Message> {
193 info!("RegenerationHealthChanged parameters: {:?}", val);
194 let source = decode_number!(val, 0, "RegenerationHealthChanged::source")?;
195 let health = decode_float!(val, 2, "RegenerationHealthChanged::health")?;
196 let max_health = decode_float!(val, 3, "RegenerationHealthChanged::max_health")?;
197 let regeneration_rate = decode_float!(val, 4, "RegenerationHealthChanged::regeneration_rate");
198
199
200 Some(Message::RegenerationHealthChanged(RegenerationHealthChanged { source, health, max_health, regeneration_rate, }))
201 }
202}
203
204#[derive(Debug, Clone, PartialEq, Default)]
205pub struct KnockedDown {
206 pub source: usize,
207 pub target: usize,
208 pub target_name: String,
209
210}
211
212impl KnockedDown {
213 pub fn parse(val: Parameters) -> Option<Message> {
214 info!("KnockedDown parameters: {:?}", val);
215 let source = decode_number!(val, 0, "KnockedDown::source")?;
216 let target = decode_number!(val, 3, "KnockedDown::target")?;
217 let target_name = decode_string!(val, 4, "KnockedDown::target_name")?;
218
219
220 Some(Message::KnockedDown(KnockedDown { source, target, target_name, }))
221 }
222}
223
224#[derive(Debug, Clone, PartialEq, Default)]
225pub struct NewCharacter {
226 pub source: usize,
227 pub character_name: String,
228 pub health: f32,
229 pub max_health: f32,
230 pub energy: f32,
231 pub max_energy: f32,
232 pub items: Items,
233
234}
235
236impl NewCharacter {
237 pub fn parse(val: Parameters) -> Option<Message> {
238 info!("NewCharacter parameters: {:?}", val);
239 let source = decode_number!(val, 0, "NewCharacter::source")?;
240 let character_name = decode_string!(val, 1, "NewCharacter::character_name")?;
241 let health = decode_float!(val, 18, "NewCharacter::health")?;
242 let max_health = decode_float!(val, 19, "NewCharacter::max_health")?;
243 let energy = decode_float!(val, 22, "NewCharacter::energy")?;
244 let max_energy = decode_float!(val, 23, "NewCharacter::max_energy")?;
245 let item_array = decode_number_vec!(val, 33, "NewCharacter::item_array")?;
246 let items = item_array.into();
247
248
249 Some(Message::NewCharacter(NewCharacter { source, character_name, health, max_health, energy, max_energy, items, }))
250 }
251}
252
253#[derive(Debug, Clone, PartialEq, Default)]
254pub struct UpdateFame {
255 pub source: usize,
256 pub fame: usize,
257
258}
259
260impl UpdateFame {
261 pub fn parse(val: Parameters) -> Option<Message> {
262 info!("UpdateFame parameters: {:?}", val);
263 let source = decode_number!(val, 0, "UpdateFame::source")?;
264 let fame = decode_number!(val, 2, "UpdateFame::fame")?;
265
266
267 Some(Message::UpdateFame(UpdateFame { source, fame, }))
268 }
269}
270
271#[derive(Debug, Clone, PartialEq, Default)]
272pub struct CharacterEquipmentChanged {
273 pub source: usize,
274 pub items: Items,
275
276}
277
278impl CharacterEquipmentChanged {
279 pub fn parse(val: Parameters) -> Option<Message> {
280 info!("CharacterEquipmentChanged parameters: {:?}", val);
281 let source = decode_number!(val, 0, "CharacterEquipmentChanged::source")?;
282 let item_array = decode_number_vec!(val, 2, "CharacterEquipmentChanged::item_array")?;
283 let items = item_array.into();
284
285
286 Some(Message::CharacterEquipmentChanged(CharacterEquipmentChanged { source, items, }))
287 }
288}
289
290#[derive(Debug, Clone, PartialEq, Default)]
291pub struct PartyInvitation {
292
293}
294
295impl PartyInvitation {
296 pub fn parse(val: Parameters) -> Option<Message> {
297 info!("PartyInvitation parameters: {:?}", val);
298
299
300 Some(Message::PartyInvitation(PartyInvitation { }))
301 }
302}
303
304#[derive(Debug, Clone, PartialEq, Default)]
305pub struct PartyJoined {
306 pub party_id: usize,
307 pub party_structures: Vec<Vec<u32>>,
308 pub character_names: Vec<String>,
309
310}
311
312impl PartyJoined {
313 pub fn parse(val: Parameters) -> Option<Message> {
314 info!("PartyJoined parameters: {:?}", val);
315 let party_id = decode_number!(val, 0, "PartyJoined::party_id")?;
316 let party_structures = decode_vec_of_number_vec!(val, 4, "PartyJoined::party_structures")?;
317 let character_names = decode_string_vec!(val, 5, "PartyJoined::character_names")?;
318
319
320 Some(Message::PartyJoined(PartyJoined { party_id, party_structures, character_names, }))
321 }
322}
323
324#[derive(Debug, Clone, PartialEq, Default)]
325pub struct PartyDisbanded {
326
327}
328
329impl PartyDisbanded {
330 pub fn parse(val: Parameters) -> Option<Message> {
331 info!("PartyDisbanded parameters: {:?}", val);
332
333
334 Some(Message::PartyDisbanded(PartyDisbanded { }))
335 }
336}
337
338#[derive(Debug, Clone, PartialEq, Default)]
339pub struct PartyPlayerJoined {
340 pub party_id: usize,
341 pub party_structure: Vec<u32>,
342 pub name: String,
343
344}
345
346impl PartyPlayerJoined {
347 pub fn parse(val: Parameters) -> Option<Message> {
348 info!("PartyPlayerJoined parameters: {:?}", val);
349 let party_id = decode_number!(val, 0, "PartyPlayerJoined::party_id")?;
350 let party_structure = decode_number_vec!(val, 1, "PartyPlayerJoined::party_structure")?;
351 let name = decode_string!(val, 2, "PartyPlayerJoined::name")?;
352
353
354 Some(Message::PartyPlayerJoined(PartyPlayerJoined { party_id, party_structure, name, }))
355 }
356}
357
358#[derive(Debug, Clone, PartialEq, Default)]
359pub struct PartyChangedOrder {
360
361}
362
363impl PartyChangedOrder {
364 pub fn parse(val: Parameters) -> Option<Message> {
365 info!("PartyChangedOrder parameters: {:?}", val);
366
367
368 Some(Message::PartyChangedOrder(PartyChangedOrder { }))
369 }
370}
371
372#[derive(Debug, Clone, PartialEq, Default)]
373pub struct PartyPlayerLeft {
374 pub party_id: usize,
375 pub party_structure: Vec<u32>,
376
377}
378
379impl PartyPlayerLeft {
380 pub fn parse(val: Parameters) -> Option<Message> {
381 info!("PartyPlayerLeft parameters: {:?}", val);
382 let party_id = decode_number!(val, 0, "PartyPlayerLeft::party_id")?;
383 let party_structure = decode_number_vec!(val, 1, "PartyPlayerLeft::party_structure")?;
384
385
386 Some(Message::PartyPlayerLeft(PartyPlayerLeft { party_id, party_structure, }))
387 }
388}
389
390#[derive(Debug, Clone, PartialEq, Default)]
391pub struct PartyLeaderChanged {
392
393}
394
395impl PartyLeaderChanged {
396 pub fn parse(val: Parameters) -> Option<Message> {
397 info!("PartyLeaderChanged parameters: {:?}", val);
398
399
400 Some(Message::PartyLeaderChanged(PartyLeaderChanged { }))
401 }
402}
403
404#[derive(Debug, Clone, PartialEq, Default)]
405pub struct PartyLootSettingChangedPlayer {
406
407}
408
409impl PartyLootSettingChangedPlayer {
410 pub fn parse(val: Parameters) -> Option<Message> {
411 info!("PartyLootSettingChangedPlayer parameters: {:?}", val);
412
413
414 Some(Message::PartyLootSettingChangedPlayer(PartyLootSettingChangedPlayer { }))
415 }
416}
417
418#[derive(Debug, Clone, PartialEq, Default)]
419pub struct PartySilverGained {
420
421}
422
423impl PartySilverGained {
424 pub fn parse(val: Parameters) -> Option<Message> {
425 info!("PartySilverGained parameters: {:?}", val);
426
427
428 Some(Message::PartySilverGained(PartySilverGained { }))
429 }
430}
431
432#[derive(Debug, Clone, PartialEq, Default)]
433pub struct PartyPlayerUpdated {
434
435}
436
437impl PartyPlayerUpdated {
438 pub fn parse(val: Parameters) -> Option<Message> {
439 info!("PartyPlayerUpdated parameters: {:?}", val);
440
441
442 Some(Message::PartyPlayerUpdated(PartyPlayerUpdated { }))
443 }
444}
445
446#[derive(Debug, Clone, PartialEq, Default)]
447pub struct PartyInvitationPlayerBusy {
448
449}
450
451impl PartyInvitationPlayerBusy {
452 pub fn parse(val: Parameters) -> Option<Message> {
453 info!("PartyInvitationPlayerBusy parameters: {:?}", val);
454
455
456 Some(Message::PartyInvitationPlayerBusy(PartyInvitationPlayerBusy { }))
457 }
458}
459
460#[derive(Debug, Clone, PartialEq, Default)]
461pub struct PartyMarkedObjectsUpdated {
462
463}
464
465impl PartyMarkedObjectsUpdated {
466 pub fn parse(val: Parameters) -> Option<Message> {
467 info!("PartyMarkedObjectsUpdated parameters: {:?}", val);
468
469
470 Some(Message::PartyMarkedObjectsUpdated(PartyMarkedObjectsUpdated { }))
471 }
472}
473
474#[derive(Debug, Clone, PartialEq, Default)]
475pub struct PartyOnClusterPartyJoined {
476
477}
478
479impl PartyOnClusterPartyJoined {
480 pub fn parse(val: Parameters) -> Option<Message> {
481 info!("PartyOnClusterPartyJoined parameters: {:?}", val);
482
483
484 Some(Message::PartyOnClusterPartyJoined(PartyOnClusterPartyJoined { }))
485 }
486}
487
488#[derive(Debug, Clone, PartialEq, Default)]
489pub struct PartySetRoleFlag {
490
491}
492
493impl PartySetRoleFlag {
494 pub fn parse(val: Parameters) -> Option<Message> {
495 info!("PartySetRoleFlag parameters: {:?}", val);
496
497
498 Some(Message::PartySetRoleFlag(PartySetRoleFlag { }))
499 }
500}
501
502#[derive(Debug, Clone, PartialEq, Default)]
503pub struct ChatSay {
504 pub text: String,
505
506}
507
508impl ChatSay {
509 pub fn parse(val: Parameters) -> Option<Message> {
510 info!("ChatSay parameters: {:?}", val);
511 let text = decode_string!(val, 0, "ChatSay::text")?;
512
513
514 Some(Message::ChatSay(ChatSay { text, }))
515 }
516}
517
518#[derive(Debug, Clone, PartialEq, Default)]
519pub struct Join {
520 pub source: usize,
521 pub character_name: String,
522 pub health: f32,
523 pub max_health: f32,
524 pub energy: f32,
525 pub max_energy: f32,
526
527}
528
529impl Join {
530 pub fn parse(val: Parameters) -> Option<Message> {
531 info!("Join parameters: {:?}", val);
532 let source = decode_number!(val, 0, "Join::source")?;
533 let character_name = decode_string!(val, 2, "Join::character_name")?;
534 let health = decode_float!(val, 11, "Join::health")?;
535 let max_health = decode_float!(val, 12, "Join::max_health")?;
536 let energy = decode_float!(val, 15, "Join::energy")?;
537 let max_energy = decode_float!(val, 16, "Join::max_energy")?;
538
539
540 Some(Message::Join(Join { source, character_name, health, max_health, energy, max_energy, }))
541 }
542}
543
544#[derive(Debug, Clone, PartialEq)]
545pub enum Message {
546 Leave(Leave),
547 HealthUpdate(HealthUpdate),
548 RegenerationHealthChanged(RegenerationHealthChanged),
549 KnockedDown(KnockedDown),
550 NewCharacter(NewCharacter),
551 UpdateFame(UpdateFame),
552 CharacterEquipmentChanged(CharacterEquipmentChanged),
553 PartyInvitation(PartyInvitation),
554 PartyJoined(PartyJoined),
555 PartyDisbanded(PartyDisbanded),
556 PartyPlayerJoined(PartyPlayerJoined),
557 PartyChangedOrder(PartyChangedOrder),
558 PartyPlayerLeft(PartyPlayerLeft),
559 PartyLeaderChanged(PartyLeaderChanged),
560 PartyLootSettingChangedPlayer(PartyLootSettingChangedPlayer),
561 PartySilverGained(PartySilverGained),
562 PartyPlayerUpdated(PartyPlayerUpdated),
563 PartyInvitationPlayerBusy(PartyInvitationPlayerBusy),
564 PartyMarkedObjectsUpdated(PartyMarkedObjectsUpdated),
565 PartyOnClusterPartyJoined(PartyOnClusterPartyJoined),
566 PartySetRoleFlag(PartySetRoleFlag),
567 ChatSay(ChatSay),
568 Join(Join),
569}
570
571pub fn into_game_message(photon_message: photon_decode::Message) -> Option<Message> {
572 debug!("Raw photon : {:?}", photon_message);
573 match photon_message {
574 photon_decode::Message::Event(photon_decode::EventData{
575 code: 1,
576 parameters
577 }) => {
578 match parameters.get(&252u8) {
579 Some(photon_decode::Value::Short(1)) => Leave::parse(parameters),
580 Some(photon_decode::Value::Short(6)) => HealthUpdate::parse(parameters),
581 Some(photon_decode::Value::Short(81)) => RegenerationHealthChanged::parse(parameters),
582 Some(photon_decode::Value::Short(154)) => KnockedDown::parse(parameters),
583 Some(photon_decode::Value::Short(25)) => NewCharacter::parse(parameters),
584 Some(photon_decode::Value::Short(73)) => UpdateFame::parse(parameters),
585 Some(photon_decode::Value::Short(80)) => CharacterEquipmentChanged::parse(parameters),
586 Some(photon_decode::Value::Short(214)) => PartyInvitation::parse(parameters),
587 Some(photon_decode::Value::Short(215)) => PartyJoined::parse(parameters),
588 Some(photon_decode::Value::Short(216)) => PartyDisbanded::parse(parameters),
589 Some(photon_decode::Value::Short(217)) => PartyPlayerJoined::parse(parameters),
590 Some(photon_decode::Value::Short(218)) => PartyChangedOrder::parse(parameters),
591 Some(photon_decode::Value::Short(219)) => PartyPlayerLeft::parse(parameters),
592 Some(photon_decode::Value::Short(220)) => PartyLeaderChanged::parse(parameters),
593 Some(photon_decode::Value::Short(221)) => PartyLootSettingChangedPlayer::parse(parameters),
594 Some(photon_decode::Value::Short(222)) => PartySilverGained::parse(parameters),
595 Some(photon_decode::Value::Short(223)) => PartyPlayerUpdated::parse(parameters),
596 Some(photon_decode::Value::Short(224)) => PartyInvitationPlayerBusy::parse(parameters),
597 Some(photon_decode::Value::Short(225)) => PartyMarkedObjectsUpdated::parse(parameters),
598 Some(photon_decode::Value::Short(226)) => PartyOnClusterPartyJoined::parse(parameters),
599 Some(photon_decode::Value::Short(227)) => PartySetRoleFlag::parse(parameters),
600
601 _ => None
602 }
603 },
604 photon_decode::Message::Request(photon_decode::OperationRequest{
605 code: 1,
606 parameters
607 }) => {
608 match parameters.get(&253u8) {
609 Some(photon_decode::Value::Short(188)) => ChatSay::parse(parameters),
610
611 _ => None
612 }
613 },
614 photon_decode::Message::Response(photon_decode::OperationResponse{
615 code: 1,
616 parameters,
617 return_code: _,
618 debug_message: _
619 }) => {
620 match parameters.get(&253u8) {
621 Some(photon_decode::Value::Short(2)) => Join::parse(parameters),
622
623 _ => None
624 }
625 },
626 _ => None
627 }
628}