tangled
alpha
login
or
join now
eldridge.cam
/
cartography
0
fork
atom
Trading card city builder game?
0
fork
atom
overview
issues
pulls
pipelines
send patches when state changes
eldridge.cam
1 month ago
37cbc425
114c318a
+36
-10
5 changed files
expand all
collapse all
unified
split
server
src
actor
game_state_watcher.gleam
db
sql
get_citizen.sql
get_field_citizens.sql
get_field_tiles.sql
get_tile.sql
+22
-6
server/src/actor/game_state_watcher.gleam
···
10
10
import gleam/string
11
11
import mist
12
12
import pog
13
13
+
import squirtle
13
14
import youid/uuid
14
15
15
16
pub type Init {
···
69
70
|> actor.start()
70
71
}
71
72
73
73
+
fn handle_stop(message, cb: fn() -> actor.Next(s, m)) {
74
74
+
case message {
75
75
+
Stop -> actor.stop()
76
76
+
_ -> cb()
77
77
+
}
78
78
+
}
79
79
+
72
80
fn handle_message(state: State, message: Message) -> actor.Next(State, Message) {
73
73
-
let result = case message {
81
81
+
use <- handle_stop(message)
82
82
+
let new_state = case message {
83
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
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
108
-
|> actor.continue()
109
117
|> Ok()
110
118
}
111
111
-
Stop -> Ok(actor.stop())
112
119
}
113
113
-
120
120
+
let result = {
121
121
+
use new_state <- result.try(new_state)
122
122
+
use Nil <- result.try(
123
123
+
game_state.diff(state.game_state, new_state.game_state)
124
124
+
|> squirtle.patches_to_string()
125
125
+
|> mist.send_text_frame(state.conn, _)
126
126
+
|> result.map_error(string.inspect),
127
127
+
)
128
128
+
Ok(state)
129
129
+
}
114
130
case result {
115
115
-
Ok(next) -> next
131
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
1
-
SELECT * FROM citizens WHERE id = $1;
1
1
+
SELECT
2
2
+
*
3
3
+
FROM
4
4
+
citizens
5
5
+
WHERE
6
6
+
id = $1;
+1
-1
server/src/db/sql/get_field_citizens.sql
···
1
1
SELECT
2
2
-
*
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
-
*
2
2
+
*
3
3
FROM
4
4
field_tiles
5
5
WHERE
+6
-1
server/src/db/sql/get_tile.sql
···
1
1
-
SELECT * FROM tiles WHERE id = $1;
1
1
+
SELECT
2
2
+
*
3
3
+
FROM
4
4
+
tiles
5
5
+
WHERE
6
6
+
id = $1;