From c29f454881564db83007f6021354ed6ef5cad5c9 Mon Sep 17 00:00:00 2001 From: Andrew Glaze Date: Fri, 23 May 2025 13:26:13 -0400 Subject: [PATCH] ToolController: impl signup --- .../Controllers/Api/ToolController.swift | 44 +++++++++++++++++-- .../Controllers/OpenApi/AuthController.swift | 1 - Sources/stella/Decoders/MsgPack.swift | 1 + Sources/stella/Migrations/CreatePlayers.swift | 4 +- Sources/stella/Models/Session.swift | 14 ++++-- Sources/stella/entrypoint.swift | 4 +- Sources/stella/routes.swift | 8 ---- 7 files changed, 56 insertions(+), 20 deletions(-) diff --git a/Sources/stella/Controllers/Api/ToolController.swift b/Sources/stella/Controllers/Api/ToolController.swift index 37c7368..156e47c 100644 --- a/Sources/stella/Controllers/Api/ToolController.swift +++ b/Sources/stella/Controllers/Api/ToolController.swift @@ -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] +} diff --git a/Sources/stella/Controllers/OpenApi/AuthController.swift b/Sources/stella/Controllers/OpenApi/AuthController.swift index 80b105f..17cd5d6 100644 --- a/Sources/stella/Controllers/OpenApi/AuthController.swift +++ b/Sources/stella/Controllers/OpenApi/AuthController.swift @@ -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 diff --git a/Sources/stella/Decoders/MsgPack.swift b/Sources/stella/Decoders/MsgPack.swift index 3ad01bc..207ef7e 100644 --- a/Sources/stella/Decoders/MsgPack.swift +++ b/Sources/stella/Decoders/MsgPack.swift @@ -5,6 +5,7 @@ extension MsgPackEncoder: @retroactive ContentEncoder, @retroactive @unchecked S public func encode(_ 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") } } diff --git a/Sources/stella/Migrations/CreatePlayers.swift b/Sources/stella/Migrations/CreatePlayers.swift index 02fd970..0816d12 100644 --- a/Sources/stella/Migrations/CreatePlayers.swift +++ b/Sources/stella/Migrations/CreatePlayers.swift @@ -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() } diff --git a/Sources/stella/Models/Session.swift b/Sources/stella/Models/Session.swift index 9003bba..0b5ca9a 100644 --- a/Sources/stella/Models/Session.swift +++ b/Sources/stella/Models/Session.swift @@ -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()) + } } } diff --git a/Sources/stella/entrypoint.swift b/Sources/stella/entrypoint.swift index 54568f9..0eb6cfb 100644 --- a/Sources/stella/entrypoint.swift +++ b/Sources/stella/entrypoint.swift @@ -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) diff --git a/Sources/stella/routes.swift b/Sources/stella/routes.swift index 54bf672..1ed02df 100644 --- a/Sources/stella/routes.swift +++ b/Sources/stella/routes.swift @@ -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())