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

Stable Structures #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Updates
  • Loading branch information
skilesare committed Feb 28, 2023
commit dfadbfb4137448fea7707a03c027b9a829e5c2cb
4 changes: 2 additions & 2 deletions canister_ids.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"reversi": {
"ic": "iabks-raaaa-aaaab-aaafq-cai"
"ic": "7d4hn-biaaa-aaaam-abfbq-cai"
},
"reversi_assets": {
"ic": "ivg37-qiaaa-aaaab-aaaga-cai"
"ic": "afkvi-zyaaa-aaaak-qb4za-cai"
}
}
4 changes: 4 additions & 0 deletions src/declarations/reversi/reversi.did
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ type Color =
};
service : {
list: () -> (ListResult) query;
list_games: () -> (vec record {
text;
text;
}) query;
move: (int, int) -> (MoveResult);
register: (text) -> (Result_1);
start: (text) -> (Result);
Expand Down
1 change: 1 addition & 0 deletions src/declarations/reversi/reversi.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export type StartError = { 'NoSelfGame' : null } |
{ 'OpponentInAnotherGame' : null };
export interface _SERVICE {
'list' : ActorMethod<[], ListResult>,
'list_games' : ActorMethod<[], Array<[string, string]>>,
'move' : ActorMethod<[bigint, bigint], MoveResult>,
'register' : ActorMethod<[string], Result_1>,
'start' : ActorMethod<[string], Result>,
Expand Down
5 changes: 5 additions & 0 deletions src/declarations/reversi/reversi.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export const idlFactory = ({ IDL }) => {
const Result = IDL.Variant({ 'ok' : GameView, 'err' : StartError });
return IDL.Service({
'list' : IDL.Func([], [ListResult], ['query']),
'list_games' : IDL.Func(
[],
[IDL.Vec(IDL.Tuple(IDL.Text, IDL.Text))],
['query'],
),
'move' : IDL.Func([IDL.Int, IDL.Int], [MoveResult], []),
'register' : IDL.Func([IDL.Text], [Result_1], []),
'start' : IDL.Func([IDL.Text], [Result], []),
Expand Down
28 changes: 27 additions & 1 deletion src/declarations/reversi/reversi.old.most
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
// Version: 1.0.0
type Board = [var ?Color];
type Color = {#black; #white};
type ColorCount = {black : Nat; white : Nat};
type Entry<K, V> = (?K, ?V, Nat, Nat);
type GameState =
{
var black : (?PlayerId, PlayerName);
board : Board;
dimension : Nat;
var last_updated : Time;
moves : Moves;
var next : Color;
var result : ?ColorCount;
var white : (?PlayerId, PlayerName)
};
type GameState__2 = GameState;
type IdMap = Map<PlayerId, PlayerName>;
type Map<K, V> = {var body : ([var Nat], [var Entry<K, V>], Nat, Nat, Nat)};
type Moves = StableBuffer<Nat8>;
type NameMap = Map<PlayerName, PlayerStateV2>;
type PlayerId = Principal;
type PlayerId__2 = PlayerId;
type PlayerName = Text;
Expand All @@ -7,7 +27,13 @@ type PlayerStateV1__1 = PlayerStateV1;
type PlayerStateV2 = {ids : [PlayerId]; name : PlayerName; var score : Score};
type PlayerState__1 = PlayerStateV2;
type Score = Nat;
type StableBuffer<X> =
{var count : Nat; var elems : [var X]; initCapacity : Nat};
type Time = Int;
actor {
stable var accounts : [(PlayerId__2, PlayerStateV1__1)];
stable var accounts_v2 : [PlayerState__1]
stable var accounts_v2 : [PlayerState__1];
stable var games :
{var count : Nat; var elems : [var GameState__2]; initCapacity : Nat};
stable var players : {id_map : IdMap; name_map : NameMap}
};
30 changes: 27 additions & 3 deletions src/reversi/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,30 @@ actor {
}
};

public shared query (msg) func list_games(): async [(Text, Text)] {

let results = Buffer.Buffer<(Text, Text)>(1);
for(thisItem in StableBuffer.toArray(games).vals()){
if(thisItem.result == null){
results.add((thisItem.black.1, thisItem.white.1));
};
};

return results.toArray();
};

public shared query (msg) func list_players(): async [(Text, [Principal])] {

let results = Buffer.Buffer<(Text, [Principal])>(1);
for(thisItem in Map.entries(players.name_map)){

results.add((thisItem.0, thisItem.1.ids));

};

return results.toArray();
};

// External interface to view the state of an on-going game.
public shared query (msg) func view() : async ?GameView {
let player_id = msg.caller;
Expand Down Expand Up @@ -475,10 +499,10 @@ actor {

// External interface that places a piece of given color at a coordinate.
// It returns "OK" when the move is valid.
public shared(msg) func move(row_: Int, col_: Int) : async Types.MoveResult {
public shared(msg) func move(move: {rowPos: Nat; colPos: Nat}) : async Types.MoveResult {
// The casting is necessary because dfx has yet to support Nat on commandline
let row : Nat = Int.abs(row_);
let col : Nat = Int.abs(col_);
let row : Nat = Int.abs(move.rowPos);
let col : Nat = Int.abs(move.colPos);
let player_id = msg.caller;
switch (lookup_game_by_id(player_id)) {
case null { #GameNotFound };
Expand Down
2 changes: 1 addition & 1 deletion src/reversi/utils.mo
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public func eq_nocase(s: Text, t: Text) : Bool {
// 2. Alphanumerical. Special characters like '_' and '-' are also allowed.
public func valid_name(name: Text): Bool {
let str : [Char] = Iter.toArray(Text.toIter(name));
if (str.size() < 3 or str.size() > 10) {
if (str.size() < 3 or str.size() > 16) {
return false;
};
for (i in Iter.range(0, str.size() - 1)) {
Expand Down
82 changes: 62 additions & 20 deletions src/reversi_assets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,11 @@ function Game() {
}
);
}
return m("div", content);
return m("div", content, m(
"a",
{ href: "https://icdevs.org/donations.html" },
"Consider donating to ICDevs.org"
));
},
};
}
Expand All @@ -304,19 +308,28 @@ function Game() {
function Observe() {
var game = null;
var boards = [];

var game_list = null;
var last_move_length = null;
var player_color = null;
var next_color = null;
var expiring = null;
var requested_teams = null;
var refresh = function (player, against) {
clearTimeout(refreshTimeout);
game_list=null;
reversi
.view_game(player, against)
.then(function (res) {
// console.log("refresh view");
// console.log(res);
if (res.length == 0) {
set_error("GameCancelled");
reversi
.list_games()
.then(function (game_list_result){
game_list = game_list_result;
m.redraw();
});

} else {

Expand Down Expand Up @@ -399,29 +412,51 @@ function Observe() {
},
view: function (vnode) {
var content;
if (game === null) {
if((requested_teams != null) && (vnode.attrs.player != requested_teams[0] || vnode.attrs.against != requested_teams[1])){
game_list = null;
};

requested_teams = [vnode.attrs.player, vnode.attrs.against];

if (game === null && game_list === null) {
start_loading();
clearTimeout(refreshTimeout);
load_put_sound(reversi_assets);
refresh(vnode.attrs.player, vnode.attrs.against);
player_color = "white";
content = m("div");
} else {
content = Board(
expiring ? "Game will expire if no one moves!" : "",
next_color,
next_color,
game,
boards,
function (){},
function (e) {
start_loading();
m.route.set("/play");
}
);
stop_loading();
}
return m("div", content);
}else {

if( game_list != null && game_list.length > 0){
let lis = [];
for (var i = 0; i < game_list.length; i++) {
lis.push(m("div", m("a", {href : "#!/observe/" + game_list[i][0] + "/" + game_list[i][1]}, game_list[i][0] + " vs. " + game_list[i][1])));
};

content = [m("h2", "That game isn't running right now, but here are some that are"),m("div", lis), m("br")];
stop_loading();
}
else{
content = Board(
expiring ? "Game will expire if no one moves!" : "",
next_color,
next_color,
game,
boards,
function (){},
function (e) {
start_loading();
m.route.set("/play");
}
);
stop_loading();
}
};
return m("div", content, m(
"a",
{ href: "https://icdevs.org/donations.html" },
"Consider donating to ICDevs.org"
));
},
};
}
Expand Down Expand Up @@ -786,7 +821,13 @@ function Play() {
m("div.error", get_error_message()),
m("div", m("form", { onsubmit: play }, form)),
]),
m("div.bottom-centered", m("div.tips", tips_on ? m(tips) : null)),
m("div.bottom-centered",
m("div.tips", tips_on ? m(tips) : null),
m(
"a",
{ href: "https://icdevs.org/donations.html" },
"Consider donating to ICDevs.org"
)),
m(
"div.bottom",
m(
Expand All @@ -795,6 +836,7 @@ function Play() {
"Source Code"
)
),

];
}
},
Expand Down