Skip to content

Commit

Permalink
Refactor sphere.web
Browse files Browse the repository at this point in the history
  • Loading branch information
semyon422 committed Sep 11, 2024
1 parent 3358d41 commit 861850c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 78 deletions.
2 changes: 1 addition & 1 deletion aqua
1 change: 1 addition & 0 deletions install
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
. ./setenv
luarocks install luasocket
luarocks install luaossl
luarocks install luautf8
luarocks install luacov
luarocks install luacov-reporter-lcov
9 changes: 4 additions & 5 deletions sphere/web/ComputeApp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ local RouterHandler = require("web.router.RouterHandler")
local PageHandler = require("web.page.PageHandler")
local SessionHandler = require("web.cookie.SessionHandler")

local WebReplayController = require("sphere.web.WebReplayController")
local WebNoteChartController = require("sphere.web.WebNoteChartController")

local WebReplayHandler = require("sphere.web.WebReplayHandler")
local WebChartHandler = require("sphere.web.WebChartHandler")

---@class sphere.ComputeApp: web.IHandler
---@operator call: sphere.ComputeApp
Expand All @@ -31,8 +30,8 @@ local ComputeApp = IHandler + {}
function ComputeApp:new()
local router = Router()

router:route("POST", "/replay", {controller = WebReplayController})
router:route("POST", "/notechart", {controller = WebNoteChartController})
router:route("POST", "/replay", {controller = WebReplayHandler()})
router:route("POST", "/notechart", {controller = WebChartHandler()})

self.handler = ErrorHandler(SequentialHandler({
RouterHandler(router),
Expand Down
66 changes: 66 additions & 0 deletions sphere/web/WebChartHandler.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
local class = require("class")
local DifficultyModel = require("sphere.models.DifficultyModel")
local ChartFactory = require("notechart.ChartFactory")

---@class sphere.WebChartHandler
---@operator call: sphere.WebChartHandler
local WebChartHandler = class()

function WebChartHandler:new()
self.difficultyModel = DifficultyModel()
end

---@param path string
---@return string?
---@return string?
local function read_file(path)
local file, err = io.open(path, "r")
if not file then
return nil, err
end
local content = file:read("*a")
file:close()
return content
end

---@param notechart table
---@return ncdk2.Chart[]?
---@return string?
function WebChartHandler:getCharts(notechart)
local content, err = read_file(notechart.path)
if not content then
return nil, err
end

return ChartFactory:getCharts("file." .. notechart.extension, content)
end

---@param notechart table
---@return ncdk2.Chart?
---@return string?
function WebChartHandler:getChart(notechart)
local charts, err = WebChartHandler:getCharts(notechart)
if not charts then
return nil, err
end
return charts[1]
end

function WebChartHandler:POST()
local charts, err = WebChartHandler:getCharts(self.params.notechart)
if not charts then
return {status = 500, json = {error = err}}
end

local metadatas = {}
for _, chart in ipairs(charts) do
local chartmeta = chart.chartmeta
self.difficultyModel:compute(chartmeta, chart, 1)
table.insert(metadatas, chartmeta)
end

return {status = 200, json = {notecharts = metadatas}}
end


return WebChartHandler
61 changes: 0 additions & 61 deletions sphere/web/WebNoteChartController.lua

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local class = require("class")
local FastplayController = require("sphere.controllers.FastplayController")
local WebNoteChartController = require("sphere.web.WebNoteChartController")
local WebChartHandler = require("sphere.web.WebChartHandler")

local Replay = require("sphere.models.ReplayModel.Replay")
local ReplayModel = require("sphere.models.ReplayModel")
Expand All @@ -9,9 +10,17 @@ local RhythmModel = require("sphere.models.RhythmModel")
local PlayContext = require("sphere.models.PlayContext")
local ModifierEncoder = require("sphere.models.ModifierEncoder")

local WebReplayController = {}
---@class sphere.WebReplayHandler
---@operator call: sphere.WebReplayHandler
local WebReplayHandler = class()

WebReplayController.getReplay = function(replay)
function WebReplayHandler:new()
self.webChartHandler = WebChartHandler()
end

---@param replay table
---@return sphere.Replay?
function WebReplayHandler:getReplay(replay)
local file = io.open(replay.path, "r")
if not file then
error("Replay not found")
Expand All @@ -22,15 +31,15 @@ WebReplayController.getReplay = function(replay)
return Replay():fromString(content)
end

function WebReplayController:POST()
function WebReplayHandler:POST()
local params = self.params

local noteChart, err = WebNoteChartController.getNoteChart(params.notechart)
if not noteChart then
local chart, err = self.webChartHandler:getChart(params.notechart)
if not chart then
return {status = 500, json = {error = err}}
end

local replay = WebReplayController.getReplay(params.replay)
local replay = WebReplayHandler:getReplay(params.replay)

local fastplayController = FastplayController()

Expand All @@ -53,13 +62,13 @@ function WebReplayController:POST()
rhythmModel:setTimings(replay.timings)
replayModel.replay = replay

fastplayController:play(noteChart, replay)
fastplayController:play(chart, replay)

local score = rhythmModel.scoreEngine.scoreSystem:getSlice()

return {json = {
score = score,
inputMode = tostring(noteChart.inputMode),
inputMode = tostring(chart.inputMode),
playContext = playContext,
modifiers = replay.modifiers,
modifiersEncoded = ModifierEncoder:encode(replay.modifiers),
Expand All @@ -68,5 +77,4 @@ function WebReplayController:POST()
}}
end


return WebReplayController
return WebReplayHandler

0 comments on commit 861850c

Please sign in to comment.