Skip to content

Commit

Permalink
Scoreboard example
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindgamesnl committed Aug 25, 2023
1 parent 653869d commit ab0e506
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions examples/src/main/java/com/meteormsg/sender/ScoreboardExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.meteormsg.sender;

import com.meteormsg.base.defaults.LoopbackTransport;
import com.meteormsg.core.Meteor;
import com.meteormsg.transport.redis.RedisTransport;

import java.util.HashMap;
import java.util.Map;

public class ScoreboardExample {

public static void main(String[] args) throws Exception{
Meteor meteor = new Meteor(new LoopbackTransport());
meteor.registerImplementation(new ScoreboardImplementation(), "parkour-leaderboard");

Scoreboard scoreboard = meteor.registerProcedure(Scoreboard.class, "parkour-leaderboard");

scoreboard.setScoreForPlayer("player1", 10);
scoreboard.setScoreForPlayer("player2", 20);
scoreboard.setScoreForPlayer("player3", 30);

Map<String, Integer> scores = scoreboard.getAllScores();
System.out.println("scores: " + scores);

int player1Score = scoreboard.getScoreForPlayer("player1");
System.out.println("player1 score: " + player1Score);

meteor.stop();
}

public interface Scoreboard {
int getScoreForPlayer(String player);
void setScoreForPlayer(String player, int score);
Map<String, Integer> getAllScores();
}

public static class ScoreboardImplementation implements Scoreboard {

private final Map<String, Integer> scores = new HashMap<>();

@Override
public int getScoreForPlayer(String player) {
return scores.getOrDefault(player, 0);
}

@Override
public void setScoreForPlayer(String player, int score) {
scores.put(player, score);
}

@Override
public Map<String, Integer> getAllScores() {
return scores;
}
}
}

0 comments on commit ab0e506

Please sign in to comment.