-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
653869d
commit ab0e506
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
examples/src/main/java/com/meteormsg/sender/ScoreboardExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |