Skip to content

Commit

Permalink
Merge pull request #794 from dwasint/fixings
Browse files Browse the repository at this point in the history
Fixings
  • Loading branch information
dwasint authored Dec 24, 2023
2 parents c6cb9a8 + 7fd507f commit 8829498
Show file tree
Hide file tree
Showing 16 changed files with 352 additions and 51 deletions.
5 changes: 5 additions & 0 deletions code/__DEFINES/chat.dm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* SPDX-License-Identifier: MIT
*/

/// How many chat payloads to keep in history
#define CHAT_RELIABILITY_HISTORY_SIZE 5
/// How many resends to allow before giving up
#define CHAT_RELIABILITY_MAX_RESENDS 3

#define MESSAGE_TYPE_SYSTEM "system"
#define MESSAGE_TYPE_LOCALCHAT "localchat"
#define MESSAGE_TYPE_RADIO "radio"
Expand Down
105 changes: 80 additions & 25 deletions code/controllers/subsystem/chat.dm
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,94 @@

SUBSYSTEM_DEF(chat)
name = "Chat"
flags = SS_TICKER
flags = SS_TICKER|SS_NO_INIT
wait = 1
priority = FIRE_PRIORITY_CHAT
init_order = INIT_ORDER_CHAT

var/list/payload_by_client = list()
/// Assosciates a ckey with a list of messages to send to them.
var/list/list/datum/chat_payload/client_to_payloads = list()

/datum/controller/subsystem/chat/Initialize()
// Just used by chat system to know that initialization is nearly finished.
// The to_chat checks could probably check the runlevel instead, but would require testing.
return SS_INIT_SUCCESS
/// Associates a ckey with an assosciative list of their last CHAT_RELIABILITY_HISTORY_SIZE messages.
var/list/list/datum/chat_payload/client_to_reliability_history = list()

/// Assosciates a ckey with their next sequence number.
var/list/client_to_sequence_number = list()

/datum/controller/subsystem/chat/proc/generate_payload(client/target, message_data)
var/sequence = client_to_sequence_number[target.ckey]
client_to_sequence_number[target.ckey] += 1

var/datum/chat_payload/payload = new
payload.sequence = sequence
payload.content = message_data

if(!(target.ckey in client_to_reliability_history))
client_to_reliability_history[target.ckey] = list()
var/list/client_history = client_to_reliability_history[target.ckey]
client_history["[sequence]"] = payload

if(length(client_history) > CHAT_RELIABILITY_HISTORY_SIZE)
var/oldest = text2num(client_history[1])
for(var/index in 2 to length(client_history))
var/test = text2num(client_history[index])
if(test < oldest)
oldest = test
client_history -= "[oldest]"
return payload

/datum/controller/subsystem/chat/proc/send_payload_to_client(client/target, datum/chat_payload/payload)
target.tgui_panel.window.send_message("chat/message", payload.into_message())
SEND_TEXT(target, payload.get_content_as_html())

/datum/controller/subsystem/chat/fire()
for(var/key in payload_by_client)
var/client/client = key
var/payload = payload_by_client[key]
payload_by_client -= key
if(client)
// Send to tgchat
client.tgui_panel?.window.send_message("chat/message", payload)
// Send to old chat
for(var/message in payload)
SEND_TEXT(client, message_to_html(message))
for(var/ckey in client_to_payloads)
var/client/target = GLOB.directory[ckey]
if(isnull(target)) // verify client still exists
LAZYREMOVE(client_to_payloads, ckey)
continue

for(var/datum/chat_payload/payload as anything in client_to_payloads[ckey])
send_payload_to_client(target, payload)
LAZYREMOVE(client_to_payloads, ckey)

if(MC_TICK_CHECK)
return

