From 3934a0487ad80f231e4088902bf8308c12c2d0b0 Mon Sep 17 00:00:00 2001 From: Jeff Halter <868228+jhalter@users.noreply.github.com> Date: Sat, 20 Apr 2024 22:19:42 -0700 Subject: Add handling for new news post transaction --- Hotline/Hotline/HotlineClient.swift | 8 ++++++++ Hotline/Models/Hotline.swift | 13 +++++++++++++ Hotline/Utility/SoundEffects.swift | 1 + 3 files changed, 22 insertions(+) diff --git a/Hotline/Hotline/HotlineClient.swift b/Hotline/Hotline/HotlineClient.swift index 7006c59..c511555 100644 --- a/Hotline/Hotline/HotlineClient.swift +++ b/Hotline/Hotline/HotlineClient.swift @@ -40,6 +40,7 @@ protocol HotlineClientDelegate: AnyObject { func hotlineReceivedUserAccess(options: HotlineUserAccessOptions) func hotlineUserChanged(user: HotlineUser) func hotlineUserDisconnected(userID: UInt16) + func hotlineReceivedNewsPost(message: String) } extension HotlineClientDelegate { @@ -51,6 +52,7 @@ extension HotlineClientDelegate { func hotlineReceivedUserAccess(options: HotlineUserAccessOptions) {} func hotlineUserChanged(user: HotlineUser) {} func hotlineUserDisconnected(userID: UInt16) {} + func hotlineReceivedNewsPost(message: String) {} } enum HotlineClientStage { @@ -342,6 +344,12 @@ class HotlineClient: NetSocketDelegate { self.delegate?.hotlineReceivedUserAccess(options: accessOptions) } } + + case .newMessage: + if let messageField = packet.getField(type: .data), + let message = messageField.getString() { + self.delegate?.hotlineReceivedNewsPost(message: message) + } default: print("HotlineClient: UNKNOWN transaction \(packet.type) with \(packet.fields.count) parameters") diff --git a/Hotline/Models/Hotline.swift b/Hotline/Models/Hotline.swift index 6b5e3a7..baf87eb 100644 --- a/Hotline/Models/Hotline.swift +++ b/Hotline/Models/Hotline.swift @@ -649,6 +649,19 @@ final class Hotline: HotlineClientDelegate, HotlineFileClientDelegate { func hotlineReceivedAgreement(text: String) { self.chat.append(ChatMessage(text: text, type: .agreement, date: Date())) } + + func hotlineReceivedNewsPost(message: String) { + soundEffects.playSoundEffect(.newNews) + let messageBoardRegex = /([\s\r\n]*[_\-]+[\s\r\n]+)/ + let matches = message.matches(of: messageBoardRegex) + + if matches.count == 1 { + let range = matches[0].range + self.messageBoard.insert(String(message[message.startIndex..