-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsGameCache.js
More file actions
109 lines (90 loc) · 2.84 KB
/
sGameCache.js
File metadata and controls
109 lines (90 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const mapCache = new Map(); //maps unique ids to player props
const nIdLength = 8;
const nCacheTimeout = 20; //in seconds
module.exports.SetPlayerCache = function (strRoomCode, strPlayerName, bIsHost) {
const strId = GetUniqueString (nIdLength);
InsertWithCode (strId, strRoomCode, strPlayerName, bIsHost, nCacheTimeout);
return strId;
}
module.exports.GetPlayerCache = function (strId) {
const value = mapCache.get (strId);
if (value) return value;
else return null;
}
module.exports.SetCache = function (obj, nTimeoutSec)
{
const strId = GetUniqueString (nIdLength);
if (!strId) { return ""; }
mapCache.set (strId, obj);
if (nTimeoutSec >= 0)
{
setTimeout (() => {
module.exports.RemoveCache (strId);
}, nTimeoutSec * 1000);
}
return strId;
}
module.exports.GetCache = function (strId) {
const value = mapCache.get (strId);
if (value) return value;
else return null;
}
module.exports.RemoveCache = function (strId)
{
if (mapCache.has (strId))
{
mapCache.delete (strId);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// nTimeout is in seconds
function InsertWithCode (strId, strRoomCode, strPlayerName, bIsHost, nTimeout)
{
if (!strId) { return; }
let obj = new Object();
obj.strRoomCode = strRoomCode;
obj.strPlayerName = strPlayerName;
obj.bIsHost = bIsHost;
mapCache.set (strId, obj);
if (nTimeout >= 0)
{
setTimeout (() => {
if (mapCache.has (strId))
{
mapCache.delete (strId);
}
}, nTimeout * 1000);
}
}
//Default cache members... This is to make testing easier
InsertWithCode ("rishia", "xyzw", "Rishi0", true, -1);
InsertWithCode ("rishib", "xyzw", "Rishi1", false, -1);
InsertWithCode ("rishic", "xyzw", "Rishi2", false, -1);
InsertWithCode ("rishid", "xyzw", "Rishi3", false, -1);
InsertWithCode ("rishie", "xyzw", "Rishi4", false, -1);
InsertWithCode ("rishif", "xyzw", "Rishi5", false, -1);
InsertWithCode ("rishig", "xyzw", "Rishi6", false, -1);
InsertWithCode ("rishih", "xyzw", "Rishi7", false, -1);
//////////////////////////////////////////////////////////////////////////////////////////////////////
const strLetters = "abcdefghijklmnopqrstuvwxyz";
function GetRandomString (nCount)
{
let str = "";
for (let i = 0; i < nCount; i++)
{
const r = Math.floor(Math.random() * strLetters.length);
str += strLetters[r];
}
return str;
}
function GetUniqueString (nCount)
{
const nMaxAttempts = 1000;
for (let i = 0; i < nMaxAttempts; i++)
{
const strId = GetRandomString (nCount);
if (!mapCache.has (strId)) return strId;
}
console.log ("sGameCache.js: GetUniqueString: Could not generate random id");
return "";
}