;;; -*- mode: Lisp; Syntax: Common-Lisp; -*-
;;;
;;; Copyright (c) 2009 by the authors.
;;;
;;; See LICENCE for details.
(in-package :hu.dwim.number-toss)
;;;;;;
;;; Request
(def function request/find-player (request)
(player/find-by-id (parse-integer (slot-value request 'player-id))))
(def function request/find-challenge (request)
;; TODO: eventually delete obsolete challenge request parameter
(challenge/find-by-id (parse-integer (or (slot-value request 'challenge)
(slot-value request 'challenge-id)))))
(def function request/find-scoreboard-tag (request)
(remove-if-not 'scoreboard/valid-character? (string-downcase (or (slot-value request 'scoreboard-tag) ""))))
;;;;;;
;;;
;;;
;;;
(def function service/process-register-player-request (request)
(bind ((player-hash (slot-value request 'player-hash))
(player-name (slot-value request 'player-name))
(player-tags (string-downcase (or (slot-value request 'player-tags) "")))
(anonymous? (string= player-name "anonymous"))
(player (unless anonymous? (player/find-by-name player-name))))
(cond ((not (player/valid-name? player-name))
)
((and player (not (string= player-hash (hash-of player))))
)
((not (scoreboard/valid-tag? player-tags))
)
(t
(bind ((player-country-code (slot-value request 'player-country-code))
(version (slot-value request 'version))
(signed-data (slot-value request 'signed-data))
(signature (slot-value request 'signature))
(player (player/ensure :hash player-hash :name player-name :tags player-tags :country-code (unless (string= player-country-code "") player-country-code)
:signed-data signed-data :signature signature :version version)))
(scoreboard/clear-all)
;; TODO: eventually delete obsolete player-status attribute
)))))
;;;;;;
;;;
;;;
;;;
(def function service/process-refresh-global-settings-request (request)
(bind ((player (request/find-player request)))
(cond ((not player)
)
((not (enabled-p player))
)
(t
(bind ((version (slot-value request 'version)))
(setf (version-of player) version)
(setf (last-started-at-of player) (now))
;; TODO: eventually delete obsolete challenge-limit attribute
)))))
;;;;;;
;;;
;;;
;;;
;;;
;;;
(def function service/process-refresh-high-scores-request (request)
(bind ((player (request/find-player request)))
(cond ((not player)
)
((not (enabled-p player))
)
(t
(bind ((scoreboard-tag (request/find-scoreboard-tag request)))
))))>)))))
;;;;;;
;;;
;;;
;;;
(def function service/process-submit-game-request (request)
(bind ((player (request/find-player request))
(challenge (request/find-challenge request)))
(cond ((not player)
)
((not (enabled-p player))
)
((not challenge)
)
(t
(bind ((moves (move-sequence/parse (slot-value request 'moves)))
(score (game/compute-score challenge moves))
(high-score (game/find-high-score challenge player)))
(if (and high-score (<= score high-score))
;; NOTE: if the game is not a player high score, then it's not worth storing
(progn
(game/make player challenge moves)
)))))))
;;;;;;
;;;
;;;
;;;
(def function service/process-submit-challenge-request (request)
(bind ((player (request/find-player request))
(challenge-name (slot-value request 'challenge-name))
(challenge (challenge/find-by-name challenge-name))
(required-high-score-game-count (when player (- (* 10 (1+ (challenge/count-by-player player)))
(game/count-by-player player)))))
(cond ((not player)
)
((not (enabled-p player))
)
((not (challenge/valid-name? challenge-name))
)
(challenge
)
((minusp required-high-score-game-count)
)
(t
(bind ((size (parse-integer (slot-value request 'size)))
(board (board/parse size (slot-value request 'board)))
(challenge (challenge/make player challenge-name size board)))
)))))
;;;;;;
;;;
;;;
;;;
;;;
;;; ...
;;;
(def function service/process-refresh-challenge-list-request (request)
(bind ((player (request/find-player request)))
(cond ((not player)
)
((not (enabled-p player))
)
(t
)>))))
;;;;;;
;;;
;;;
;;;
;;;
;;; ...
;;;
(def function service/process-refresh-challenge-scoreboard-request (request)
(bind ((player (request/find-player request)))
(cond ((not player)
)
((not (enabled-p player))
)
(t
(bind ((challenge (request/find-challenge request))
(scoreboard-tag (request/find-scoreboard-tag request))
(challenge-scoreboard (challenge-scoreboard/ensure (id-of challenge) scoreboard-tag))
(entries (entries-of challenge-scoreboard))
(rank-entries (rank-entries-of challenge-scoreboard)))
))))>)))))
;;;;;;
;;;
;;;
;;;
;;;
;;; ...
;;;
(def function service/process-refresh-challenge-player-scoreboard-request (request)
(bind ((player (request/find-player request)))
(cond ((not player)
)
((not (enabled-p player))
)
((not (featured-p player))
)
(t
(bind ((target-player (player/find-by-id (parse-integer (slot-value request 'target-player-id))))
(challenge (request/find-challenge request))
(scoreboard-tag (request/find-scoreboard-tag request))
(records (select ((score-of game) (submitted-at-of game))
(from (game game))
(where (and (eq challenge (challenge-of game))
(eq target-player (player-of game))))
(order-by :descending (submitted-at-of game)))))
)>)))))
;;;;;;
;;;
;;;
;;;
;;;
;;; ...
;;;
(def function service/process-refresh-overall-scoreboard-request (request)
(bind ((player (request/find-player request)))
(cond ((not player)
)
((not (enabled-p player))
)
(t
(bind ((size (parse-integer (slot-value request 'size)))
(scoreboard-tag (request/find-scoreboard-tag request))
(overall-scoreboard (overall-scoreboard/ensure size scoreboard-tag))
(entries (entries-of overall-scoreboard))
(rank-entries (rank-entries-of overall-scoreboard)))
))))>)))))
;;;;;;
;;;
;;;
;;;
;;;
;;; ...
;;;
(def function service/process-refresh-overall-player-scoreboard-request (request)
(bind ((player (request/find-player request)))
(cond ((not player)
)
((not (enabled-p player))
)
((not (featured-p player))
)
(t
(bind ((target-player-id (slot-value request 'target-player-id))
(scoreboard-tag (request/find-scoreboard-tag request))
(target-player (player/find-by-id (parse-integer target-player-id)))
(size (slot-value request 'size))
(records (select ((challenge-of game) (max (score-of game)))
(from (game game))
(where (and (= size (size-of (challenge-of game)))
(eq target-player (player-of game))))
(group-by (challenge-of game))
(order-by :ascending (challenge-of game)))))
)>)))))
;;;;;;
;;;
;;;
;;;
;;;
;;; ...
;;;
(def function service/process-refresh-challenge-suggestion-request (request)
(bind ((player (request/find-player request)))
(cond ((not player)
)
((not (enabled-p player))
)
((not (featured-p player))
)
(t
(bind ((size (parse-integer (slot-value request 'size)))
(scoreboard-tag (request/find-scoreboard-tag request)))
))>)))))