View/Services refactor

This commit is contained in:
573dev 2020-11-06 22:21:47 -06:00
parent 0ab43e800e
commit 1d009a8db1
6 changed files with 100 additions and 158 deletions

View File

@ -1,5 +1,3 @@
from lxml.builder import E
from v8_server import db
from v8_server.eamuse.services.services import ServiceRequest
from v8_server.eamuse.xml.utils import (
@ -8,6 +6,7 @@ from v8_server.eamuse.xml.utils import (
load_xml_template,
)
from v8_server.model.user import Card, Profile, RefID, User, UserAccount
from v8_server.utils.convert import bool_to_int as btoi
class CardStatus(object):
@ -76,7 +75,7 @@ class CardMng(object):
args["refid"] = refid.refid
args["newflag"] = 0
args["binded"] = bound
args["binded"] = btoi(bound)
args["status"] = CardStatus.SUCCESS
result = load_xml_template("cardmng", "inquire", args)

View File

@ -244,17 +244,16 @@ class Local(object):
refid = req.xml[0].find("card/refid").text
user = User.from_refid(refid)
if user is None:
if (
user is None
or (account := UserAccount.from_userid(user.userid)) is None
):
response = E.response(
E.cardutil(
E.card(E.kind("0", e_type(T.s8)), {"no": "1", "state": "0"})
)
)
else:
account = UserAccount.from_userid(user.userid)
if account is None:
raise Exception("Account is none, wut?")
response = E.response(
E.cardutil(
E.card(

View File

@ -1,3 +1,4 @@
import logging
from datetime import datetime
from lxml import etree
@ -6,6 +7,9 @@ from lxml.builder import E
from v8_server.eamuse.services.services import ServiceRequest
logger = logging.getLogger(__name__)
class PCBEventItem(object):
"""
Contains the data for a PCBEvent Item
@ -80,13 +84,17 @@ class PCBEvent(object):
PUT = "put"
@classmethod
def put(cls):
def put(cls, req: ServiceRequest):
"""
Example:
<response>
<pcbevent expire="600"/>
</response>
"""
# Log the event
event = PCBEvent(req)
logger.info(event)
return E.response(E.pcbevent({"expire": "600"}))
def __repr__(self) -> str:

View File

@ -6,7 +6,7 @@ from datetime import datetime
from enum import IntEnum
from random import randint
from time import time
from typing import Any, Callable, Dict, Optional, Tuple, Union
from typing import Dict, Optional, Tuple, Union
from flask import Request
from kbinxml import KBinXML
@ -14,7 +14,7 @@ from lxml import etree
from lxml.builder import E
from lxml.etree import _Element as eElement
from v8_server import LOG_PATH, app
from v8_server import LOG_PATH
from v8_server.eamuse.utils.arc4 import EAmuseARC4
from v8_server.eamuse.utils.eamuse import Model
from v8_server.eamuse.utils.lz77 import Lz77
@ -35,34 +35,12 @@ class ServiceType(IntEnum):
PACKAGE = 5
CARDMNG = 6
LOCAL = 7
# Extra for testing
# BINARY = 8
DLSTATUS = 9
EACOIN = 10
# EEMALL = 11
# INFO = 12
LOBBY = 13
NETLOG = 14
# NUMBERING = 15
# PKGLIST = 16
# POSEVENT = 17
# REFERENCE = 18
# SHOPINF = 19
SIDMGR = 20
USERDATA = 21
USERID = 22
TRACEROUTE = 23
DLSTATUS = 8
class Services(object):
"""
Handles a service request from the eAmuse Server
Example:
<call model="K32:J:B:A:2011033000" srcid="00010203040506070809">
<services method="get"/>
</call>
"""
# Default service url that GFDM uses. You will need to set up your network so that
@ -72,12 +50,15 @@ class Services(object):
# The base route that GFDM uses to query the eAmuse server to get the list of
# offered services
SERVICES_ROUTE = "/service/services/services/"
SERVICES_ROUTE = "/services/"
# The custom route that we tell GFDM to use to query the eAmuse server
SERVICE_ROUTE = "/service"
# Default NTP url
# XXX: Maybe needs to be configurable? What if the machine this is running on
# doesn't have internet access?
NTP_URL = "ntp//pool.ntp.org"
NTP_URL = "ntp://pool.ntp.org"
def __init__(
self,
@ -113,20 +94,12 @@ class Services(object):
f"pa={ip}&ia={ip}&ga={ip}&ma={ip}&t1=2&t2=10"
),
**{
n.lower(): f"{self.SERVICE_URL}/{m.value}"
n.lower(): f"{self.SERVICE_URL}/{self.SERVICE_ROUTE}/{m}/"
for n, m in ServiceType.__members__.items()
},
}
return services
@classmethod
def route(cls, _type: ServiceType) -> Callable[[Any], Any]:
def decorator(f):
func = app.route(f"/{_type.value}", methods=["POST"])(f)
return func
return decorator
def __repr__(self) -> str:
return (
f"Services<expire: {self.expire}, "

View File

@ -0,0 +1,7 @@
def bool_to_int(value: bool) -> int:
"""
Convert a boolean to an integer.
True = 1, False = 0
"""
return 1 if value else 0

View File

@ -46,122 +46,78 @@ def catch_all(u_path: str) -> str:
return "You want path: %s" % u_path
@Services.route(ServiceType.PCBTRACKER)
def pcbtracker_service() -> FlaskResponse:
@app.route(f"{Services.SERVICE_ROUTE}/<int:route>/", methods=["POST"])
def service_service(route: int) -> FlaskResponse:
# TODO: Ideally we want to simplify this where we send the request to some method
# that handles checking the module/method/etc and dispaches it to the correct
# function rather than having this if chain in here. I'd like to keep the view as
# simple as possible
req = ServiceRequest(request)
if req.method == PCBTracker.ALIVE:
response = PCBTracker.alive()
if route == ServiceType.PCBTRACKER.value:
if req.method == PCBTracker.ALIVE:
response = PCBTracker.alive()
else:
raise Exception(f"Not sure how to handle this PCBTracker Request: {req}")
elif route == ServiceType.MESSAGE.value:
if req.method == Message.GET:
response = Message.get()
else:
raise Exception(f"Not sure how to handle this Message Request: {req}")
elif route == ServiceType.PCBEVENT.value:
if req.method == PCBEvent.PUT:
response = PCBEvent.put(req)
else:
raise Exception(f"Not sure how to handle this PCBEvent Request: {req}")
elif route == ServiceType.PACKAGE.value:
if req.method == Package.LIST:
response = Package.list(req)
else:
raise Exception(f"Not sure how to handle this Package Request: {req}")
elif route == ServiceType.FACILITY.value:
if req.method == Facility.GET:
response = Facility.get()
else:
raise Exception(f"Not sure how to handle this Facility Request: {req}")
elif route == ServiceType.DLSTATUS.value:
if req.method == DLStatus.PROGRESS:
response = DLStatus.progress()
else:
raise Exception(f"Not sure how to handle this DLStatus Request: {req}")
elif route == ServiceType.CARDMNG.value:
if req.method == CardMng.INQUIRE:
response = CardMng.inquire(req)
elif req.method == CardMng.GETREFID:
response = CardMng.getrefid(req)
elif req.method == CardMng.AUTHPASS:
response = CardMng.authpass(req)
elif req.method == CardMng.BINDMODEL:
response = CardMng.bindmodel(req)
elif req.method == CardMng.GETKEEPSPAN:
response = CardMng.getkeepspan()
elif req.method == CardMng.GETDATALIST:
response = CardMng.getdatalist()
else:
raise Exception(f"Not sure how to handle this Cardmng Request: {req}")
elif route == ServiceType.LOCAL.value:
if req.module == Local.SHOPINFO:
response = Local.shopinfo(req)
elif req.module == Local.DEMODATA:
response = Local.demodata(req)
elif req.module == Local.CARDUTIL:
response = Local.cardutil(req)
elif req.module == Local.GAMEINFO:
response = Local.gameinfo(req)
elif req.module == Local.GAMEEND:
response = Local.gameend(req)
elif req.module == Local.GAMETOP:
response = Local.gametop(req)
elif req.module == Local.CUSTOMIZE:
response = Local.customize(req)
else:
raise Exception(f"Not sure how to handle this Local Request: {req}")
else:
raise Exception(f"Not sure how to handle this PCBTracker Request: {req}")
return req.response(response)
@Services.route(ServiceType.MESSAGE)
def message_service() -> FlaskResponse:
req = ServiceRequest(request)
if req.method == Message.GET:
response = Message.get()
else:
raise Exception(f"Not sure how to handle this Message Request: {req}")
return req.response(response)
@Services.route(ServiceType.PCBEVENT)
def pcbevent_service() -> FlaskResponse:
req = ServiceRequest(request)
event = PCBEvent(req)
app.logger.info(event)
if req.method == PCBEvent.PUT:
response = PCBEvent.put()
else:
raise Exception(f"Not sure how to handle this PCBEvent Request: {req}")
return req.response(response)
@Services.route(ServiceType.PACKAGE)
def package_service() -> FlaskResponse:
req = ServiceRequest(request)
if req.method == Package.LIST:
response = Package.list(req)
else:
raise Exception(f"Not sure how to handle this Package Request: {req}")
return req.response(response)
@Services.route(ServiceType.FACILITY)
def facility_service() -> FlaskResponse:
req = ServiceRequest(request)
if req.method == Facility.GET:
response = Facility.get()
else:
raise Exception(f"Not sure how to handle this Facility Request: {req}")
return req.response(response)
@Services.route(ServiceType.DLSTATUS)
def dlstatus_service() -> FlaskResponse:
req = ServiceRequest(request)
if req.method == DLStatus.PROGRESS:
response = DLStatus.progress()
else:
raise Exception(f"Not sure how to handle this DLStatus Request: {req}")
return req.response(response)
@Services.route(ServiceType.CARDMNG)
def cardmng_service() -> FlaskResponse:
req = ServiceRequest(request)
if req.method == CardMng.INQUIRE:
response = CardMng.inquire(req)
elif req.method == CardMng.GETREFID:
response = CardMng.getrefid(req)
elif req.method == CardMng.AUTHPASS:
response = CardMng.authpass(req)
elif req.method == CardMng.BINDMODEL:
response = CardMng.bindmodel(req)
elif req.method == CardMng.GETKEEPSPAN:
response = CardMng.getkeepspan()
elif req.method == CardMng.GETDATALIST:
response = CardMng.getdatalist()
else:
raise Exception(f"Not sure how to handle this Cardmng Request: {req}")
return req.response(response)
@Services.route(ServiceType.LOCAL)
def local_service() -> FlaskResponse:
req = ServiceRequest(request)
if req.module == Local.SHOPINFO:
response = Local.shopinfo(req)
elif req.module == Local.DEMODATA:
response = Local.demodata(req)
elif req.module == Local.CARDUTIL:
response = Local.cardutil(req)
elif req.module == Local.GAMEINFO:
response = Local.gameinfo(req)
elif req.module == Local.GAMEEND:
response = Local.gameend(req)
elif req.module == Local.GAMETOP:
response = Local.gametop(req)
elif req.module == Local.CUSTOMIZE:
response = Local.customize(req)
else:
raise Exception(f"Not sure how to handle this Local Request: {req}")
raise Exception(f"Not sure how to handle this Request: {req}")
return req.response(response)