<commit_before>import Foundation
import JavaScriptCore

public typealias StateUpdateBlock = (String) -> Void
public typealias ObserverBlock = (Any?) -> Void

public class Game {
    fileprivate let context = JSContext()

    public let inputs:[Input]
    public let outputs:[Output]
    public let observers:[ObserverBlock]

    public var onStateUpdate: StateUpdateBlock?

    public init(story:String, inputs:[String:Input]?, outputs:[String:Output]?, observers:[String:ObserverBlock]?, onStateUpdate:StateUpdateBlock?) {
        if let inputs = inputs {
            self.inputs = Array(inputs.values)
        } else {
            self.inputs = []
        }

        if let outputs = outputs {
            self.outputs = Array(outputs.values)
        } else {
            self.outputs = []
        }
        
        if let observers = observers {
            self.observers = Array(observers.values)
        } else {
            self.observers = []
        }

        self.onStateUpdate = onStateUpdate<commit_msg>Can use subscript syntax to access game state<commit_after>import Foundation
import JavaScriptCore

public typealias StateUpdateBlock = (String) -> Void
public typealias ObserverBlock = (Any?) -> Void

public class Game {
    fileprivate let context = JSContext()

    public let inputs:[Input]
    public let outputs:[Output]
    public let observers:[ObserverBlock]

    public var onStateUpdate: StateUpdateBlock?

    public subscript(key: String) -> JSValue? {
        return self.context?.evaluateScript("game.state[\"\(key)\"]")
    }

    public init(story:String, inputs:[String:Input]?, outputs:[String:Output]?, observers:[String:ObserverBlock]?, onStateUpdate:StateUpdateBlock?) {
        if let inputs = inputs {
            self.inputs = Array(inputs.values)
        } else {
            self.inputs = []
        }

        if let outputs = outputs {
            self.outputs = Array(outputs.values)
        } else {
            self.outputs = []
        }
        
        if let observers = observers {
            self.observers = Array(observers.values)
        } else {
            self.observers = []
        }

        self.onStateUpdate = onStateUpdate
{'fraction_non_alphanumeric': 0.10086718393427659, 'fraction_numerical': 0.0, 'mean_word_length': 2.4090202177293936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5071021', 'n_tokens_mistral': 594, 'n_tokens_neox': 560, 'n_words': 144}