Commit faeea9ad authored by treyzania's avatar treyzania

Added label generation code, and reports if we failed to generate an invoice properly.

parent 634bfda6
......@@ -57,6 +57,8 @@ impl Game {
const PROTO_NAME: &'static str = "gameoflight";
const SERVER_BROKE_ERROR_MSG: &'static str = "The server broke, message @trey:foobar.style on Matrix or @treyzania#7744 on Discord about it.";
/*
* A lot of this connection code was indirectly borrowed from:
* https://github.com/websockets-rs/rust-websocket/blob/master/examples/async-server.rs
......@@ -136,8 +138,16 @@ pub fn start_server(
.map_err(|e| println!("error: {}", e));
/// Handle crap for setting up crap.
let ticks_fut = create_sim_task(game.clone()).map(|_| ()).map_err(|e| {
println!("error ticking, aborting");
let ticks_fut = create_sim_task(game.clone()).map(|_| ()).map_err({
let game = game.clone();
|e| {
println!("error ticking, aborting!!!");
// Send a message to everyone that it broken.
game.queue_send_all_msg(messages::NetMessage::Alert(String::from(
SERVER_BROKE_ERROR_MSG,
)));
}
});
// Actually start the websocket thing.
......@@ -341,6 +351,8 @@ fn to_uuid_from_token(token: &str) -> Uuid {
const MSAT_PER_TILE: i64 = 5000;
const INVOICE_ERROR_STRING: &'static str = "There was a problem processing your submission.\nContact me at @trey:foobar.style on Matrix or @[email protected]#7744 on Discord.";
fn process_message(game: Arc<Game>, pkt: &PendingPacket) -> Result<(), String> {
use messages::NetMessage::*;
......@@ -378,17 +390,30 @@ fn process_message(game: Arc<Game>, pkt: &PendingPacket) -> Result<(), String> {
.as_ref()
.cl_node
.create_new_invoice(cost, label.clone(), desc)
.map_err(|e| println!("error creating invoice: {:?}", e))
.and_then({
let label = label.clone();
move |bolt11| {
let prefix: String = bolt11.chars().take(10).collect();
println!(
"[submission] new invoice {} for {} tiles created as {}",
label, num_tiles, prefix
);
.then({
let client = client.clone();
move |r| match r {
Ok(bolt11) => {
let label = label.clone();
let prefix: String = bolt11.chars().take(10).collect();
println!(
"[submission] new invoice {} for {} tiles created as {}",
label, num_tiles, prefix
);
client.queue_msg(messages::NetMessage::Invoice(label, bolt11))
client.queue_msg(messages::NetMessage::Invoice(label, bolt11));
Ok(())
}
Err(e) => {
println!("error processing invoice {}: {:?}", label, e);
// Send message to people saying it broke.
client.queue_msg(messages::NetMessage::Alert(format!(
"{}\n\nInclude this invoice label: {}",
INVOICE_ERROR_STRING, label
)));
Err(e)
}
}
})
.map_err(|e| println!("error sending invoice!"));
......
......@@ -8,6 +8,7 @@ use futures::{
prelude::*,
sync::{mpsc, oneshot},
};
use rand::{self, prelude::*};
use tokio_threadpool::blocking;
use crate::util;
......@@ -127,5 +128,8 @@ pub enum NodeError {
}
pub fn new_rand_invoice_label() -> String {
unimplemented!()
let mut rng = rand::thread_rng();
let mut buf = [0; 8];
rng.fill_bytes(&mut buf);
buf.iter().map(|b| format!("{:02x}", b)).collect::<String>()
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment