Created
April 16, 2015 14:32
-
-
Save mopemope/57f804fc6385d6df8979 to your computer and use it in GitHub Desktop.
curling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmodule curling-accumulator | |
(export all) | |
(behavior 'gen_event)) | |
(defrecord state | |
(teams (orddict:new)) | |
(round 0)) | |
(defun init | |
(('()) `#(ok ,(make-state)))) | |
(defun handle_event | |
((`#(set-teams ,team-a ,team-b) (= (match-state teams t) s)) | |
(let ((teams (orddict:store team-a 0 (orddict:store team-b 0 t)))) | |
`#(ok ,(set-state s teams teams)))) | |
((`#(add-points ,team ,n) (= (match-state teams t) s)) | |
(let ((teams (orddict:update_counter team n t))) | |
`#(ok ,(set-state s teams teams)))) | |
(('next-round (= (match-state) s)) | |
`#(ok ,(set-state s round (+ 1 (state-round s))))) | |
((_ (= (match-state) state)) | |
`#(ok ,state))) | |
(defun handle_call | |
(('game-data (= (match-state teams t round r) s)) | |
`#(ok ,`#(,(orddict:to_list t) ,`#(round ,r)) ,s)) | |
((_ state) | |
`#(ok ok ,state))) | |
(defun handle_info (_ state) | |
`#(ok ,state)) | |
(defun code_change (_oldvsn state _extra) | |
`#(ok ,state)) | |
(defun terminate (_reason _state) | |
'ok) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmodule curling-scoreboard-hw | |
(export all)) | |
(defun set-teams (team-a team-b) | |
(io:format "Scoreboard: Team ~s vs. Team ~s~n" `(,team-a ,team-b))) | |
(defun next-round () | |
(io:format "Scoreboard: round over~n")) | |
(defun add-point (team) | |
(io:format "Scoreboard: increased score of team ~s by 1~n" `(,team))) | |
(defun reset-board () | |
(io:format "Scoreboard: All teams are undefined and all scores are 0~n")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmodule curling-scoreboard | |
(export all) | |
(behavior 'gen_event)) | |
(defun init | |
(('()) `#(ok ,()))) | |
(defun handle_event | |
((`#(set-teams ,team-a ,team-b) state) | |
(curling-scoreboard-hw:set-teams team-a team-b) | |
`#(ok ,state)) | |
((`#(add-points ,team ,n) state) | |
(lc ((<- _ (lists:seq 1 n))) | |
(curling-scoreboard-hw:add-point team)) | |
`#(ok ,state)) | |
(('next-round state) | |
(curling-scoreboard-hw:next-round) | |
`#(ok ,state)) | |
((_ state) | |
`#(ok ,state))) | |
(defun handle_call (_ state) | |
`#(ok ok ,state)) | |
(defun handle_info (_ state) | |
`#(ok ,state)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmodule curling | |
(export all)) | |
(defun start-link (team-a team-b) | |
(let ((`#(ok ,pid) (gen_event:start_link))) | |
(gen_event:add_handler pid 'curling-scoreboard '()) | |
(gen_event:add_handler pid 'curling-accumulator '()) | |
(set-teams pid team-a team-b) | |
`#(ok ,pid))) | |
(defun set-teams (pid team-a team-b) | |
(gen_event:notify pid `#(set-teams ,team-a ,team-b))) | |
(defun add-points (pid team n) | |
(gen_event:notify pid `#(add-points ,team ,n))) | |
(defun next-round (pid) | |
(gen_event:notify pid 'next-round)) | |
(defun join-feed (pid to-pid) | |
(let ((handler-id `#(curling-feed ,(make_ref)))) | |
(gen_event:add_handler pid handler-id `(,to-pid)) | |
handler-id)) | |
(defun leave-feed (pid handler-id) | |
(gen_event:delete_handler pid handler-id 'leave-feed)) | |
(defun game-info (pid) | |
(gen_event:call pid 'curling-accumulator 'game-data)) | |
(defun test () | |
(let ((`#(ok ,pid) (start-link "Pengeons" "Eagles"))) | |
(add-points pid "Pengeons" 2) | |
(next-round pid) | |
(add-points pid "Eagles" 3) | |
(next-round pid) | |
(game-info pid))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment