2021-09-04 17:17:22 +02:00
|
|
|
# vim: set fileencoding=utf-8
|
|
|
|
import base64
|
2021-09-06 02:16:06 +02:00
|
|
|
from typing import List
|
2021-09-04 17:17:22 +02:00
|
|
|
|
|
|
|
from bemani.backend.mga.base import MetalGearArcadeBase
|
|
|
|
from bemani.backend.ess import EventLogHandler
|
|
|
|
from bemani.common import Profile, VersionConstants, Time
|
|
|
|
from bemani.data import UserID
|
|
|
|
from bemani.protocol import Node
|
|
|
|
|
|
|
|
|
|
|
|
class MetalGearArcade(
|
|
|
|
EventLogHandler,
|
|
|
|
MetalGearArcadeBase,
|
|
|
|
):
|
|
|
|
|
2021-09-07 19:56:15 +02:00
|
|
|
name: str = "Metal Gear Arcade"
|
|
|
|
version: int = VersionConstants.MGA
|
2021-09-04 17:17:22 +02:00
|
|
|
|
|
|
|
def __update_shop_name(self, profiledata: bytes) -> None:
|
|
|
|
# Figure out the profile type
|
2022-10-15 20:56:30 +02:00
|
|
|
csvs = profiledata.split(b",")
|
2021-09-04 17:17:22 +02:00
|
|
|
if len(csvs) < 2:
|
|
|
|
# Not long enough to care about
|
|
|
|
return
|
2022-10-15 20:56:30 +02:00
|
|
|
datatype = csvs[1].decode("ascii")
|
|
|
|
if datatype != "PLAYDATA":
|
2021-09-04 17:17:22 +02:00
|
|
|
# Not the right profile type requested
|
|
|
|
return
|
|
|
|
|
|
|
|
# Grab the shop name
|
|
|
|
try:
|
2022-10-15 20:56:30 +02:00
|
|
|
shopname = csvs[30].decode("shift-jis")
|
2021-09-04 17:17:22 +02:00
|
|
|
except Exception:
|
|
|
|
return
|
|
|
|
self.update_machine_name(shopname)
|
|
|
|
|
|
|
|
def handle_system_getmaster_request(self, request: Node) -> Node:
|
|
|
|
# See if we can grab the request
|
2022-10-15 20:56:30 +02:00
|
|
|
data = request.child("data")
|
2021-09-04 17:17:22 +02:00
|
|
|
if not data:
|
2022-10-15 20:56:30 +02:00
|
|
|
root = Node.void("system")
|
|
|
|
root.add_child(Node.s32("result", 0))
|
2021-09-04 17:17:22 +02:00
|
|
|
return root
|
|
|
|
|
|
|
|
# Figure out what type of messsage this is
|
2022-10-15 20:56:30 +02:00
|
|
|
reqtype = data.child_value("datatype")
|
|
|
|
reqkey = data.child_value("datakey")
|
2021-09-04 17:17:22 +02:00
|
|
|
|
|
|
|
# System message
|
2022-10-15 20:56:30 +02:00
|
|
|
root = Node.void("system")
|
2021-09-04 17:17:22 +02:00
|
|
|
|
|
|
|
if reqtype == "S_SRVMSG" and reqkey == "INFO":
|
|
|
|
# Generate system message
|
|
|
|
settings1_str = "2011081000:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1"
|
|
|
|
settings2_str = "1,1,1,1,1,1,1,1,1,1,1,1,1,1"
|
|
|
|
|
|
|
|
# Send it to the client, making sure to inform the client that it was valid.
|
2022-10-15 20:56:30 +02:00
|
|
|
root.add_child(
|
|
|
|
Node.string(
|
|
|
|
"strdata1",
|
|
|
|
base64.b64encode(settings1_str.encode("ascii")).decode("ascii"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
root.add_child(
|
|
|
|
Node.string(
|
|
|
|
"strdata2",
|
|
|
|
base64.b64encode(settings2_str.encode("ascii")).decode("ascii"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
root.add_child(Node.u64("updatedate", Time.now() * 1000))
|
|
|
|
root.add_child(Node.s32("result", 1))
|
2021-09-04 17:17:22 +02:00
|
|
|
else:
|
|
|
|
# Unknown message.
|
2022-10-15 20:56:30 +02:00
|
|
|
root.add_child(Node.s32("result", 0))
|
2021-09-04 17:17:22 +02:00
|
|
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
def handle_playerdata_usergamedata_send_request(self, request: Node) -> Node:
|
|
|
|
# Look up user by refid
|
2022-10-15 20:56:30 +02:00
|
|
|
refid = request.child_value("data/eaid")
|
2021-09-04 17:17:22 +02:00
|
|
|
userid = self.data.remote.user.from_refid(self.game, self.version, refid)
|
|
|
|
if userid is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
root = Node.void("playerdata")
|
|
|
|
root.add_child(
|
|
|
|
Node.s32("result", 1)
|
|
|
|
) # Unclear if this is the right thing to do here.
|
2021-09-04 17:17:22 +02:00
|
|
|
return root
|
|
|
|
|
|
|
|
# Extract new profile info from old profile
|
|
|
|
oldprofile = self.get_profile(userid)
|
|
|
|
is_new = False
|
|
|
|
if oldprofile is None:
|
|
|
|
oldprofile = Profile(self.game, self.version, refid, 0)
|
|
|
|
is_new = True
|
|
|
|
newprofile = self.unformat_profile(userid, request, oldprofile, is_new)
|
|
|
|
|
|
|
|
# Write new profile
|
|
|
|
self.put_profile(userid, newprofile)
|
|
|
|
|
|
|
|
# Return success!
|
2022-10-15 20:56:30 +02:00
|
|
|
root = Node.void("playerdata")
|
|
|
|
root.add_child(Node.s32("result", 0))
|
2021-09-04 17:17:22 +02:00
|
|
|
return root
|
|
|
|
|
|
|
|
def handle_playerdata_usergamedata_recv_request(self, request: Node) -> Node:
|
|
|
|
# Look up user by refid
|
2022-10-15 20:56:30 +02:00
|
|
|
refid = request.child_value("data/eaid")
|
|
|
|
profiletypes = request.child_value("data/recv_csv").split(",")
|
2021-09-04 17:17:22 +02:00
|
|
|
profile = None
|
|
|
|
userid = None
|
|
|
|
if refid is not None:
|
|
|
|
userid = self.data.remote.user.from_refid(self.game, self.version, refid)
|
|
|
|
if userid is not None:
|
|
|
|
profile = self.get_profile(userid)
|
|
|
|
if profile is not None:
|
|
|
|
return self.format_profile(userid, profiletypes, profile)
|
|
|
|
else:
|
2022-10-15 20:56:30 +02:00
|
|
|
root = Node.void("playerdata")
|
|
|
|
root.add_child(
|
|
|
|
Node.s32("result", 1)
|
|
|
|
) # Unclear if this is the right thing to do here.
|
2021-09-04 17:17:22 +02:00
|
|
|
return root
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
def format_profile(
|
|
|
|
self, userid: UserID, profiletypes: List[str], profile: Profile
|
|
|
|
) -> Node:
|
|
|
|
root = Node.void("playerdata")
|
|
|
|
root.add_child(Node.s32("result", 0))
|
|
|
|
player = Node.void("player")
|
2021-09-04 17:17:22 +02:00
|
|
|
root.add_child(player)
|
|
|
|
records = 0
|
2022-10-15 20:56:30 +02:00
|
|
|
record = Node.void("record")
|
2021-09-04 17:17:22 +02:00
|
|
|
player.add_child(record)
|
|
|
|
|
|
|
|
for profiletype in profiletypes:
|
|
|
|
if profiletype == "3fffffffff":
|
|
|
|
continue
|
2022-10-15 20:56:30 +02:00
|
|
|
for j in range(len(profile["strdatas"])):
|
|
|
|
strdata = profile["strdatas"][j]
|
|
|
|
bindata = profile["bindatas"][j]
|
2021-09-04 17:17:22 +02:00
|
|
|
|
|
|
|
# Figure out the profile type
|
2022-10-15 20:56:30 +02:00
|
|
|
csvs = strdata.split(b",")
|
2021-09-04 17:17:22 +02:00
|
|
|
if len(csvs) < 2:
|
|
|
|
# Not long enough to care about
|
|
|
|
continue
|
2022-10-15 20:56:30 +02:00
|
|
|
datatype = csvs[1].decode("ascii")
|
2021-09-04 17:17:22 +02:00
|
|
|
if datatype != profiletype:
|
|
|
|
# Not the right profile type requested
|
|
|
|
continue
|
|
|
|
|
|
|
|
# This is a valid profile node for this type, lets return only the profile values
|
2022-10-15 20:56:30 +02:00
|
|
|
strdata = b",".join(csvs[2:])
|
|
|
|
d = Node.string("d", base64.b64encode(strdata).decode("ascii"))
|
2021-09-04 17:17:22 +02:00
|
|
|
record.add_child(d)
|
2022-10-15 20:56:30 +02:00
|
|
|
d.add_child(
|
|
|
|
Node.string("bin1", base64.b64encode(bindata).decode("ascii"))
|
|
|
|
)
|
2021-09-04 17:17:22 +02:00
|
|
|
|
|
|
|
# Remember that we had this record
|
|
|
|
records = records + 1
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
player.add_child(Node.u32("record_num", records))
|
2021-09-04 17:17:22 +02:00
|
|
|
return root
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
def unformat_profile(
|
|
|
|
self, userid: UserID, request: Node, oldprofile: Profile, is_new: bool
|
|
|
|
) -> Profile:
|
2021-09-04 17:17:22 +02:00
|
|
|
# Profile save request, data values are base64 encoded.
|
|
|
|
# d is a CSV, and bin1 is binary data.
|
2021-09-07 19:56:15 +02:00
|
|
|
newprofile = oldprofile.clone()
|
2021-09-04 17:17:22 +02:00
|
|
|
strdatas: List[bytes] = []
|
|
|
|
bindatas: List[bytes] = []
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
record = request.child("data/record")
|
2021-09-04 17:17:22 +02:00
|
|
|
for node in record.children:
|
2022-10-15 20:56:30 +02:00
|
|
|
if node.name != "d":
|
2021-09-04 17:17:22 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
profile = base64.b64decode(node.value)
|
|
|
|
# Update the shop name if this is a new profile, since we know it came
|
|
|
|
# from this cabinet. This is the only source of truth for what the
|
|
|
|
# cabinet shop name is set to.
|
|
|
|
if is_new:
|
|
|
|
self.__update_shop_name(profile)
|
|
|
|
strdatas.append(profile)
|
2022-10-15 20:56:30 +02:00
|
|
|
bindatas.append(base64.b64decode(node.child_value("bin1")))
|
2021-09-04 17:17:22 +02:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
newprofile["strdatas"] = strdatas
|
|
|
|
newprofile["bindatas"] = bindatas
|
2021-09-04 17:17:22 +02:00
|
|
|
|
|
|
|
# Keep track of play statistics across all versions
|
|
|
|
self.update_play_statistics(userid)
|
|
|
|
|
|
|
|
return newprofile
|