Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

Added support for storing character and account login date #186

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions alembic/versions/489dbe73a913_added_login_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Added Login Table

Revision ID: 489dbe73a913
Revises: b53c793562c7
Create Date: 2023-05-16 19:03:34.855509

"""
from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils


# revision identifiers, used by Alembic.
revision = "489dbe73a913"
down_revision = "b53c793562c7"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"logins",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column("character_id", sa.Integer(), nullable=True),
sa.Column("login_time", sqlalchemy_utils.types.arrow.ArrowType(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
with op.batch_alter_table("characters", schema=None) as batch_op:
batch_op.add_column(sa.Column("last_login", sqlalchemy_utils.types.arrow.ArrowType(), nullable=True))

# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("characters", schema=None) as batch_op:
batch_op.drop_column("last_login")

op.drop_table("logins")
# ### end Alembic commands ###
11 changes: 9 additions & 2 deletions dndserver/handlers/character.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import arrow

from dndserver.data import perks as pk
from dndserver.data import skills as sk
Expand Down Expand Up @@ -84,7 +85,12 @@ def list_characters(ctx, msg):
req = SC2S_ACCOUNT_CHARACTER_LIST_REQ()
req.ParseFromString(msg)

query = db.query(Character).filter_by(account_id=sessions[ctx.transport].account.id).all()
query = (
db.query(Character)
.filter_by(account_id=sessions[ctx.transport].account.id)
.order_by(Character.last_login)
.all()
)
res = SS2C_ACCOUNT_CHARACTER_LIST_RES(totalCharacterCount=len(query), pageIndex=req.pageIndex)

start = (res.pageIndex - 1) * 7
Expand All @@ -100,7 +106,7 @@ def list_characters(ctx, msg):
characterClass=CharacterClass(result.character_class).value,
gender=Gender(result.gender).value,
createAt=result.created_at.int_timestamp,
# lastloginDate=result.last_logged_at # TODO: Need to implement access logs.
lastloginDate=result.last_login.int_timestamp,
)

for item, attributes in inventory.get_all_items(result.id, Define_Item.InventoryId.EQUIPMENT):
Expand Down Expand Up @@ -137,6 +143,7 @@ def create_character(ctx, msg):
streaming_nickname=f"Fighter#{random.randrange(1000000, 1700000)}",
gender=Gender(req.gender),
character_class=char_class,
last_login=arrow.utcnow(),
)

# select the default perks and skills
Expand Down
8 changes: 7 additions & 1 deletion dndserver/handlers/lobby.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import arrow
from dndserver.database import db
from dndserver.handlers import character
from dndserver.models import Character
from dndserver.models import Character, Login
from dndserver.objects.party import Party
from dndserver.objects.state import State
from dndserver.persistent import parties, sessions
Expand Down Expand Up @@ -29,6 +30,11 @@ def enter_lobby(ctx, msg):
sessions[ctx.transport].character = query
sessions[ctx.transport].state = State()

# update the last login time of the character and Login table
query.last_login = arrow.utcnow()
q_login = Login(account_id=query.account_id, login_time=arrow.utcnow(), character_id=query.id)
q_login.save()

party = Party(player_1=sessions[ctx.transport])
sessions[ctx.transport].party = party
parties.append(party)
Expand Down
6 changes: 5 additions & 1 deletion dndserver/handlers/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import arrow

from dndserver.database import db
from dndserver.models import Hwid, Account
from dndserver.models import Hwid, Account, Login
from dndserver.persistent import sessions
from dndserver.protos.Account import SC2S_ACCOUNT_LOGIN_REQ, SLOGIN_ACCOUNT_INFO, SS2C_ACCOUNT_LOGIN_RES
from dndserver.protos.Common import SS2C_SERVICE_POLICY_NOT, FSERVICE_POLICY
Expand Down Expand Up @@ -40,6 +40,10 @@ def process_login(ctx, msg):

res.secretToken = account.secret_token

# # add a new login
login = Login(account_id=account.id, login_time=arrow.utcnow())
login.save()

# Check if an hwId is associated to an account_id, if not add to db
for hwid in req.hwIds:
if not db.query(Hwid).filter_by(hwid=hwid).filter_by(account_id=account.id).first():
Expand Down
21 changes: 15 additions & 6 deletions dndserver/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import arrow

from sqlalchemy import Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import Boolean, Enum, Integer, String, Text
Expand All @@ -12,6 +13,19 @@
base = declarative_base()


class Login(base):
__tablename__ = "logins"

id = Column(Integer, primary_key=True, autoincrement="auto")
account_id = Column(Integer, nullable=False)
character_id = Column(Integer, nullable=True)
login_time = Column(ArrowType, default=arrow.utcnow())

def save(self):
db.add(self)
db.commit()


class Account(base):
__tablename__ = "accounts"

Expand Down Expand Up @@ -40,6 +54,7 @@ class Character(base):
experience = Column(Integer, default=0)
karma_rating = Column(Integer, default=0)
streaming_nickname = Column(String(15))
last_login = Column(ArrowType, default=arrow.utcnow())

perk0 = Column(String, default="")
perk1 = Column(String, default="")
Expand All @@ -55,7 +70,6 @@ class Character(base):
ranking_adventure = Column(Integer, default=0)
ranking_lich = Column(Integer, default=0)
ranking_ghostking = Column(Integer, default=0)
# TODO: store all logins in a database and grab the latest from that

def save(self):
db.add(self)
Expand Down Expand Up @@ -202,8 +216,3 @@ def save(self):
def delete(self):
db.delete(self)
db.commit()


# class Login(base):
# __tablename__ = "logins"
# id = Column(Integer, primary_key=True, autoincrement="auto")