Delphi 2005 Architect prize awarded for BorCon 2004 Diamondback referral campaign

By: John Kaster

Abstract: John Kaster created a Delphi web service and ASP.NET client to help select the winner of Delphi 2005 Architect

Before BorCon last year, I invited everyone to post an announcement of the Diamondback preview at BorCon 2004. Everyone who posted a link to BorCon 2004 became eligible to win a copy of Delphi 2005 Architect.

At the bottom of the article is the invitation to participate in the referral program. I said "I will randomly select a winner from the list of people who provide a link to their copy of this announcement as a comment to this article". So, I wrote a random list selector web service and ASP.NET client application in Delphi 2005 to make sure that my selection was indeed random enough to fulfill my promise.

The random list selector accepts a list of items, then generates a random number in the appropriate range. When I write simple utilities/demonstration applications like this one, I usually just try get a working version first, then start improving its performance after I have it working. I wanted to use the standard .NET classes to get more familiar with its string processing classes. While I miss something like TStringList (there's a port of it for .NET available on CodeCentral) I was still able to get something usable rather quickly. Here's the most relevant code:

function RandomService.RandomList(List, Delimiter: string;
  PickCount: integer): string;
var
  Splitter: array of string;
  Items: StringCollection;
  Max,
  i,
  Picked: Integer;
begin
  Items := StringCollection.Create;
  Splitter := List.Split(Delimiter.ToCharArray);
  Items.AddRange(Splitter);
  Max := Items.Count;
  Result := '';
  if PickCount > Max then
    PickCount := Max;
  Picked := 0;
  while Picked < PickCount do
  begin
    i := RandomMax(Max);
    if Picked > 0 then
      Result := Result + Delimiter + Items[i]
    else
      Result := Items[i];
    Items.Remove(Items[i]);
    Dec(Max);
    Inc(Picked);
  end;
  Max := Length(Splitter);
  Result := Result + Delimiter
    + System.String.Format('{0} items picked from {1} items listed',
    [Picked, Max]);
end;

There's many things that can be done to increase performance of this code, such as using the .NET StringBuilder class. I could also support regular expressions for extracting items from the list. I left it alone for now after getting this working, because my immediate goal was to select the winner of Delphi Architect for the campaign.

After I used the Delphi 2005 deployment manager to post this ASP.NET application to my "research" area on a server in the appropriate location for the new virtual directory I created, I added a web reference to a new ASP.NET web form application so I could call the web service. After the required data entry components and labels were on the web form, I added these two methods to initialize and call the web service for selecting the winner:

procedure ListPickForm.Page_Load(sender: System.Object; e: System.EventArgs);
begin
  // TODO: Put user code to initialize the page here
  randomizer := RandomService.Create;
end;

procedure ListPickForm.BtnPick_Click(sender: System.Object; e: System.EventArgs);
begin
  Selections.Text := randomizer.RandomList(List.Text, Chr(13)+Chr(10),
    System.Convert.ToInt16(PickCount.Text));
  Selections.Visible := True;
  LabelSelection.Visible := True;
end;

I then used the Delphi 2005 deployment manager (shown below) to deploy the ASP.NET client to the server as well, and tested it.

Deployment manager
Delphi 2005 Deployment Manager view

In this view, you can see that some files on my live server are up to date, but the ASP.NET client page and web service have been modified and need to be deployed to the server. The deployment manager is a very powerful and flexible productivity tool that deserves at least one article all its own, so additional details on it will have to come at another time. If you have Delphi 2005, you should definitely investigate what the deployment manager can do to make your web development for ASP.NET and IntraWeb far more productive.

Finally, picking the winner

I pasted the list of names and URLs from people who posted the BorCon announcement on their site into the text box for the ASP.NET input form, then specified that I wanted 1 (one) winner. As you can see from the screenshot below, the application selected Martin Strohal.

Random list selector
Congratulations, Martin!

Version 2

If I ever do get a chance to work on an update to this application, some of the things I'd like to do are putting in support for regular expressions, speeding up the selection logic, and supporting selection of a number of raffle tickets in sequence. I'll leave this web service up for others to use. Hopefully some user groups will find it handy for selecting the winners of prizes they give away.

You can play with the web service or ASP.NET web form on our server, or create your own version by downloading the program group from CodeCentral. If you make any improvements to it, I'd love to hear about it.


Server Response from: ETNASC03