public void OnDisconnect(IWebSession session)
        {
            //Console.WriteLine(m_LocalNode.GetName() + " - OnDisconnect: " + session.GetRemoteAddress());

            var clientMsg = new
            {
                msg = "client_info",

                client_id = session.GetRemoteAddress(),
                front_end = m_LocalNode.GetName(),
                action    = "disconnect"
            };

            var server = m_LocalNode.GetPublicServer();

            if (server != null && server.IsWorking())
            {
                session.Send(m_LocalNode.GetJsonHelper().ToJsonString(clientMsg));
            }
        }
        public void OnConnect(IWebSession session)
        {
            //Console.WriteLine(m_LocalNode.GetName() + " - OnConnect: " + session.GetRemoteAddress());

            //System.Diagnostics.Debugger.Break();

            //m_LocalNode.GetLogger().Info("OnClientConnect: " + session.GetRequestPath());

            var clientMsg = new
            {
                msg = "client_info",

                client_id = session.GetRemoteAddress(),
                front_end = m_LocalNode.GetName(),
                action    = "connect"
            };

            var server = m_LocalNode.GetPublicServer();

            if (server != null && server.IsWorking())
            {
                session.Send(m_LocalNode.GetJsonHelper().ToJsonString(clientMsg));
            }
        }
        public void OnConnect(IWebSession session)
        {
            //Console.WriteLine(m_LocalNode.GetName() + " - OnConnect: " + session.GetRemoteAddress());
            //m_LocalNode.GetLogger().Info("OnClientConnect: " + session.GetRequestPath());

            if (m_Clients == null)
            {
                session.CloseConnection();
                return;
            }

            var count        = 0;
            var playerId     = "";
            var merchantCode = "";
            var currencyCode = "";
            var sessionId    = "";
            var parts        = session.GetRequestPath().Split('/');

            foreach (var part in parts)
            {
                if (part.Length <= 0)
                {
                    continue;
                }
                count++;
                if (count == 1)
                {
                    merchantCode = part;
                }
                if (count == 2)
                {
                    currencyCode = part;
                }
                if (count == 3)
                {
                    playerId = part;
                }
                if (count == 4)
                {
                    sessionId = part;
                }
                if (count > 4)
                {
                    break;
                }
            }

            var okay        = false;
            var clientToken = "";

            if (!String.IsNullOrEmpty(merchantCode) &&
                !String.IsNullOrEmpty(currencyCode) &&
                !String.IsNullOrEmpty(playerId) &&
                !String.IsNullOrEmpty(sessionId))
            {
                var dbhelper = m_LocalNode.GetDataHelper();
                using (var cnn = dbhelper.OpenDatabase(m_MainCache))
                {
                    using (var cmd = cnn.CreateCommand())
                    {
                        dbhelper.AddParam(cmd, "@session_id", sessionId);
                        dbhelper.AddParam(cmd, "@merchant_code", merchantCode);
                        dbhelper.AddParam(cmd, "@currency_code", currencyCode);
                        dbhelper.AddParam(cmd, "@player_id", playerId);

                        cmd.CommandText = " select * from tbl_player_session "
                                          + " where merchant_code = @merchant_code "
                                          + " and currency_code = @currency_code "
                                          + " and player_id = @player_id "
                                          + " and session_id = @session_id "
                        ;

                        using (var reader = cmd.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                okay = true;
                            }
                        }
                    }

                    if (okay)
                    {
                        clientToken = Guid.NewGuid().ToString();
                        using (var cmd = cnn.CreateCommand())
                        {
                            dbhelper.AddParam(cmd, "@client_token", clientToken);
                            dbhelper.AddParam(cmd, "@session_id", sessionId);
                            dbhelper.AddParam(cmd, "@merchant_code", merchantCode);
                            dbhelper.AddParam(cmd, "@currency_code", currencyCode);
                            dbhelper.AddParam(cmd, "@player_id", playerId);

                            cmd.CommandText = " update tbl_player_session "
                                              + " set client_token = @client_token, update_time = NOW() "
                                              + " where merchant_code = @merchant_code "
                                              + " and currency_code = @currency_code "
                                              + " and player_id = @player_id "
                                              + " and session_id = @session_id "
                            ;

                            okay = cmd.ExecuteNonQuery() > 0;
                        }
                    }
                }
            }

            //if (okay) m_LocalNode.GetLogger().Info("Client session is ok: " + sessionId);
            //else m_LocalNode.GetLogger().Info("Invalid session: " + sessionId);

            if (okay && m_Clients != null)
            {
                m_Clients.AddClient(session.GetRemoteAddress(), merchantCode, currencyCode, playerId, session);

                var clientMsg = new
                {
                    msg = "client_info",

                    client_id    = session.GetRemoteAddress(),
                    front_end    = m_LocalNode.GetName(),
                    client_token = clientToken,
                    action       = "connect"
                };

                var server = m_LocalNode.GetPublicServer();
                if (server != null && server.IsWorking())
                {
                    session.Send(m_LocalNode.GetJsonHelper().ToJsonString(clientMsg));
                }
            }
            else
            {
                session.CloseConnection();
            }
        }
