protected override void OnOpen() { if (!Handler.findToken(Context.CookieCollection[AuthToken.SessionToken].Value, out var bUser, out _)) { Context.WebSocket.Close(CloseStatusCode.Normal, "Authentication failed."); return; } BotUser = bUser; Service ??= Program.Services.GetRequiredService <GroupMuteService>(); foreach (var guild in Program.Client.Guilds) { foreach (var voice in guild.VoiceChannels) { foreach (var usr in voice.Users) { if (usr.Id == BotUser.Id) { User = usr; VC = voice; break; } } if (VC != null) { break; } } if (VC != null) { break; } } if (VC == null) { Context.WebSocket.Close(CloseStatusCode.Normal, "You must join a voice channel first."); return; } if (Service.Games.TryGetValue(VC, out var game)) { game.Do(() => { game.Listeners.Add(this); game.Broadcast(false); }); Game = game; } else { var g = new GroupGame(VC); g.Listeners.Add(this); Service.Games[VC] = g; g.Broadcast(true); Game = g; } }
// DELETE api/GroupGame/5 public HttpResponseMessage DeleteGroupGame(int id) { GroupGame GroupGame = db.GroupGames.Where(gw => gw.ID == id).FirstOrDefault(); if (GroupGame == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } db.GroupGames.DeleteObject(GroupGame); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } return(Request.CreateResponse(HttpStatusCode.OK, GroupGame)); }
// PUT api/GroupGame/5 public HttpResponseMessage PutGroupGame(int id, GroupGame GroupGame) { if (ModelState.IsValid && id == GroupGame.ID) { //db.Entry(GroupGame).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } }
public HttpResponseMessage PutGroupGame(int id, int SortOrder) //new sort order { GroupGame GroupGame = db.GroupGames.Where(le => le.ID == id).Single(); int newRank = SortOrder; int? oldRank = GroupGame.SortOrder; //if (newRank > oldRank) newRank++; List <GroupGame> lpld = new List <GroupGame>(); lpld.AddRange(db.GroupGames.Where(le => le.GroupID == GroupGame.GroupID && le.SortOrder < newRank && le.ID != id).OrderBy(ob => ob.SortOrder).ToList()); lpld.Add(db.GroupGames.Where(le => le.ID == id).Single()); foreach (GroupGame l in db.GroupGames.Where(le => le.GroupID == GroupGame.GroupID).OrderBy(ob => ob.SortOrder)) { if (lpld.Where(le => le.ID == l.ID).Count() == 0) { lpld.Add(l); } } for (int i = 0; i < lpld.Count; i++) { lpld[i].SortOrder = i + 1; } try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
// POST api/GroupGame public HttpResponseMessage PostGroupGame(GroupGame GroupGame) { if (ModelState.IsValid) { //if (GroupGame.HiveID != null) // if (GroupGame.HiveID != "undefined") // { // GroupGame.sp = db.Sports.Single(s => s.HivePrefix == GroupGame.OnAirName).ID; // } // //if (GroupGame.HiveID == "undefined") // else // GroupGame.HiveID = null; //if (GroupGame.SortOrder > 0) // GroupGame.SortOrder--; db.GroupGames.AddObject(GroupGame); db.SaveChanges(); int?newRank = GroupGame.SortOrder; if (newRank == db.GroupGames.Where(le => le.GroupID == GroupGame.GroupID).Count()) { newRank++; } //int? oldRank; //if (newRank > oldRank) newRank++; List <GroupGame> lpld = new List <GroupGame>(); lpld.AddRange(db.GroupGames.Where(le => le.GroupID == GroupGame.GroupID && le.SortOrder < newRank && le.ID != GroupGame.ID).OrderBy(ob => ob.SortOrder).ToList()); //lpld.Add(GroupGame); lpld.Add(db.GroupGames.Where(le => le.ID == GroupGame.ID).Single()); foreach (GroupGame l in db.GroupGames.Where(le => le.GroupID == GroupGame.GroupID).OrderBy(ob => ob.SortOrder)) { if (lpld.Where(le => le.ID == l.ID).Count() == 0) { lpld.Add(l); } } for (int i = 0; i < lpld.Count; i++) { lpld[i].SortOrder = i + 1; } db.SaveChanges(); var correctedResponse = new { Data = new[] { GroupGame } }; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, correctedResponse); //HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, GroupGame); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = GroupGame.ID })); return(response); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } }