public CancelDelegate Resolve(List <RISMilestoneBase> stones, ResultCallback cb) { WebClient client = new WebClient(); CancelDelegate cd = () => {}; RISMilestoneBase 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); var ht = Json.Deserialize(json) as Dictionary <string, object>; 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(RISMilestoneBase 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); var ht = Json.Deserialize(json) as Dictionary <string, object>; 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}&tier={5}", inGame, ourName, stone.completed.year, stone.completed.day, stone.name, stone.tier)); return(client.CancelAsync); }
public CancelDelegate Sync(ResultCallback cb) { RISMilestoneBase stone = monitor.CheckAll(); CancelDelegate cd = () => {}; if (stone != null) { cd += Report(stone, (bool ok) => { if (ok) { cd += Sync(cb); } else { cb.Invoke(false); } }); return(cd); } cd += SyncTail((bool ok) => { if (ok) { List <RISMilestoneBase> toResolve = monitor.ToResolve(); if (toResolve.Count > 0) { cd += Resolve(toResolve, cb); } else { cb.Invoke(true); } } else { cb.Invoke(false); } }); return(cd); }