/datum/controller/subsystem/chat/proc/queue(target, message)
if(islist(target))
for(var/_target in target)
var/client/client = CLIENT_FROM_VAR(_target)
if(client)
LAZYADD(payload_by_client[client], list(message))
/datum/controller/subsystem/chat/proc/queue(queue_target, list/message_data)
var/list/targets = islist(queue_target) ? queue_target : list(queue_target)
for(var/target in targets)
var/client/client = CLIENT_FROM_VAR(target)
if(isnull(client))
continue
LAZYADDASSOCLIST(client_to_payloads, client.ckey, generate_payload(client, message_data))

/datum/controller/subsystem/chat/proc/send_immediate(send_target, list/message_data)
var/list/targets = islist(send_target) ? send_target : list(send_target)
for(var/target in targets)
var/client/client = CLIENT_FROM_VAR(target)
if(isnull(client))
continue
send_payload_to_client(client, generate_payload(client, message_data))

/datum/controller/subsystem/chat/proc/handle_resend(client/client, sequence)
var/list/client_history = client_to_reliability_history[client.ckey]
sequence = "[sequence]"
if(isnull(client_history) || !(sequence in client_history))
return
var/client/client = CLIENT_FROM_VAR(target)
if(client)
LAZYADD(payload_by_client[client], list(message))

var/datum/chat_payload/payload = client_history[sequence]
if(payload.resends > CHAT_RELIABILITY_MAX_RESENDS)
return // we tried but byond said no

payload.resends += 1
send_payload_to_client(client, client_history[sequence])
SSblackbox.record_feedback(
"nested tally",
"chat_resend_byond_version",
1,
list(
"[client.byond_version]",
"[client.byond_build]",
),
)
16 changes: 16 additions & 0 deletions code/datums/chat_payload.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// Stores information about a chat payload
/datum/chat_payload
/// Sequence number of this payload
var/sequence = 0
/// Message we are sending
var/list/content
/// Resend count
var/resends = 0

/// Converts the chat payload into a JSON string
/datum/chat_payload/proc/into_message()
return "{\"sequence\":[sequence],\"content\":[json_encode(content)]}"

/// Returns an HTML-encoded message from our contents.
/datum/chat_payload/proc/get_content_as_html()
return message_to_html(content)
30 changes: 30 additions & 0 deletions code/modules/tgchat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## /TG/ Chat

/TG/ Chat, which will be referred to as TgChat from this point onwards, is a system in which we can send messages to clients in a controlled and semi-reliable manner. The standard way of sending messages to BYOND clients simply dumps whatever you output to them directly into their chat window, however BYOND allows us to load our own code on the client to change this behaviour in a way that allows us to do some pretty neat things.

### Message Format

TgChat handles sending messages from the server to the client through the use of JSON payloads, of which the format will change depending on the type of message and the intended client endpoint. An example of the payload for chat messages is as follows:
```json
{
"sequence": 0,
"content": {
"type": ". . .", // ?optional
"text": ". . .", // ?optional !atleast-one
"html": ". . .", // ?optional !atleast-one
"avoidHighlighting": 0 // ?optional
},
}
```

### Reliability

In the past there have been issues where BYOND will silently and without reason lose a message we sent to the client, to detect this and recover from it seamlessly TgChat also has a baked in reliability layer. This reliability layer is very primitive, and simply keeps track of recieved sequence numbers. Should the client recieve an unexpected sequence number TgChat asks the server to resend any missing packets.

### Ping System

TgChat supports a round trip time ping measurement, which is displayed to the client so they can know how long it takes for their commands and inputs to reach the server. This is done by sending the client a ping request, `ping/soft`, which tells the client to send a ping to the server. When the server recieves said ping it sends a reply, `ping/reply`, to the client with a payload containing the current DateTime which the client can reference against the initial ping request.

### Chat Tabs, Local Storage, and Highlighting

