about summary refs log tree commit diff
path: root/src/util.rs
blob: a746cb344b590e69b2322802f655680eb93258a0 (plain)
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
//! Uh-oh, a `util.rs` file. That can't be good.

use twilight_model::id::{Id, marker::UserMarker};

use crate::database::{BoardRow, Placement};

#[macro_export]
macro_rules! bail {
	($msg:expr) => {
		return twilight_util::builder::InteractionResponseDataBuilder::new()
			.content(&format!("❌ {}", $msg))
			.build()
	};
}

#[macro_export]
macro_rules! fail {
	($msg:expr) => {
		twilight_util::builder::InteractionResponseDataBuilder::new()
			.content(&format!("❌ {}", $msg))
			.build()
	};
}

#[macro_export]
macro_rules! success {
	($msg:expr) => {
		twilight_util::builder::InteractionResponseDataBuilder::new()
			.content(&format!("✅ {}", $msg))
			.build()
	};
}

pub fn tiebreak_shared_positions(board: Vec<BoardRow>) -> Vec<Placement> {
	let first = match board.first() {
		Some(r) => r.clone(),
		None => return vec![],
	};

	let mut last_score = first.points;
	let mut last_placed = 1;
	let mut placed = vec![Placement {
		row: first,
		placement: 1,
		placement_tie: 1,
		placement_first_of_tie: true,
	}];

	for (idx, row) in board.into_iter().enumerate().skip(1) {
		if row.points == last_score {
			placed.push(Placement {
				row,
				placement: idx + 1,
				placement_tie: last_placed,
				placement_first_of_tie: false,
			});
		} else {
			last_score = row.points;
			last_placed = idx + 1;

			placed.push(Placement {
				row,
				placement: idx + 1,
				placement_tie: last_placed,
				placement_first_of_tie: true,
			});
		}
	}

	placed
}

/// Takes a `&str` and extracts all mentions of users, sticking them in a Vec
pub fn extract_user_mentions(mut raw: &str) -> Vec<Id<UserMarker>> {
	let mut ret = vec![];

	loop {
		let Some(start) = raw.find("<@") else {
			break;
		};

		let Some(end) = raw.find('>') else {
			break;
		};

		let id_str = &raw[start + 2..end];
		raw = &raw[end + 1..];

		let Ok(id) = u64::from_str_radix(id_str, 10) else {
			continue;
		};

		ret.push(Id::new(id));
	}

	ret
}