public static Boolean UpdateUser(Member user, UpdateModel model, OnGameContext _db) { Boolean success = false; Member userDB =_db.members.Find(user.ID); if(userDB != null){ Trace.WriteLine("not null"); var membersModel = from m in _db.members where m.email == model.Email select m; if(membersModel.Count() == 0){ Trace.WriteLine("doesn't exist"); try { userDB.email = model.Email; userDB.description = model.Description; userDB.password = model.Password; _db.Entry(userDB).State = EntityState.Modified; _db.SaveChanges(); success = true; } catch (Exception ex) { Trace.WriteLine(ex.Message); } } } return success; }
/** * Create a new user. * Boolean - return the bool saying if the query was succesful */ public static MemberCreateStatus CreateUser(RegisterModel model, OnGameContext _db) { MemberCreateStatus success = MemberCreateStatus.UserRejected; try { var modelExist = from m in _db.members where m.email == model.Email select m; if (modelExist.Count() == 0) { modelExist = from m in _db.members where m.userName == model.UserName select m; if (modelExist.Count() == 0) { _db.members.Add(new Member { firstName = model.FirstName, lastName = model.LastName, banned = 0, email = model.Email, role = 1, userName = model.UserName, password = model.Password, photo = "Unknown.jpg" }); _db.SaveChanges(); success = MemberCreateStatus.Success; } else { success = MemberCreateStatus.UserExist; } } else { success = MemberCreateStatus.EmailExist; } } catch (Exception ex) { Trace.WriteLine(ex.Message); } return success; }
public static MemberCreateStatus AcceptChallenge(Member user, Challenge model, OnGameContext _db, int _teamID) { MemberCreateStatus success = MemberCreateStatus.UserRejected; try { Challenge newModel = _db.challenges.Find(model.ID); if (newModel != null) { if (model.size == 1) // Player vs Player { newModel.awayID = user.ID; newModel.status = 1; _db.Entry(newModel).State = EntityState.Modified; _db.SaveChanges(); Member newModel2 = _db.members.Find(user.ID); newModel2.solde -= model.price; _db.Entry(newModel2).State = EntityState.Modified; _db.SaveChanges(); success = MemberCreateStatus.Success; } else // Team vs Team { newModel.awayID = _teamID; newModel.status = 1; _db.Entry(newModel).State = EntityState.Modified; _db.SaveChanges(); Team newModel2 = _db.teams.Find(_teamID); newModel2.solde -= model.price; _db.Entry(newModel2).State = EntityState.Modified; _db.SaveChanges(); success = MemberCreateStatus.Success; } } } catch (Exception ex) { Trace.WriteLine(ex.Message); } return success; }
public static MemberCreateStatus CreateTeam(TeamModel model, OnGameContext _db) { MemberCreateStatus success = MemberCreateStatus.UserRejected; try { var modelExist = from m in _db.teams where m.name == model.TeamName select m; if (modelExist.Count() == 0) { modelExist = from m in _db.teams where m.tag == model.TeamTag select m; if (modelExist.Count() == 0) { _db.teams.Add(new Team { name = model.TeamName, tag = model.TeamTag, description = model.Description, photo = "Unknown.jpg" }); _db.SaveChanges(); success = MemberCreateStatus.Success; } else { success = MemberCreateStatus.UserExist; } } else { success = MemberCreateStatus.EmailExist; } } catch (Exception ex) { Trace.WriteLine(ex.Message); } return success; }
public static Boolean AcceptMember(Member user, Team team, OnGameContext _db) { try { var teamRecruit = from t in _db.teamRecruits where t.memberID.ID == user.ID && t.teamID.ID == team.ID select t; if (teamRecruit.Count() == 1) { TeamRecruit tp = teamRecruit.First(); if (teamRecruit.Count() == 1) { try { _db.teamRecruits.Remove(_db.teamRecruits.Find(tp.ID)); _db.teamPlayers.Add(new TeamPlayer{ status = 2, memberID = _db.members.Find(user.ID), teamID = _db.teams.Find(team.ID) }); } catch (NullReferenceException NullEx) { Trace.WriteLine(NullEx.StackTrace); } _db.SaveChanges(); return true; } } return false; } catch (Exception ex) { Trace.WriteLine(ex.StackTrace); return false; } }
public static MemberCreateStatus CompleteChallenge(int userID, int chalID, Member user, OnGameContext _db, string photoName) { MemberCreateStatus success = MemberCreateStatus.UserRejected; try { Challenge newModel = _db.challenges.Find(chalID); if (newModel != null) { #region Players if (newModel.size == 1) // Players { if (newModel.homeID == user.ID) // Is Home { if (newModel.score1 == 0) // Winner not entered yet { newModel.score1 = userID; newModel.screenshot1 = photoName; newModel.status = 2; _db.Entry(newModel).State = EntityState.Modified; _db.SaveChanges(); success = MemberCreateStatus.Success; } else // Winner already entered { } } else // Is Away { if (newModel.score2 == 0) // Winner not entered yet { newModel.score2 = userID; newModel.screenshot2 = photoName; newModel.status = 2; _db.Entry(newModel).State = EntityState.Modified; _db.SaveChanges(); success = MemberCreateStatus.Success; } else // Winner already entered { } } } #endregion #region Teams else // Teams { // Find the good team based on the challenge and user // List of teams where the user is the leader var teamsModel = from t in _db.teamPlayers where t.memberID.ID == user.ID && t.status == 3 select t; // Find the team with the challenge Team team = null; foreach(var item in teamsModel){ if (item.teamID.ID == newModel.homeID) // Is Home and team has been found { team = item.teamID; } } if (team == null) // Is Away { foreach (var item in teamsModel) { if (item.teamID.ID == newModel.awayID) // Team has been found { team = item.teamID; } } if (newModel.score2 == 0) // Winner not entered yet { newModel.score2 = userID; newModel.screenshot2 = photoName; newModel.status = 2; _db.Entry(newModel).State = EntityState.Modified; _db.SaveChanges(); success = MemberCreateStatus.Success; } else // Winner already entered { } } else // Is Home { if (newModel.score1 == 0) // Winner not entered yet { newModel.score1 = userID; newModel.screenshot1 = photoName; newModel.status = 2; _db.Entry(newModel).State = EntityState.Modified; _db.SaveChanges(); success = MemberCreateStatus.Success; } else // Winner already entered { } } } #endregion if (newModel.score1 != 0 && newModel.score2 != 0) // Complete challenge if good { if (newModel.score1 == newModel.score1) { newModel.status = 3; newModel.victorID = newModel.score1; _db.Entry(newModel).State = EntityState.Modified; _db.SaveChanges(); if (newModel.size == 1) { Member user2 = _db.members.Find(newModel.victorID); user2.solde += (newModel.price * 2); _db.Entry(user2).State = EntityState.Modified; _db.SaveChanges(); } else { Team team2 = _db.teams.Find(newModel.victorID); team2.solde += (newModel.price * 2); _db.Entry(team2).State = EntityState.Modified; _db.SaveChanges(); } success = MemberCreateStatus.Success; // Should be something else } } } } catch (Exception ex) { Trace.WriteLine(ex.Message); } return success; }
public static MemberCreateStatus CreateChallenge(Member creator, ChallengeModel model, OnGameContext _db) { MemberCreateStatus success = MemberCreateStatus.UserRejected; try { DateTime date = DateTime.Parse(model.startDate); var gameModel = from g in _db.games where g.ID == model.gameID select g; Trace.WriteLine(gameModel.Count()); Game game = gameModel.First(); if (model.size == 1) // Player vs Player { _db.challenges.Add(new Challenge { homeID = creator.ID, size = model.size, gameID = game, price = model.price, startDate = date, comment = model.comment, endDate = DateTime.Now, status = 0 }); _db.SaveChanges(); Member newModel = _db.members.Find(creator.ID); newModel.solde -= model.price; _db.Entry(newModel).State = EntityState.Modified; _db.SaveChanges(); success = MemberCreateStatus.Success; } else // Team vs Team { _db.challenges.Add(new Challenge { homeID = model.teamID, size = model.size, gameID = game, price = model.price, startDate = date, comment = model.comment, endDate = DateTime.Now, status = 0 }); _db.SaveChanges(); Team newModel = _db.teams.Find(model.teamID); newModel.solde -= model.price; _db.Entry(newModel).State = EntityState.Modified; _db.SaveChanges(); success = MemberCreateStatus.Success; } } catch (Exception ex) { Trace.WriteLine(ex.StackTrace); } return success; }
public static Boolean UpdateTeam(string description, Team model, OnGameContext _db) { Boolean success = false; Team teamDB = _db.teams.Find(model.ID); if (teamDB != null) { try { teamDB.description = description; _db.Entry(teamDB).State = EntityState.Modified; _db.SaveChanges(); success = true; } catch (Exception ex) { Trace.WriteLine(ex.Message); } } return success; }
public static MemberCreateStatus LinkChief(TeamModel model, Member creator, OnGameContext _db) { MemberCreateStatus success = MemberCreateStatus.UserRejected; try { Trace.WriteLine(creator.ID); var teamModel = from t in _db.teams where t.name == model.TeamName select t; _db.teamPlayers.Add(new TeamPlayer { teamID = teamModel.First(), memberID = _db.members.Find(creator.ID), status = 3 }); _db.SaveChanges(); success = MemberCreateStatus.Success; } catch (Exception ex) { success = MemberCreateStatus.EmailExist; Trace.WriteLine(ex.Message); } return success; }
public static Boolean LeaveTeam(Member user, Team team, OnGameContext _db) { try { var teamPlayer = from t in _db.teamPlayers where t.memberID.ID == user.ID && t.teamID.ID == team.ID select t; if (teamPlayer.Count() == 1) { TeamPlayer tp = teamPlayer.First(); if (tp.status == 3) { var teamPlayers = from tps in _db.teamPlayers where tps.teamID.ID == team.ID select tps; if (teamPlayers.Count() > 0) { try { teamPlayers.ToList<TeamPlayer>().ForEach(row => _db.teamPlayers.Remove(row)); } catch (NullReferenceException NullEx) { Trace.WriteLine(NullEx.StackTrace); } _db.SaveChanges(); } return true; } else { _db.teamPlayers.Remove(teamPlayer.First()); _db.SaveChanges(); return true; } } else { return false; } } catch (Exception ex) { Trace.WriteLine(ex.StackTrace); return false; } }
public static Boolean KickMember(Member user, Team team, OnGameContext _db) { try { var teamPlayer = from t in _db.teamPlayers where t.memberID.ID == user.ID && t.teamID.ID == team.ID select t; if (teamPlayer.Count() == 1) { TeamPlayer tp = teamPlayer.First(); if (tp.status != 3) { try { _db.teamPlayers.Remove(_db.teamPlayers.Find(tp.ID)); } catch (NullReferenceException NullEx) { Trace.WriteLine(NullEx.StackTrace); } _db.SaveChanges(); return true; } } return false; } catch (Exception ex) { Trace.WriteLine(ex.StackTrace); return false; } }
public static Boolean JoinTeam(Member user,Team team ,OnGameContext _db) { try { var teamRecruitModel = from t in _db.teamRecruits where t.memberID.ID == user.ID && t.teamID.ID == team.ID select t; if (teamRecruitModel.Count() == 0) { var teamPlayer = from tp in _db.teamPlayers where tp.memberID.ID == user.ID && tp.teamID.ID == team.ID select tp; if (teamPlayer.Count() == 0) { _db.teamRecruits.Add(new TeamRecruit { memberID = _db.members.Find(user.ID), teamID = _db.teams.Find(team.ID) }); _db.SaveChanges(); return true; } } return false; }catch(Exception ex){ Trace.WriteLine(ex.Message); return false; } }