Skip to content

Commit

Permalink
Merge pull request #109 from zediious/0.3.2
Browse files Browse the repository at this point in the history
Changes for 0.3.2
  • Loading branch information
zediious authored Nov 9, 2023
2 parents fb0c585 + 249c496 commit d1b23bf
Show file tree
Hide file tree
Showing 17 changed files with 387 additions and 22 deletions.
12 changes: 7 additions & 5 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dependencies:
- python=3.11.5
- pip
- pip:
- django==4.2.5
- django==4.2.7
- requests==2.31.0
- argon2-cffi==21.3.0
- argon2-cffi-bindings==21.2.0
Expand All @@ -14,18 +14,20 @@ dependencies:
- django-bootstrap5==23.3
- dnspython==2.4.2
- mcstatus==11.0.1
- pillow==10.0.1
- pillow==10.1.0
- prompt-toolkit==3.0.39
- wcwidth==0.2.7
- wcwidth==0.2.9
- daphne==4.0.0
- whitenoise==6.5.0
- whitenoise==6.6.0
- django-tinymce==3.6.1
- django-js-asset==2.1.0
- python-dotenv==1.0.0
- mysqlclient==2.1.1
- django-jazzmin==2.6.0
- discord.py==2.3.2
- django-ranged-response==0.2.0
- django-simple-captcha==0.5.18
- django-simple-captcha==0.5.20
- six==1.16.0
- django-resized==1.0.2
- pandas==2.1.2
- plotly==5.18.0
41 changes: 37 additions & 4 deletions raptorWeb/gameservers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from tinymce.widgets import TinyMCE

from raptorWeb.gameservers.models import Server, Player, ServerStatistic
from raptorWeb.gameservers.models import Server, Player, ServerStatistic, PlayerCountHistoric

class ServerAdminForm(ModelForm):
class Meta:
Expand Down Expand Up @@ -70,20 +70,52 @@ class PlayerAdmin(admin.ModelAdmin):
('Player Information', {
'fields': (
'server',
'name')
'name',
'online',
'last_online')
}),
)

readonly_fields: tuple[str] = (
'server',
'name'
'name',
'online',
'last_online'
)

search_fields: list[str] = [
'name',
]

list_display: list[str] = ['name', 'server']
list_display: list[str] = ['name', 'server', 'online', 'last_online']

def has_add_permission(self, request, obj=None):
return False

def has_change_permission(self, request, obj=None):
return False

class PlayerCountHistoricAdmin(admin.ModelAdmin):
"""
Object defining behavior and display of
PlayerCountHistoric in the Django admin interface.
"""
fieldsets: tuple[tuple[str, dict[str, tuple[str]]]] = (
('Player Information', {
'fields': (
'server',
'player_count',
'checked_time')
}),
)

readonly_fields: tuple[str] = (
'server',
'player_count',
'checked_time'
)

list_display: list[str] = ['server', 'player_count', 'checked_time']

def has_add_permission(self, request, obj=None):
return False
Expand Down Expand Up @@ -117,4 +149,5 @@ def has_change_permission(self, request, obj=None):

admin.site.register(Server, ServerAdmin)
admin.site.register(Player, PlayerAdmin)
admin.site.register(PlayerCountHistoric, PlayerCountHistoricAdmin)
admin.site.register(ServerStatistic, ServerStatisticAdmin)
34 changes: 34 additions & 0 deletions raptorWeb/gameservers/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django import forms

from raptorWeb.gameservers.models import Server

class StatisticFilterForm(forms.Form):
start = forms.DateTimeField(
required=False,
widget=forms.DateTimeInput(attrs={'type': 'datetime-local'}))

end = forms.DateTimeField(
required=False,
widget=forms.DateTimeInput(attrs={'type': 'datetime-local'}))

server = forms.ModelChoiceField(
queryset=Server.objects.filter(archived=False),
empty_label="Choose a Server")

class StatisticFilterFormFireFox(forms.Form):
"""
Firefox does not support the datetime-local HTML5 widget
and as such needs to be supplied with only a date widget.
"""
start = forms.DateTimeField(
required=False,
widget=forms.DateTimeInput(attrs={'type': 'date'}))

end = forms.DateTimeField(
required=False,
widget=forms.DateTimeInput(attrs={'type': 'date'}))

server = forms.ModelChoiceField(
queryset=Server.objects.filter(archived=False),
to_field_name='modpack_name',
empty_label="Choose a Server")
18 changes: 18 additions & 0 deletions raptorWeb/gameservers/migrations/0004_player_online.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2023-11-06 18:16

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('gameservers', '0003_server_archived'),
]

operations = [
migrations.AddField(
model_name='player',
name='online',
field=models.BooleanField(default=False),
),
]
19 changes: 19 additions & 0 deletions raptorWeb/gameservers/migrations/0005_player_last_online.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.7 on 2023-11-06 18:35

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('gameservers', '0004_player_online'),
]

operations = [
migrations.AddField(
model_name='player',
name='last_online',
field=models.DateTimeField(auto_now_add=True, verbose_name='Last Online'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 4.2.7 on 2023-11-06 19:59

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('gameservers', '0005_player_last_online'),
]

operations = [
migrations.AlterField(
model_name='player',
name='server',
field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.PROTECT, to='gameservers.server'),
),
migrations.CreateModel(
name='PlayerCountHistoric',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('player_count', models.IntegerField(verbose_name='Players Online')),
('checked_time', models.DateTimeField(auto_now_add=True, verbose_name='Time of Query')),
('server', models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='gameservers.server')),
],
options={
'verbose_name': 'Historic Player Count',
'verbose_name_plural': 'Historic Player Counts',
},
),
]
88 changes: 78 additions & 10 deletions raptorWeb/gameservers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Optional

from django.db import models
from django.utils.timezone import localtime
from django.utils.timezone import localtime, now
from django.conf import settings

from mcstatus import JavaServer
Expand Down Expand Up @@ -42,6 +42,9 @@ def _update_announcement_count(server: Server) -> None:
server=server
).count()

all_players = Player.objects.all()
online_players = []

if do_query == True:
try:
serverJSON: QueryResponse = JavaServer(
Expand All @@ -53,15 +56,33 @@ def _update_announcement_count(server: Server) -> None:
server.server_state = True
_update_announcement_count(server)
server.save()
[Player.objects.create(
name=player,
server=server).save() for player in serverJSON.players.names]

for player in serverJSON.players.names:
checked_player = all_players.filter(name=player).first()
# If a Player exists, update their information
if checked_player is not None:
online_players.append(checked_player.name)
checked_player.server = server
checked_player.online = True
checked_player.last_online = now()
checked_player.save()
# If not, create a new Player
else:
online_players.append(player)
new_player = Player.objects.create(
name=player,
server=server,
online=True
)
new_player.save()

except TimeoutError:
_set_offline_server(server)

else:
_set_offline_server(server)

return online_players

def poll_servers(self, servers: list['Server'], statistic_model: 'ServerStatistic') -> None:
if statistic_model.time_last_polled == None:
Expand All @@ -72,8 +93,8 @@ def poll_servers(self, servers: list['Server'], statistic_model: 'ServerStatisti

if minutes_since_poll > 1 or self._has_run == False:
statistic_model.total_player_count = 0
Player.objects.all().delete()


all_online_players = []
for server in servers:
if (server.server_address == "Default"
or server.in_maintenance == True
Expand All @@ -83,8 +104,19 @@ def poll_servers(self, servers: list['Server'], statistic_model: 'ServerStatisti
do_query = False)

else:
self._query_and_update_server(server)
online_players = self._query_and_update_server(server)
all_online_players.extend(online_players)
PlayerCountHistoric.objects.create(
server=server,
player_count=server.player_count
)
statistic_model.total_player_count += server.player_count

# Mark players who were not queried as online, but are marked as online in the database, as offline.
newly_offline_players = Player.objects.filter(online=True).exclude(name__in=all_online_players)
for player in newly_offline_players:
player.online = False
player.save()

self._has_run = True
statistic_model.time_last_polled = localtime()
Expand Down Expand Up @@ -338,16 +370,23 @@ class Meta:

class Player(models.Model):
"""
Playesr that are currently logged in to a Server
Players that have joined a server at some point.
"""
server = models.ForeignKey(
Server,
default=0,
on_delete=models.CASCADE)
default=0,
on_delete=models.PROTECT)

name = models.CharField(
max_length=50,
unique=True)

online = models.BooleanField(
default=False)

last_online = models.DateTimeField(
verbose_name="Last Online",
auto_now_add=True)

def __str__(self):
return self.name
Expand All @@ -361,3 +400,32 @@ def get_server(self):
class Meta:
verbose_name = "Player"
verbose_name_plural = "Players"


class PlayerCountHistoric(models.Model):
"""
The total count of players on a server at a
specific point in time. These are created each
time servers are queried, for each server.
"""
server = models.ForeignKey(
Server,
default=0,
on_delete=models.CASCADE)

player_count = models.IntegerField(
verbose_name="Players Online")

checked_time = models.DateTimeField(
verbose_name="Time of Query",
auto_now_add=True)

def get_player_count(self):
return self.player_count

def get_server(self):
return self.server

class Meta:
verbose_name = "Historic Player Count"
verbose_name_plural = "Historic Player Counts"
4 changes: 4 additions & 0 deletions raptorWeb/gameservers/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
path('html/server_banned_items_poll/', views.Server_List_Base.as_view(template_name='gameservers/server_list_banneditems.html'), name="server_banned_items_poll"),
path('html/server_voting_poll/', views.Server_List_Base.as_view(template_name='gameservers/server_list_voting.html'), name="server_voting_poll"),
path('html/server_announcements_poll/', views.Server_List_Base.as_view(template_name='gameservers/server_list_announcements.html'), name="server_announcements_poll"),
# Forms
path('html/forms/statistic_filter', views.Statistic_Filter_Form.as_view(), name="statistic_filter"),
# Statistics
path('html/statistics/player_counts', views.Player_Count_Statistics.as_view(), name="player_statistics_chart"),
# Command API
path('action/import_server_data/', views.Import_Servers.as_view(), name="action_import_server_data"),
path('action/export_server_data/', views.Export_Servers.as_view(), name="action_export_server_data")
Expand Down
Loading

0 comments on commit d1b23bf

Please sign in to comment.