… | |
… | |
42 | aio_rename "$cf::LOCALDIR/highscore~", "$cf::LOCALDIR/highscore" |
42 | aio_rename "$cf::LOCALDIR/highscore~", "$cf::LOCALDIR/highscore" |
43 | and return; |
43 | and return; |
44 | |
44 | |
45 | aio_pathsync "$cf::LOCALDIR"; |
45 | aio_pathsync "$cf::LOCALDIR"; |
46 | |
46 | |
47 | warn "saved highscore file.\n"; |
47 | cf::trace "saved highscore file.\n"; |
48 | |
48 | |
49 | 1 |
49 | 1 |
50 | } |
50 | } |
51 | |
51 | |
52 | our $HEADER = " | rank | name | experience | killer | HP |mana |grace|\n"; |
52 | our $HEADER = " | rank | name | experience | reason | HP |mana |grace|\n"; |
53 | our $SEP = " +------+-------------|--------------------|----------------------+-----+-----+-----+\n"; |
53 | our $SEP = " +------+--------------------|--------------|----------------------+-----+-----+-----+\n"; |
54 | our $FORMAT = " | %4s | %-11.11s | %18s | %-20.20s | %3s | %3s | %3s |\n"; |
54 | our $FORMAT = " | %4s | %-18.18s | %12s | %-20.20s | %3s | %3s | %3s |\n"; |
55 | |
55 | |
56 | our $SCORE_CHANNEL = { |
56 | our $SCORE_CHANNEL = { |
57 | id => "highscore", |
57 | id => "highscore", |
58 | title => "Highscore", |
58 | title => "Highscore", |
59 | tooltip => "Your highscore ranking.", |
59 | tooltip => "Your highscore ranking.", |
… | |
… | |
78 | $ob->map ? $ob->map->name || $ob->map->path : "", |
78 | $ob->map ? $ob->map->name || $ob->map->path : "", |
79 | $ob->stats->maxhp, |
79 | $ob->stats->maxhp, |
80 | $ob->stats->maxsp, |
80 | $ob->stats->maxsp, |
81 | $ob->stats->maxgrace, |
81 | $ob->stats->maxgrace, |
82 | $ob->uuid, |
82 | $ob->uuid, |
83 | int EV::now, |
83 | int AE::now, |
84 | ]; |
84 | ]; |
85 | |
85 | |
86 | cf::async { |
|
|
87 | my $guard = cf::lock_acquire "highscore:check"; |
86 | my $guard = cf::lock_acquire "highscore:check"; |
88 | |
87 | |
89 | # load hiscore, patch, save hiscore |
88 | # load hiscore, patch, save hiscore |
90 | my $hiscore = load_hiscore; |
89 | my $hiscore = load_hiscore; |
91 | |
90 | |
92 | cf::get_slot 0.01, 0, "highscore check"; |
91 | cf::get_slot 0.01, 0, "highscore check"; |
93 | |
92 | |
94 | my ($pre, $ins, $save); |
93 | my ($pre, $ins, $save); |
95 | |
94 | |
96 | pop @$hiscore while @$hiscore > $HIGHSCORE_ENTRIES; |
95 | pop @$hiscore while @$hiscore > $HIGHSCORE_ENTRIES; |
97 | |
96 | |
98 | # find previous entry, and new position |
97 | # find previous entry, and new position |
99 | |
98 | |
100 | for (0 .. $#$hiscore) { |
99 | for (0 .. $#$hiscore) { |
101 | $pre //= $_ if $hiscore->[$_][0] eq $score->[0]; |
100 | $pre //= $_ if $hiscore->[$_][0] eq $score->[0]; |
102 | $ins //= $_ if $hiscore->[$_][2] < $score->[2]; |
101 | $ins //= $_ if $hiscore->[$_][2] < $score->[2]; |
103 | } |
102 | } |
104 | cf::cede_to_tick; # we need an "interruptible" block... |
103 | cf::cede_to_tick; # we need an "interruptible" block... |
105 | |
104 | |
106 | my $msg; |
105 | my $msg; |
107 | |
106 | |
108 | if (defined $pre) { |
107 | if (defined $pre) { |
109 | # we are already in the list |
108 | # we are already in the list |
110 | if ($hiscore->[$pre][2] < $score->[2]) { |
109 | if ($hiscore->[$pre][2] < $score->[2]) { |
111 | $msg = "T<You beat your last score!>\n\n" |
110 | $msg = "T<You beat your last score!>\n\n" |
112 | . $SEP |
|
|
113 | . $HEADER |
|
|
114 | . $SEP |
|
|
115 | . (fmt $ins, $score) |
|
|
116 | . (fmt $pre, $hiscore->[$pre]) |
|
|
117 | . $SEP; |
|
|
118 | |
|
|
119 | splice @$hiscore, $pre, 1; |
|
|
120 | splice @$hiscore, $ins, 0, $score; |
|
|
121 | $save = 1; |
|
|
122 | } else { |
|
|
123 | $msg = "T<You did not beat your last score.>\n\n" |
|
|
124 | . $SEP |
|
|
125 | . $HEADER |
|
|
126 | . $SEP |
|
|
127 | . (fmt $pre , $hiscore->[$pre]) |
|
|
128 | . (fmt undef, $score) |
|
|
129 | . $SEP; |
|
|
130 | } |
|
|
131 | } elsif (defined $ins or @$hiscore < $HIGHSCORE_ENTRIES) { |
|
|
132 | $ins //= @$hiscore; |
|
|
133 | |
|
|
134 | $msg = "T<You entered the highscore list!>\n\n" |
|
|
135 | . $SEP |
111 | . $SEP |
136 | . $HEADER |
112 | . $HEADER |
137 | . $SEP |
113 | . $SEP |
138 | . (fmt $ins, $score) |
114 | . (fmt $ins, $score) |
|
|
115 | . (fmt $pre, $hiscore->[$pre]) |
139 | . $SEP; |
116 | . $SEP; |
140 | |
117 | |
|
|
118 | splice @$hiscore, $pre, 1; |
141 | splice @$hiscore, $ins, 0, $score; |
119 | splice @$hiscore, $ins, 0, $score; |
142 | $save = 1; |
120 | $save = 1; |
143 | } else { |
121 | } else { |
144 | $msg = "T<You did not enter the highscore list.>\n\n" |
122 | $msg = "T<You did not beat your last score.>\n\n" |
145 | . $SEP |
123 | . $SEP |
146 | . $HEADER |
124 | . $HEADER |
147 | . $SEP |
125 | . $SEP |
148 | . (fmt -1 + (scalar @$hiscore), $hiscore->[-1]) |
126 | . (fmt $pre , $hiscore->[$pre]) |
149 | . (fmt undef, $score) |
127 | . (fmt undef, $score) |
150 | . $SEP; |
128 | . $SEP; |
151 | } |
129 | } |
|
|
130 | } elsif (defined $ins or @$hiscore < $HIGHSCORE_ENTRIES) { |
|
|
131 | $ins //= @$hiscore; |
152 | |
132 | |
153 | warn $msg;#d# remove once working stable |
133 | $msg = "T<You entered the highscore list!>\n\n" |
|
|
134 | . $SEP |
|
|
135 | . $HEADER |
|
|
136 | . $SEP |
|
|
137 | . (fmt $ins, $score) |
|
|
138 | . $SEP; |
154 | |
139 | |
155 | $ob->send_msg ($SCORE_CHANNEL => $msg, cf::NDI_CLEAR); |
140 | splice @$hiscore, $ins, 0, $score; |
|
|
141 | $save = 1; |
|
|
142 | } else { |
|
|
143 | $msg = "T<You did not enter the highscore list.>\n\n" |
|
|
144 | . $SEP |
|
|
145 | . $HEADER |
|
|
146 | . $SEP |
|
|
147 | . (fmt -1 + (scalar @$hiscore), $hiscore->[-1]) |
|
|
148 | . (fmt undef, $score) |
|
|
149 | . $SEP; |
|
|
150 | } |
156 | |
151 | |
|
|
152 | cf::info $msg;#d# remove once working stable |
|
|
153 | |
|
|
154 | $ob->send_msg ($SCORE_CHANNEL => $msg, cf::NDI_CLEAR); |
|
|
155 | |
157 | if ($save) { |
156 | if ($save) { |
158 | save_hiscore $hiscore |
157 | save_hiscore $hiscore |
159 | or die "unable to write highscore file: $!"; |
158 | or die "unable to write highscore file: $!"; |
160 | } |
|
|
161 | }; |
159 | } |
162 | } |
160 | } |
163 | |
161 | |
164 | |
|
|