Skip to content

Instantly share code, notes, and snippets.

Created July 6, 2016 15:35
Show Gist options
  • Save anonymous/d960de4e2a88ac841d531ab3a7990943 to your computer and use it in GitHub Desktop.
Save anonymous/d960de4e2a88ac841d531ab3a7990943 to your computer and use it in GitHub Desktop.
pinball-pinball-pinball
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>pinball-pinball-pinball</title>
<link rel="stylesheet" href="styles.css">
<script type="application/dart" src="main.dart"></script>
</head>
<body>
<html>
<head>
<title>Pinball, PinBall, PINBALL</title>
</head>
<body>
<div id='matches_container'>
<h2>Active Matches</h2>
<ul id='matches'></ul>
</div>
<div id='waiting_container'>
<h2>Wait Queue</h2>
<ol id='waiting'></ul>
</div>
<div id='history_container'>
<h2>Match History</h2>
<ul id='history'></ul>
</div>
</body>
</html>
</body>
</html>
import 'dart:collection';
import 'dart:html';
import 'dart:math';
class Player {
String name;
Player(this.name);
}
class Location {
String name;
Player loser;
Location(this.name);
}
class Match {
Location location;
Player first;
Player second;
Player loser;
Player winner;
Match(this.location, this.first, this.second);
}
var players = [
new Player('Alice'),
new Player('Bob'),
new Player('Charlie'),
new Player('Dana'),
new Player('Eric'),
new Player('Freya'),
new Player('Gilbert'),
new Player('Ida'),
];
var locations = [
new Location('Scared Stiff'),
new Location('Metallica'),
new Location('Ghostbusters'),
];
var matches = new SplayTreeMap<String, Match>();
var matchHistory = new List<Match>();
var waitQueue = new ListQueue<Player>();
var random = new Random.secure();
void _winner(Match match, Player winner, Player loser) {
match.loser = loser;
match.winner = winner;
Player toQueue;
Player fromQueue = waitQueue.removeFirst();
if (match.location.loser == loser) {
// player has lost twice on this machine, send them to the back of the queue
toQueue = loser;
} else {
// send the winner to the back of the queue
toQueue = winner;
}
Player first;
Player second;
if (match.first == toQueue) {
first = fromQueue;
second = match.second;
} else {
first = match.first;
second = fromQueue;
}
Match nextMatch = new Match(match.location, first, second);
match.location.loser = loser;
matches[match.location.name] = nextMatch;
matchHistory.add(match);
waitQueue.addLast(toQueue);
renderMatches(querySelector('#matches'), matches);
renderQueue(querySelector('#waiting'), waitQueue);
renderHistory(querySelector('#history'), matchHistory);
}
void renderMatches(UListElement container, var matches) {
container.children.clear();
for (var key in matches.keys) {
var match = matches[key];
var item = new LIElement();
var first = new Element.tag('span');
first.text = match.first.name;
var firstWinner = new Element.tag('a');
firstWinner.text = '[Winner]';
firstWinner.onClick.listen((e) => _winner(match, match.first, match.second));
var vs = new Element.tag('span');
vs.text = 'vs';
var second = new Element.tag('span');
second.text = match.second.name;
var secondWinner = new Element.tag('a');
secondWinner.text = '[Winner]';
secondWinner.onClick.listen((e) => _winner(match, match.second, match.first));
var location = new Element.tag('span');
location.text = '@ ${match.location.name}';
item.nodes.add(first);
item.nodes.add(firstWinner);
item.nodes.add(vs);
item.nodes.add(second);
item.nodes.add(secondWinner);
item.nodes.add(location);
container.children.add(item);
}
}
void renderQueue(OListElement container, var queue) {
container.children.clear();
for (var player in queue) {
var item = new LIElement();
item.text = '${player.name}';
container.children.add(item);
}
}
void renderHistory(UListElement container, var history) {
container.children.clear();
for (var match in history) {
var item = new Element.tag('li');
var winner = new Element.tag('span');
winner.text = match.winner.name;
var defeated = new Element.tag('span');
defeated.text = 'defeated';
var loser = new Element.tag('span');
loser.text = match.loser.name;
var location = new Element.tag('span');
location.text = '@ ${match.location.name}';
item.nodes.add(winner);
item.nodes.add(defeated);
item.nodes.add(loser);
item.nodes.add(location);
container.children.add(item);
}
}
void main() {
players.shuffle(random);
locations.shuffle(random);
int location_idx = 0;
Player first = null;
for (var player in players) {
if (location_idx < locations.length) {
Location location = locations[location_idx];
if (first == null) {
first = player;
} else {
Match match = new Match(location, first, player);
matches[location.name] = match;
location_idx += 1;
first = null;
}
} else {
// stuff players into queue
waitQueue.addLast(player);
}
}
renderMatches(querySelector('#matches'), matches);
renderQueue(querySelector('#waiting'), waitQueue);
renderHistory(querySelector('#history'), matchHistory);
}
a {
color: #3D5C9D;
cursor: pointer;
text-decoration: none;
}
li a {
margin-left: 8px;
}
li span {
margin-left: 8px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment