Skip to content

Commit

Permalink
add sleep tag (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
mui-z authored Oct 11, 2022
1 parent 404aa0e commit 7e027fd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Tags must be enclosed in `[]`.
| tw | `.tapWait` | tap wait |
| twn | `.tapWaitAndNewline` | tap wait and newline |
| cl | `.clear` | clear |
| sleep duration=xxxx | `.sleep(duration: Double)` | sleep for the specified time. duration unit is milliseconds. |
| delay speed=xxxx | `.delay(speed: Double)` | change delay character displayed speed. speed unit is milliseconds. |
| setDefaultDelay speed=xxxx | `.setDefaultDelay(speed: Double)` | change default delay character displayed speed. speed unit is milliseconds. |
| resetDelay | `.resetDelay` | reset delay speed |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public class NovelController: Controller {

let offset: Int = index

let checkListRange = displayEvents[offset ..< displayEvents.count]
let checkListRange = displayEvents[offset..<displayEvents.count]
let endIndex = checkListRange
.firstIndex(where: { $0 == .tapWaitAndNewline || $0 == .tapWait || $0 == .end })
.map { $0 - 1 }!
Expand Down Expand Up @@ -193,6 +193,8 @@ public class NovelController: Controller {
state = .pause
case .end:
reset()
case .sleep(let duration):
try! await Task.sleep(nanoseconds: UInt64(duration * Double(pow(10.0, 6))))
default:
break
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/EffectiveNovelCore/Domain/Parser/ScriptParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ internal struct ScriptParser: Parser {
case (let tag) where tag.hasPrefix("delay speed"):
let speed = Double(tag.split(separator: "=").last!)!
return .delay(speed: speed)
case (let tag) where tag.hasPrefix("sleep duration"):
let duration = Double(tag.split(separator: "=").last!)!
return .sleep(duration: duration)
case "e":
return .end
default:
Expand Down
1 change: 1 addition & 0 deletions Sources/EffectiveNovelCore/Domain/Syntax/NovelSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public enum DisplayEvent: Equatable {
case clear
case resetDelay
case delay(speed: Double)
case sleep(duration: Double)
case end
}
26 changes: 25 additions & 1 deletion Tests/EffectiveNovelCoreTests/Adapter/ControllerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,31 @@ final class ControllerTest: XCTestCase {
}
}

func testSleep() {
let expectation = expectation(description: #function)
let controller = NovelController()

expectation.expectedFulfillmentCount = 4

let result = controller.load(raw: "s123[sleep duration=2000]0123456789[e]")

switch result {
case .valid(let script):
controller.start(script: script)
.sink { event in
expectation.fulfill()
}
.store(in: &cancellables)

waitForExpectations(timeout: 1)

XCTAssertEqual(controller.state, .running)

case .invalid:
XCTFail()
}
}

func testPause() {
let expectation = expectation(description: #function)
let controller = NovelController()
Expand Down Expand Up @@ -261,6 +286,5 @@ final class ControllerTest: XCTestCase {
case .invalid:
XCTFail()
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class ScriptParserTests: XCTestCase {
XCTAssertEqual(try! parser.parse(rawString: "[setDefaultDelay speed=1000][e]"), [.setDefaultDelay(speed: 1000), .end])
XCTAssertEqual(try! parser.parse(rawString: "[delay speed=1000][e]"), [.delay(speed: 1000), .end])
XCTAssertEqual(try! parser.parse(rawString: "[resetDelay][e]"), [.resetDelay, .end])
XCTAssertEqual(try! parser.parse(rawString: "[sleep duration=200]"), [.sleep(duration: 200)])
XCTAssertEqual(try! parser.parse(rawString: "[e]"), [.end])
}

Expand Down

0 comments on commit 7e027fd

Please sign in to comment.