Trading card city builder game?

send patches when state changes

+36 -10
+22 -6
server/src/actor/game_state_watcher.gleam
··· 10 10 import gleam/string 11 11 import mist 12 12 import pog 13 + import squirtle 13 14 import youid/uuid 14 15 15 16 pub type Init { ··· 69 70 |> actor.start() 70 71 } 71 72 73 + fn handle_stop(message, cb: fn() -> actor.Next(s, m)) { 74 + case message { 75 + Stop -> actor.stop() 76 + _ -> cb() 77 + } 78 + } 79 + 72 80 fn handle_message(state: State, message: Message) -> actor.Next(State, Message) { 73 - let result = case message { 81 + use <- handle_stop(message) 82 + let new_state = case message { 83 + Stop -> panic as "unreachable" 74 84 CardCreated(game_state.TileId(tile_id)) -> { 75 85 use tile <- result.try( 76 86 state.db ··· 86 96 tile_type_id: game_state.TileTypeId(tile.tile_type_id), 87 97 )) 88 98 |> fn(gs) { State(..state, game_state: gs) } 89 - |> actor.continue() 90 99 |> Ok() 91 100 } 92 101 CardCreated(game_state.CitizenId(citizen_id)) -> { ··· 105 114 home_tile_id: option.map(citizen.home_tile_id, game_state.TileId), 106 115 )) 107 116 |> fn(gs) { State(..state, game_state: gs) } 108 - |> actor.continue() 109 117 |> Ok() 110 118 } 111 - Stop -> Ok(actor.stop()) 112 119 } 113 - 120 + let result = { 121 + use new_state <- result.try(new_state) 122 + use Nil <- result.try( 123 + game_state.diff(state.game_state, new_state.game_state) 124 + |> squirtle.patches_to_string() 125 + |> mist.send_text_frame(state.conn, _) 126 + |> result.map_error(string.inspect), 127 + ) 128 + Ok(state) 129 + } 114 130 case result { 115 - Ok(next) -> next 131 + Ok(state) -> actor.continue(state) 116 132 Error(error) -> actor.stop_abnormal(error) 117 133 } 118 134 }
+6 -1
server/src/db/sql/get_citizen.sql
··· 1 - SELECT * FROM citizens WHERE id = $1; 1 + SELECT 2 + * 3 + FROM 4 + citizens 5 + WHERE 6 + id = $1;
+1 -1
server/src/db/sql/get_field_citizens.sql
··· 1 1 SELECT 2 - * 2 + * 3 3 FROM 4 4 field_citizens 5 5 WHERE
+1 -1
server/src/db/sql/get_field_tiles.sql
··· 1 1 SELECT 2 - * 2 + * 3 3 FROM 4 4 field_tiles 5 5 WHERE
+6 -1
server/src/db/sql/get_tile.sql
··· 1 - SELECT * FROM tiles WHERE id = $1; 1 + SELECT 2 + * 3 + FROM 4 + tiles 5 + WHERE 6 + id = $1;