Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update scoring algorithm #11

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions main.w
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ struct SelectWinnerResponse {

class Util {
extern "./util.js" static inflight jsonToSelectWinnerRequest(json: Json): SelectWinnerRequest;

static inflight clamp(value: num, min: num, max: num): num {
if value < min {
return min;
} elif value > max {
return max;
}
return value;
}
}

// --- application ---
Expand All @@ -57,6 +66,19 @@ class Store {
this.table.putItem(_entryToMap(entry));
}

inflight getEntry(name: str): Entry {
let item = this.table.getItem(Map<ddb.Attribute> {
"Name" => ddb.Attribute {
type: ddb.AttributeType.String,
value: name,
},
});
return Entry {
name: name,
score: num.fromStr(str.fromJson(item.get("Score").value)),
};
}

inflight getRandomPair(): Array<Entry> {
let entries = this.table.scan();

Expand All @@ -74,18 +96,14 @@ class Store {
inflight updateScores(winner: str, loser: str): Array<num> {
let entries = this.list();

let var winnerScore = 0;
let var loserScore = 0;
for entry in entries {
if entry.name == winner {
winnerScore = entry.score;
} elif entry.name == loser {
loserScore = entry.score;
}
}
let winnerScore = this.getEntry(winner).score;
let loserScore = this.getEntry(loser).score;

// probability that the winner should have won
let pWinner = 1.0 / (1.0 + 10 ** (loserScore - winnerScore) / 400.0);

let var winnerNewScore = winnerScore + 1;
let var loserNewScore = loserScore - 1;
let winnerNewScore = Util.clamp(winnerScore + 32 * (1.0 - pWinner), 1000, 2000);
let loserNewScore = Util.clamp(loserScore + 32 * (pWinner - 1.0), 1000, 2000);

this.setEntry(Entry { name: winner, score: winnerNewScore });
this.setEntry(Entry { name: loser, score: loserNewScore });
Expand Down Expand Up @@ -154,16 +172,14 @@ new cloud.OnDeploy(inflight () => {
for food in foods {
store.setEntry(Entry {
name: food,
score: 0,
score: 1500,
});
}
}) as "InitializeTable";

let api = new cloud.Api() as "VotingAppApi";

let website = new cloud.Website(
path: "./website/build",
);
let website = new cloud.Website(path: "./website/build");
website.addJson("config.json", { apiUrl: api.url });

// Select two random items from the list of items for the user to choose between
Expand Down