ToolController: impl signup

This commit is contained in:
Andrew Glaze
2025-05-23 13:26:13 -04:00
parent 49cd62da1d
commit c29f454881
7 changed files with 56 additions and 20 deletions

View File

@@ -14,6 +14,10 @@ struct ToolController: RouteCollection {
func signup(req: Request) async throws -> Response {
let body = try req.content.decode(SignupReq.self, using: MsgPackDecoder())
guard let udid = req.headers["udid"].first else {
throw Abort(.badRequest)
}
guard
let session = try await Session.query(on: req.db).filter(\.$id == body.access_token).first(),
session.type == SessionType.ZAT
@@ -22,9 +26,7 @@ struct ToolController: RouteCollection {
}
let accountId = session.$account.id
if let player = try await Player.query(on: req.db).filter(\.$account.$id == accountId).first() {
} else {
if try await Player.query(on: req.db).filter(\.$account.$id == accountId).first() == nil {
guard let account = try await Account.query(on: req.db).filter(\.$id == accountId).first() else {
throw Abort(.forbidden, reason: "Account ID does not exist")
}
@@ -33,7 +35,28 @@ struct ToolController: RouteCollection {
try await player.save(on: req.db)
}
throw Abort(.notImplemented)
let viewerSession = try await Session.query(on: req.db)
.filter(\.$account.$id == accountId)
.filter(\.$type == .VIEWER)
.first() ?? Session(accountId: accountId, expires: Date.now, type: .VIEWER)
if (viewerSession.hasChanges) {
try await viewerSession.save(on: req.db)
}
let response = Response(status:.ok)
try response.content.encode(SignupRes(
data_headers: DataHeadersRes(
short_udid: 0,
viewer_id: Int(try viewerSession.requireID())!,
udid: udid,
servertime: Int(Date.now.timeIntervalSince1970 * 100),
result_code: 1
),
data: []
), using: MsgPackEncoder())
return response
}
}
@@ -46,3 +69,16 @@ struct SignupReq: Content {
let device_id: Double
let idp_code: String
}
struct DataHeadersRes: Content {
let short_udid: Int
let viewer_id: Int
let udid: String
let servertime: Int
let result_code: Int
}
struct SignupRes: Content {
let data_headers: DataHeadersRes
let data: [String]
}

View File

@@ -87,7 +87,6 @@ struct AuthController: RouteCollection {
guard let account = account else {
throw Abort(.badRequest)
}
print("got here")
if account.idpAlias != idpAlias {
account.idpAlias = idpAlias

View File

@@ -5,6 +5,7 @@ extension MsgPackEncoder: @retroactive ContentEncoder, @retroactive @unchecked S
public func encode<E>(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders) throws where E : Encodable {
let data = try self.encode(encodable)
body.writeString(data.base64EncodedString())
headers.contentType = .init(type: "application", subType: "x-msgpack")
}
}

View File

@@ -28,8 +28,8 @@ struct CreatePlayers: AsyncMigration {
.field("paid_mana", .int, .required)
.field("enable_auto_3x", .bool, .required)
.field("account_id", .int, .required, .references("accounts", "id"))
.field("tutorial_step", .int, .required)
.field("tutorial_skip_flag", .int, .required)
.field("tutorial_step", .int)
.field("tutorial_skip_flag", .int)
.create()
}

View File

@@ -17,11 +17,19 @@ final class Session: Model, @unchecked Sendable {
init() { }
init(account: Account, expires: Date, type: SessionType) throws {
self.id = UUID().uuidString
self.$account.id = try account.requireID()
convenience init(account: Account, expires: Date, type: SessionType) throws {
self.init(accountId: try account.requireID(), expires: expires, type: type)
}
init(accountId: Int, expires: Date, type: SessionType) {
self.$account.id = accountId
self.expires = expires
self.type = type
if (type != .VIEWER) {
self.id = UUID().uuidString
} else {
self.id = String(Int.random())
}
}
}

View File

@@ -15,8 +15,8 @@ enum Entrypoint {
// You can enable it if you'd like to reduce the amount of context switching between NIO and Swift Concurrency.
// Note: this has caused issues with some libraries that use `.wait()` and cleanly shutting down.
// If enabled, you should be careful about calling async functions before this point as it can cause assertion failures.
let executorTakeoverSuccess = NIOSingletons.unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor()
app.logger.debug("Tried to install SwiftNIO's EventLoopGroup as Swift's global concurrency executor", metadata: ["success": .stringConvertible(executorTakeoverSuccess)])
//let executorTakeoverSuccess = NIOSingletons.unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor()
//app.logger.debug("Tried to install SwiftNIO's EventLoopGroup as Swift's global concurrency executor", metadata: ["success": .stringConvertible(executorTakeoverSuccess)])
do {
try await configure(app)

View File

@@ -2,14 +2,6 @@ import Fluent
import Vapor
func routes(_ app: Application) throws {
app.get { req async in
"It works!"
}
app.get("hello") { req async -> String in
"Hello, world!"
}
try app.register(collection: InfodeskController())
try app.register(collection: OpenApiController())
try app.register(collection: ApiController())