use datafile;

var bountyfile := DFOPenDataFile("BountySystem", 1);

function AddBounty(killer, amt)
  var entry := DFFindElement(bountyfile, Cstr(killer.serial), 1);
  print(entry);
  entry.SetProp("Name", killer.name);
  entry.SetProp("Bounty", Cint(DFGetProp(entry, "Bounty", 1, 0) + amt));
  entry.SetProp("Murders", Cint(DFGetProp(entry, "Murders", 1, 0) + 1));
  entry.SetProp("Expires", ReadGameClock() + 1209600);
endfunction

function FindBounty(who, serial)
  var elem := DFFindElement(bountyfile, Cstr(serial));
  if(elem)
    var holder := array;
    holder.append(DFGetProp(elem, "Name"));
    holder.append(DFGetProp(elem, "Bounty"));
    holder.append(DFGetProp(elem, "Murders"));
    bountyfile.DeleteElement(Cstr(serial));
    return holder;
  else
    return 0;
  endif
endfunction

function ComposeBountyBoard()
  var pnames := bountyfile.keys();
  var data := array;
  Foreach thing in pnames
    var elem := DFFindElement(bountyfile, thing);
    var holder := array;
    holder.append(DFGetProp(elem, "Name"));
    holder.append(Cint(thing));
    holder.append(DFGetProp(elem, "Bounty"));
    holder.append(DFGetProp(elem, "Murders", 1, 1));
    data.append(holder);
  endforeach
  return bubble_sort_array(data, 3);
endfunction

function MaintainBountySystem()
  var count := 0;
  foreach thing in (bountyfile.keys())
    var elem := DFFindElement(bountyfile, thing);
    if(ReadGameClock() > DFGetProp(elem, "Expires"))
      bountyfile.DeleteElement(thing);
      count := count + 1;
    endif
    sleep(1);
  endforeach
  print("//////////////////////////////////////////");
  print("// Bounty System Maintenance");
  print("// Removed " + count + " expired bounties.");
  print("//////////////////////////////////////////");
endfunction

