66 "net/http"
77
88 "github.com/gorilla/mux"
9+ "github.com/zond/godip"
910 "github.com/zond/godip/variants"
1011 "google.golang.org/appengine"
1112)
@@ -22,6 +23,7 @@ func preflight(w http.ResponseWriter, r *http.Request) {
2223
2324func resolve (w http.ResponseWriter , r * http.Request ) {
2425 corsHeaders (w )
26+
2527 variantName := mux .Vars (r )["variant" ]
2628 variant , found := variants .Variants [variantName ]
2729 if ! found {
@@ -51,8 +53,55 @@ func resolve(w http.ResponseWriter, r *http.Request) {
5153 }
5254}
5355
56+ func resolveWithOptions (w http.ResponseWriter , r * http.Request ) {
57+ corsHeaders (w )
58+
59+ variantName := mux .Vars (r )["variant" ]
60+ variant , found := variants .Variants [variantName ]
61+ if ! found {
62+ http .Error (w , fmt .Sprintf ("Variant %q not found" , variantName ), 404 )
63+ return
64+ }
65+ p := & Phase {}
66+ if err := json .NewDecoder (r .Body ).Decode (p ); err != nil {
67+ http .Error (w , err .Error (), 400 )
68+ return
69+ }
70+ state , err := p .State (variant )
71+ if err != nil {
72+ http .Error (w , err .Error (), 400 )
73+ return
74+ }
75+ if err = state .Next (); err != nil {
76+ http .Error (w , err .Error (), 500 )
77+ return
78+ }
79+
80+ nextPhase := NewPhase (state )
81+
82+ options := map [godip.Nation ]godip.Options {}
83+ for _ , nation := range state .Graph ().Nations () {
84+ options [nation ] = state .Phase ().Options (state , nation )
85+ }
86+
87+ response := struct {
88+ Phase * Phase `json:"phase"`
89+ Options map [godip.Nation ]godip.Options `json:"options"`
90+ }{
91+ Phase : nextPhase ,
92+ Options : options ,
93+ }
94+
95+ w .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
96+ if err = json .NewEncoder (w ).Encode (response ); err != nil {
97+ http .Error (w , err .Error (), 500 )
98+ return
99+ }
100+ }
101+
54102func start (w http.ResponseWriter , r * http.Request ) {
55103 corsHeaders (w )
104+
56105 variantName := mux .Vars (r )["variant" ]
57106 variant , found := variants .Variants [variantName ]
58107 if ! found {
@@ -65,13 +114,49 @@ func start(w http.ResponseWriter, r *http.Request) {
65114 return
66115 }
67116 phase := NewPhase (state )
117+
68118 w .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
69119 if err = json .NewEncoder (w ).Encode (phase ); err != nil {
70120 http .Error (w , err .Error (), 500 )
71121 return
72122 }
73123}
74124
125+ func startWithOptions (w http.ResponseWriter , r * http.Request ) {
126+ corsHeaders (w )
127+
128+ variantName := mux .Vars (r )["variant" ]
129+ variant , found := variants .Variants [variantName ]
130+ if ! found {
131+ http .Error (w , fmt .Sprintf ("Variant %q not found" , variantName ), 404 )
132+ return
133+ }
134+ state , err := variant .Start ()
135+ if err != nil {
136+ http .Error (w , err .Error (), 500 )
137+ return
138+ }
139+ phase := NewPhase (state )
140+
141+ options := map [godip.Nation ]godip.Options {}
142+ for _ , nation := range state .Graph ().Nations () {
143+ options [nation ] = state .Phase ().Options (state , nation )
144+ }
145+ response := struct {
146+ Phase * Phase `json:"phase"`
147+ Options map [godip.Nation ]godip.Options `json:"options"`
148+ }{
149+ Phase : phase ,
150+ Options : options ,
151+ }
152+
153+ w .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
154+ if err = json .NewEncoder (w ).Encode (response ); err != nil {
155+ http .Error (w , err .Error (), 500 )
156+ return
157+ }
158+ }
159+
75160func listVariants (w http.ResponseWriter , r * http.Request ) {
76161 corsHeaders (w )
77162 w .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
@@ -86,7 +171,9 @@ func main() {
86171 r .Methods ("OPTIONS" ).HandlerFunc (preflight )
87172 variants := r .Path ("/{variant}" ).Subrouter ()
88173 variants .Methods ("POST" ).HandlerFunc (resolve )
174+ variants .Path ("/resolve-with-options" ).Methods ("POST" ).HandlerFunc (resolveWithOptions )
89175 variants .Methods ("GET" ).HandlerFunc (start )
176+ r .Path ("/start-with-options/{variant}" ).Methods ("GET" ).HandlerFunc (startWithOptions )
90177 r .Path ("/" ).HandlerFunc (listVariants )
91178 http .Handle ("/" , r )
92179 appengine .Main ()
0 commit comments