protected override void OnLoad(EventArgs e) { Login = AuthModule.GetAuthLogin(); var msgs = DbStorage.FindMessages(Login); var files = DbStorage.FindFiles(Login); if(msgs.Length == 0) { var answer = new Msg {Text = ElCapitan.StartMessage, Time = DateTime.UtcNow, Type = MsgType.Answer}; DbStorage.AddDialog(Login, null, new[] {answer}, null, files = ElCapitan.StartFiles); msgs = new[] {answer}; } Chat.Messages = msgs; Explorer.Files = files; var score = DbStorage.FindScores(Login).FirstOrDefault(); if(score != null) { Avatar = score.Avatar; Stars = score.Stars; } var flags = DbStorage.FindFlags(Login); HasBombTimer = ElCapitan.HasBombTimer(flags); var user = DbStorage.FindUserByLogin(Login); if(user == null) throw new HttpException(403, "Access denied"); EndTime = user.EndTime != DateTime.MinValue ? user.EndTime : Settings.BombTimerEnd; StartBombTimer.Visible = HasBombTimer; }
public static void AddDialog(string login, Msg question, Msg[] answers, Flag flag, File[] files, long? broadcast = null) { Settings.ConnectionString.UsingConnection(conn => conn.UsingTransaction(tran => { AddMessage(tran, question, login); Array.ForEach(answers, answer => AddMessage(tran, answer, login)); AddFlag(tran, flag, login); AddFiles(tran, files, login); SetBroadcast(tran, broadcast, login); })); }
protected override AjaxResult ProcessRequestInternal(HttpContext context) { var login = AuthModule.GetAuthLogin(); AntiFlood.CheckFlood($"{context.Request.CurrentExecutionFilePath}:{login}"); var flags = DbStorage.FindFlags(login); if(ElCapitan.GameEnded(flags)) throw new HttpException(403, "The End"); var user = DbStorage.FindUserByLogin(login); if(user == null) throw new HttpException(403, "Access denied"); if(user.EndTime != DateTime.MinValue && user.EndTime < DateTime.UtcNow) throw new HttpException(403, "The End"); var question = context.Request.Form["question"].TrimToNull(); if(question == null) throw new HttpException(400, "Message is empty"); if(question.Length > Settings.MaxMsgLength) throw new HttpException(400, "Message too large"); Flag flag; File[] files; DateTime timer; var answer = ElCapitan.GetAnswer(question, flags, out flag, out files, out timer); var msg = new Msg {Text = answer, Time = DateTime.UtcNow, Type = MsgType.Answer}; DbStorage.AddDialog(login, new Msg {Text = question, Time = DateTime.UtcNow, Type = MsgType.Question}, new[] {msg}, flag, files); return new AjaxResult {Messages = new[] {msg}, Files = files, Score = flag != null ? 1 : 0, Timer = timer == DateTime.MinValue ? DateTime.MinValue : (user.EndTime != DateTime.MinValue ? user.EndTime : timer)}; }
private static void AddMessage(DbTransaction tran, Msg msg, string login) { if(msg == null) return; tran.Connection.UsingCommand("insert into chat ([dt], [type], [login], [text]) values (@dt, @type, @login, @text)", cmd => { cmd.Transaction = tran; cmd.AddParam("dt", msg.Time, DbType.DateTime2); cmd.AddParam("type", msg.Type.ToString(), DbType.String); cmd.AddParam("login", login, DbType.String); cmd.AddParam("text", msg.Text, DbType.String); if(cmd.ExecuteNonQuery() > 0) Log.DebugFormat("Add {0} msg '{1}'", msg.Type, msg.Text.SafeToLog()); }); }