Massive refactor of several different components including networking fixes

This commit is contained in:
Ceikry
2021-12-19 05:40:17 +00:00
parent ecaed745ef
commit 10219652b0
506 changed files with 2116 additions and 5072 deletions
@@ -1,135 +0,0 @@
package rs09.worker
import api.ContentAPI
import core.game.system.SystemManager
import core.game.system.SystemState
import core.game.system.task.Pulse
import core.plugin.CorePluginTypes.Managers
import gui.GuiEvent
import gui.ServerMonitor
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import rs09.Server
import rs09.ServerConstants
import rs09.ServerStore
import rs09.game.ai.general.GeneralBotCreator
import rs09.game.system.SystemLogger
import rs09.game.world.GameWorld
import rs09.game.world.repository.Repository
import rs09.game.world.update.UpdateSequence
import rs09.net.packet.PacketWriteQueue
import rs09.tools.stringtools.colorize
import java.lang.Long.max
import java.lang.Long.min
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.system.exitProcess
/**
* Handles the running of pulses and writing of masks, etc
* @author Ceikry
*/
class MajorUpdateWorker {
var started = false
val sequence = UpdateSequence()
val sdf = SimpleDateFormat("HHmmss")
val worker = Thread {
Thread.currentThread().name = "Major Update Worker"
started = true
Thread.sleep(600L)
while(true){
val start = System.currentTimeMillis()
val rmlist = ArrayList<Pulse>()
val list = ArrayList(GameWorld.Pulser.TASKS)
Server.heartbeat()
//run our pulses
for(pulse in list) {
val b = System.currentTimeMillis()
if (pulse == null || pulse.update()) rmlist.add(pulse)
val time = System.currentTimeMillis() - b
if(time >= 100){
if(pulse is GeneralBotCreator.BotScriptPulse){
SystemLogger.logWarn("CRITICALLY Long Botscript Tick: ${pulse.botScript.javaClass.name} - $time ms")
} else {
SystemLogger.logWarn("CRITICALLY long running pulse: ${pulse.javaClass.name} - $time ms")
}
}
else if(time >= 30){
if(pulse is GeneralBotCreator.BotScriptPulse){
SystemLogger.logWarn("Long Botscript Tick: ${pulse.botScript.javaClass.name} - $time ms")
} else {
SystemLogger.logWarn("Long Running Pulse: ${pulse.javaClass.name} - $time ms")
}
}
}
//remove all null or finished pulses from the list
rmlist.forEach {
if(GameWorld.Pulser.TASKS.contains(it)) GameWorld.Pulser.TASKS.remove(it)
}
rmlist.clear()
//perform our update sequence where we write masks, etc
try {
sequence.start()
sequence.run()
sequence.end()
GlobalScope.launch {
PacketWriteQueue.flush()
}
} catch (e: Exception){
e.printStackTrace()
}
//increment global ticks variable
GameWorld.pulse()
//disconnect all players waiting to be disconnected
Repository.disconnectionQueue.update()
//tick all manager plugins
Managers.tick()
//Handle daily restart if enabled
if(sdf.format(Date()).toInt() == 0){
if(GameWorld.checkDay() == 1) {//monday
ServerStore.clearWeeklyEntries()
}
ServerStore.clearDailyEntries()
if(ServerConstants.DAILY_RESTART ) {
Repository.sendNews(colorize("%RSERVER GOING DOWN FOR DAILY RESTART IN 5 MINUTES!"))
ServerConstants.DAILY_RESTART = false
ContentAPI.submitWorldPulse(object : Pulse(100) {
var counter = 0
override fun pulse(): Boolean {
counter++
if (counter == 5) {
exitProcess(0)
}
Repository.sendNews(colorize("%RSERVER GOING DOWN FOR DAILY RESTART IN ${5 - counter} MINUTE${if (counter < 4) "S" else ""}!"))
return false
}
})
}
}
val end = System.currentTimeMillis()
ServerMonitor.eventQueue.add(GuiEvent.UpdateTickTime(end - start))
ServerMonitor.eventQueue.add(GuiEvent.UpdatePulseCount(GameWorld.Pulser.TASKS.size))
Thread.sleep(max(600 - (end - start), 0))
}
}
fun start() {
if(!started){
worker.start()
}
if (ServerConstants.ALLOW_GUI)
ServerMonitor.open()
}
}
@@ -0,0 +1,88 @@
package rs09.worker
import api.ContentAPI
import core.game.system.task.Pulse
import core.plugin.CorePluginTypes.Managers
import gui.GuiEvent
import gui.ServerMonitor
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import rs09.Server
import rs09.ServerConstants
import rs09.ServerStore
import rs09.game.ai.general.GeneralBotCreator
import rs09.game.system.SystemLogger
import rs09.game.world.World
import rs09.game.world.repository.Repository
import rs09.game.world.update.UpdateSequence
import rs09.net.packet.PacketWriteQueue
import rs09.tools.stringtools.colorize
import java.lang.Long.max
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.system.exitProcess
/**
* Handles the running of pulses and writing of masks, etc
* @author Ceikry
*/
class WorldClock {
var started = false
private val sdf = SimpleDateFormat("HHmmss")
private val worker = Thread {
Thread.currentThread().name = "Major Update Worker"
started = true
Thread.sleep(600L)
while(true){
val start = System.currentTimeMillis()
try {
World.tick()
} catch(e: Exception){
SystemLogger.logErr(e.stackTraceToString())
}
//Handle daily restart if enabled
if(sdf.format(Date()).toInt() == 0){
if(checkDay() == 1) {//monday
ServerStore.clearWeeklyEntries()
}
ServerStore.clearDailyEntries()
if(ServerConstants.DAILY_RESTART) {
ContentAPI.sendNews(colorize("%RSERVER GOING DOWN FOR DAILY RESTART IN 5 MINUTES!"))
ServerConstants.DAILY_RESTART = false
ContentAPI.submitWorldPulse(object : Pulse(100) {
var counter = 0
override fun pulse(): Boolean {
counter++
if (counter == 5) {
exitProcess(0)
}
ContentAPI.sendNews(colorize("%RSERVER GOING DOWN FOR DAILY RESTART IN ${5 - counter} MINUTE${if (counter < 4) "S" else ""}!"))
return false
}
})
}
}
val end = System.currentTimeMillis()
ServerMonitor.eventQueue.add(GuiEvent.UpdateTickTime(end - start))
ServerMonitor.eventQueue.add(GuiEvent.UpdatePulseCount(World.Pulser.TASKS.size))
Thread.sleep(max(600 - (end - start), 0))
}
}
fun start() {
if(!started){
worker.start()
}
if (ServerConstants.ALLOW_GUI)
ServerMonitor.open()
}
fun checkDay(): Int {
val weeklySdf = SimpleDateFormat("u")
return weeklySdf.format(Date()).toInt()
}
}