To make organizing and managing chat easier and more functional for both players and admins, TgChat has the ability to filter out messages based on their primary tag, such as individual departmental radios, to a dedicated chat tab for easier reading and comprehension. These tabs can also be configured to highlist messages based on a simple keyword search. You can set a multitude of different keywords to search for and they will be highlighting for instant alerting of the client. Said tabs, highlighting rules, and your chat history will persist thanks to use of local storage on the client. Using local storage TgChat can ensure that your preferences are saved and maintained between client restarts and switching between other /TG/ servers. Local Storage is also used to keep your chat history aswell, should you need to scroll through your chat logs.
20 changes: 3 additions & 17 deletions code/modules/tgchat/to_chat.dm
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,9 @@
if(text) message["text"] = text
if(html) message["html"] = html
if(avoid_highlighting) message["avoidHighlighting"] = avoid_highlighting
var/message_blob = TGUI_CREATE_MESSAGE("chat/message", message)
var/message_html = message_to_html(message)
if(islist(target))
for(var/_target in target)
var/client/client = CLIENT_FROM_VAR(_target)
if(client)
// Send to tgchat
client.tgui_panel?.window.send_raw_message(message_blob)
// Send to old chat
SEND_TEXT(client, message_html)
return
var/client/client = CLIENT_FROM_VAR(target)
if(client)
// Send to tgchat
client.tgui_panel?.window.send_raw_message(message_blob)
// Send to old chat
SEND_TEXT(client, message_html)

// send it immediately
SSchat.send_immediate(target, message)

