chat wheelu

This commit is contained in:
ntr
2019-10-18 15:38:40 +11:00
parent 189d1023c6
commit ddca205f0c
11 changed files with 143 additions and 39 deletions
+13
View File
@@ -65,6 +65,19 @@ pub fn select(db: &Db, id: Uuid) -> Result<Account, Error> {
Account::try_from(row)
}
pub fn chat_wheel(_db: &Db, _id: Uuid) -> Result<Vec<String>, Error> {
return Ok(vec![
"gl".to_string(),
"hf".to_string(),
"gg".to_string(),
"thx".to_string(),
"nice".to_string(),
"hmm".to_string(),
"ok".to_string(),
"...".to_string(),
])
}
pub fn select_name(db: &Db, name: &String) -> Result<Account, Error> {
let query = "
SELECT id, name, balance, subscribed, img
+60 -3
View File
@@ -58,7 +58,9 @@ pub enum Event {
Invite(Id),
Join(Id, String),
Joined(Id),
Chat(Id, Uuid, String)
Chat(Id, Uuid, String),
ChatClear(Id, Uuid),
}
struct WsClient {
@@ -66,6 +68,7 @@ struct WsClient {
account: Option<Uuid>,
tx: Sender<RpcMessage>,
subs: HashSet<Uuid>,
chat: Option<(Uuid, String)>,
pvp: bool,
invite: Option<String>,
}
@@ -121,7 +124,15 @@ impl Events {
None => None,
};
let client = WsClient { id, tx, account: account_id, subs: HashSet::new(), pvp: false, invite: None };
let client = WsClient { id,
tx,
account: account_id,
subs: HashSet::new(),
pvp: false,
invite: None,
chat: None,
};
self.clients.insert(id, client);
info!("clients={:?}", self.clients.len());
@@ -216,7 +227,7 @@ impl Events {
}
// create the req for the already queued opponent
if let Some(opp_req) = match self.clients.iter_mut().find(|(_c_id, c)| c.pvp) {
if let Some(opp_req) = match self.clients.iter_mut().find(|(c_id, c)| c.pvp && **c_id != id) {
Some((q_id, q)) => {
q.pvp = false;
Some(PvpRequest { id: *q_id, account: q.account.unwrap(), tx: q.tx.clone() })
@@ -292,6 +303,52 @@ impl Events {
return Ok(());
},
Event::Chat(id, instance, msg) => {
// set the chat state of this connection
{
let c = self.clients.get_mut(&id)
.ok_or(format_err!("connection not found id={:?}", id))?;
if c.chat.is_some() {
return Err(err_msg("you must wait"));
}
c.chat = Some((instance, msg));
}
// now collect all listeners of this instance
let chat_state: HashMap<Uuid, String> = self.clients.iter()
.filter(|(_id, c)| c.account.is_some())
.filter(|(_id, c)| match c.chat {
Some(ref chat) => chat.0 == instance,
None => false,
})
.map(|(_id, c)| (c.account.unwrap(), c.chat.clone().unwrap().1))
.collect();
return self.event(Event::Push(instance, RpcMessage::InstanceChat(chat_state)));
},
Event::ChatClear(id, instance) => {
{
match self.clients.get_mut(&id) {
Some(c) => c.chat = None,
None => (),
};
}
let chat_state: HashMap<Uuid, String> = self.clients.iter()
.filter(|(_id, c)| c.account.is_some())
.filter(|(_id, c)| match c.chat {
Some(ref chat) => chat.0 == instance,
None => false,
})
.map(|(_id, c)| (c.account.unwrap(), c.chat.clone().unwrap().1))
.collect();
return self.event(Event::Push(instance, RpcMessage::InstanceChat(chat_state)));
}
}
}
}
+3
View File
@@ -1,4 +1,5 @@
use std::fs::File;
use std::collections::{HashMap};
use uuid::Uuid;
@@ -31,6 +32,8 @@ enum InstancePhase {
Finished,
}
pub type ChatState = HashMap<Uuid, String>;
#[derive(Debug,Clone,Serialize,Deserialize)]
struct Round {
game_id: Option<Uuid>,
+26 -15
View File
@@ -1,5 +1,7 @@
use std::time::{Instant};
use std::thread::spawn;
use std::thread::{spawn, sleep};
use std::time;
use std::str;
use uuid::Uuid;
@@ -21,7 +23,7 @@ use account;
use construct::{Construct};
use events::{Event};
use game::{Game, game_state, game_skill, game_skill_clear, game_ready};
use instance::{Instance, instance_state, instance_practice, instance_ready, instance_abandon, demo};
use instance::{Instance, ChatState, instance_state, instance_practice, instance_ready, instance_abandon, demo};
use item::{Item, ItemInfoCtr, item_info};
use mtx;
use mail;
@@ -50,6 +52,8 @@ pub enum RpcMessage {
ItemInfo(ItemInfoCtr),
InstanceState(Instance),
InstanceChat(ChatState),
ChatWheel(Vec<String>),
EmailState(Option<Email>),
SubscriptionState(Option<Subscription>),
@@ -66,6 +70,8 @@ pub enum RpcMessage {
Invite(String),
Joining(()),
Processing(()),
Error(String),
}
@@ -102,6 +108,7 @@ pub enum RpcRequest {
InstanceAbandon { instance_id: Uuid },
InstanceReady { instance_id: Uuid },
InstanceState { instance_id: Uuid },
InstanceChat { instance_id: Uuid, index: usize },
VboxAccept { instance_id: Uuid, group: usize, index: usize },
VboxDiscard { instance_id: Uuid },
@@ -164,21 +171,22 @@ impl Connection {
return Err(err_msg("subscribe to unlock chat"))
}
let chat = vec![
"gl",
"hf",
"gg",
"thx",
"nice",
"hmm",
"ok",
"...",
];
let wheel = account::chat_wheel(&db, account.id)?;
self.events.send(Event::Chat(self.id, instance_id, chat[index]))?;
if let Some(c) = wheel.get(index) {
self.events.send(Event::Chat(self.id, instance_id, c.to_string()))?;
} else {
return Err(err_msg("invalid chat index"));
}
// evnts sends to subs
Ok(RpcMessage::Joining(()))
let events_tx = self.events.clone();
let id = self.id;
spawn(move || {
sleep(time::Duration::from_secs(3));
events_tx.send(Event::ChatClear(id, instance_id)).unwrap();
});
Ok(RpcMessage::Processing(()))
},
_ => {
// all good, let's make a tx and process
@@ -337,6 +345,9 @@ impl Handler for Connection {
let team = account::team(&mut tx, &a).unwrap();
self.send(RpcMessage::AccountTeam(team)).unwrap();
let wheel = account::chat_wheel(&db, a.id).unwrap();
self.send(RpcMessage::ChatWheel(wheel)).unwrap();
// tx should do nothing
tx.commit().unwrap();
} else {