Example #4
0
        private async void Deliver()
        {
            Dictionary <string, List <dynamic> > betResults = new Dictionary <string, List <dynamic> >();
            var dbhelper = m_Node.GetDataHelper();

            using (var cnn = dbhelper.OpenDatabase(m_MainCache))
            {
                using (var cmd = cnn.CreateCommand())
                {
                    dbhelper.AddParam(cmd, "@front_end", m_ServerName);

                    cmd.CommandText = " update tbl_bet_record "
                                      + " set bet_state = 2 " // that means we are going to send them
                                      + " where front_end = @front_end and bet_state = 1 ";

                    cmd.ExecuteNonQuery();
                }

                using (var cmd = cnn.CreateCommand())
                {
                    dbhelper.AddParam(cmd, "@front_end", m_ServerName);

                    // select records which are ready to be sent
                    cmd.CommandText = " select * from tbl_bet_record "
                                      + " where front_end = @front_end and bet_state = 2 ";

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var item = new
                            {
                                client = reader["client_id"].ToString(),
                                server = reader["server_code"].ToString(),
                                table  = reader["table_code"].ToString(),
                                shoe   = reader["shoe_code"].ToString(),
                                round  = Convert.ToInt32(reader["round_number"].ToString()),
                                pool   = Convert.ToInt32(reader["bet_pool"].ToString()),
                                bet    = Convert.ToDecimal(reader["bet_amount"].ToString()),
                                payout = Convert.ToDecimal(reader["pay_amount"].ToString()),
                                result = Convert.ToInt32(reader["game_result"].ToString())
                            };

                            if (betResults.ContainsKey(item.client))
                            {
                                var list = betResults[item.client];
                                list.Add(item);
                            }
                            else
                            {
                                var list = new List <dynamic>();
                                list.Add(item);
                                betResults.Add(item.client, list);
                            }
                        }
                    }
                }

                using (var cmd = cnn.CreateCommand())
                {
                    dbhelper.AddParam(cmd, "@front_end", m_ServerName);

                    // remove them
                    cmd.CommandText = " delete from tbl_bet_record "
                                      + " where front_end = @front_end and bet_state = 2 ";

                    cmd.ExecuteNonQuery();
                }
            }

            foreach (var item in betResults)
            {
                try
                {
                    var list      = item.Value;
                    var clientMsg = new
                    {
                        msg     = "bet_result",
                        results = list
                    };
                    IWebSession client = null;
                    if (m_Clients.TryGetValue(item.Key, out client))
                    {
                        await client.Send(m_Node.GetJsonHelper().ToJsonString(clientMsg));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("FES Send-Bet-Result Error - " + ex.ToString());
                }
            }

            if (betResults.Count > 0)
            {
                m_Logger.Info("Sent bet results - " + betResults.Count);
            }
        }