Skip to content

Commit

Permalink
Merge pull request #245 from Zekiah-A/main
Browse files Browse the repository at this point in the history
Announce all channels, live chat delete final fix
  • Loading branch information
Zekiah-A authored Feb 25, 2024
2 parents 4d7c7b0 + 1fce9f9 commit 77d9666
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
27 changes: 16 additions & 11 deletions db-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { parentPort } from 'worker_threads'
import { Database } from 'bun:sqlite'
import { Queue } from '@datastructures-js/queue'

const db = new Database("server.db")
let db = new Database("server.db")
let dbClosed = false

export type LiveChatMessage = {
messageId: number,
Expand Down Expand Up @@ -79,6 +80,7 @@ export type LiveChatDeletion = {
reason: string,
deletionDate: number
}
type LiveChatUpdateDeletion = { deletionId: number, messageId: number }
export type AuthenticateUser = { token: string, ip: string }
export type DbInternals = {
setUserChatName: (data: { newName: string, intId: number }) => void,
Expand All @@ -96,7 +98,6 @@ export type DbInternals = {
exec: (data: { stmt: string, params: any }) => any[]|null
}

try{
const createLiveChatMessages = `
CREATE TABLE IF NOT EXISTS LiveChatMessages (
messageId INTEGER PRIMARY KEY,
Expand Down Expand Up @@ -358,8 +359,11 @@ const internal: DbInternals = {
return maxMessageId
},
commitShutdown: function() {
performBulkInsertions()
db.close()
if (!dbClosed) {
performBulkInsertions()
db.close()
dbClosed = true
}
},
/* Send date is seconds unix epoch offset */
insertLiveChat: function(data) {
Expand All @@ -382,8 +386,9 @@ const internal: DbInternals = {
}
}

const query = db.query<void, LiveChatDeletion>("UPDATE LiveChatMessages SET deletionId = $deletionId WHERE messageId = $messageId")
query.run(toQueryObject(deletion))
const query = db.query<void, LiveChatUpdateDeletion>("UPDATE LiveChatMessages SET deletionId = $deletionId WHERE messageId = $messageId")
const deletionUpdate:LiveChatUpdateDeletion = { deletionId: deletion.deletionId, messageId: data.messageId }
query.run(toQueryObject(deletionUpdate))
},
insertPlaceChat: function(data) {
placeChatInserts.push(data)
Expand Down Expand Up @@ -414,13 +419,13 @@ const internal: DbInternals = {
return null
}
},
reInitialise: function() { // Re-create the DB
internal.commitShutdown()
db = new Database("server.db")
}
}

parentPort?.on("message", (message) => {
const result = internal[message.call] && internal[message.call](message.data)
parentPort?.postMessage({ handle: message.handle, data: result })
})
}
catch(e){
console.error("Error from DB worker:", e)
}
})
15 changes: 10 additions & 5 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1349,13 +1349,18 @@ function blacklist(identifier: ServerWebSocket<any>|number|string) {
fs.appendFile("./blacklist.txt", "\n" + ip)
}

const defaultChannels = ["en", "zh", "hi", "es", "fr", "ar", "bn", "ru", "pt", "ur", "de", "jp", "tr", "vi", "ko", "it", "fa", "sr", "az"]
/**
* Broadcast a message as the server to a specific client (p) or all players, in a channel
* Broadcast a message as the server to all default channels, or a specific provided channel
*/
function announce(msg: string, channel: string, p:ServerWebSocket<any>|null = null, repliesTo:number|null = null) {
const packet = createChatPacket(0, msg, Math.floor(NOW / 1000), 0, 0, channel, repliesTo)
if (p != null) p.send(packet)
else for (const c of wss.clients) c.send(packet)
function announce(msg: string, channel: string|null = null, repliesTo:number|null = null) {
const targetChannels = channel ? [channel] : defaultChannels
for (const ch of targetChannels) {
const packet = createChatPacket(0, msg, Math.floor(NOW / 1000), 0, 0, ch, repliesTo)
for (const c of wss.clients) {
c.send(packet)
}
}
}

let shutdown = false
Expand Down

0 comments on commit 77d9666

Please sign in to comment.