| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# qrqscore - Posts your score and retrieves scores from the an international |
| 4 |
# internet toplist for 'qrq'. |
| 5 |
# |
| •6 |
# Copyright (C) 2007-2025 Fabian Kurz, DJ5CW |
| 7 |
# |
| 8 |
# This program is free software; you can redistribute it and/or modify it under |
| 9 |
# the terms of the GNU General Public License as published by the Free Software |
| 10 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 11 |
# version. |
| 12 |
# |
| 13 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 |
# more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License along with |
| 19 |
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin |
| 20 |
# Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
|
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use LWP::UserAgent; |
| 26 |
|
| 27 |
my $ua = LWP::UserAgent->new; |
| •28 |
$ua->agent("qrq-score/0.0.2"); |
| 29 |
|
| 30 |
my $res; |
| 31 |
my ($qrqrc, $toplist); |
| 32 |
my $mycall=''; |
| 33 |
my $noupload=0; |
| 34 |
|
| 35 |
my @publictoplist; |
| 36 |
my @mytoplist; |
| 37 |
my @mergelist; |
| •38 |
my @url = ('https://fkurz.net/ham/qrqtop.php', |
| 39 |
'https://dj1yfk.de/ham/qrqtop.php'); |
| 40 |
|
| •41 |
print "qrq-score (2025-02-19) - toplist synchronizer for morse trainer 'qrq'."; |
| 42 |
print "\n\n"; |
| 43 |
print "Populates your local toplist file with the latest scores from other\n"; |
| •44 |
print "users (https://fkurz.net/ham/qrqtop.php) and uploads your own personal\n"; |
| 45 |
print "record to the online toplist.\n\n"; |
| 46 |
|
| 47 |
unless (@ARGV) { |
| 48 |
print "Usage:\n"; |
| 49 |
print " qrqscore -u Updates local toplist and uploads new highscore\n"; |
| 50 |
print " (if available) for your 'mycall' in 'qrqrc'.\n"; |
| 51 |
|
| 52 |
print " qrqscore -d Only updates your local toplist from the\n"; |
| 53 |
print " internet list, doesn't upload your score.\n"; |
| 54 |
exit(0); |
| 55 |
} |
| 56 |
elsif ($ARGV[0] eq '-d') { |
| 57 |
$noupload = 1; |
| 58 |
} |
| 59 |
|
| 60 |
############################################################################### |
| 61 |
# Find the local toplist file and get the highest score for the call in |
| 62 |
# ~/.qrq/qrqrc |
| 63 |
############################################################################### |
| 64 |
|
| 65 |
if ((-e 'qrqrc') && (-e 'toplist')) { |
| 66 |
print "Found your 'qrqrc' and 'toplist' files in current directory...\n"; |
| 67 |
$qrqrc = 'qrqrc'; |
| 68 |
$toplist = 'toplist'; |
| 69 |
} |
| 70 |
elsif ((-e $ENV{HOME}.'/.qrq/qrqrc') && (-e $ENV{HOME}.'/.qrq/toplist')) { |
| 71 |
print "Found your 'qrqrc' and 'toplist' files in $ENV{HOME}/.qrq/ ...\n"; |
| 72 |
$qrqrc = $ENV{HOME}.'/.qrq/qrqrc'; |
| 73 |
$toplist = $ENV{HOME}.'/.qrq/toplist'; |
| 74 |
} |
| 75 |
else { |
| 76 |
print "Can't find 'qrqrc' and 'toplist'. Exiting.\n"; |
| 77 |
exit(1); |
| 78 |
} |
| 79 |
|
| 80 |
open RC, $qrqrc; |
| 81 |
while (my $line = <RC>) { |
| 82 |
if ($line =~ /callsign=(\w+)/) { |
| 83 |
$mycall = $1; |
| 84 |
last; |
| 85 |
} |
| 86 |
} |
| 87 |
close RC; |
| 88 |
|
| 89 |
open TL, $toplist; |
| 90 |
@mytoplist = <TL>; |
| 91 |
close TL; |
| 92 |
|
| 93 |
open TL, ">$toplist-old"; |
| 94 |
print TL @mytoplist; |
| 95 |
close TL; |
| 96 |
|
| 97 |
chomp(@mytoplist); |
| 98 |
|
| 99 |
print "Old toplist backed up as $toplist-old.\n"; |
| 100 |
|
| 101 |
############################################################################### |
| 102 |
# Merging the internet toplist with yours. Remove double entries for calls from |
| 103 |
# the toplist that are not $mycall, take the better score. |
| 104 |
############################################################################### |
| 105 |
|
| 106 |
my $x=0; |
| 107 |
do { |
| 108 |
$res = $ua->post($url[$x], ['mode' => 'plain']); |
| 109 |
$x++; |
| 110 |
} while (!$res->is_success); |
| 111 |
|
| 112 |
if ($res->is_success) { |
| 113 |
print "Downloaded list from $url[$x-1]...\n"; |
| 114 |
@publictoplist = split(/\n/, $res->content); |
| 115 |
} |
| 116 |
else { |
| 117 |
die "Unable to reach any of the servers. $res->status_line\n"; |
| 118 |
} |
| 119 |
|
| 120 |
@mergelist = (@publictoplist, @mytoplist); |
| 121 |
|
| 122 |
@mytoplist = (); |
| 123 |
|
| 124 |
# Find the top score for every callsign in the List. Might be higher locally |
| 125 |
# than on the internet list, or vice versa... |
| 126 |
|
| 127 |
my %topscores; # key=call, value=topscore |
| 128 |
foreach (@mergelist) { |
| 129 |
next unless($_ =~ /^(\w+)\s+(\d+)/); |
| 130 |
if (defined($topscores{$1})) { |
| 131 |
if ($topscores{$1} <= $2) { |
| 132 |
$topscores{$1} = $2; |
| 133 |
} |
| 134 |
} |
| 135 |
else { |
| 136 |
$topscores{$1} = $2; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
# Generate a new local toplist with all results of $mycall and the best scores |
| 141 |
# from each other callsign. Remove dupes. |
| 142 |
|
| 143 |
my $tmp=''; |
| 144 |
foreach (@mergelist) { |
| 145 |
next unless ($_ =~ /^(\w+)\s+(\d+)/); |
| 146 |
next if (index($tmp, $_) > -1); # dupe |
| 147 |
if (($2 == $topscores{$1}) || ($1 eq $mycall)) { |
| 148 |
$topscores{$1} = 0; |
| 149 |
push @mytoplist, $_; |
| 150 |
$tmp .= $_; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
# Sort list by score |
| 155 |
|
| 156 |
@mytoplist = sort { |
| 157 |
(split(/\s+/, $b))[1] <=> (split(/\s+/, $a))[1] |
| 158 |
} @mytoplist; |
| 159 |
|
| 160 |
|
| 161 |
# Write to $toplist and create backup. |
| 162 |
|
| 163 |
@mytoplist = map "$_\n", @mytoplist; # unchomp |
| 164 |
|
| 165 |
open TL, ">$toplist"; |
| 166 |
print TL @mytoplist; |
| 167 |
close TL; |
| 168 |
|
| 169 |
print "Wrote new toplist to '$toplist'.\n\n"; |
| 170 |
|
| 171 |
if ($noupload) { print "Done.\n"; exit(0);} |
| 172 |
|
| 173 |
# Upload own record... |
| 174 |
|
| 175 |
open TL, $toplist; |
| 176 |
@mytoplist = <TL>; |
| 177 |
close TL; |
| 178 |
|
| 179 |
my $line; |
| 180 |
foreach (@mytoplist) { |
| 181 |
if ($_ =~ /$mycall/) { |
| 182 |
$line = $_; |
| 183 |
last; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
print "Now uploading your personal record...\n"; |
| 188 |
|
| 189 |
$res = $ua->post($url[$x-1], ['mode' => 'update', |
| 190 |
'line' => $line]); |
| 191 |
|
| 192 |
unless ($res->is_success) { |
| 193 |
print "Failed to post score...\n"; |
| 194 |
} |
| 195 |
|
| 196 |
print "Server response: ".$res->content."\n"; |
| 197 |
|
| 198 |
print "Done.\n"; |