private bool TryRegisterServer(IPAddress who_requests, DiceGameServerEntry new_server) { if (IsRecentIp(who_requests) || !DbServer.InsertDiceGameServer(new_server)) { return(false); } _recent_server_creators_IPs.Enqueue(new TimestampedIP(who_requests, DateTime.UtcNow)); OnMessageToGameServer?.Invoke( this, new MessageToGameServerEventArgs(new_server, HeadToGameServerMessage.AckRegister) ); return(true); }
private void OnServerCreation(object sender, EventArgs e) { var options = new DiceGameServerEntry() { Name = serverSubform.txtName.Text, MaxPlayers = (int)serverSubform.numMaxPlayers.Value, TurnTimeSec = (int)dicegameSubform.numTurnTime.Value, ScoreGoal = (int)dicegameSubform.numScoreGoal.Value, IsJokerAllowed = dicegameSubform.chkIncludedJoker.Checked, DiceNumber = (int)dicegameSubform.numDiceNumber.Value }; OptionsSubmitted?.Invoke( this, new ServerCreationEventArgs(options) ); this.Close(); }
public bool InsertDiceGameServer(DiceGameServerEntry new_server) { using (SqlTransaction trans = _connection.BeginTransaction()) using (SqlCommand insert_command = new SqlCommand("insert_dice_game_server", _connection, trans) { CommandType = CommandType.StoredProcedure }) { insert_command.Parameters.Add("@name", SqlDbType.NVarChar).Value = new_server.Name; insert_command.Parameters.Add("@max_players", SqlDbType.Int).Value = new_server.MaxPlayers; insert_command.Parameters.Add("@ip", SqlDbType.BigInt).Value = new_server.Socket.Address.Address; insert_command.Parameters.Add("@port", SqlDbType.Int).Value = new_server.Socket.Port; insert_command.Parameters.Add("@creator_name", SqlDbType.NVarChar).Value = new_server.CreatorName; //TODO insert_command.Parameters.Add("@enroll_time", SqlDbType.DateTime).Value = DateTime.UtcNow; insert_command.Parameters.Add("@turn_time_sec", SqlDbType.SmallInt).Value = new_server.TurnTimeSec; insert_command.Parameters.Add("@score_goal", SqlDbType.Int).Value = new_server.ScoreGoal; insert_command.Parameters.Add("@dice_number", SqlDbType.Int).Value = new_server.DiceNumber; insert_command.Parameters.Add("@is_joker_allowed", SqlDbType.Bit).Value = new_server.IsJokerAllowed ? 1 : 0; insert_command.Parameters.Add("@is_active", SqlDbType.Bit).Value = new_server.IsActive ? 1 : 0; insert_command.Parameters.Add("@is_inserted", SqlDbType.Int).Direction = ParameterDirection.Output; int result_count = insert_command.ExecuteNonQuery(); if (result_count == 0) { trans.Rollback(); return(false); } int is_ok = Convert.ToInt32(insert_command.Parameters["@is_inserted"].Value); if (is_ok == 1) { trans.Commit(); return(true); } trans.Rollback(); return(false); } }