From 91523a80a10e0079983eff96cb897d867e5b0dca Mon Sep 17 00:00:00 2001 From: yukirij Date: Mon, 19 Aug 2024 01:39:03 -0700 Subject: [PATCH] Implement retire. --- server/src/manager/data.rs | 33 ++++++++++++++----------- server/src/manager/ws.rs | 10 ++++++++ server/src/protocol/packet/game_play.rs | 4 +-- server/src/system/filesystem/mod.rs | 8 +++--- www/js/interface.js | 16 ++++++------ www/js/scene.js | 30 ++++++++++++++-------- www/js/system.js | 12 ++++----- 7 files changed, 70 insertions(+), 43 deletions(-) diff --git a/server/src/manager/data.rs b/server/src/manager/data.rs index 57c9686..ea103f1 100644 --- a/server/src/manager/data.rs +++ b/server/src/manager/data.rs @@ -279,8 +279,8 @@ pub async fn thread_system(mut app:App, bus:Bus) // - IsLive must have both users connected to the session. let mut valid = match request.game_state { - 1 => session.p_dawn.user.is_none() || session.p_dusk.user.is_none(), - 2 => session.p_dawn.user.is_some() && session.p_dusk.user.is_some(), + 1 => !session.game.complete && (session.p_dawn.user.is_none() || session.p_dusk.user.is_none()), + 2 => !session.game.complete && (session.p_dawn.user.is_some() && session.p_dusk.user.is_some()), 3 => session.game.complete, _ => true, }; @@ -495,18 +495,23 @@ pub async fn thread_system(mut app:App, bus:Bus) }; if let Some(session) = app.sessions.get_mut(&request.token) { - if !session.game.complete { - session.game.process(&play).ok(); - - for (cid, _) in session.get_connections() { - packets.push(QRPacket::new( - cid, - QRPacketData::QGamePlay(PacketGamePlay { - status: STATUS_OK, - turn: session.game.turn, - play, - }), - )); + if !session.game.complete && (session.p_dawn.user.is_some() && session.p_dusk.user.is_some()) { + if (user_id == session.p_dawn.user && session.game.turn & 1 == 0) + || (user_id == session.p_dusk.user && session.game.turn & 1 == 1) { + + session.game.process(&play).ok(); + app.filesystem.session_history_push(session.id, play).ok(); + + for (cid, _) in session.get_connections() { + packets.push(QRPacket::new( + cid, + QRPacketData::QGamePlay(PacketGamePlay { + status: STATUS_OK, + turn: session.game.turn, + play, + }), + )); + } } } } diff --git a/server/src/manager/ws.rs b/server/src/manager/ws.rs index 4004fbf..284d985 100644 --- a/server/src/manager/ws.rs +++ b/server/src/manager/ws.rs @@ -131,6 +131,16 @@ pub async fn handle_ws(ws:WebSocketStream>, args:HttpServiceAr ).ok(); } + CODE_SESSION_RETIRE => match PacketSessionRetire::decode(&data, &mut index) { + Ok(packet) => { + args.bus.send( + bus_ds, + QRPacket::new(conn_id, QRPacketData::QSessionRetire(packet)) + ).ok(); + } + Err(_) => { println!("error: packet decode failed."); } + } + CODE_GAME_STATE => match PacketGameState::decode(&data, &mut index) { Ok(packet) => { args.bus.send( diff --git a/server/src/protocol/packet/game_play.rs b/server/src/protocol/packet/game_play.rs index 3745e93..a771219 100644 --- a/server/src/protocol/packet/game_play.rs +++ b/server/src/protocol/packet/game_play.rs @@ -47,8 +47,8 @@ impl Packet for PacketGamePlay { { let mut data = 0; data |= self.play.source as u16; - data |= (self.play.from as u16) << 1; - data |= (self.play.to as u16) << 7; + data |= (self.play.from as u16) << 4; + data |= (self.play.to as u16) << 10; [ pack_u16(self.status), pack_u16(self.turn), diff --git a/server/src/system/filesystem/mod.rs b/server/src/system/filesystem/mod.rs index 5f13fc9..833d9ff 100644 --- a/server/src/system/filesystem/mod.rs +++ b/server/src/system/filesystem/mod.rs @@ -211,7 +211,7 @@ impl FileSystem { pub fn session_history_push(&mut self, id:u32, history:Play) -> Result<(),()> { - let play_data :u16 = (history.source as u16) | ((history.from as u16) << 1) | ((history.to as u16) << 7); + let play_data :u16 = (history.source as u16) | ((history.from as u16) << 4) | ((history.to as u16) << 10); let bucket_index = id & !HANDLE_BUCKET_MASK; let dir_index = id & HANDLE_BUCKET_MASK; @@ -266,9 +266,9 @@ impl FileSystem { file.read_exact(&mut buffer).map_err(|_| ())?; let data = unpack_u16(&buffer, &mut 0); result.push(Play { - source:(data & 1) as u8, - from:((data >> 1) & 0x3F) as u8, - to:((data >> 7) & 0x3F) as u8, + source:(data & 0xF) as u8, + from:((data >> 4) & 0x3F) as u8, + to:((data >> 10) & 0x3F) as u8, }); } diff --git a/www/js/interface.js b/www/js/interface.js index 8170aa1..b52ee2a 100644 --- a/www/js/interface.js +++ b/www/js/interface.js @@ -754,13 +754,13 @@ const INTERFACE = { }, uninit() { - if(INTERFACE_DATA.online) { - MESSAGE_COMPOSE([ - PACK.u16(OpCode.SessionLeave), - ]); - } - if(INTERFACE_DATA !== null) { + if(INTERFACE_DATA.online) { + MESSAGE_COMPOSE([ + PACK.u16(OpCode.SessionLeave), + ]); + } + MAIN.removeChild(INTERFACE_DATA.canvas); INTERFACE_DATA = null; } @@ -782,6 +782,8 @@ const INTERFACE = { GAME_DATA.turn = data.turn; INTERFACE_DATA.play = data.play; + console.log(data.play.source); + if(INTERFACE_DATA.play.source == 2) { GAME_DATA.state.code = 2; } @@ -833,7 +835,7 @@ const INTERFACE = { retire() { if(INTERFACE_DATA.online) { MESSAGE_COMPOSE([ - OpCode.SessionRetire, + PACK.u16(OpCode.SessionRetire), INTERFACE_DATA.token, ]); } diff --git a/www/js/scene.js b/www/js/scene.js index 068a909..9a672e5 100644 --- a/www/js/scene.js +++ b/www/js/scene.js @@ -434,35 +434,45 @@ const SCENES = { UI.mainmenu(); + let left_buttons = [ ]; + UI.mainnav( - [ ], + left_buttons, [ UI.div([UI.text("0 - 0 of 0")]), UI.button("◀", null), UI.button("▶", null), + UI.button("Refresh", null), ] ); let table = document.createElement("table"); table.setAttribute("id", "content"); + table.setAttribute("class", "list"); MAIN.appendChild(table); - MAIN.setAttribute("class", "list"); - SCENE.refresh(); return true; }, refresh() { - + MESSAGE_SESSION_LIST(0, 3, false, false); }, message(code, data) { - if(code == OpCode.SessionList) { - let table = document.getElementById("content"); - UI.clear(table); + switch(code) { + case OpCode.SessionList: { + let table = document.getElementById("content"); + UI.clear(table); - if(data !== null) { - table.appendChild(UI.session_table(data.records)); - } + if(data !== null) { + table.appendChild(UI.session_table(data.records)); + } + } break; + case OpCode.SessionCreate: + case OpCode.SessionJoin: { + if(data.status == Status.Ok) { + LOAD(SCENES.Game, data); + } + } break; } }, disconnect() { diff --git a/www/js/system.js b/www/js/system.js index 6f54c73..f17196a 100644 --- a/www/js/system.js +++ b/www/js/system.js @@ -230,9 +230,9 @@ function MESSAGE(event) { // Last Play result = UNPACK.u16(bytes, index); index = result.index; - data.play.source = result.data & 1; - data.play.from = (result.data >> 1) & 0x3F; - data.play.to = (result.data >> 7) & 0x3F; + data.play.source = result.data & 0xF; + data.play.from = (result.data >> 4) & 0x3F; + data.play.to = (result.data >> 10) & 0x3F; // Turn result = UNPACK.u16(bytes, index); @@ -314,9 +314,9 @@ function MESSAGE(event) { // Play description result = UNPACK.u16(bytes, index); index = result.index; - data.play.source = result.data & 1; - data.play.from = (result.data >> 1) & 0x3F; - data.play.to = (result.data >> 7) & 0x3F; + data.play.source = result.data & 0xF; + data.play.from = (result.data >> 4) & 0x3F; + data.play.to = (result.data >> 10) & 0x3F; } break; default: