Skip to content

Instantly share code, notes, and snippets.

struct Person {
// The name of the person.
var name: String
// The age of the person.
var age: Int
// The name of their pet, if it exists.
var pet: String?
}
/// Protocol that defines the entities in our database.
/// They must have an identifier that is used to retrieve them.
protocol Entity {
/// The identifier of the resource.
var id: String { get }
}
extension Entity {
/// Helper to retrieve the table name from the Type of the entity.
static var tableName: String { return String(reflecting: Self.self) }
// Create the timer
timer = DispatchSource.makeTimerSource(flags: .strict, queue: DispatchQueue.global())
timer.setEventHandler {
//do work
}
// schedule the timer
timer.schedule(deadline: .now() + firstFireTimeInterval, repeating: repeatingTimeInterval, leeway: tolerance)
// resume the source
timer.resume()
let fireInterval: TimeInterval = 1.0 // number of seconds between two triggering
let shouldRepeate: Bool = true // whether the timer should tick every fireInterval or if it should fire once
let timer = scheduledTimer(withTimeInterval: fireInterval, repeats: shouldRepeat) {
timer in
//... operations to perform every time the timer fires.
}

Lupus

Lupus is a role-playing game where a group of people interprets members of a medieval village, infested by supernatural entities which fights to survive the terrors of the night.

At every player is randomly assigned a role, and each role has an aura (that can be good or evil), a faction and some special powers. The roles are secrets: once you receive your role, you should not revela it to anyone.

In every game, there are always at least two factions: the villagers and the wolves. The purpose of the villagers is to kill all the wolves while the wolves are trying to eat all the villagers.

When the party of players is numerous enough, we can introduce a third faction: the vampires. When this faction comes into play, its goal is to convert as many villagers they can so that they can kill all the wolves.

How the game works

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var backgroundTaskId: UIBackgroundTaskIdentifier? = nil
var vc: ViewController!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
struct Execution: Codable {
let startTime: TimeInterval
let endTime: TimeInterval
}
struct State: Codable {
var executions: [Execution] = []
}
func retrieveAndUpdateState() -> State {
// get the reference to the UserDefaults
let ud = UserDefaults.standard
// retrieve the AppState stored at the previous execution
var state: State = ud.codable(forKey: stateKey) ?? State()
// Retrieve the information from the user defaults
let lastTaskStart: TimeInterval = ud.double(forKey: appEntereBGKey)
let lastTaskEnd: TimeInterval = ud.double(forKey: bgTaskExpiredKey)
func applicationDidEnterBackground(_ application: UIApplication) {
UserDefaults.standard.set(
Date().timeIntervalSince1970,
forKey: appEntereBGKey
)
}