Skip to content

Commit

Permalink
Merge pull request #272 from golters/dev
Browse files Browse the repository at this point in the history
possible ping fix and event winning spam fix
  • Loading branch information
IamtheMa authored Dec 13, 2023
2 parents b5f3245 + afe6c09 commit dad3a2c
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"editor.tabSize": 2,
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.fixAll.eslint": "explicit"
},
}
2 changes: 1 addition & 1 deletion client/src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ function drawInbox(){
const messageBox = document.createElement('span')
messageBox.classList.add("message-box")
const message = document.createElement('span')
//message.classList.add("chat-message")
message.classList.add("chat-message")
const date = document.createElement('span')
date.classList.add("date")
date.appendChild(document.createTextNode(timestamp))
Expand Down
3 changes: 2 additions & 1 deletion client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const startMessages: string[] = [
"Mudgolt stands for Multi User Dungeon... The other four letters represent a secret blend of herbs and spices for legal reasons I am unable to share.",
"Mudgolt was invented in 1972 in an attempt to communicate with multi dimensional beings. But it turned out to be the ultimate tool for humans to communicate too!",
"This version of Mudgolt was discovered on a floppy disk in a yard sale in 1986. It was later uploaded to the internet in the 2020s.",
"Somebody loves you."
"Somebody loves you.",
"This is the real metaverse"
]

const init = async () => {
Expand Down
28 changes: 20 additions & 8 deletions client/src/network/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TP_EVENT,
LOG_EVENT,
UFO_EVENT,
PONG_EVENT,
} from "../../../events"
import {
store,
Expand Down Expand Up @@ -60,7 +61,7 @@ export const networkTask = () => new Promise<void>((resolve) => {
reconnectAttempts++

client.addEventListener("open", () => {
//pushToLog("Connected to server")
pushToLog("Connected to server")

reconnectAttempts = 0
})
Expand Down Expand Up @@ -133,17 +134,28 @@ setInterval(() => {
}
}, 15 * 10)

let ping = setInterval(() => {
if(localStorage.getItem("focus") === "open"){
setTimeout(() => {
console.log("new ping")
sendEvent(PING_EVENT, client)
sendEvent(PAY_EVENT, store.player?.id)
}else{
if(Math.random()*10000 < 1){
sendEvent(UFO_EVENT, store.player?.id)
}
}
}, 15 * 1000)

//make settimeout then ping server, server return event to start a new timeout
networkEmitter.on(PONG_EVENT, () => {
console.log("pong")
setTimeout(() => {
console.log("ping")
sendEvent(PING_EVENT, client)
if(localStorage.getItem("focus") === "open"){
sendEvent(PAY_EVENT, store.player?.id)
}else{
if(Math.random()*10000 < 5){
sendEvent(UFO_EVENT, store.player?.id)
}
}
}, 15 * 1000)
})

window.addEventListener("focus", (event) => {
localStorage.setItem("focus","open")
})
Expand Down
1 change: 1 addition & 0 deletions events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const MAKE_ROOM_EVENT = "MAKE_ROOM"
export const GO_EVENT = "GO"
export const USERNAME_CHANGE_EVENT = "USERNAME_CHANGE"
export const PING_EVENT = "PING"
export const PONG_EVENT = "PONG"
export const CHAT_HISTORY_EVENT = "CHAT_HISTORY"
export const LOOK_EVENT = "LOOK"
export const DRAW_EVENT = "DRAW"
Expand Down
2 changes: 2 additions & 0 deletions server/network/events/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
import {
PING_EVENT,
ERROR_EVENT,
PONG_EVENT,
} from "../../../events"
import {
online,
Expand All @@ -20,6 +21,7 @@ const handler: NetworkEventHandler = (socket, player) => {
//sendEvent<string>(socket, ERROR_EVENT, "a bug has occured, you are no longer online")
}
sendEvent<null>(socket, PING_EVENT, null)
sendEvent<string>(socket, PONG_EVENT, "pong " + player)
}

networkEmitter.on(PING_EVENT, handler)
15 changes: 14 additions & 1 deletion server/services/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ export const getUpcomingEvents = async (time: number): Promise<Event[]> => {

//spamming offline/afk users with backloged event ending messages
export const clearOldEvents = async (time: number): Promise<void> => {
console.log()
const oldEvents = await db.all<Event[]>(/*sql*/`
SELECT * FROM events
WHERE "end" < $1
Expand Down Expand Up @@ -716,7 +717,7 @@ export const createRandomEvent = async (time: number): Promise<void> => {
targetDate.setMinutes(0)
targetDate.setSeconds(0)
const start = targetDate.getTime() - new Date().getTime()
const length = 4.32e+7
const length = 15 * 4.32e+7
const type = Math.random() * 3
createEvent(events[Math.floor(type)],time + start, time + start + length)
}
Expand Down Expand Up @@ -941,6 +942,18 @@ export const pollResults = async (Event: Event): Promise<string> => {
}

export const electionWinner = async (event: number): Promise<void> => {
const eventcheck = await db.get<Event>(/*sql*/`
SELECT * FROM events WHERE id = $1;
`, [event])

if(!eventcheck){
return
}

await db.run(/*sql*/`
DELETE FROM events
WHERE "id" = $1;
`, [event])

const votes = await db.all<EventTag[]>(/*sql*/`
SELECT * FROM eventTags
Expand Down

0 comments on commit dad3a2c

Please sign in to comment.