Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Refactor to Modular Program #3

Merged
merged 4 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# Ignore log files
/log/
/test/
/tests/config/config.json
6 changes: 6 additions & 0 deletions .idea/codestream.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

459 changes: 459 additions & 0 deletions .idea/dbnavigator.xml

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion .idea/planetside.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ Get game events and sync to database.
"database": "example",
"user": "user",
"password": "password"
},
"planetside2": {
"api": "wss://push.planetside2.com/streaming?environment=ps2&service-id=s: ...",
"subscription": "{\"service\":\"event\", ... , \"logicalAndCharactersWithWorlds\":true}"
}
}
```

2. You will need following third-party dependencies to run the application.

```requirements.txt
pymysql >= 1.0.2
websockets >= 10.1
setuptools==57.0.0
mysql-connector-python==8.0.28
websockets==10.1
```

3. Use the command below to start.

```shell
python -m update_database.py
python -m ps2cpcdata
```
2 changes: 0 additions & 2 deletions config/.gitignore

This file was deleted.

9 changes: 0 additions & 9 deletions config/config.json

This file was deleted.

32 changes: 0 additions & 32 deletions config/logging.json

This file was deleted.

60 changes: 0 additions & 60 deletions controller/mysql_controller.py

This file was deleted.

8 changes: 8 additions & 0 deletions ps2cpcdata/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""The program initializes the configuration file and executes the main program."""
import json
import logging

logger = logging.getLogger(__name__)

with open("ps2cpcdata/config/config.json", "r") as config:
__config__ = json.load(config)
56 changes: 56 additions & 0 deletions ps2cpcdata/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""This module implements the core developer interface for ps2cpcdata."""
import asyncio
import json
import logging
import sys

import websockets

from ps2cpcdata import __config__
from ps2cpcdata import database

logging.basicConfig(stream=sys.stderr, level="NOTSET")
logger = logging.getLogger(__name__)


class PS2CPCData(object):
def __init__(self):
self.planetside = __config__["planetside2"]
self.api = self.planetside["api"]
self.subscription = self.planetside["subscription"]
self.database = database.Mysql()

async def connect(self):
async with websockets.connect(self.api, ping_timeout=None) as ws:
await ws.send(self.subscription)
while True:
message = await ws.recv()
data = json.loads(message)
await self.update_database(data)

async def update_database(self, data):
try:
payload = data["payload"]
except KeyError:
return

if "Death" in payload.values():
await self.database.death(payload)

elif "MetagameEvent" in payload.values():
await self.database.alert(payload)


ps2cpc_data = PS2CPCData()

while True:
try:
asyncio.run(ps2cpc_data.connect())

except KeyboardInterrupt:
logger.info("The program was closed by the user.")
break

except websockets.WebSocketException:
logger.warning("Connection failed, try to reconnect.")
continue
13 changes: 13 additions & 0 deletions ps2cpcdata/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"database": {
"host": "localhost",
"port": 3306,
"database": "example",
"user": "user",
"password": "password"
},
"planetside2": {
"api": "wss://push.planetside2.com/streaming?environment=ps2&service-id=s:yinxue",
"subscription": "{\"service\":\"event\",\"action\":\"subscribe\",\"characters\":[\"all\"],\"eventNames\":[\"Death\", \"MetagameEvent\"],\"worlds\":[\"1\", \"10\", \"13\", \"17\", \"40\"], \"logicalAndCharactersWithWorlds\":true}"
}
}
64 changes: 64 additions & 0 deletions ps2cpcdata/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""This module contains all database operation."""
import logging

import mysql.connector

from ps2cpcdata import __config__

logger = logging.getLogger(__name__)


class Mysql(object):
"""Mysql database operation."""

def __init__(self):
self.database = __config__["database"]
self.conn = mysql.connector.connect(**self.database)

async def _update(self, insert_event, event):
cursor = None
try:
cursor = self.conn.cursor()
cursor.execute(insert_event, event)
except mysql.connector.errors.Error as err:
logger.exception(err)
else:
self.conn.commit()
finally:
cursor.close()

async def death(self, payload):
"""Update death event.

Args:
payload: Death event.

Returns: None.

"""
insert_death = "INSERT INTO ps2_death (attacker_character_id, attacker_fire_mode_id, attacker_loadout_id, " \
"attacker_vehicle_id, attacker_weapon_id, character_id, character_loadout_id, is_headshot, " \
"world_id, zone_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
death = (payload["attacker_character_id"], payload["attacker_fire_mode_id"], payload["attacker_loadout_id"],
payload["attacker_vehicle_id"], payload["attacker_weapon_id"], payload["character_id"],
payload["character_loadout_id"], payload["is_headshot"], payload["world_id"], payload["zone_id"])
await self._update(insert_death, death)

async def alert(self, payload):
"""Update death event.

Args:
payload: Alert event.

Returns: None.

"""
insert_alert = "INSERT INTO ps2_jingbao (faction_vs, faction_tr, faction_nc, world_id, zone_id, " \
"metagame_event_id, metagame_event_state) VALUES (%s, %s, %s, %s, %s, %s, %s)"
alert = ((payload["faction_vs"], payload["faction_tr"], payload["faction_nc"], payload["world_id"],
payload["zone_id"], payload["metagame_event_id"], payload["metagame_event_state"]))
await self._update(insert_alert, alert)


if __name__ == '__main__':
pass
5 changes: 5 additions & 0 deletions ps2cpcdata/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Just a version information."""
__version__ = "2.0.0"

if __name__ == '__main__':
print(__version__)
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
setuptools==57.0.0
mysql-connector-python==8.0.28
websockets==10.1
17 changes: 17 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from setuptools import setup

from ps2cpcdata.version import __version__

setup(
name="ps2cpcdata",
version=__version__,
description="A tool for updating data in real time for the database.",
author="ecss11",
maintainer="ecss11",
url="https://github.com/PlanetSide2-CPC/PS2-DatabaseSync",
packages=["ps2cpcdata"],
install_requires=[
"mysql-connector-python==8.0.28",
"websockets==10.1"
]
)
Empty file removed test/.gitkeeper
Empty file.
Empty file removed test/config/.gitkeeper
Empty file.
File renamed without changes.
Loading