public CancelDelegate Resolve(List <RISMilestone> stones, ResultCallback cb) { WebClient client = new WebClient(); CancelDelegate cd = () => {}; RISMilestone stone = stones[0]; stones.RemoveAt(0); Logging.LogFormat("Resolving {0}", stone.name); client.DownloadStringCompleted += (object sender, DownloadStringCompletedEventArgs e) => { bool result = false; try { if (e.Cancelled) { Logging.Log("Resolve(Sync) cancelled"); } else if (e.Error != null) { Logging.LogException(e.Error); } else { string json = e.Result; Logging.Log("Resolve: " + json); Hashtable ht = MiniJSON.jsonDecode(json) as Hashtable; checkError(ht); Result r = new Result(ht, ourName); stone.Resolve(r.first); result = true; } } catch (Exception exc) { /* Job failed, but we still have to exit job state */ Logging.LogException(exc); } if (result && stones.Count > 0) { cd += Resolve(stones, cb); return; } cb.Invoke(result); }; client.DownloadStringAsync(Page("/result", "game={0}&contract={1}", inGame, stone.name)); cd += client.CancelAsync; return(cd); }
public CancelDelegate Report(RISMilestone stone, ResultCallback cb) { WebClient client = new WebClient(); Logging.LogFormat("Reporting {0} completed at {1}", stone.name, stone.completed.ToString()); client.DownloadStringCompleted += (object sender, DownloadStringCompletedEventArgs e) => { bool result = false; try { if (e.Cancelled) { Logging.Log("Report(Sync) cancelled"); } else if (e.Error != null) { Logging.LogException(e.Error); } else { string json = e.Result; Logging.Log("Report: " + json); Hashtable ht = MiniJSON.jsonDecode(json) as Hashtable; checkError(ht); Result r = new Result(ht, ourName); if (r.date != null) { stone.reported = true; } stone.Resolve(r.first); result = true; } } catch (Exception exc) { /* Job failed, but we still have to exit job state */ Logging.LogException(exc); } cb.Invoke(result); }; client.DownloadStringAsync(Page("/completed", "game={0}&player={1}&year={2:D}&day={3:D}&contract={4}", inGame, ourName, stone.completed.year, stone.completed.day, stone.name)); return(client.CancelAsync); }