-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent.rs
195 lines (181 loc) · 4.43 KB
/
event.rs
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
use std::borrow::Cow;
use crate::model::*;
use serde::{Deserialize, Serialize};
use std::hash::Hash;
macro_rules! define_event {
($(
$name:ident{$(
$(#[$attrs:meta])*
$arg:ident: $ty:ty
),*$(,)?}
),*$(,)?) => {
#[derive(Clone, Debug, Serialize, Deserialize, Hash)]
#[serde(tag = "cmd", content="data")]
pub enum EventData {
$($name ($name)),*
}
$(
#[derive(Clone, Debug, Serialize, Deserialize, Hash)]
pub struct $name {
$(
$(#[$attrs])*
pub $arg: $ty
),*
}
impl From<$name> for EventData {
fn from(event: $name) -> Self {
EventData::$name(event)
}
}
)*
};
}
define_event! {
DanmakuEvent {
/// 第一位:是否是抽奖弹幕,2~4位,舰长类型
flag: u64,
message: DanmakuMessage,
user: User,
fans_medal: Option<FansMedal>,
ts: u64,
},
EnterRoomEvent {
user: User,
fans_medal: Option<FansMedal>
},
BlindboxGiftEvent {
user: User,
fans_medal: Option<FansMedal>,
blindbox_gift_type: GiftType,
gift: Gift,
},
GiftEvent {
user: User,
fans_medal: Option<FansMedal>,
blindbox: Option<GiftType>,
gift: Gift,
rnd: String,
},
GuardBuyEvent {
level: u64,
price: u64,
user: User
},
SuperChatEvent {
user: User,
fans_medal: Option<FansMedal>,
price: u64,
message: String,
message_jpn: Option<String>
},
WatchedUpdateEvent {
num: u64
},
PopularityUpdateEvent {
popularity: u32,
},
GuardEnterRoomEvent {
user: User,
},
HotRankChangedEvent {
area: String,
rank: u64,
description: String,
},
HotRankSettlementEvent {
uname: String,
face: String,
area: String,
rank: u64,
},
OnlineRankCountEvent {
count: u64,
},
StopLiveEvent{
room_id_list: Vec<u64>
},
RoomChange {
area_id: u32,
area_name: String,
live_key: String,
parent_area_id: u32,
parent_area_name: String,
sub_session_key: String,
title: String,
},
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Event {
pub data: EventData,
pub meta: EventMeta,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EventMeta {
pub lib_version: Cow<'static, str>,
pub source: Option<EventSource>,
pub time: chrono::DateTime<chrono::Utc>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EventSource {
pub room_id: u64,
pub url: reqwest::Url,
}
impl Default for EventMeta {
fn default() -> Self {
EventMeta {
lib_version: Cow::Borrowed(env!("CARGO_PKG_VERSION")),
source: None,
time: chrono::Utc::now(),
}
}
}
impl EventMeta {
pub fn with_source(source: EventSource) -> Self {
EventMeta {
source: Some(source),
..Default::default()
}
}
pub fn new() -> Self {
Self::default()
}
}
impl Event {
pub fn is_stop_live(&self) -> bool {
if let EventData::StopLiveEvent(StopLiveEvent { room_id_list }) = &self.data {
if let Some(source) = &self.meta.source {
return room_id_list.contains(&source.room_id);
}
}
false
}
}
#[cfg(feature = "bincode")]
impl Event {
pub fn to_bincode(&self) -> bincode::Result<Vec<u8>> {
bincode::serialize::<Self>(self)
}
pub fn from_bincode(bincode: &[u8]) -> bincode::Result<Self> {
bincode::deserialize(bincode)
}
}
#[cfg(feature = "json")]
impl Event {
pub fn to_json(&self) -> serde_json::Result<String> {
serde_json::to_string(self)
}
pub fn from_json(json: &str) -> serde_json::Result<Self> {
serde_json::from_str::<Self>(json)
}
}
#[cfg(feature = "rt_wasm")]
impl From<Event> for wasm_bindgen::JsValue {
fn from(val: Event) -> Self {
serde_wasm_bindgen::to_value(&val)
.expect("this should not happen, event data are defined by ourselves")
}
}
#[cfg(feature = "rt_wasm")]
impl wasm_bindgen::describe::WasmDescribe for Event {
fn describe() {}
}