rewrite backend in rust #32

Merged
lbailly merged 14 commits from rewrite into master 2024-06-13 13:20:05 +02:00
4 changed files with 70 additions and 62 deletions
Showing only changes of commit 6e0e9ae4a5 - Show all commits

View file

@ -17,6 +17,8 @@ use std::{
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
#[derive(Serialize, Clone)]
#[serde(crate = "rocket::serde")]
enum TrackedState { enum TrackedState {
Conscrit { Conscrit {
invisible: bool, invisible: bool,
@ -172,8 +174,10 @@ struct TrackedInfo {
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
struct AdminTrackedInfo { struct AdminTrackedInfo {
name: String, name: String,
id: String,
pos: (f32, f32), pos: (f32, f32),
color: u8, color: u8,
state: TrackedState,
} }
impl From<AdminTrackedInfo> for TrackedInfo { impl From<AdminTrackedInfo> for TrackedInfo {
@ -197,8 +201,10 @@ fn base_view(team: &Tracked) -> TrackedInfo {
fn admin_view(team: &Tracked) -> AdminTrackedInfo { fn admin_view(team: &Tracked) -> AdminTrackedInfo {
AdminTrackedInfo { AdminTrackedInfo {
name: team.name.clone(), name: team.name.clone(),
id: team.id.clone(),
pos: team.pos, pos: team.pos,
color: team.state.admin_color(), color: team.state.admin_color(),
state: team.state.clone(),
} }
} }
@ -237,8 +243,13 @@ fn apparent_info(watcher: &Tracked, team: &Tracked) -> Option<TrackedInfo> {
} }
} }
#[get("/track/<id>?<gpslog>")] #[get("/track/<id>?<gpslog>&<dbg>")]
fn tracked_view(id: &str, gpslog: Option<bool>, tracking: &State<Tracking>) -> Option<Template> { fn tracked_view(
id: &str,
gpslog: Option<bool>,
dbg: Option<bool>,
tracking: &State<Tracking>,
) -> Option<Template> {
if let Some(tracked) = tracking.read().unwrap().get(&id.to_string()) { if let Some(tracked) = tracking.read().unwrap().get(&id.to_string()) {
Some(Template::render( Some(Template::render(
match tracked.read().unwrap().state { match tracked.read().unwrap().state {
@ -248,7 +259,8 @@ fn tracked_view(id: &str, gpslog: Option<bool>, tracking: &State<Tracking>) -> O
context! { context! {
name: &tracked.read().unwrap().name, name: &tracked.read().unwrap().name,
id: &id, id: &id,
gpslog: gpslog.unwrap_or(true) gpslog: gpslog.unwrap_or(true),
dbg: dbg.unwrap_or(false),
}, },
)) ))
} else { } else {
@ -281,7 +293,16 @@ fn tracked_events<'a>(
mut shutdown: Shutdown, mut shutdown: Shutdown,
) -> Option<EventStream![Event + 'a]> { ) -> Option<EventStream![Event + 'a]> {
if evt_queue.read().unwrap().contains_key(&id.to_string()) { if evt_queue.read().unwrap().contains_key(&id.to_string()) {
state_update(&tracking.read().unwrap().get(&id.to_string()).unwrap().read().unwrap(), &evt_queue); state_update(
&tracking
.read()
.unwrap()
.get(&id.to_string())
.unwrap()
.read()
.unwrap(),
&evt_queue,
);
Some(EventStream! { Some(EventStream! {
let mut interval = time::interval(Duration::from_millis(100)); let mut interval = time::interval(Duration::from_millis(100));
loop { loop {

View file

@ -3,6 +3,7 @@ var markers = [];
var self_marker; var self_marker;
var name; var name;
var id; var id;
var dbg;
var CircleIcon = L.Icon.extend({ var CircleIcon = L.Icon.extend({
options: { options: {
@ -88,6 +89,7 @@ function setup_evtlisten_common(){
evtSource = new EventSource("/track/"+id+"/events"); evtSource = new EventSource("/track/"+id+"/events");
evtSource.addEventListener("coords", (event) => { evtSource.addEventListener("coords", (event) => {
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
if(dbg) console.log('coords: ', data);
var i = 0; var i = 0;
for (tracked of data) { for (tracked of data) {
if (i == markers.length) { if (i == markers.length) {
@ -106,8 +108,10 @@ function setup_evtlisten_common(){
}); });
evtSource.addEventListener("self_info", (event) => { evtSource.addEventListener("self_info", (event) => {
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
if(dbg) console.log('self: ', data);
self_marker.setLatLng(data.pos); self_marker.setLatLng(data.pos);
self_marker.setIcon(self_icons[data.color]); self_marker.setIcon(self_icons[data.color]);
self_state_hook(data.state);
}); });
//socket.on("popup", function(data){ //socket.on("popup", function(data){

View file

@ -38,36 +38,28 @@
<script type="text/javascript"> <script type="text/javascript">
id = "{{id}}"; id = "{{id}}";
name = "{{name}}"; name = "{{name}}";
////////////////////////////////////////////////////////////////////////////// dbg = {{dbg}};
// SETUP MAP
setup_map(); setup_map();
// {{#if gpslog}}setup_geoLoc();{{/if}}
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// EVENT LISTENNING // EVENT LISTENNING
function self_state_hook(state){
var code_buttons = "";
if(state.Conscrit.invisibility_codes > 0)//onclick="socket.emit('useCode', 'invisibility')"
code_buttons += `<button>
Invisibilité
</button>`;
if(state.Conscrit.blur_codes > 0)//onclick="socket.emit('useCode', 'blurred')"
code_buttons += `<button>
Brouillage
</button>`;
document.getElementById(`codes`).innerHTML = code_buttons;
};
setup_evtlisten_common(); setup_evtlisten_common();
// //////////////////////////////////////////////////////////////////////////////
// // SETTINGS -- CODE
// socket.on('setCodes', function(codes){
// var code_buttons = "";
// if(codes.invisibility > 0)
// code_buttons += `<button onclick="socket.emit('useCode', 'invisibility')">
// Invisibilité
// </button>`;
// if(codes.blurred > 0)
// code_buttons += `<button onclick="socket.emit('useCode', 'blurred')">
// Brouillage
// </button>`;
// document.getElementById(`codes`).innerHTML = code_buttons;
// });
// //////////////////////////////////////////////////////////////////////////////
// // GEOLOCALISATION
// if({{gpslog}}) setup_geoLoc();
</script> </script>
</body> </body>
</html> </html>

View file

@ -34,25 +34,25 @@
<input type="checkbox" id="invisibility" name="invisibility" checked onclick='sendState()' /> <input type="checkbox" id="invisibility" name="invisibility" checked onclick='sendState()' />
<label for="invisibility">Invisible</label> <label for="invisibility">Invisible</label>
<input type="radio" id="npc0" name="color" value=1 onclick='sendState()' /> <input type="radio" id="npc1" name="color" value=1 onclick='sendState()' />
<label for="npc0">Traqueur</label> <label for="npc0">Traqueur</label>
<input type="radio" id="npc1" name="color" value=3 checked onclick='sendState()' /> <input type="radio" id="npc3" name="color" value=3 checked onclick='sendState()' />
<label for="npc1"><img src="/icons/3.png" height=15px></label> <label for="npc1"><img src="/icons/3.png" height=15px></label>
<input type="radio" id="npc2" name="color" value=4 checked onclick='sendState()' /> <input type="radio" id="npc4" name="color" value=4 checked onclick='sendState()' />
<label for="npc2"><img src="/icons/4.png" height=15px></label> <label for="npc2"><img src="/icons/4.png" height=15px></label>
<input type="radio" id="npc3" name="color" value=5 checked onclick='sendState()' /> <input type="radio" id="npc5" name="color" value=5 checked onclick='sendState()' />
<label for="npc3"><img src="/icons/5.png" height=15px></label> <label for="npc3"><img src="/icons/5.png" height=15px></label>
<input type="radio" id="npc4" name="color" value=6 checked onclick='sendState()' /> <input type="radio" id="npc6" name="color" value=6 checked onclick='sendState()' />
<label for="npc4"><img src="/icons/6.png" height=15px></label> <label for="npc4"><img src="/icons/6.png" height=15px></label>
<input type="radio" id="npc5" name="color" value=7 checked onclick='sendState()' /> <input type="radio" id="npc7" name="color" value=7 checked onclick='sendState()' />
<label for="npc5"><img src="/icons/7.png" height=15px></label> <label for="npc5"><img src="/icons/7.png" height=15px></label>
<input type="radio" id="npc6" name="color" value=8 checked onclick='sendState()' /> <input type="radio" id="npc8" name="color" value=8 checked onclick='sendState()' />
<label for="npc6"><img src="/icons/8.png" height=15px></label> <label for="npc6"><img src="/icons/8.png" height=15px></label>
<input type="radio" id="npc7" name="color" value=9 checked onclick='sendState()' /> <input type="radio" id="npc9" name="color" value=9 checked onclick='sendState()' />
<label for="npc7"><img src="/icons/9.png" height=15px></label> <label for="npc7"><img src="/icons/9.png" height=15px></label>
<input type="radio" id="npc8" name="color" value=10 checked onclick='sendState()' /> <input type="radio" id="npc10" name="color" value=10 checked onclick='sendState()' />
<label for="npc8"><img src="/icons/10.png" height=15px></label> <label for="npc8"><img src="/icons/10.png" height=15px></label>
<input type="radio" id="npc9" name="color" value=11 checked onclick='sendState()' /> <input type="radio" id="npc11" name="color" value=11 checked onclick='sendState()' />
<label for="npc9"><img src="/icons/11.png" height=15px></label> <label for="npc9"><img src="/icons/11.png" height=15px></label>
</form><br/> </form><br/>
@ -64,19 +64,14 @@
<script type="text/javascript"> <script type="text/javascript">
id = "{{id}}"; id = "{{id}}";
name = "{{name}}"; name = "{{name}}";
////////////////////////////////////////////////////////////////////////////// dbg = {{dbg}};
// SETUP MAP
setup_map(); setup_map();
// {{#if gpslog}}setup_geoLoc();{{/if}}
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// EVENT LISTENNING // EVENT LISTENNING
setup_evtlisten_common();
//////////////////////////////////////////////////////////////////////////////
// SETTINGS -- State
var form = document.querySelector("#state"); var form = document.querySelector("#state");
function sendState(){ function sendState(){
const data = new FormData(form); const data = new FormData(form);
@ -90,25 +85,21 @@
); );
} }
// socket.on('newState', function(state){ function self_state_hook(state){
// document.querySelector("#invisibility").checked = state.invisibility; document.querySelector("#invisibility").checked = state.Vieux.invisible;
// document.querySelector("#blurred").checked = state.blurred; document.querySelector("#npc1").checked = state.Vieux.color == 1;
// document.querySelector("#npc0").checked = state.npc == 0; document.querySelector("#npc3").checked = state.Vieux.color == 3;
// document.querySelector("#npc1").checked = state.npc == 1; document.querySelector("#npc4").checked = state.Vieux.color == 4;
// document.querySelector("#npc2").checked = state.npc == 2; document.querySelector("#npc5").checked = state.Vieux.color == 5;
// document.querySelector("#npc3").checked = state.npc == 3; document.querySelector("#npc6").checked = state.Vieux.color == 6;
// document.querySelector("#npc4").checked = state.npc == 4; document.querySelector("#npc7").checked = state.Vieux.color == 7;
// document.querySelector("#npc5").checked = state.npc == 5; document.querySelector("#npc8").checked = state.Vieux.color == 8;
// document.querySelector("#npc6").checked = state.npc == 6; document.querySelector("#npc9").checked = state.Vieux.color == 9;
// document.querySelector("#npc7").checked = state.npc == 7; document.querySelector("#npc10").checked = state.Vieux.color == 10;
// document.querySelector("#npc8").checked = state.npc == 8; document.querySelector("#npc11").checked = state.Vieux.color == 11;
// document.querySelector("#npc9").checked = state.npc == 9; };
// });
// ////////////////////////////////////////////////////////////////////////////// setup_evtlisten_common();
// // GEOLOCALISATION
// if({{gpslog}}) setup_geoLoc();
</script> </script>
</body> </body>
</html> </html>