/**
* Sends the message to the recipient (target).
Expand Down
2 changes: 2 additions & 0 deletions code/modules/tgui/tgui_window.dm
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@
client << link(href_list["url"])
if("cacheReloaded")
reinitialize()
if("chat/resend")
SSchat.handle_resend(client, payload)

/datum/tgui_window/vv_edit_var(var_name, var_value)
return var_name != NAMEOF(src, id) && ..()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
var/datum/artifact_fault/chosen_fault
///the amount of freebies we get
var/freebies = 3
///if we have a special examine IE borgers
var/explict_examine

/datum/component/artifact/Initialize(forced_origin = null)
. = ..()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
SIGNAL_HANDLER
if(examine_hint)
examine_list += examine_hint
if(explict_examine)
examine_list += explict_examine

/datum/component/artifact/proc/on_sticker(atom/source, obj/item/sticker/sticker, mob/user)
SIGNAL_HANDLER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/datum/artifact_activator/touch/carbon,
/datum/artifact_activator/touch/silicon
)
explict_examine = "Vaguely Foreboding"
///the time between each limb replacement
var/limb_replace_time = 1 SECONDS
COOLDOWN_DECLARE(borg_cooldown)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/datum/component/artifact/emotegen
associated_object = /obj/structure/artifact/emotegen
weight = ARTIFACT_UNCOMMON
type_name = "Emote Forcefield"
activation_message = "springs to life and starts emitting a forcefield!"
deactivation_message = "shuts down, its forcefields shutting down with it."
valid_activators = list(
/datum/artifact_activator/touch/carbon,
/datum/artifact_activator/touch/silicon,
/datum/artifact_activator/range/force
)
var/cooldown_time //cooldown AFTER the shield lowers
var/radius
var/shield_time
var/list/all_emotes = list(
"flip",
"fart",
"spin",
"clap",
"droll",
"scream",
"deathgasp",
"yawn",
"blink",
"snore",
"cry",
)
var/list/picked_emotes = list()
COOLDOWN_DECLARE(cooldown)

/datum/component/artifact/emotegen/setup()
for(var/i = 1 to rand(3,4))
picked_emotes += pick(all_emotes)

activation_sound = pick('sound/mecha/mech_shield_drop.ogg')
deactivation_sound = pick('sound/mecha/mech_shield_raise.ogg','sound/magic/forcewall.ogg')
shield_time = rand(10,40) SECONDS
radius = rand(1,10)
cooldown_time = shield_time / 3
potency += radius * 3 + shield_time / 30

/datum/component/artifact/emotegen/effect_activate(silent)
if(!COOLDOWN_FINISHED(src,cooldown))
holder.visible_message(span_notice("[holder] wheezes, shutting down."))
artifact_deactivate(TRUE)
return
addtimer(CALLBACK(src, TYPE_PROC_REF(/datum/component/artifact, artifact_deactivate)), shield_time)
COOLDOWN_START(src,cooldown,shield_time + cooldown_time)

/datum/component/artifact/emotegen/effect_process()
var/current_emote = pick(picked_emotes)

holder.anchored = TRUE
var/turf/our_turf = get_turf(holder)
for(var/turf/open/floor in range(radius,holder))
if(floor == our_turf)
continue
for(var/mob/living/living in floor)
living.emote(current_emote, intentional = FALSE)

/datum/component/artifact/emotegen/effect_deactivate()
holder.anchored = FALSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/datum/component/artifact/smoke
associated_object = /obj/structure/artifact/smoke
weight = ARTIFACT_UNCOMMON
type_name = "Smoke Machine"
activation_message = "starts spewing out smoke!"
deactivation_message = "becomes silent."

var/list/valid_chemicals = list(
/datum/reagent/colorful_reagent,
/datum/reagent/colorful_reagent/powder/black,
/datum/reagent/colorful_reagent/powder/blue,
/datum/reagent/colorful_reagent/powder/purple,
/datum/reagent/colorful_reagent/powder/orange,
/datum/reagent/colorful_reagent/powder/red,
/datum/reagent/colorful_reagent/powder/yellow,
)
var/per_chemical_amount = 5
var/chemicals_chosen = 2
var/list/chemicals = list()
var/smoke_range = 3

/datum/component/artifact/smoke/setup()
per_chemical_amount = rand(5, 10)
chemicals_chosen = rand(1, 5)
smoke_range = rand(1, 5)
potency += per_chemical_amount * 3 + chemicals_chosen * 3 + smoke_range * 2

for(var/i = 1 to chemicals_chosen)
chemicals += pick(valid_chemicals)

/datum/component/artifact/smoke/effect_activate(silent)
for(var/chemical in chemicals)
do_chem_smoke(smoke_range, holder = holder, location = get_turf(holder), reagent_type = chemical, reagent_volume = per_chemical_amount, log = TRUE)
artifact_deactivate()

/datum/component/artifact/smoke/toxin
associated_object = /obj/structure/artifact/smoke/toxin
weight = ARTIFACT_RARE
activation_message = "starts spewing out toxic smoke!"
valid_chemicals = list(
/datum/reagent/toxin/bonehurtingjuice,
/datum/reagent/toxin/itching_powder,
/datum/reagent/toxin/mindbreaker,
/datum/reagent/toxin/spewium,
)

/datum/component/artifact/smoke/flesh
associated_object = /obj/structure/artifact/smoke/flesh
weight = ARTIFACT_RARE
activation_message = "starts spewing out flesh mending smoke!"
valid_chemicals = list(
/datum/reagent/medicine/c2/synthflesh
)

/datum/component/artifact/smoke/exotic
associated_object = /obj/structure/artifact/smoke/exotic
weight = ARTIFACT_RARE
activation_message = "starts spewing out exotic smoke!"
valid_chemicals = list(
/datum/reagent/wittel,
/datum/reagent/medicine/omnizine/protozine,
/datum/reagent/water/hollowwater,
/datum/reagent/plasma_oxide,
/datum/reagent/australium,
/datum/reagent/shakeium,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/datum/component/artifact/surgery
associated_object = /obj/structure/artifact/surgery
weight = ARTIFACT_VERYUNCOMMON
type_name = "Surgery Object"
activation_message = "springs to life!"
deactivation_message = "becomes silent."
valid_activators = list(
/datum/artifact_activator/touch/carbon,
/datum/artifact_activator/touch/silicon
)
COOLDOWN_DECLARE(surgery_cooldown)


/datum/component/artifact/surgery/effect_touched(mob/living/user)
if(!COOLDOWN_FINISHED(src, surgery_cooldown))
holder.visible_message(span_notice("[holder] wheezes, shutting down."))
return
if(!ishuman(user))
return
var/mob/living/carbon/human/human = user
human.bioscramble(holder.name)

COOLDOWN_START(src,surgery_cooldown, 5 SECONDS)
Loading

0 comments on commit 8829498

Please sign in to comment.