Skip to content

Commit

Permalink
Add room naming command
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklanng committed Apr 8, 2024
1 parent cc5a37f commit ef26488
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/data/world.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@ pub fn insert_exit(
}
}

pub fn update_room_name(
conn_string,
room_id: Int,
name: String,
) -> Result(Nil, Error) {
use conn <- sqlight.with_connection(conn_string)

let sql =
"UPDATE `rooms` SET `name` = '"
<> name
<> "' WHERE id = "
<> int.to_string(room_id)
<> ";"

case sqlight.exec(sql, on: conn) {
Ok(Nil) -> Ok(Nil)
Error(sqlight.SqlightError(_code, message, _offset)) -> {
Error(SqlError(message))
}
}
}

pub fn update_room_description(
conn_string,
room_id: Int,
Expand Down
66 changes: 66 additions & 0 deletions src/simulation.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub type Command {
target_room_id: Int,
reverse_dir: world.Direction,
)
AdminRoomName(entity_id: Int, name: String)
AdminRoomDescription(entity_id: Int, description: String)
}

Expand Down Expand Up @@ -446,6 +447,58 @@ fn loop(message: Command, state: State) -> actor.Next(Command, State) {
}
}
}
AdminRoomName(entity_id, name) -> {
let assert Ok(controlled_entity) =
dict.get(state.controlled_entities, entity_id)

case controlled_entity.room_id == 0 {
False ->
case
world.update_room_name(
state.conn_string,
controlled_entity.room_id,
name,
)
{
Ok(Nil) -> {
let new_state =
state
|> set_room_name(controlled_entity.room_id, name)

let assert Ok(room) =
dict.get(new_state.rooms, controlled_entity.room_id)

let entities = list_entities(entity_id, room)
process.send(
controlled_entity.update_subject,
UpdateRoomDescription(
name: room.template.name,
description: room.template.description,
exits: room.template.exits,
sentient_entities: entities.0,
static_entities: entities.1,
),
)

actor.continue(new_state)
}
Error(world.SqlError(message)) -> {
process.send(
controlled_entity.update_subject,
UpdateCommandFailed(reason: "SQL Error: " <> message),
)
actor.continue(state)
}
}
True -> {
process.send(
controlled_entity.update_subject,
UpdateCommandFailed(reason: "Cannot update room #0."),
)
actor.continue(state)
}
}
}
AdminRoomDescription(entity_id, description) -> {
let assert Ok(controlled_entity) =
dict.get(state.controlled_entities, entity_id)
Expand Down Expand Up @@ -637,6 +690,19 @@ fn build_exit(
)
}

fn set_room_name(state: State, room_id: Int, name: String) -> State {
let assert Ok(room) = dict.get(state.rooms, room_id)

State(
..state,
rooms: dict.insert(
state.rooms,
room_id,
Room(..room, template: world.RoomTemplate(..room.template, name: name)),
),
)
}

fn set_room_description(
state: State,
room_id: Int,
Expand Down
5 changes: 4 additions & 1 deletion src/telnet/game_connection.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ fn parse_command(
) -> Result(simulation.Command, ParseCommandError) {
case string.split(str, " ") {
["quit", ..] -> Ok(simulation.CommandQuit(entity_id))
["look", ..] -> Ok(simulation.CommandLook(entity_id))
["look", ..] | ["l", ..] -> Ok(simulation.CommandLook(entity_id))
["say", ..rest] ->
case list.length(rest) {
0 -> Error(SayWhat)
Expand Down Expand Up @@ -375,6 +375,9 @@ fn parse_command(
usage: "@tunnel <dir:Direction> <room_id:Int> [reverse_dir:Direction]",
))
}
["@name"] -> Error(InvalidCommand(usage: "@name <room_name:String>"))
["@name", ..name] ->
Ok(simulation.AdminRoomName(entity_id, string.join(name, " ")))

_ -> Error(UnknownCommand)
}
Expand Down

0 comments on commit ef26488

Please sign in to comment.