server.player.chat
Constants #
const announcement_key = '%chat.type.announcement'
announcement_key is the translation key the client renders as an announcement, naming the player the message came from.
fn global #
fn global() &Chat
global returns the chat every player is subscribed to while they are online.
fn new #
fn new() &Chat
new returns a Chat with no subscribers.
fn translate #
fn translate(key string, parameters []string) Translation
translate returns a Translation for the key passed, with the parameters filled in in the order the key expects them.
interface Subscriber #
interface Subscriber {
// subscriber_id returns a value that is unique for as long as the
// subscriber is subscribed.
subscriber_id() u64
mut:
// message delivers a line of chat to the subscriber.
message(text string)
}
Subscriber is anything that can receive chat messages: a player session, the console, or a log sink. Subscribers are compared by id so one may be removed again without holding on to the value itself.
struct Chat #
struct Chat {
mut:
mutex &sync.Mutex = sync.new_mutex()
subscribers map[u64]Subscriber
}
Chat is a channel messages are written to and every subscriber of which receives them. Servers use the global chat for public messages, but separate channels may be built for teams, staff or worlds.
fn (Chat) subscribe #
fn (mut c Chat) subscribe(sub Subscriber)
subscribe adds sub to the Chat, replacing an earlier subscriber with the same id.
fn (Chat) unsubscribe #
fn (mut c Chat) unsubscribe(id u64)
unsubscribe removes the subscriber with the id passed, if it is subscribed.
fn (Chat) subscriber_count #
fn (c &Chat) subscriber_count() int
subscriber_count returns how many subscribers the Chat has.
fn (Chat) write #
fn (mut c Chat) write(text string)
write sends text to every subscriber. Subscribers are collected under the lock and written to outside of it, so a slow subscriber cannot block another thread from subscribing.
struct Translation #
struct Translation {
pub:
key string
parameters []string
}
Translation is a message the client renders in its own language. The key names an entry in the client's language files and the parameters fill in its placeholders.
fn (Translation) with #
fn (t Translation) with(parameters []string) Translation
with returns a copy of the Translation with the parameters passed, leaving the original alone so a key may be declared once and reused.