<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Web.Script.Serialization;
using System.Globalization;

namespace Battlezeppelins.Models
{
    public class Chat
    {
        public static void sendMessage(Player player, string message)
        {
            MySqlConnection conn = new MySqlConnection(
            ConfigurationManager.ConnectionStrings["BattlezConnection"].ConnectionString);
            MySqlCommand myCommand = conn.CreateCommand();
            conn.Open();

            try
            {
                myCommand.CommandText = "INSERT INTO battlezeppelins.message (player, time, text) VALUES (@player, @time, @text)";
                myCommand.Parameters.AddWithValue("@player", player.id);
                myCommand.Parameters.AddWithValue("@time", DateTime.Now);
                myCommand.Parameters.AddWithValue("@text", message);
                myCommand.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }

        public class Message
        {
            public string id;
            public string user;
            public string time;
            public string message;
        }

        public static List<Message> getMessages(int fromId)
        {
            MySqlConnection conn = new MySqlConnection(
            ConfigurationManager.ConnectionStrings["BattlezConnection"].ConnectionString);
            MySqlCommand myCommand = conn.CreateCommand();
            conn.Open();

            List<Message> messages = new List<Message>();
            int maxId;

            try
            {
                myCommand.CommandText = "SELECT COALESCE(MAX(id), 0) AS id FROM battlezeppelins.message";
                
                using (MySqlDataReader reader = myCommand.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        string maxIdStr = reader.GetString(reader.GetOrdinal("id"));
                        maxId = Int32.Parse(maxIdStr);
                    }
                    else
                    {
                        maxId = 0;
                    }
                }

                if (fromId < 0)
                {
                    fromId = maxId + fromId;
                }

                if (fromId < 0) fromId = 0;

                myCommand.CommandText = "SELECT Message.id, Player.name, Message.time, Message.text FROM battlezeppelins.message " +
                    "INNER JOIN Player ON Player.id=Message.player " +
                    "WHERE Message.id > @fromId " +
                    "ORDER BY Message.id";
                myCommand.Parameters.AddWithValue("@fromId", fromId);

                using (MySqlDataReader reader = myCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string id = reader.GetString(reader.GetOrdinal("id"));
                        string user = reader.GetString(reader.GetOrdinal("name"));<commit_msg>Better negative message id handling.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Web.Script.Serialization;
using System.Globalization;

namespace Battlezeppelins.Models
{
    public class Chat
    {
        public static void sendMessage(Player player, string message)
        {
            MySqlConnection conn = new MySqlConnection(
            ConfigurationManager.ConnectionStrings["BattlezConnection"].ConnectionString);
            MySqlCommand myCommand = conn.CreateCommand();
            conn.Open();

            try
            {
                myCommand.CommandText = "INSERT INTO battlezeppelins.message (player, time, text) VALUES (@player, @time, @text)";
                myCommand.Parameters.AddWithValue("@player", player.id);
                myCommand.Parameters.AddWithValue("@time", DateTime.Now);
                myCommand.Parameters.AddWithValue("@text", message);
                myCommand.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }

        public class Message
        {
            public string id;
            public string user;
            public string time;
            public string message;
        }

        public static List<Message> getMessages(int fromId)
        {
            MySqlConnection conn = new MySqlConnection(
            ConfigurationManager.ConnectionStrings["BattlezConnection"].ConnectionString);
            MySqlCommand myCommand = conn.CreateCommand();
            conn.Open();

            List<Message> messages = new List<Message>();

            try
            {
                if (fromId < 0)
                {
                    // Negative fromId will be subtracted from max id
                    int maxId = 0;
                    myCommand.CommandText = "SELECT COALESCE(MAX(id), 0) AS id FROM battlezeppelins.message";
                
                    using (MySqlDataReader reader = myCommand.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            string maxIdStr = reader.GetString(reader.GetOrdinal("id"));
                            maxId = Int32.Parse(maxIdStr);
                        }
                    }
                
                    fromId = maxId + fromId;
                    if (fromId < 0) fromId = 0;
                }

                myCommand.CommandText = "SELECT Message.id, Player.name, Message.time, Message.text FROM battlezeppelins.message " +
                    "INNER JOIN Player ON Player.id=Message.player " +
                    "WHERE Message.id > @fromId " +
                    "ORDER BY Message.id";
                myCommand.Parameters.AddWithValue("@fromId", fromId);

                using (MySqlDataReader reader = myCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string id = reader.GetString(reader.GetOrdinal("id"));
                        string user = reader.GetString(reader.GetOrdinal("name"));
{'fraction_non_alphanumeric': 0.08740927737456611, 'fraction_numerical': 0.002208898706216472, 'mean_word_length': 1.4723088923556942, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3687533', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1446, 'n_words': 329}